From af4127ab8476a24fa28394dfe638205749ff329f Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Tue, 28 Jul 2020 16:30:48 +0000 Subject: [PATCH] Upgrade/Install: Enhance auto-updates to be disabled for plugins that don't suport updates. This removes the toggle UI when updates aren't available. When plugins use the filter, the UI is updated to show that they are being controlled via code. And then removed entirely when not available. See #50798. Fixes #50280. Props elrae, pbiron, SergeyBiryukov, audrasjb, azaozz, StephenCronin, whyisjake, dd32, TimothyBlynJacobs, desrosj. git-svn-id: https://develop.svn.wordpress.org/trunk@48669 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-plugins-list-table.php | 88 ++++++++++++++----- src/wp-admin/plugins.php | 1 + 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/src/wp-admin/includes/class-wp-plugins-list-table.php b/src/wp-admin/includes/class-wp-plugins-list-table.php index 56bc665b66..5041911b33 100644 --- a/src/wp-admin/includes/class-wp-plugins-list-table.php +++ b/src/wp-admin/includes/class-wp-plugins-list-table.php @@ -203,19 +203,44 @@ class WP_Plugins_List_Table extends WP_List_Table { foreach ( (array) $plugins['all'] as $plugin_file => $plugin_data ) { // Extra info if known. array_merge() ensures $plugin_data has precedence if keys collide. if ( isset( $plugin_info->response[ $plugin_file ] ) ) { - $plugin_data = array_merge( (array) $plugin_info->response[ $plugin_file ], $plugin_data ); - $plugins['all'][ $plugin_file ] = $plugin_data; - // Make sure that $plugins['upgrade'] also receives the extra info since it is used on ?plugin_status=upgrade. - if ( isset( $plugins['upgrade'][ $plugin_file ] ) ) { - $plugins['upgrade'][ $plugin_file ] = $plugin_data; - } + $plugin_data = array_merge( (array) $plugin_info->response[ $plugin_file ], array( 'auto-update-supported' => true ), $plugin_data ); } elseif ( isset( $plugin_info->no_update[ $plugin_file ] ) ) { - $plugin_data = array_merge( (array) $plugin_info->no_update[ $plugin_file ], $plugin_data ); - $plugins['all'][ $plugin_file ] = $plugin_data; - // Make sure that $plugins['upgrade'] also receives the extra info since it is used on ?plugin_status=upgrade. - if ( isset( $plugins['upgrade'][ $plugin_file ] ) ) { - $plugins['upgrade'][ $plugin_file ] = $plugin_data; - } + $plugin_data = array_merge( (array) $plugin_info->no_update[ $plugin_file ], array( 'auto-update-supported' => true ), $plugin_data ); + } elseif ( empty( $plugin_data['auto-update-supported'] ) ) { + $plugin_data['auto-update-supported'] = false; + } + + /* + * Create the payload that's used for the auto_update_plugin filter. + * This is the same data contained within $plugin_info->(response|no_update) however + * not all plugins will be contained in those keys, this avoids unexpected warnings. + */ + $filter_payload = array( + 'id' => $plugin_file, + 'slug' => '', + 'plugin' => $plugin_file, + 'new_version' => '', + 'url' => '', + 'package' => '', + 'icons' => array(), + 'banners' => array(), + 'banners_rtl' => array(), + 'tested' => '', + 'requires_php' => '', + 'compatibility' => new stdClass(), + ); + $filter_payload = (object) array_merge( $filter_payload, array_intersect_key( $plugin_data, $filter_payload ) ); + + /** This action is documented in wp-admin/includes/class-wp-automatic-updater.php */ + $auto_update_forced = apply_filters( 'auto_update_plugin', null, $filter_payload ); + if ( ! is_null( $auto_update_forced ) ) { + $plugin_data['auto-update-forced'] = $auto_update_forced; + } + + $plugins['all'][ $plugin_file ] = $plugin_data; + // Make sure that $plugins['upgrade'] also receives the extra info since it is used on ?plugin_status=upgrade. + if ( isset( $plugins['upgrade'][ $plugin_file ] ) ) { + $plugins['upgrade'][ $plugin_file ] = $plugin_data; } // Filter into individual sections. @@ -1060,7 +1085,20 @@ class WP_Plugins_List_Table extends WP_List_Table { $html = array(); - if ( in_array( $plugin_file, $auto_updates, true ) ) { + if ( isset( $plugin_data['auto-update-forced'] ) ) { + if ( $plugin_data['auto-update-forced'] ) { + // Forced on + $text = __( 'Auto-updates enabled' ); + } else { + $text = __( 'Auto-updates disabled' ); + } + $action = 'unavailable'; + $time_class = ' hidden'; + } elseif ( ! $plugin_data['auto-update-supported'] ) { + $text = ''; + $action = 'unavailable'; + $time_class = ' hidden'; + } elseif ( in_array( $plugin_file, $auto_updates, true ) ) { $text = __( 'Disable auto-updates' ); $action = 'disable'; $time_class = ''; @@ -1079,19 +1117,21 @@ class WP_Plugins_List_Table extends WP_List_Table { $url = add_query_arg( $query_args, 'plugins.php' ); - $html[] = sprintf( - '', - wp_nonce_url( $url, 'updates' ), - $action - ); + if ( 'unavailable' == $action ) { + $html[] = '' . $text . ''; + } else { + $html[] = sprintf( + '', + wp_nonce_url( $url, 'updates' ), + $action + ); - $html[] = ''; - $html[] = '' . $text . ''; - $html[] = ''; + $html[] = ''; + $html[] = '' . $text . ''; + $html[] = ''; + } - $available_updates = get_site_transient( 'update_plugins' ); - - if ( isset( $available_updates->response[ $plugin_file ] ) ) { + if ( ! empty( $plugin_data['update'] ) ) { $html[] = sprintf( '
%s
', $time_class, diff --git a/src/wp-admin/plugins.php b/src/wp-admin/plugins.php index 42f196f15a..fe8ea41fb2 100644 --- a/src/wp-admin/plugins.php +++ b/src/wp-admin/plugins.php @@ -574,6 +574,7 @@ if ( current_user_can( 'update_plugins' ) && wp_is_auto_update_enabled_for_type( 'title' => __( 'Auto-updates' ), 'content' => '

' . __( 'Auto-updates can be enabled or disabled for each individual plugin. Plugins with auto-updates enabled will display the estimated date of the next auto-update. Auto-updates depends on the WP-Cron task scheduling system.' ) . '

' . + '

' . __( 'Auto-updates are only available for plugins recognized by WordPress.org, or that include a compatible update system.' ) . '

' . '

' . __( 'Please note: Third-party themes and plugins, or custom code, may override WordPress scheduling.' ) . '

', ) );