Skip to content

Commit 2998b65

Browse files
committed
Plugin Directory: API: Add phased rollout logic into the API handlers for "manual updates for the first 24hrs only".
See #8009. git-svn-id: https://meta.svn.wordpress.org/sites/trunk@14500 74240141-8908-4e6f-9713-ba540dce6ec7
1 parent a615c5c commit 2998b65

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/plugin-update-helpers.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,66 @@
33

44
/**
55
* 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.
77
*
88
* NOTE: This file is executed without WordPress being loaded.
99
* 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().
1111
*
1212
* @link https://api.wordpress.org/plugins/update-check/{1.0,1.1}/
1313
*/
1414

1515
/**
1616
* This function acts as a filter on the update that's presented to the site.
1717
*
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-
*
2118
* @param object $plugin_info The plugin update details.
2219
* @param object $plugin_details The plugin details.
2320
* @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.
2523
* @return object The plugin update details.
2624
*/
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+
}
2966

3067
return $plugin_info;
3168
}

0 commit comments

Comments
 (0)