Make WordPress Core


Ignore:
Timestamp:
05/20/2020 06:47:24 PM (6 years ago)
Author:
whyisjake
Message:

Security: Add user interface to auto-update themes and plugins.

Building on core update mechanisms, this adds the ability to enable automatic updates for themes and plugins to the WordPress admin.

Fixes: #50052.
Props: afercia, afragen, audrasjb, azaozz, bookdude13, davidperonne, desrosj, gmays, gmays, javiercasares, karmatosed, knutsp, mapk, mukesh27, netweb, nicolaskulka, nielsdeblaauw, paaljoachim, passoniate, pbiron, pedromendonca, whodunitagency, whyisjake, wpamitkumar, and xkon.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-plugins-list-table.php

    r47808 r47835  
    1717 */
    1818class WP_Plugins_List_Table extends WP_List_Table {
     19        /**
     20         * Whether to show the auto-updates UI.
     21         *
     22         * @since 5.5.0
     23         *
     24         * @var bool True if auto-updates UI is to be shown, false otherwise.
     25         */
     26        protected $show_autoupdates = true;
    1927
    2028        /**
     
    4048                );
    4149
    42                 $status_whitelist = array( 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search', 'paused' );
     50                $status_whitelist = array( 'active', 'inactive', 'recently_activated', 'upgrade', 'mustuse', 'dropins', 'search', 'paused', 'auto-update-enabled', 'auto-update-disabled' );
    4351
    4452                $status = 'all';
     
    5260
    5361                $page = $this->get_pagenum();
     62
     63                $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'plugin' ) &&
     64                        current_user_can( 'update_plugins' ) &&
     65                        ( ! is_multisite() || $this->screen->in_admin( 'network' ) );
    5466        }
    5567
     
    104116                        'paused'             => array(),
    105117                );
     118                if ( $this->show_autoupdates ) {
     119                        $auto_updates = (array) get_site_option( 'auto_update_plugins', array() );
     120
     121                        $plugins['auto-update-enabled']  = array();
     122                        $plugins['auto-update-disabled'] = array();
     123                }
    106124
    107125                $screen = $this->screen;
     
    234252                                $plugins['inactive'][ $plugin_file ] = $plugin_data;
    235253                        }
     254
     255                        if ( $this->show_autoupdates ) {
     256                                if ( in_array( $plugin_file, $auto_updates, true ) ) {
     257                                        $plugins['auto-update-enabled'][ $plugin_file ] = $plugins['all'][ $plugin_file ];
     258                                } else {
     259                                        $plugins['auto-update-disabled'][ $plugin_file ] = $plugins['all'][ $plugin_file ];
     260                                }
     261                        }
    236262                }
    237263
     
    400426                global $status;
    401427
    402                 return array(
     428                $columns = array(
    403429                        'cb'          => ! in_array( $status, array( 'mustuse', 'dropins' ), true ) ? '<input type="checkbox" />' : '',
    404430                        'name'        => __( 'Plugin' ),
    405431                        'description' => __( 'Description' ),
    406432                );
     433
     434                if ( $this->show_autoupdates ) {
     435                        $columns['auto-updates'] = __( 'Automatic Updates' );
     436                }
     437
     438                return $columns;
    407439        }
    408440
     
    494526                                        );
    495527                                        break;
     528                                case 'auto-update-enabled':
     529                                        /* translators: %s: Number of plugins. */
     530                                        $text = _n(
     531                                                'Auto-updates Enabled <span class="count">(%s)</span>',
     532                                                'Auto-updates Enabled <span class="count">(%s)</span>',
     533                                                $count
     534                                        );
     535                                        break;
     536                                case 'auto-update-disabled':
     537                                        /* translators: %s: Number of plugins. */
     538                                        $text = _n(
     539                                                'Auto-updates Disabled <span class="count">(%s)</span>',
     540                                                'Auto-updates Disabled <span class="count">(%s)</span>',
     541                                                $count
     542                                        );
     543                                        break;
    496544                        }
    497545
     
    533581                        if ( current_user_can( 'delete_plugins' ) && ( 'active' !== $status ) ) {
    534582                                $actions['delete-selected'] = __( 'Delete' );
     583                        }
     584
     585                        if ( $this->show_autoupdates ) {
     586                                if ( 'auto-update-enabled' !== $status ) {
     587                                        $actions['enable-auto-update-selected'] = __( 'Enable Auto-updates' );
     588                                }
     589                                if ( 'auto-update-disabled' !== $status ) {
     590                                        $actions['disable-auto-update-selected'] = __( 'Disable Auto-updates' );
     591                                }
    535592                        }
    536593                }
     
    882939
    883940                list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
     941
     942                $auto_updates      = (array) get_site_option( 'auto_update_plugins', array() );
     943                $available_updates = get_site_transient( 'update_plugins' );
    884944
    885945                foreach ( $columns as $column_name => $column_display_name ) {
     
    9751035                                        echo '</td>';
    9761036                                        break;
     1037                                case 'auto-updates':
     1038                                        if ( ! $this->show_autoupdates ) {
     1039                                                break;
     1040                                        }
     1041
     1042                                        echo "<td class='column-auto-updates{$extra_classes}'>";
     1043
     1044                                        if ( in_array( $plugin_file, $auto_updates, true ) ) {
     1045                                                $text       = __( 'Disable auto-updates' );
     1046                                                $action     = 'disable';
     1047                                                $time_class = '';
     1048                                        } else {
     1049                                                $text       = __( 'Enable auto-updates' );
     1050                                                $action     = 'enable';
     1051                                                $time_class = ' hidden';
     1052                                        }
     1053
     1054                                        $query_args = array(
     1055                                                'action'        => "{$action}-auto-update",
     1056                                                'plugin'        => $plugin_file,
     1057                                                'paged'         => $page,
     1058                                                'plugin_status' => $status,
     1059                                        );
     1060
     1061                                        $url = add_query_arg( $query_args, 'plugins.php' );
     1062
     1063                                        printf(
     1064                                                '<a href="%s" class="toggle-auto-update" data-wp-action="%s">',
     1065                                                wp_nonce_url( $url, 'updates' ),
     1066                                                $action
     1067                                        );
     1068
     1069                                        echo '<span class="dashicons dashicons-update spin hidden"></span>';
     1070                                        echo '<span class="label">' . $text . '</span>';
     1071                                        echo '</a>';
     1072
     1073                                        $available_updates = get_site_transient( 'update_plugins' );
     1074
     1075                                        if ( isset( $available_updates->response[ $plugin_file ] ) ) {
     1076                                                printf(
     1077                                                        '<div class="auto-update-time%s">%s</div>',
     1078                                                        $time_class,
     1079                                                        wp_get_auto_update_message()
     1080                                                );
     1081                                        }
     1082
     1083                                        echo '<div class="inline notice error hidden"><p></p></div>';
     1084                                        echo '</td>';
     1085
     1086                                        break;
    9771087                                default:
    9781088                                        $classes = "$column_name column-$column_name $class";
     
    10011111                 *
    10021112                 * @since 2.3.0
     1113                 * @since 5.5.0 Added 'Auto-updates Enabled' and 'Auto-updates Disabled' `$status`.
    10031114                 *
    10041115                 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
     
    10061117                 * @param string $status      Status of the plugin. Defaults are 'All', 'Active',
    10071118                 *                            'Inactive', 'Recently Activated', 'Upgrade', 'Must-Use',
    1008                  *                            'Drop-ins', 'Search', 'Paused'.
     1119                 *                            'Drop-ins', 'Search', 'Paused', 'Auto-updates Enabled',
     1120                 *                            'Auto-updates Disabled'.
    10091121                 */
    10101122                do_action( 'after_plugin_row', $plugin_file, $plugin_data, $status );
     
    10171129                 *
    10181130                 * @since 2.7.0
     1131                 * @since 5.5.0 Added 'Auto-updates Enabled' and 'Auto-updates Disabled' `$status`.
    10191132                 *
    10201133                 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
     
    10221135                 * @param string $status      Status of the plugin. Defaults are 'All', 'Active',
    10231136                 *                            'Inactive', 'Recently Activated', 'Upgrade', 'Must-Use',
    1024                  *                            'Drop-ins', 'Search', 'Paused'.
     1137                 *                            'Drop-ins', 'Search', 'Paused', 'Auto-updates Enabled',
     1138                 *                            'Auto-updates Disabled'.
    10251139                 */
    10261140                do_action( "after_plugin_row_{$plugin_file}", $plugin_file, $plugin_data, $status );
Note: See TracChangeset for help on using the changeset viewer.