Skip to content

Commit 4ab7e06

Browse files
authored
Merge pull request #3372 from andrewlimaza/fix-race-condition
Add function_exists checks to addons.php
2 parents e9e5c16 + 15f1e1d commit 4ab7e06

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
/**
@@ -378,57 +380,59 @@ function pmpro_plugins_api( $api, $action = '', $args = null ) {
378380
*
379381
* @since 1.8.5
380382
*/
381-
function pmpro_getPluginAPIObjectFromAddon( $addon ) {
382-
$api = new stdClass();
383+
if ( ! function_exists( 'pmpro_getPluginAPIObjectFromAddon' ) ) {
384+
function pmpro_getPluginAPIObjectFromAddon( $addon ) {
385+
$api = new stdClass();
383386

384-
if ( empty( $addon ) ) {
385-
return $api;
386-
}
387+
if ( empty( $addon ) ) {
388+
return $api;
389+
}
387390

388-
// add info
389-
$api->name = isset( $addon['Name'] ) ? $addon['Name'] : '';
390-
$api->slug = isset( $addon['Slug'] ) ? $addon['Slug'] : '';
391-
$api->plugin = isset( $addon['plugin'] ) ? $addon['plugin'] : '';
392-
$api->version = isset( $addon['Version'] ) ? $addon['Version'] : '';
393-
$api->author = isset( $addon['Author'] ) ? $addon['Author'] : '';
394-
$api->author_profile = isset( $addon['AuthorURI'] ) ? $addon['AuthorURI'] : '';
395-
$api->requires = isset( $addon['Requires'] ) ? $addon['Requires'] : '';
396-
$api->tested = isset( $addon['Tested'] ) ? $addon['Tested'] : '';
397-
$api->last_updated = isset( $addon['LastUpdated'] ) ? $addon['LastUpdated'] : '';
398-
$api->homepage = isset( $addon['URI'] ) ? $addon['URI'] : '';
399-
$api->download_link = isset( $addon['Download'] ) ? $addon['Download'] : '';
400-
$api->package = isset( $addon['Download'] ) ? $addon['Download'] : '';
401-
402-
// add sections
403-
if ( !empty( $addon['Description'] ) ) {
404-
$api->sections['description'] = $addon['Description'];
405-
}
406-
if ( !empty( $addon['Installation'] ) ) {
407-
$api->sections['installation'] = $addon['Installation'];
408-
}
409-
if ( !empty( $addon['FAQ'] ) ) {
410-
$api->sections['faq'] = $addon['FAQ'];
411-
}
412-
if ( !empty( $addon['Changelog'] ) ) {
413-
$api->sections['changelog'] = $addon['Changelog'];
414-
}
391+
// add info
392+
$api->name = isset( $addon['Name'] ) ? $addon['Name'] : '';
393+
$api->slug = isset( $addon['Slug'] ) ? $addon['Slug'] : '';
394+
$api->plugin = isset( $addon['plugin'] ) ? $addon['plugin'] : '';
395+
$api->version = isset( $addon['Version'] ) ? $addon['Version'] : '';
396+
$api->author = isset( $addon['Author'] ) ? $addon['Author'] : '';
397+
$api->author_profile = isset( $addon['AuthorURI'] ) ? $addon['AuthorURI'] : '';
398+
$api->requires = isset( $addon['Requires'] ) ? $addon['Requires'] : '';
399+
$api->tested = isset( $addon['Tested'] ) ? $addon['Tested'] : '';
400+
$api->last_updated = isset( $addon['LastUpdated'] ) ? $addon['LastUpdated'] : '';
401+
$api->homepage = isset( $addon['URI'] ) ? $addon['URI'] : '';
402+
$api->download_link = isset( $addon['Download'] ) ? $addon['Download'] : '';
403+
$api->package = isset( $addon['Download'] ) ? $addon['Download'] : '';
404+
405+
// add sections
406+
if ( !empty( $addon['Description'] ) ) {
407+
$api->sections['description'] = $addon['Description'];
408+
}
409+
if ( !empty( $addon['Installation'] ) ) {
410+
$api->sections['installation'] = $addon['Installation'];
411+
}
412+
if ( !empty( $addon['FAQ'] ) ) {
413+
$api->sections['faq'] = $addon['FAQ'];
414+
}
415+
if ( !empty( $addon['Changelog'] ) ) {
416+
$api->sections['changelog'] = $addon['Changelog'];
417+
}
415418

416-
// get license key if one is available
417-
$key = get_option( 'pmpro_license_key', '' );
418-
if ( ! empty( $key ) && ! empty( $api->download_link ) ) {
419-
$api->download_link = add_query_arg( 'key', $key, $api->download_link );
420-
}
421-
if ( ! empty( $key ) && ! empty( $api->package ) ) {
422-
$api->package = add_query_arg( 'key', $key, $api->package );
423-
}
424-
425-
if ( empty( $api->upgrade_notice ) && pmpro_license_type_is_premium( $addon['License'] ) ) {
426-
if ( ! pmpro_license_isValid( null, $addon['License'] ) ) {
427-
$api->upgrade_notice = sprintf( __( 'Important: This plugin requires a valid PMPro %s license key to update.', 'paid-memberships-pro' ), ucwords( $addon['License'] ) );
419+
// get license key if one is available
420+
$key = get_option( 'pmpro_license_key', '' );
421+
if ( ! empty( $key ) && ! empty( $api->download_link ) ) {
422+
$api->download_link = add_query_arg( 'key', $key, $api->download_link );
423+
}
424+
if ( ! empty( $key ) && ! empty( $api->package ) ) {
425+
$api->package = add_query_arg( 'key', $key, $api->package );
428426
}
429-
}
427+
428+
if ( empty( $api->upgrade_notice ) && pmpro_license_type_is_premium( $addon['License'] ) ) {
429+
if ( ! pmpro_license_isValid( null, $addon['License'] ) ) {
430+
$api->upgrade_notice = sprintf( __( 'Important: This plugin requires a valid PMPro %s license key to update.', 'paid-memberships-pro' ), ucwords( $addon['License'] ) );
431+
}
432+
}
430433

431-
return $api;
434+
return $api;
435+
}
432436
}
433437

434438
/**

0 commit comments

Comments
 (0)