|
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * This file contains some functions that are used in the plugin update-check API. |
6 | | - * The API is currently closed source, but these functions are made public through this file. |
| 6 | + * The API is currently not open-source, but these functions are made public through this file. |
7 | 7 | * |
8 | 8 | * NOTE: This file is executed without WordPress being loaded. |
9 | 9 | * Please ensure no WordPress dependencies, or other plugin file dependencies are added. |
10 | | - * Certain methods are polyfilled in that environment with no-op variants such as __() and apply_filters(). |
| 10 | + * Certain methods MAY BE polyfilled in that environment with no-op variants such as __() and apply_filters(). |
11 | 11 | * |
12 | 12 | * @link https://api.wordpress.org/plugins/update-check/{1.0,1.1}/ |
13 | 13 | */ |
14 | 14 |
|
15 | 15 | /** |
16 | 16 | * This function acts as a filter on the update that's presented to the site. |
17 | 17 | * |
18 | | - * @global string $wp_url The WordPress site URL. Extracted from the HTTP User Agent header. |
19 | | - * @global string $req_wp_version_base The WordPress client version. Empty if not a WordPress client. Excludes `-alpha` type suffixes. |
20 | | - * |
21 | 18 | * @param object $plugin_info The plugin update details. |
22 | 19 | * @param object $plugin_details The plugin details. |
23 | 20 | * @param string $installed_version The currently installed version of the plugin. |
24 | | - * @param string $wp_version The WordPress version. Empty if not a WordPress client. |
| 21 | + * @param string $wp_version The WordPress version. Empty if not a WordPress client. Excludes `-alpha` type suffixes. |
| 22 | + * @param string $wp_url The WordPress site URL. Extracted from the HTTP User Agent header. |
25 | 23 | * @return object The plugin update details. |
26 | 24 | */ |
27 | | -function alter_update( $plugin_info, $plugin_details, $installed_version ) { |
28 | | - global $wp_url, $req_wp_version_base; |
| 25 | +function alter_update( $plugin_info, $plugin_details, $installed_version, $wp_version, $wp_url ) { |
| 26 | + |
| 27 | + // Apply the Phased Rollout / Staged Rollout / Gradual Rollout strategy to the plugin update. |
| 28 | + $plugin_info = phased_rollout( $plugin_info, $plugin_details, $installed_version ); |
| 29 | + |
| 30 | + return $plugin_info; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Apply the Phased / Staged rollout strategies to the plugin update. |
| 35 | + * |
| 36 | + * @see https://meta.trac.wordpress.org/ticket/8009 |
| 37 | + * |
| 38 | + * @param object $plugin_info The plugin update details. |
| 39 | + * @param object $plugin_details The plugin details. |
| 40 | + * @param string $installed_version The currently installed version of the plugin. |
| 41 | + * @return object The updated plugin update details. |
| 42 | + */ |
| 43 | +function phased_rollout( $plugin_info, $plugin_details, $installed_version ) { |
| 44 | + $strategy = $plugin_info->meta->rollout['strategy'] ?? false; |
| 45 | + |
| 46 | + // If no strategy is set, or it's immediate, return the plugin info unchanged. |
| 47 | + if ( ! $strategy || 'immediate' === $strategy ) { |
| 48 | + return $plugin_info; |
| 49 | + } |
| 50 | + |
| 51 | + // Calculate the number of hours since the plugin was released. |
| 52 | + $hours_since_release = ( time() - ( $plugin_details->meta->release_time ?? '' ) ) / 3600 /* HOUR_IN_SECONDS */; |
| 53 | + |
| 54 | + // If more than 5 days have passed, always assume the update is available. |
| 55 | + if ( $hours_since_release > 120 ) { |
| 56 | + return $plugin_info; |
| 57 | + } |
| 58 | + |
| 59 | + // If the strategy is manual updates only for the first 24hrs, and we've not passed that, then disable auto-updates. |
| 60 | + if ( |
| 61 | + 'manual-updates-24hr' === $strategy && |
| 62 | + $hours_since_release <= 24 |
| 63 | + ) { |
| 64 | + $plugin_info->disable_autoupdates = true; |
| 65 | + } |
29 | 66 |
|
30 | 67 | return $plugin_info; |
31 | 68 | } |
0 commit comments