diff --git a/src/js/_enqueues/wp/updates.js b/src/js/_enqueues/wp/updates.js index d7e0de1c39..8b52bfcdd6 100644 --- a/src/js/_enqueues/wp/updates.js +++ b/src/js/_enqueues/wp/updates.js @@ -409,6 +409,7 @@ * * @since 4.2.0 * @since 4.6.0 More accurately named `updatePluginSuccess`. + * @since 5.5.0 Auto-update "time to next update" text cleared. * * @param {object} response Response from the server. * @param {string} response.slug Slug of the plugin to be updated. @@ -431,6 +432,9 @@ // Update the version number in the row. newText = $pluginRow.find( '.plugin-version-author-uri' ).html().replace( response.oldVersion, response.newVersion ); $pluginRow.find( '.plugin-version-author-uri' ).html( newText ); + + // Clear the "time to next auto-update" text. + $pluginRow.find( '.auto-update-time' ).empty(); } else if ( 'plugin-install' === pagenow || 'plugin-install-network' === pagenow ) { $updateMessage = $( '.plugin-card-' + response.slug ).find( '.update-now' ) .removeClass( 'updating-message' ) @@ -969,6 +973,7 @@ * Updates the UI appropriately after a successful theme update. * * @since 4.6.0 + * @since 5.5.0 Auto-update "time to next update" text cleared. * * @param {object} response * @param {string} response.slug Slug of the theme to be updated. @@ -1002,12 +1007,16 @@ // Update the version number in the row. newText = $theme.find( '.theme-version-author-uri' ).html().replace( response.oldVersion, response.newVersion ); $theme.find( '.theme-version-author-uri' ).html( newText ); + + // Clear the "time to next auto-update" text. + $theme.find( '.auto-update-time' ).empty(); } else { $notice = $( '.theme-info .notice' ).add( $theme.find( '.update-message' ) ); // Focus on Customize button after updating. if ( isModalOpen ) { $( '.load-customize:visible' ).focus(); + $( '.theme-info .theme-autoupdate' ).find( '.auto-update-time' ).empty(); } else { $theme.find( '.load-customize' ).focus(); } @@ -2461,5 +2470,144 @@ * @since 4.2.0 */ $( window ).on( 'beforeunload', wp.updates.beforeunload ); + + /** + * Click handler for enabling and disabling plugin and theme auto-updates. + * + * @since 5.5.0 + */ + $document.on( 'click', '.column-auto-updates a.toggle-auto-update, .theme-overlay a.toggle-auto-update', function( event ) { + var data, asset, type, $parent; + var $anchor = $( this ), + action = $anchor.attr( 'data-wp-action' ), + $label = $anchor.find( '.label' ); + + if ( 'themes' !== pagenow ) { + $parent = $anchor.closest( '.column-auto-updates' ); + } else { + $parent = $anchor.closest( '.theme-autoupdate' ); + } + + event.preventDefault(); + + // Prevent multiple simultaneous requests. + if ( $anchor.attr( 'data-doing-ajax' ) === 'yes' ) { + return; + } + + $anchor.attr( 'data-doing-ajax', 'yes' ); + + switch ( pagenow ) { + case 'plugins': + case 'plugins-network': + type = 'plugin'; + asset = $anchor.closest( 'tr' ).attr( 'data-plugin' ); + break; + case 'themes-network': + type = 'theme'; + asset = $anchor.closest( 'tr' ).attr( 'data-slug' ); + break; + case 'themes': + type = 'theme'; + asset = $anchor.attr( 'data-slug' ); + break; + } + + // Clear any previous errors. + $parent.find( '.notice.error' ).addClass( 'hidden' ); + + // Show loading status. + if ( 'enable' === action ) { + $label.text( wp.updates.l10n.autoUpdatesEnabling ); + } else { + $label.text( wp.updates.l10n.autoUpdatesDisabling ); + } + + $anchor.find( '.dashicons-update' ).removeClass( 'hidden' ); + + data = { + action: 'toggle-auto-updates', + _ajax_nonce: settings.ajax_nonce, + state: action, + type: type, + asset: asset + }; + + $.post( window.ajaxurl, data ) + .done( function( response ) { + var $enabled, $disabled, enabledNumber, disabledNumber, errorMessage; + var href = $anchor.attr( 'href' ); + + if ( ! response.success ) { + // if WP returns 0 for response (which can happen in a few cases), + // output the general error message since we won't have response.data.error. + if ( response.data && response.data.error ) { + errorMessage = response.data.error; + } else { + errorMessage = wp.updates.l10n.autoUpdatesError; + } + + $parent.find( '.notice.error' ).removeClass( 'hidden' ).find( 'p' ).text( errorMessage ); + wp.a11y.speak( errorMessage, 'polite' ); + return; + } + + // Update the counts in the enabled/disabled views if on a screen + // with a list table. + if ( 'themes' !== pagenow ) { + $enabled = $( '.auto-update-enabled span' ); + $disabled = $( '.auto-update-disabled span' ); + enabledNumber = parseInt( $enabled.text().replace( /[^\d]+/g, '' ), 10 ) || 0; + disabledNumber = parseInt( $disabled.text().replace( /[^\d]+/g, '' ), 10 ) || 0; + + switch ( action ) { + case 'enable': + ++enabledNumber; + --disabledNumber; + break; + case 'disable': + --enabledNumber; + ++disabledNumber; + break; + } + + enabledNumber = Math.max( 0, enabledNumber ); + disabledNumber = Math.max( 0, disabledNumber ); + + $enabled.text( '(' + enabledNumber + ')' ); + $disabled.text( '(' + disabledNumber + ')' ); + } + + if ( 'enable' === action ) { + href = href.replace( 'action=enable-auto-update', 'action=disable-auto-update' ); + $anchor.attr( { + 'data-wp-action': 'disable', + href: href + } ); + + $label.text( wp.updates.l10n.autoUpdatesDisable ); + $parent.find( '.auto-update-time' ).removeClass( 'hidden' ); + wp.a11y.speak( wp.updates.l10n.autoUpdatesEnabled, 'polite' ); + } else { + href = href.replace( 'action=disable-auto-update', 'action=enable-auto-update' ); + $anchor.attr( { + 'data-wp-action': 'enable', + href: href + } ); + + $label.text( wp.updates.l10n.autoUpdatesEnable ); + $parent.find( '.auto-update-time' ).addClass( 'hidden' ); + wp.a11y.speak( wp.updates.l10n.autoUpdatesDisabled, 'polite' ); + } + } ) + .fail( function() { + $parent.find( '.notice.error' ).removeClass( 'hidden' ).find( 'p' ).text( wp.updates.l10n.autoUpdatesError ); + wp.a11y.speak( wp.updates.l10n.autoUpdatesError, 'polite' ); + } ) + .always( function() { + $anchor.removeAttr( 'data-doing-ajax' ).find( '.dashicons-update' ).addClass( 'hidden' ); + } ); + } + ); } ); })( jQuery, window.wp, window._wpUpdatesSettings ); diff --git a/src/wp-admin/admin-ajax.php b/src/wp-admin/admin-ajax.php index 4f41a31bd6..28caf3d227 100644 --- a/src/wp-admin/admin-ajax.php +++ b/src/wp-admin/admin-ajax.php @@ -139,6 +139,7 @@ $core_actions_post = array( 'health-check-background-updates', 'health-check-loopback-requests', 'health-check-get-sizes', + 'toggle-auto-updates', ); // Deprecated. diff --git a/src/wp-admin/css/common.css b/src/wp-admin/css/common.css index 3d0dacf277..fbf69fe767 100644 --- a/src/wp-admin/css/common.css +++ b/src/wp-admin/css/common.css @@ -1524,7 +1524,9 @@ div.error { .updating-message p:before, .import-php .updating-message:before, .button.updating-message:before, -.button.installing:before { +.button.installing:before, +.plugins .column-auto-updates .dashicons-update.spin, +.theme-overlay .theme-autoupdate .dashicons-update.spin { animation: rotation 2s infinite linear; } diff --git a/src/wp-admin/css/list-tables.css b/src/wp-admin/css/list-tables.css index f621aef2ab..9aaaefebe0 100644 --- a/src/wp-admin/css/list-tables.css +++ b/src/wp-admin/css/list-tables.css @@ -1236,6 +1236,10 @@ ul.cat-checklist { width: 85px; } +.plugins .column-auto-updates { + width: 14.2em; +} + .plugins .inactive .plugin-title strong { font-weight: 400; } diff --git a/src/wp-admin/css/themes.css b/src/wp-admin/css/themes.css index a5ef219f77..7d87a06aeb 100644 --- a/src/wp-admin/css/themes.css +++ b/src/wp-admin/css/themes.css @@ -679,7 +679,8 @@ body.folded .theme-browser ~ .theme-overlay .theme-wrap { line-height: inherit; } -.theme-overlay .theme-author a { +.theme-overlay .theme-author a, +.theme-overlay .theme-autoupdate a { text-decoration: none; } diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 5b740af44c..9b74b9f4ba 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -4567,6 +4567,9 @@ function wp_ajax_delete_plugin() { function wp_ajax_search_plugins() { check_ajax_referer( 'updates' ); + // Ensure after_plugin_row_{$plugin_file} gets hooked. + wp_plugin_update_rows(); + $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; if ( 'plugins-network' === $pagenow || 'plugins' === $pagenow ) { set_current_screen( $pagenow ); @@ -5267,3 +5270,73 @@ function wp_ajax_health_check_get_sizes() { function wp_ajax_rest_nonce() { exit( wp_create_nonce( 'wp_rest' ) ); } + +/** + * Ajax handler to enable or disable plugin and theme auto-updates. + * + * @since 5.5.0 + */ +function wp_ajax_toggle_auto_updates() { + check_ajax_referer( 'updates' ); + + if ( empty( $_POST['type'] ) || empty( $_POST['asset'] ) || empty( $_POST['state'] ) ) { + wp_send_json_error( array( 'error' => __( 'Invalid data. No selected item.' ) ) ); + } + + $asset = sanitize_text_field( urldecode( $_POST['asset'] ) ); + + if ( 'enable' !== $_POST['state'] && 'disable' !== $_POST['state'] ) { + wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown state.' ) ) ); + } + $state = $_POST['state']; + + if ( 'plugin' !== $_POST['type'] && 'theme' !== $_POST['type'] ) { + wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown type.' ) ) ); + } + $type = $_POST['type']; + + switch ( $type ) { + case 'plugin': + if ( ! current_user_can( 'update_plugins' ) ) { + $error_message = __( 'You do not have permission to modify plugins.' ); + wp_send_json_error( array( 'error' => $error_message ) ); + } + + $option = 'auto_update_plugins'; + /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ + $all_items = apply_filters( 'all_plugins', get_plugins() ); + break; + case 'theme': + if ( ! current_user_can( 'update_themes' ) ) { + $error_message = __( 'You do not have permission to modify themes.' ); + wp_send_json_error( array( 'error' => $error_message ) ); + } + + $option = 'auto_update_themes'; + $all_items = wp_get_themes(); + break; + default: + wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown type.' ) ) ); + } + + if ( ! array_key_exists( $asset, $all_items ) ) { + $error_message = __( 'Invalid data. The item does not exist.' ); + wp_send_json_error( array( 'error' => $error_message ) ); + } + + $auto_updates = (array) get_site_option( $option, array() ); + + if ( 'disable' === $state ) { + $auto_updates = array_diff( $auto_updates, array( $asset ) ); + } else { + $auto_updates[] = $asset; + $auto_updates = array_unique( $auto_updates ); + } + + // Remove items that have been deleted since the site option was last updated. + $auto_updates = array_intersect( $auto_updates, array_keys( $all_items ) ); + + update_site_option( $option, $auto_updates ); + + wp_send_json_success(); +} diff --git a/src/wp-admin/includes/class-wp-automatic-updater.php b/src/wp-admin/includes/class-wp-automatic-updater.php index 3780512efa..20f1847de7 100644 --- a/src/wp-admin/includes/class-wp-automatic-updater.php +++ b/src/wp-admin/includes/class-wp-automatic-updater.php @@ -158,6 +158,14 @@ class WP_Automatic_Updater { // Next up, is this an item we can update? if ( 'core' === $type ) { $update = Core_Upgrader::should_update_to_version( $item->current ); + } elseif ( 'plugin' === $type || 'theme' === $type ) { + $update = ! empty( $item->autoupdate ); + + if ( ! $update && wp_is_auto_update_enabled_for_type( $type ) ) { + // Check if the site admin has enabled auto-updates by default for the specific item. + $auto_updates = (array) get_site_option( "auto_update_{$type}s", array() ); + $update = in_array( $item->{$type}, $auto_updates, true ); + } } else { $update = ! empty( $item->autoupdate ); } @@ -501,6 +509,8 @@ class WP_Automatic_Updater { if ( ! empty( $this->update_results['core'] ) ) { $this->after_core_update( $this->update_results['core'][0] ); + } elseif ( ! empty( $this->update_results['plugin'] ) || ! empty( $this->update_results['theme'] ) ) { + $this->after_plugin_theme_update( $this->update_results ); } /** @@ -854,6 +864,186 @@ class WP_Automatic_Updater { wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] ); } + + /** + * If we tried to perform plugin or theme updates, check if we should send an email. + * + * @since 5.5.0 + * + * @param object $results The result of updates tasks. + */ + protected function after_plugin_theme_update( $update_results ) { + $successful_updates = array(); + $failed_updates = array(); + + /** + * Filters whether to send an email following an automatic background plugin update. + * + * @since 5.5.0 + * + * @param bool $enabled True if plugins notifications are enabled, false otherwise. + */ + $notifications_enabled = apply_filters( 'auto_plugin_update_send_email', true ); + + if ( ! empty( $update_results['plugin'] ) && $notifications_enabled ) { + foreach ( $update_results['plugin'] as $update_result ) { + if ( true === $update_result->result ) { + $successful_updates['plugin'][] = $update_result; + } else { + $failed_updates['plugin'][] = $update_result; + } + } + } + + /** + * Filters whether to send an email following an automatic background theme update. + * + * @since 5.5.0 + * + * @param bool $enabled True if notifications are enabled, false otherwise. + */ + $notifications_enabled = apply_filters( 'send_theme_auto_update_email', true ); + + if ( ! empty( $update_results['theme'] ) && $notifications_enabled ) { + foreach ( $update_results['theme'] as $update_result ) { + if ( true === $update_result->result ) { + $successful_updates['theme'][] = $update_result; + } else { + $failed_updates['theme'][] = $update_result; + } + } + } + + if ( empty( $successful_updates ) && empty( $failed_updates ) ) { + return; + } + + if ( empty( $failed_updates ) ) { + $this->send_plugin_theme_email( 'success', $successful_updates, $failed_updates ); + } elseif ( empty( $successful_updates ) ) { + $this->send_plugin_theme_email( 'fail', $successful_updates, $failed_updates ); + } else { + $this->send_plugin_theme_email( 'mixed', $successful_updates, $failed_updates ); + } + } + + /** + * Sends an email upon the completion or failure of a plugin or theme background update. + * + * @since 5.5.0 + * + * @param string $type The type of email to send. Can be one of 'success', 'failure', 'mixed'. + * @param array $successful_updates A list of updates that succeeded. + * @param array $failed_updates A list of updates that failed. + */ + protected function send_plugin_theme_email( $type, $successful_updates, $failed_updates ) { + // No updates were attempted. + if ( empty( $successful_updates ) && empty( $failed_updates ) ) { + return; + } + $body = array(); + + switch ( $type ) { + case 'success': + /* translators: %s: Site title. */ + $subject = __( '[%s] Some plugins or themes were automatically updated' ); + break; + case 'fail': + /* translators: %s: Site title. */ + $subject = __( '[%s] Some plugins or themes have failed to update' ); + $body[] = sprintf( + /* translators: %s: Home URL. */ + __( 'Howdy! Failures occurred when attempting to update plugins/themes on your site at %s.' ), + home_url() + ); + $body[] = "\n"; + $body[] = __( 'Please check out your site now. It’s possible that everything is working. If it says you need to update, you should do so.' ); + break; + case 'mixed': + /* translators: %s: Site title. */ + $subject = __( '[%s] Some plugins or themes were automatically updated' ); + $body[] = sprintf( + /* translators: %s: Home URL. */ + __( 'Howdy! Failures occurred when attempting to update plugins/themes on your site at %s.' ), + home_url() + ); + $body[] = "\n"; + $body[] = __( 'Please check out your site now. It’s possible that everything is working. If it says you need to update, you should do so.' ); + $body[] = "\n"; + break; + } + + // Get failed plugin updates. + if ( in_array( $type, array( 'fail', 'mixed' ), true ) && ! empty( $failed_updates['plugin'] ) ) { + $body[] = __( 'The following plugins failed to update:' ); + // List failed updates. + foreach ( $failed_updates['plugin'] as $item ) { + $body[] = "- {$item->name}"; + } + $body[] = "\n"; + } + // Get failed theme updates. + if ( in_array( $type, array( 'fail', 'mixed' ), true ) && ! empty( $failed_updates['theme'] ) ) { + $body[] = __( 'The following themes failed to update:' ); + // List failed updates. + foreach ( $failed_updates['theme'] as $item ) { + $body[] = "- {$item->name}"; + } + $body[] = "\n"; + } + // Get successful plugin updates. + if ( in_array( $type, array( 'success', 'mixed' ), true ) && ! empty( $successful_updates['plugin'] ) ) { + $body[] = __( 'The following plugins were successfully updated:' ); + // List successful updates. + foreach ( $successful_updates['plugin'] as $item ) { + $body[] = "- {$item->name}"; + } + $body[] = "\n"; + } + // Get successful theme updates. + if ( in_array( $type, array( 'success', 'mixed' ), true ) && ! empty( $successful_updates['theme'] ) ) { + $body[] = __( 'The following themes were successfully updated:' ); + // List successful updates. + foreach ( $successful_updates['theme'] as $item ) { + $body[] = "- {$item->name}"; + } + $body[] = "\n"; + } + $body[] = "\n"; + + // Add a note about the support forums. + $body[] = __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' ); + $body[] = __( 'https://wordpress.org/support/forums/' ); + $body[] = "\n" . __( 'The WordPress Team' ); + + $body = implode( "\n", $body ); + $to = get_site_option( 'admin_email' ); + $subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ); + $headers = ''; + + $email = compact( 'to', 'subject', 'body', 'headers' ); + + /** + * Filters the email sent following an automatic background plugin update. + * + * @param array $email { + * Array of email arguments that will be passed to wp_mail(). + * + * @type string $to The email recipient. An array of emails + * can be returned, as handled by wp_mail(). + * @type string $subject The email's subject. + * @type string $body The email message body. + * @type string $headers Any email headers, defaults to no headers. + * } + * @param string $type The type of email being sent. Can be one of + * 'success', 'fail', 'mixed'. + * @param object $successful_updates The updates that succeeded. + * @param object $failed_updates The updates that failed. + */ + $email = apply_filters( 'auto_plugin_theme_update_email', $email, $type, $successful_updates, $failed_updates ); + wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] ); + } + /** * Prepares and sends an email of a full log of background update results, useful for debugging and geekery. * diff --git a/src/wp-admin/includes/class-wp-debug-data.php b/src/wp-admin/includes/class-wp-debug-data.php index 476bf55d59..41ce09ef84 100644 --- a/src/wp-admin/includes/class-wp-debug-data.php +++ b/src/wp-admin/includes/class-wp-debug-data.php @@ -858,6 +858,15 @@ class WP_Debug_Data { // List all available plugins. $plugins = get_plugins(); $plugin_updates = get_plugin_updates(); + $auto_updates = array(); + + $auto_updates_enabled = wp_is_auto_update_enabled_for_type( 'plugin' ); + $auto_updates_enabled_str = __( 'Auto-updates enabled' ); + $auto_updates_disabled_str = __( 'Auto-updates disabled' ); + + if ( $auto_updates_enabled ) { + $auto_updates = (array) get_site_option( 'auto_update_plugins', array() ); + } foreach ( $plugins as $plugin_path => $plugin ) { $plugin_part = ( is_plugin_active( $plugin_path ) ) ? 'wp-plugins-active' : 'wp-plugins-inactive'; @@ -892,6 +901,16 @@ class WP_Debug_Data { $plugin_version_string_debug .= sprintf( ' (latest version: %s)', $plugin_updates[ $plugin_path ]->update->new_version ); } + if ( $auto_updates_enabled ) { + if ( in_array( $plugin_path, $auto_updates, true ) ) { + $plugin_version_string .= ' | ' . $auto_updates_enabled_str; + $plugin_version_string_debug .= ', ' . $auto_updates_enabled_str; + } else { + $plugin_version_string .= ' | ' . $auto_updates_disabled_str; + $plugin_version_string_debug .= ', ' . $auto_updates_disabled_str; + } + } + $info[ $plugin_part ]['fields'][ sanitize_text_field( $plugin['Name'] ) ] = array( 'label' => $plugin['Name'], 'value' => $plugin_version_string, @@ -915,6 +934,12 @@ class WP_Debug_Data { $active_theme_version = $active_theme->version; $active_theme_version_debug = $active_theme_version; + $auto_updates = array(); + $auto_updates_enabled = wp_is_auto_update_enabled_for_type( 'theme' ); + if ( $auto_updates_enabled ) { + $auto_updates = (array) get_site_option( 'auto_update_themes', array() ); + } + if ( array_key_exists( $active_theme->stylesheet, $theme_updates ) ) { $theme_update_new_version = $theme_updates[ $active_theme->stylesheet ]->update['new_version']; @@ -980,7 +1005,19 @@ class WP_Debug_Data { 'value' => get_stylesheet_directory(), ), ); + if ( $auto_updates_enabled ) { + if ( in_array( $active_theme->stylesheet, $auto_updates ) ) { + $theme_auto_update_string = __( 'Enabled' ); + } else { + $theme_auto_update_string = __( 'Disabled' ); + } + $info['wp-active-theme']['fields']['auto_update'] = array( + 'label' => __( 'Auto-update' ), + 'value' => $theme_auto_update_string, + 'debug' => $theme_auto_update_string, + ); + } $parent_theme = $active_theme->parent(); if ( $parent_theme ) { @@ -1026,6 +1063,19 @@ class WP_Debug_Data { 'value' => get_template_directory(), ), ); + if ( $auto_updates_enabled ) { + if ( in_array( $parent_theme->stylesheet, $auto_updates ) ) { + $parent_theme_auto_update_string = __( 'Enabled' ); + } else { + $parent_theme_auto_update_string = __( 'Disabled' ); + } + + $info['wp-parent-theme']['fields']['auto_update'] = array( + 'label' => __( 'Auto-update' ), + 'value' => $parent_theme_auto_update_string, + 'debug' => $parent_theme_auto_update_string, + ); + } } // Populate a list of all themes available in the install. @@ -1075,6 +1125,16 @@ class WP_Debug_Data { $theme_version_string_debug .= sprintf( ' (latest version: %s)', $theme_updates[ $theme_slug ]->update['new_version'] ); } + if ( $auto_updates_enabled ) { + if ( in_array( $theme_slug, $auto_updates ) ) { + $theme_version_string .= ' | ' . $auto_updates_enabled_str; + $theme_version_string_debug .= ',' . $auto_updates_enabled_str; + } else { + $theme_version_string .= ' | ' . $auto_updates_disabled_str; + $theme_version_string_debug .= ', ' . $auto_updates_disabled_str; + } + } + $info['wp-themes-inactive']['fields'][ sanitize_text_field( $theme->name ) ] = array( 'label' => sprintf( /* translators: 1: Theme name. 2: Theme slug. */ diff --git a/src/wp-admin/includes/class-wp-ms-themes-list-table.php b/src/wp-admin/includes/class-wp-ms-themes-list-table.php index 169f783ec8..11ef7391de 100644 --- a/src/wp-admin/includes/class-wp-ms-themes-list-table.php +++ b/src/wp-admin/includes/class-wp-ms-themes-list-table.php @@ -22,6 +22,15 @@ class WP_MS_Themes_List_Table extends WP_List_Table { private $has_items; + /** + * Whether to show the auto-updates UI. + * + * @since 5.5.0 + * + * @var bool True if auto-updates UI is to be shown, false otherwise. + */ + protected $show_autoupdates = true; + /** * Constructor. * @@ -45,7 +54,7 @@ class WP_MS_Themes_List_Table extends WP_List_Table { ); $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all'; - if ( ! in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken' ), true ) ) { + if ( ! in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken', 'auto-update-enabled', 'auto-update-disabled' ), true ) ) { $status = 'all'; } @@ -56,6 +65,9 @@ class WP_MS_Themes_List_Table extends WP_List_Table { if ( $this->is_site_themes ) { $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0; } + + $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'theme' ) && + ! $this->is_site_themes && current_user_can( 'update_themes' ); } /** @@ -107,6 +119,13 @@ class WP_MS_Themes_List_Table extends WP_List_Table { 'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ), ); + if ( $this->show_autoupdates ) { + $auto_updates = (array) get_site_option( 'auto_update_themes', array() ); + + $themes['auto-update-enabled'] = array(); + $themes['auto-update-disabled'] = array(); + } + if ( $this->is_site_themes ) { $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' ); $allowed_where = 'site'; @@ -131,6 +150,14 @@ class WP_MS_Themes_List_Table extends WP_List_Table { $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled'; $themes[ $filter ][ $key ] = $themes['all'][ $key ]; + + if ( $this->show_autoupdates ) { + if ( in_array( $key, $auto_updates, true ) ) { + $themes['auto-update-enabled'][ $key ] = $themes['all'][ $key ]; + } else { + $themes['auto-update-disabled'][ $key ] = $themes['all'][ $key ]; + } + } } if ( $s ) { @@ -257,11 +284,17 @@ class WP_MS_Themes_List_Table extends WP_List_Table { * @return array */ public function get_columns() { - return array( + $columns = array( 'cb' => '', 'name' => __( 'Theme' ), 'description' => __( 'Description' ), ); + + if ( $this->show_autoupdates ) { + $columns['auto-updates'] = __( 'Automatic Updates' ); + } + + return $columns; } /** @@ -344,6 +377,22 @@ class WP_MS_Themes_List_Table extends WP_List_Table { 'themes' ); break; + case 'auto-update-enabled': + /* translators: %s: Number of themes. */ + $text = _n( + 'Auto-updates Enabled (%s)', + 'Auto-updates Enabled (%s)', + $count + ); + break; + case 'auto-update-disabled': + /* translators: %s: Number of themes. */ + $text = _n( + 'Auto-updates Disabled (%s)', + 'Auto-updates Disabled (%s)', + $count + ); + break; } if ( $this->is_site_themes ) { @@ -388,6 +437,17 @@ class WP_MS_Themes_List_Table extends WP_List_Table { $actions['delete-selected'] = __( 'Delete' ); } } + + if ( $this->show_autoupdates ) { + if ( 'auto-update-enabled' !== $status ) { + $actions['enable-auto-update-selected'] = __( 'Enable Auto-updates' ); + } + + if ( 'auto-update-disabled' !== $status ) { + $actions['disable-auto-update-selected'] = __( 'Disable Auto-updates' ); + } + } + return $actions; } @@ -639,6 +699,70 @@ class WP_MS_Themes_List_Table extends WP_List_Table { echo ''; } + /** + * Handles the auto-updates column output. + * + * @since 5.5.0 + * + * @global string $status + * @global int $page + * + * @param WP_Theme $theme The current WP_Theme object. + */ + public function column_autoupdates( $theme ) { + global $status, $page; + + static $auto_updates, $available_updates; + + if ( ! $auto_updates ) { + $auto_updates = (array) get_site_option( 'auto_update_themes', array() ); + } + if ( ! $available_updates ) { + $available_updates = get_site_transient( 'update_themes' ); + } + + $stylesheet = $theme->get_stylesheet(); + + if ( in_array( $stylesheet, $auto_updates, true ) ) { + $text = __( 'Disable auto-updates' ); + $action = 'disable'; + $time_class = ''; + } else { + $text = __( 'Enable auto-updates' ); + $action = 'enable'; + $time_class = ' hidden'; + } + + $query_args = array( + 'action' => "{$action}-auto-update", + 'theme' => $stylesheet, + 'paged' => $page, + 'theme_status' => $status, + ); + + $url = add_query_arg( $query_args, 'themes.php' ); + + printf( + '', + wp_nonce_url( $url, 'updates' ), + $action + ); + + echo ''; + echo '' . $text . ''; + echo ''; + + $available_updates = get_site_transient( 'update_themes' ); + if ( isset( $available_updates->response[ $stylesheet ] ) ) { + printf( + '
' . sprintf( $message, number_format_i18n( $enabled ) ) . '
'; } elseif ( isset( $_GET['disabled'] ) ) { $disabled = absint( $_GET['disabled'] ); - if ( 1 == $disabled ) { + if ( 1 === $disabled ) { $message = __( 'Theme disabled.' ); } else { /* translators: %s: Number of themes. */ @@ -302,16 +363,34 @@ if ( isset( $_GET['enabled'] ) ) { echo '' . sprintf( $message, number_format_i18n( $disabled ) ) . '
' . sprintf( $message, number_format_i18n( $deleted ) ) . '
' . sprintf( $message, number_format_i18n( $enabled ) ) . '
' . sprintf( $message, number_format_i18n( $disabled ) ) . '
' . __( 'No theme selected.' ) . '
' . __( 'You cannot delete a theme while it is active on the main site.' ) . '
' . __( 'The following themes are installed but incomplete.' ) . '
'; } ?> diff --git a/src/wp-admin/plugins.php b/src/wp-admin/plugins.php index 8352e207ab..62726ba12f 100644 --- a/src/wp-admin/plugins.php +++ b/src/wp-admin/plugins.php @@ -22,7 +22,21 @@ $plugin = isset( $_REQUEST['plugin'] ) ? wp_unslash( $_REQUEST['plugin'] ) : ''; $s = isset( $_REQUEST['s'] ) ? urlencode( wp_unslash( $_REQUEST['s'] ) ) : ''; // Clean up request URI from temporary args for screen options/paging uri's to work as expected. -$_SERVER['REQUEST_URI'] = remove_query_arg( array( 'error', 'deleted', 'activate', 'activate-multi', 'deactivate', 'deactivate-multi', '_error_nonce' ), $_SERVER['REQUEST_URI'] ); +$query_args_to_remove = array( + 'error', + 'deleted', + 'activate', + 'activate-multi', + 'deactivate', + 'deactivate-multi', + 'enabled-auto-update', + 'disabled-auto-update', + 'enabled-auto-update-multi', + 'disabled-auto-update-multi', + '_error_nonce', +); + +$_SERVER['REQUEST_URI'] = remove_query_arg( $query_args_to_remove, $_SERVER['REQUEST_URI'] ); wp_enqueue_script( 'updates' ); @@ -284,11 +298,14 @@ if ( $action ) { if ( ! isset( $_REQUEST['verify-delete'] ) ) { wp_enqueue_script( 'jquery' ); require_once ABSPATH . 'wp-admin/admin-header.php'; + ?> -
-
fatal error.' ); ?>
+ <# if ( data.autoupdate ) { #>
+
+
+
+
+ <# } else { #>
+
+
+
+
+ <# } #>
+ <# if ( data.hasUpdate ) { #>
+ <# if ( data.autoupdate) { #>
+
+ <# } else { #>
+
+ <# } #>
+ <# } #>
+
+