From 19c9feb9b8b9e40e1de486169a9581f26069ab08 Mon Sep 17 00:00:00 2001 From: Paul Biron Date: Thu, 27 Feb 2020 11:52:46 -0700 Subject: [PATCH 1/3] Add auto-update-enabled and auto-update-disabled views on the plugins screen. Fixes #17 --- wp-autoupdates.php | 135 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/wp-autoupdates.php b/wp-autoupdates.php index 67c5b8d..d41ca6b 100755 --- a/wp-autoupdates.php +++ b/wp-autoupdates.php @@ -28,7 +28,7 @@ function wp_autoupdates_enqueues( $hook ) { } wp_register_style( 'wp-autoupdates', plugin_dir_url( __FILE__ ) . 'css/wp-autoupdates.css', array() ); wp_enqueue_style( 'wp-autoupdates' ); - + // Update core screen JS hack (due to lack of filters) if ( 'update-core.php' === $hook ) { $script = 'jQuery( document ).ready(function() {'; @@ -305,3 +305,136 @@ function wp_autoupdates_notices() { } add_action( 'admin_notices', 'wp_autoupdates_notices' ); +/** + * Add views for auto-update enabled/disabled. + * + * This is modeled on `WP_Plugins_List_Table::get_views()`. If this is merged into core, + * then this should be encorporated there. + * + * @global array $totals Counts by plugin_status, set in `WP_Plugins_List_Table::prepare_items()`. + */ +function wp_autoupdates_plugins_status_links( $status_links ) { + global $totals; + + if ( ! current_user_can( 'update_plugins' ) || ! wp_autoupdates_is_plugins_auto_update_enabled() ) { + return $status_links; + } + + $enabled_count = count( get_site_option( 'wp_auto_update_plugins', array() ) ); + + // when merged, these counts will need to be set in WP_Plugins_List_Table::prepare_items(). + $counts = array( + 'auto-update-enabled' => $enabled_count, + 'auto-update-disabled' => $totals['all'] - $enabled_count, + ); + + // we can't use the global $status set in WP_Plugin_List_Table::__construct() because + // it will be 'all' for our "custom statuses". + $status = isset( $_REQUEST['plugin_status'] ) ? $_REQUEST['plugin_status'] : 'all'; + + foreach ( $counts as $type => $count ) { + switch( $type ) { + case 'auto-update-enabled': + /* translators: %s: Number of plugins. */ + $text = _n( + 'Auto-update enabled (%s)', + 'Auto-update enabled (%s)', + $count + ); + + break; + case 'auto-update-disabled': + /* translators: %s: Number of plugins. */ + $text = _n( + 'Auto-update disabled (%s)', + 'Auto-update disabled (%s)', + $count + ); + } + + $status_links[ $type ] = sprintf( + "%s", + add_query_arg( 'plugin_status', $type, 'plugins.php' ), + ( $type === $status ) ? ' class="current" aria-current="page"' : '', + sprintf( $text, number_format_i18n( $count ) ) + ); + } + + // make the 'all' status link not current if one of our "custom statuses" is current. + if ( in_array( $status, array_keys( $counts ) ) ) { + $status_links['all'] = str_replace( ' class="current" aria-current="page"', '', $status_links['all'] ); + } + + return $status_links; +} +add_action( 'views_plugins', 'wp_autoupdates_plugins_status_links' ); + +/** + * Filter plugins shown in the list table when status is 'auto-update-enabled' or 'auto-update-disabled'. + * + * This is modeled on `WP_Plugins_List_Table::prepare_items()`. If this is merged into core, + * then this should be encorporated there. + * + * This action this is hooked to is fired in `wp-admin/plugins.php`. + * + * @global WP_Plugins_List_Table $wp_list_table The global list table object. Set in `wp-admin/plugins.php`. + * @global int $page The current page of plugins displayed. Set in WP_Plugins_List_Table::__construct(). + */ +function wp_autoupdates_plugins_filter_plugins_by_status( $plugins ) { + global $wp_list_table, $page; + + $custom_statuses = array( + 'auto-update-enabled', + 'auto-update-disabled', + ); + + if ( ! ( isset( $_REQUEST['plugin_status'] ) && + in_array( $_REQUEST['plugin_status'], $custom_statuses ) ) ) { + // current request is not for one of our statuses. + // nothing to do, so bail. + return; + } + + $wp_auto_update_plguins = get_site_option( 'wp_auto_update_plugins', array() ); + $_plugins = array(); + foreach ( $plugins as $plugin_file => $plugin_data ) { + switch ( $_REQUEST['plugin_status'] ) { + case 'auto-update-enabled': + if ( in_array( $plugin_file, $wp_auto_update_plguins ) ) { + $_plugins[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true ); + } + + break; + case 'auto-update-disabled': + if ( ! in_array( $plugin_file, $wp_auto_update_plguins ) ) { + $_plugins[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true ); + } + + break; + } + } + + // set the list table's items array to just those plugins with our custom status. + $wp_list_table->items = $_plugins; + + // now, update the pagination properties of the list table accordingly. + $total_this_page = count( $_plugins ); + + $plugins_per_page = $wp_list_table->get_items_per_page( str_replace( '-', '_', $wp_list_table->screen->id . '_per_page' ), 999 ); + + $start = ( $page - 1 ) * $plugins_per_page; + + if ( $total_this_page > $plugins_per_page ) { + $wp_list_table->items = array_slice( $wp_list_table->items, $start, $plugins_per_page ); + } + + $wp_list_table->set_pagination_args( + array( + 'total_items' => $total_this_page, + 'per_page' => $plugins_per_page, + ) + ); + + return; +} +add_action( 'pre_current_active_plugins', 'wp_autoupdates_plugins_filter_plugins_by_status' ); From 667119c5275738736f786856bbf62f0f0ab707d4 Mon Sep 17 00:00:00 2001 From: Paul Biron Date: Thu, 27 Feb 2020 15:10:46 -0700 Subject: [PATCH 2/3] Use the same strings and array indexes for the status links as in https://core.trac.wordpress.org/attachment/ticket/48850/48850.11.diff. Also, in multisite only add the views on the plugins-network screen. --- wp-autoupdates.php | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/wp-autoupdates.php b/wp-autoupdates.php index d41ca6b..e1ae817 100755 --- a/wp-autoupdates.php +++ b/wp-autoupdates.php @@ -324,8 +324,8 @@ function wp_autoupdates_plugins_status_links( $status_links ) { // when merged, these counts will need to be set in WP_Plugins_List_Table::prepare_items(). $counts = array( - 'auto-update-enabled' => $enabled_count, - 'auto-update-disabled' => $totals['all'] - $enabled_count, + 'autoupdate_enabled' => $enabled_count, + 'autoupdate_disabled' => $totals['all'] - $enabled_count, ); // we can't use the global $status set in WP_Plugin_List_Table::__construct() because @@ -334,30 +334,32 @@ function wp_autoupdates_plugins_status_links( $status_links ) { foreach ( $counts as $type => $count ) { switch( $type ) { - case 'auto-update-enabled': + case 'autoupdate_enabled': /* translators: %s: Number of plugins. */ $text = _n( - 'Auto-update enabled (%s)', - 'Auto-update enabled (%s)', - $count + 'Automatic Update Enabled (%s)', + 'Automatic Update Enabled (%s)', + $count, + 'wp-autoupdates' ); break; - case 'auto-update-disabled': + case 'autoupdate_disabled': /* translators: %s: Number of plugins. */ $text = _n( - 'Auto-update disabled (%s)', - 'Auto-update disabled (%s)', - $count + 'Automatic Update Disabled (%s)', + 'Automatic Update Disabled (%s)', + $count, + 'wp-autoupdates' ); - } + } - $status_links[ $type ] = sprintf( - "%s", - add_query_arg( 'plugin_status', $type, 'plugins.php' ), - ( $type === $status ) ? ' class="current" aria-current="page"' : '', - sprintf( $text, number_format_i18n( $count ) ) - ); + $status_links[ $type ] = sprintf( + "%s", + add_query_arg( 'plugin_status', $type, 'plugins.php' ), + ( $type === $status ) ? ' class="current" aria-current="page"' : '', + sprintf( $text, number_format_i18n( $count ) ) + ); } // make the 'all' status link not current if one of our "custom statuses" is current. @@ -367,7 +369,7 @@ function wp_autoupdates_plugins_status_links( $status_links ) { return $status_links; } -add_action( 'views_plugins', 'wp_autoupdates_plugins_status_links' ); +add_action( is_multisite() ? 'views_plugins-network' : 'views_plugins', 'wp_autoupdates_plugins_status_links' ); /** * Filter plugins shown in the list table when status is 'auto-update-enabled' or 'auto-update-disabled'. From fb9be5cac19f36bd02eeaa887752b017b5c7c065 Mon Sep 17 00:00:00 2001 From: Paul Biron Date: Fri, 28 Feb 2020 08:39:42 -0700 Subject: [PATCH 3/3] 1) ensure status links array indices match those used in https://core.trac.wordpress.org/attachment/ticket/48850/48850.11.diff; 2) fix typo in $wp_autoupdate_plugins variable name; 3) when computing the counts of plugins in each view, remove entries from the wp_auto_update_plugins site option that are no longer installed. --- wp-autoupdates.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/wp-autoupdates.php b/wp-autoupdates.php index e1ae817..b1dcd6b 100755 --- a/wp-autoupdates.php +++ b/wp-autoupdates.php @@ -320,7 +320,9 @@ function wp_autoupdates_plugins_status_links( $status_links ) { return $status_links; } - $enabled_count = count( get_site_option( 'wp_auto_update_plugins', array() ) ); + $wp_autoupdate_plugins = get_site_option( 'wp_auto_update_plugins', array() ); + $wp_autoupdate_plugins = array_intersect( $wp_autoupdate_plugins, array_keys( get_plugins() ) ); + $enabled_count = count( $wp_autoupdate_plugins ); // when merged, these counts will need to be set in WP_Plugins_List_Table::prepare_items(). $counts = array( @@ -386,8 +388,8 @@ function wp_autoupdates_plugins_filter_plugins_by_status( $plugins ) { global $wp_list_table, $page; $custom_statuses = array( - 'auto-update-enabled', - 'auto-update-disabled', + 'autoupdate_enabled', + 'autoupdate_disabled', ); if ( ! ( isset( $_REQUEST['plugin_status'] ) && @@ -397,18 +399,18 @@ function wp_autoupdates_plugins_filter_plugins_by_status( $plugins ) { return; } - $wp_auto_update_plguins = get_site_option( 'wp_auto_update_plugins', array() ); + $wp_auto_update_plugins = get_site_option( 'wp_auto_update_plugins', array() ); $_plugins = array(); foreach ( $plugins as $plugin_file => $plugin_data ) { switch ( $_REQUEST['plugin_status'] ) { - case 'auto-update-enabled': - if ( in_array( $plugin_file, $wp_auto_update_plguins ) ) { + case 'autoupdate_enabled': + if ( in_array( $plugin_file, $wp_auto_update_plugins ) ) { $_plugins[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true ); } break; - case 'auto-update-disabled': - if ( ! in_array( $plugin_file, $wp_auto_update_plguins ) ) { + case 'autoupdate_disabled': + if ( ! in_array( $plugin_file, $wp_auto_update_plugins ) ) { $_plugins[ $plugin_file ] = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, false, true ); }