From 4f8e396594d7cf9b77cd1ad1eef5e48dffc81b91 Mon Sep 17 00:00:00 2001 From: Nick Green Date: Thu, 10 Oct 2024 12:45:46 -0700 Subject: [PATCH 1/7] Add delay message to update box on plugins page --- class-plugin-autoupdate-filter.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index b31f741..5b41dd5 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -161,7 +161,25 @@ public function filter_enforce_delay( $update, $item ): bool { $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 ); + if ( false === $has_delay_passed ) { + $option_key = 'plugin_update_delays'; + $delays = get_option( $option_key, array() ); + $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' ); + $formatted_date = date_i18n( $datetime_format, $delay_date ); + + // adds message to update notice box for that plugin on the plugins page + add_filter( + "in_plugin_update_message-{$plugin_file}", + function() use ( $plugin_new_version, $formatted_date ) { + echo ' Autoupdate to ' . $plugin_new_version . ' will be delayed until after ' . $formatted_date . ' UTC.' ; + }, + 10, + 2 + ); $update = false; } } From b9694c57dbe9359fb780bc0e9f60248d7868cfc6 Mon Sep 17 00:00:00 2001 From: Nick Green Date: Thu, 10 Oct 2024 12:54:27 -0700 Subject: [PATCH 2/7] PHPCS --- class-plugin-autoupdate-filter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index 5b41dd5..a993596 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -170,12 +170,12 @@ public function filter_enforce_delay( $update, $item ): bool { // Get the site's date and time format settings. $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); $formatted_date = date_i18n( $datetime_format, $delay_date ); - + // adds message to update notice box for that plugin on the plugins page add_filter( "in_plugin_update_message-{$plugin_file}", function() use ( $plugin_new_version, $formatted_date ) { - echo ' Autoupdate to ' . $plugin_new_version . ' will be delayed until after ' . $formatted_date . ' UTC.' ; + echo ' Autoupdate to ' . esc_html( $plugin_new_version ) . ' will be delayed until after ' . esc_html( $formatted_date ) . ' UTC.'; }, 10, 2 From dc1e0ed67129b5622983fdbea3b43218d34809d0 Mon Sep 17 00:00:00 2001 From: Nick Green Date: Mon, 14 Oct 2024 17:09:16 -0700 Subject: [PATCH 3/7] Prevent null warnings with null array key --- class-plugin-autoupdate-filter.php | 34 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index a993596..24d8e8f 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -152,7 +152,7 @@ public function filter_enforce_delay( $update, $item ): bool { return $update; } - // otherwise add delay to plugin updates + // otherwise apply delay logic if ( true === $update ) { $helpers = new Plugin_Autoupdate_Filter_Helpers(); @@ -165,21 +165,23 @@ public function filter_enforce_delay( $update, $item ): bool { if ( false === $has_delay_passed ) { $option_key = 'plugin_update_delays'; $delays = get_option( $option_key, array() ); - $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' ); - $formatted_date = date_i18n( $datetime_format, $delay_date ); - - // adds message to update notice box for that plugin on the plugins page - add_filter( - "in_plugin_update_message-{$plugin_file}", - function() use ( $plugin_new_version, $formatted_date ) { - echo ' Autoupdate to ' . esc_html( $plugin_new_version ) . ' will be delayed until after ' . esc_html( $formatted_date ) . ' UTC.'; - }, - 10, - 2 - ); + if ( isset( $delays[ $plugin_file ][ $plugin_new_version ] ) ) { + $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' ); + $formatted_date = date_i18n( $datetime_format, $delay_date ); + + // adds message to update notice box for that plugin on the plugins page + add_filter( + "in_plugin_update_message-{$plugin_file}", + function() use ( $plugin_new_version, $formatted_date ) { + echo ' For stability, autoupdates operate on a slight delay. Autoupdate to ' . esc_html( $plugin_new_version ) . ' is currently estimated to run after ' . esc_html( $formatted_date ) . ' UTC.'; + }, + 10, + 2 + ); + } $update = false; } } From 64f1cd4fc7c03a760038362d6ac00f9f435f694e Mon Sep 17 00:00:00 2001 From: Nick Green Date: Mon, 14 Oct 2024 19:29:07 -0700 Subject: [PATCH 4/7] Add delay message even if not during business hours --- class-plugin-autoupdate-filter.php | 63 ++++++++++++++++-------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index 24d8e8f..42eb7a6 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -146,6 +146,11 @@ 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 ) ) { @@ -153,37 +158,35 @@ public function filter_enforce_delay( $update, $item ): bool { } // otherwise apply delay logic - if ( true === $update ) { - $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; - - $has_delay_passed = $helpers->has_delay_passed( $plugin_slug, $plugin_new_version, $plugin_file ); - - if ( false === $has_delay_passed ) { - $option_key = 'plugin_update_delays'; - $delays = get_option( $option_key, array() ); - if ( isset( $delays[ $plugin_file ][ $plugin_new_version ] ) ) { - $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' ); - $formatted_date = date_i18n( $datetime_format, $delay_date ); - - // adds message to update notice box for that plugin on the plugins page - add_filter( - "in_plugin_update_message-{$plugin_file}", - function() use ( $plugin_new_version, $formatted_date ) { - echo ' For stability, autoupdates operate on a slight delay. Autoupdate to ' . esc_html( $plugin_new_version ) . ' is currently estimated to run after ' . esc_html( $formatted_date ) . ' UTC.'; - }, - 10, - 2 - ); - } - $update = false; + $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; + + $has_delay_passed = $helpers->has_delay_passed( $plugin_slug, $plugin_new_version, $plugin_file ); + + if ( false === $has_delay_passed ) { + $option_key = 'plugin_update_delays'; + $delays = get_option( $option_key, array() ); + if ( isset( $delays[ $plugin_file ][ $plugin_new_version ] ) ) { + $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' ); + $formatted_date = date_i18n( $datetime_format, $delay_date ); + + // adds message to update notice box for that plugin on the plugins page + add_filter( + "in_plugin_update_message-{$plugin_file}", + function() use ( $plugin_new_version, $formatted_date ) { + 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; From 74dd54c222f2b7efa56284a177f0d5eb5bb89df6 Mon Sep 17 00:00:00 2001 From: Nick Green Date: Wed, 16 Oct 2024 16:04:57 -0700 Subject: [PATCH 5/7] Set to UTC time --- class-plugin-autoupdate-filter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index 42eb7a6..74d1738 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -174,7 +174,8 @@ public function filter_enforce_delay( $update, $item ): bool { // Get the site's date and time format settings. $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); - $formatted_date = date_i18n( $datetime_format, $delay_date ); + // 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( From 4470387fcdaa86e2447bbb0afe9cc6fd6fd3d18c Mon Sep 17 00:00:00 2001 From: Nick Green Date: Thu, 17 Oct 2024 08:57:57 -0700 Subject: [PATCH 6/7] Check if timestamp is numeric to prevent warnings --- class-plugin-autoupdate-filter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index 74d1738..b83cbef 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -169,7 +169,7 @@ public function filter_enforce_delay( $update, $item ): bool { if ( false === $has_delay_passed ) { $option_key = 'plugin_update_delays'; $delays = get_option( $option_key, array() ); - if ( isset( $delays[ $plugin_file ][ $plugin_new_version ] ) ) { + if ( isset( $delays[ $plugin_file ][ $plugin_new_version ] ) && is_numeric( $delays[ $plugin_file ][ $plugin_new_version ] ) ) { $delay_date = $delays[ $plugin_file ][ $plugin_new_version ]; // Get the site's date and time format settings. From 095e5bde51783c8fc8fcda50a2aa89a31c29194d Mon Sep 17 00:00:00 2001 From: Nick Green Date: Thu, 17 Oct 2024 10:50:00 -0700 Subject: [PATCH 7/7] Don't show msg if autoupdates aren't available or if plugin is deactivated e.g. If not connected to WC.com, updates for paid plugins won't be available, so we shouldn't show the autoupdated message. Also, plugins won't autoupdate if deactivated, so no message. --- class-plugin-autoupdate-filter.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/class-plugin-autoupdate-filter.php b/class-plugin-autoupdate-filter.php index b83cbef..a65b7f7 100644 --- a/class-plugin-autoupdate-filter.php +++ b/class-plugin-autoupdate-filter.php @@ -5,6 +5,8 @@ * @package Plugin_Autoupdate_Filter */ +use AutomateWoo\Error; + if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } @@ -169,7 +171,7 @@ public function filter_enforce_delay( $update, $item ): bool { 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 ] ) ) { + 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. @@ -180,8 +182,10 @@ public function filter_enforce_delay( $update, $item ): bool { // adds message to update notice box for that plugin on the plugins page add_filter( "in_plugin_update_message-{$plugin_file}", - function() use ( $plugin_new_version, $formatted_date ) { - 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.'; + 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