Skip to content

Commit 15f1e1d

Browse files
committed
Add function_exists checks to addons.php
* BUG FIX: Fixes a race condition when activating Paid Memberships Pro after the Update Manager is already activated.
1 parent f2bd47e commit 15f1e1d

1 file changed

Lines changed: 98 additions & 94 deletions

File tree

includes/addons.php

Lines changed: 98 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,64 @@ function pmpro_setupAddonUpdateInfo() {
2121
*
2222
* @since 1.8.5
2323
*/
24-
function pmpro_getAddons() {
25-
// check if forcing a pull from the server
26-
$addons = get_option( 'pmpro_addons', array() );
27-
$addons_timestamp = get_option( 'pmpro_addons_timestamp', 0 );
28-
29-
// if no addons locally, we need to hit the server
30-
if ( empty( $addons ) || ! empty( $_REQUEST['force-check'] ) || current_time( 'timestamp' ) > $addons_timestamp + 86400 ) {
31-
/**
32-
* Filter to change the timeout for this wp_remote_get() request.
33-
*
34-
* @since 1.8.5.1
35-
*
36-
* @param int $timeout The number of seconds before the request times out
37-
*/
38-
$timeout = apply_filters( 'pmpro_get_addons_timeout', 5 );
39-
40-
// get em
41-
$remote_addons = wp_remote_get( PMPRO_LICENSE_SERVER . 'addons/', $timeout );
42-
43-
// make sure we have at least an array to pass back
44-
if ( empty( $addons ) ) {
45-
$addons = array();
46-
}
47-
48-
// test response
49-
if ( is_wp_error( $remote_addons ) ) {
50-
// error
51-
pmpro_setMessage( 'Could not connect to the PMPro License Server to update addon information. Try again later.', 'error' );
52-
} elseif ( ! empty( $remote_addons ) && $remote_addons['response']['code'] == 200 ) {
53-
// Update addons in cache.
54-
$addons = json_decode( wp_remote_retrieve_body( $remote_addons ), true );
55-
56-
// If we don't have any addons, bail.
24+
if ( ! function_exists( 'pmpro_getAddons' ) ) {
25+
function pmpro_getAddons() {
26+
// check if forcing a pull from the server
27+
$addons = get_option( 'pmpro_addons', array() );
28+
$addons_timestamp = get_option( 'pmpro_addons_timestamp', 0 );
29+
30+
// if no addons locally, we need to hit the server
31+
if ( empty( $addons ) || ! empty( $_REQUEST['force-check'] ) || current_time( 'timestamp' ) > $addons_timestamp + 86400 ) {
32+
/**
33+
* Filter to change the timeout for this wp_remote_get() request.
34+
*
35+
* @since 1.8.5.1
36+
*
37+
* @param int $timeout The number of seconds before the request times out
38+
*/
39+
$timeout = apply_filters( 'pmpro_get_addons_timeout', 5 );
40+
41+
// get em
42+
$remote_addons = wp_remote_get( PMPRO_LICENSE_SERVER . 'addons/', $timeout );
43+
44+
// make sure we have at least an array to pass back
5745
if ( empty( $addons ) ) {
58-
return array();
46+
$addons = array();
5947
}
6048

61-
// Create a short name for each Add On.
62-
foreach ( $addons as $key => $value ) {
63-
$addons[$key]['ShortName'] = trim( str_replace( array( 'Add On', 'Paid Memberships Pro - ' ), '', $addons[$key]['Title'] ) );
64-
}
49+
// test response
50+
if ( is_wp_error( $remote_addons ) ) {
51+
// error
52+
pmpro_setMessage( 'Could not connect to the PMPro License Server to update addon information. Try again later.', 'error' );
53+
} elseif ( ! empty( $remote_addons ) && $remote_addons['response']['code'] == 200 ) {
54+
// Update addons in cache.
55+
$addons = json_decode( wp_remote_retrieve_body( $remote_addons ), true );
56+
57+
// If we don't have any addons, bail.
58+
if ( empty( $addons ) ) {
59+
return array();
60+
}
61+
62+
// Create a short name for each Add On.
63+
foreach ( $addons as $key => $value ) {
64+
$addons[$key]['ShortName'] = trim( str_replace( array( 'Add On', 'Paid Memberships Pro - ' ), '', $addons[$key]['Title'] ) );
65+
}
66+
67+
// Alphabetize the list by ShortName.
68+
$short_names = array_column( $addons, 'ShortName' );
69+
array_multisort( $short_names, SORT_ASC, SORT_STRING | SORT_FLAG_CASE, $addons );
6570

66-
// Alphabetize the list by ShortName.
67-
$short_names = array_column( $addons, 'ShortName' );
68-
array_multisort( $short_names, SORT_ASC, SORT_STRING | SORT_FLAG_CASE, $addons );
71+
delete_option( 'pmpro_addons' );
72+
add_option( 'pmpro_addons', $addons, null, 'no' );
73+
}
6974

70-
delete_option( 'pmpro_addons' );
71-
add_option( 'pmpro_addons', $addons, null, 'no' );
75+
// save timestamp of last update
76+
delete_option( 'pmpro_addons_timestamp' );
77+
add_option( 'pmpro_addons_timestamp', current_time( 'timestamp' ), null, 'no' );
7278
}
7379

74-
// save timestamp of last update
75-
delete_option( 'pmpro_addons_timestamp' );
76-
add_option( 'pmpro_addons_timestamp', current_time( 'timestamp' ), null, 'no' );
80+
return $addons;
7781
}
78-
79-
return $addons;
8082
}
8183

8284
/**
@@ -361,57 +363,59 @@ function pmpro_plugins_api( $api, $action = '', $args = null ) {
361363
*
362364
* @since 1.8.5
363365
*/
364-
function pmpro_getPluginAPIObjectFromAddon( $addon ) {
365-
$api = new stdClass();
366+
if ( ! function_exists( 'pmpro_getPluginAPIObjectFromAddon' ) ) {
367+
function pmpro_getPluginAPIObjectFromAddon( $addon ) {
368+
$api = new stdClass();
366369

367-
if ( empty( $addon ) ) {
368-
return $api;
369-
}
370+
if ( empty( $addon ) ) {
371+
return $api;
372+
}
370373

371-
// add info
372-
$api->name = isset( $addon['Name'] ) ? $addon['Name'] : '';
373-
$api->slug = isset( $addon['Slug'] ) ? $addon['Slug'] : '';
374-
$api->plugin = isset( $addon['plugin'] ) ? $addon['plugin'] : '';
375-
$api->version = isset( $addon['Version'] ) ? $addon['Version'] : '';
376-
$api->author = isset( $addon['Author'] ) ? $addon['Author'] : '';
377-
$api->author_profile = isset( $addon['AuthorURI'] ) ? $addon['AuthorURI'] : '';
378-
$api->requires = isset( $addon['Requires'] ) ? $addon['Requires'] : '';
379-
$api->tested = isset( $addon['Tested'] ) ? $addon['Tested'] : '';
380-
$api->last_updated = isset( $addon['LastUpdated'] ) ? $addon['LastUpdated'] : '';
381-
$api->homepage = isset( $addon['URI'] ) ? $addon['URI'] : '';
382-
$api->download_link = isset( $addon['Download'] ) ? $addon['Download'] : '';
383-
$api->package = isset( $addon['Download'] ) ? $addon['Download'] : '';
384-
385-
// add sections
386-
if ( !empty( $addon['Description'] ) ) {
387-
$api->sections['description'] = $addon['Description'];
388-
}
389-
if ( !empty( $addon['Installation'] ) ) {
390-
$api->sections['installation'] = $addon['Installation'];
391-
}
392-
if ( !empty( $addon['FAQ'] ) ) {
393-
$api->sections['faq'] = $addon['FAQ'];
394-
}
395-
if ( !empty( $addon['Changelog'] ) ) {
396-
$api->sections['changelog'] = $addon['Changelog'];
397-
}
374+
// add info
375+
$api->name = isset( $addon['Name'] ) ? $addon['Name'] : '';
376+
$api->slug = isset( $addon['Slug'] ) ? $addon['Slug'] : '';
377+
$api->plugin = isset( $addon['plugin'] ) ? $addon['plugin'] : '';
378+
$api->version = isset( $addon['Version'] ) ? $addon['Version'] : '';
379+
$api->author = isset( $addon['Author'] ) ? $addon['Author'] : '';
380+
$api->author_profile = isset( $addon['AuthorURI'] ) ? $addon['AuthorURI'] : '';
381+
$api->requires = isset( $addon['Requires'] ) ? $addon['Requires'] : '';
382+
$api->tested = isset( $addon['Tested'] ) ? $addon['Tested'] : '';
383+
$api->last_updated = isset( $addon['LastUpdated'] ) ? $addon['LastUpdated'] : '';
384+
$api->homepage = isset( $addon['URI'] ) ? $addon['URI'] : '';
385+
$api->download_link = isset( $addon['Download'] ) ? $addon['Download'] : '';
386+
$api->package = isset( $addon['Download'] ) ? $addon['Download'] : '';
387+
388+
// add sections
389+
if ( !empty( $addon['Description'] ) ) {
390+
$api->sections['description'] = $addon['Description'];
391+
}
392+
if ( !empty( $addon['Installation'] ) ) {
393+
$api->sections['installation'] = $addon['Installation'];
394+
}
395+
if ( !empty( $addon['FAQ'] ) ) {
396+
$api->sections['faq'] = $addon['FAQ'];
397+
}
398+
if ( !empty( $addon['Changelog'] ) ) {
399+
$api->sections['changelog'] = $addon['Changelog'];
400+
}
398401

399-
// get license key if one is available
400-
$key = get_option( 'pmpro_license_key', '' );
401-
if ( ! empty( $key ) && ! empty( $api->download_link ) ) {
402-
$api->download_link = add_query_arg( 'key', $key, $api->download_link );
403-
}
404-
if ( ! empty( $key ) && ! empty( $api->package ) ) {
405-
$api->package = add_query_arg( 'key', $key, $api->package );
406-
}
407-
408-
if ( empty( $api->upgrade_notice ) && pmpro_license_type_is_premium( $addon['License'] ) ) {
409-
if ( ! pmpro_license_isValid( null, $addon['License'] ) ) {
410-
$api->upgrade_notice = sprintf( __( 'Important: This plugin requires a valid PMPro %s license key to update.', 'paid-memberships-pro' ), ucwords( $addon['License'] ) );
402+
// get license key if one is available
403+
$key = get_option( 'pmpro_license_key', '' );
404+
if ( ! empty( $key ) && ! empty( $api->download_link ) ) {
405+
$api->download_link = add_query_arg( 'key', $key, $api->download_link );
406+
}
407+
if ( ! empty( $key ) && ! empty( $api->package ) ) {
408+
$api->package = add_query_arg( 'key', $key, $api->package );
411409
}
412-
}
410+
411+
if ( empty( $api->upgrade_notice ) && pmpro_license_type_is_premium( $addon['License'] ) ) {
412+
if ( ! pmpro_license_isValid( null, $addon['License'] ) ) {
413+
$api->upgrade_notice = sprintf( __( 'Important: This plugin requires a valid PMPro %s license key to update.', 'paid-memberships-pro' ), ucwords( $addon['License'] ) );
414+
}
415+
}
413416

414-
return $api;
417+
return $api;
418+
}
415419
}
416420

417421
/**

0 commit comments

Comments
 (0)