Making WordPress.org

Changeset 14500


Ignore:
Timestamp:
07/31/2025 02:58:39 AM (11 months ago)
Author:
dd32
Message:

Plugin Directory: API: Add phased rollout logic into the API handlers for "manual updates for the first 24hrs only".

See #8009.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/plugin-update-helpers.php

    r14491 r14500  
    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}/
     
    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.
     18 * @param object $plugin_info       The plugin update details.
     19 * @param object $plugin_details    The plugin details.
     20 * @param string $installed_version The currently installed version of the plugin.
     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.
     23 * @return object The plugin update details.
     24 */
     25function 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
    2037 *
    2138 * @param object $plugin_info       The plugin update details.
    2239 * @param object $plugin_details    The plugin details.
    2340 * @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.
    25  * @return object The plugin update details.
     41 * @return object The updated plugin update details.
    2642 */
    27 function alter_update( $plugin_info, $plugin_details, $installed_version ) {
    28     global $wp_url, $req_wp_version_base;
     43function 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;
Note: See TracChangeset for help on using the changeset viewer.