Skip to content

Commit

Permalink
Merge pull request #46 from a8cteam51/add-delay-message
Browse files Browse the repository at this point in the history
Add delay message
  • Loading branch information
NickGreen authored Oct 17, 2024
2 parents 6ce69e0 + 095e5bd commit 29c115c
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions class-plugin-autoupdate-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package Plugin_Autoupdate_Filter
*/

use AutomateWoo\Error;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
Expand Down Expand Up @@ -146,24 +148,50 @@ public function filter_maybe_disable_all_autoupdates( $update, $item ): bool {
* @return bool True to update, false to not update.
*/
public function filter_enforce_delay( $update, $item ): bool {
// protect against non-bool being returned from this function
if ( null === $update ) {
$update = false;
}

// no delay if site is a canary site
$site_url = wp_parse_url( home_url(), PHP_URL_HOST );
if ( isset( $this->settings->canary_sites ) && in_array( $site_url, $this->settings->canary_sites, true ) ) {
return $update;
}

// otherwise add delay to plugin updates
if ( true === $update ) {
$helpers = new Plugin_Autoupdate_Filter_Helpers();
// otherwise apply delay logic
$helpers = new Plugin_Autoupdate_Filter_Helpers();

$plugin_file = empty( $item->plugin ) ? '' : $item->plugin;
$plugin_slug = empty( $item->slug ) ? '' : $item->slug;
$plugin_new_version = empty( $item->new_version ) ? '0.0.0' : $item->new_version;

$plugin_file = empty( $item->plugin ) ? '' : $item->plugin;
$plugin_slug = empty( $item->slug ) ? '' : $item->slug;
$plugin_new_version = empty( $item->new_version ) ? '0.0.0' : $item->new_version;
$has_delay_passed = $helpers->has_delay_passed( $plugin_slug, $plugin_new_version, $plugin_file );

$has_delay_passed = $helpers->has_delay_passed( $plugin_slug, $plugin_new_version, $plugin_file );
if ( false === $has_delay_passed ) {
$update = false;
if ( false === $has_delay_passed ) {
$option_key = 'plugin_update_delays';
$delays = get_option( $option_key, array() );
if ( isset( $delays[ $plugin_file ][ $plugin_new_version ] ) && is_numeric( $delays[ $plugin_file ][ $plugin_new_version ] ) && ( ! empty( $plugin_file ) && is_plugin_active( $plugin_file ) ) ) {
$delay_date = $delays[ $plugin_file ][ $plugin_new_version ];

// Get the site's date and time format settings.
$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
// Set the $gmt parameter to true for UTC time
$formatted_date = date_i18n( $datetime_format, $delay_date, true );

// adds message to update notice box for that plugin on the plugins page
add_filter(
"in_plugin_update_message-{$plugin_file}",
function( $plugin_data, $response ) use ( $plugin_new_version, $formatted_date ) {
if ( ! empty( $response->package ) ) {
echo ' For stability, autoupdates operate on a slight delay. Autoupdate to version ' . esc_html( $plugin_new_version ) . ' is currently estimated to run after ' . esc_html( $formatted_date ) . ' UTC.';
}
},
10,
2
);
}
$update = false;
}

return $update;
Expand Down

0 comments on commit 29c115c

Please sign in to comment.