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-ms-themes-list-table.php

    r47808 r47835  
    2424
    2525        /**
     26         * Whether to show the auto-updates UI.
     27         *
     28         * @since 5.5.0
     29         *
     30         * @var bool True if auto-updates UI is to be shown, false otherwise.
     31         */
     32        protected $show_autoupdates = true;
     33
     34        /**
    2635         * Constructor.
    2736         *
     
    4655
    4756                $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
    48                 if ( ! in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken' ), true ) ) {
     57                if ( ! in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken', 'auto-update-enabled', 'auto-update-disabled' ), true ) ) {
    4958                        $status = 'all';
    5059                }
     
    5766                        $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
    5867                }
     68
     69                $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'theme' ) &&
     70                        ! $this->is_site_themes && current_user_can( 'update_themes' );
    5971        }
    6072
     
    108120                );
    109121
     122                if ( $this->show_autoupdates ) {
     123                        $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
     124
     125                        $themes['auto-update-enabled']  = array();
     126                        $themes['auto-update-disabled'] = array();
     127                }
     128
    110129                if ( $this->is_site_themes ) {
    111130                        $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
     
    132151                        $filter                    = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
    133152                        $themes[ $filter ][ $key ] = $themes['all'][ $key ];
     153
     154                        if ( $this->show_autoupdates ) {
     155                                if ( in_array( $key, $auto_updates, true ) ) {
     156                                        $themes['auto-update-enabled'][ $key ] = $themes['all'][ $key ];
     157                                } else {
     158                                        $themes['auto-update-disabled'][ $key ] = $themes['all'][ $key ];
     159                                }
     160                        }
    134161                }
    135162
     
    258285         */
    259286        public function get_columns() {
    260                 return array(
     287                $columns = array(
    261288                        'cb'          => '<input type="checkbox" />',
    262289                        'name'        => __( 'Theme' ),
    263290                        'description' => __( 'Description' ),
    264291                );
     292
     293                if ( $this->show_autoupdates ) {
     294                        $columns['auto-updates'] = __( 'Automatic Updates' );
     295                }
     296
     297                return $columns;
    265298        }
    266299
     
    345378                                        );
    346379                                        break;
     380                                case 'auto-update-enabled':
     381                                        /* translators: %s: Number of themes. */
     382                                        $text = _n(
     383                                                'Auto-updates Enabled <span class="count">(%s)</span>',
     384                                                'Auto-updates Enabled <span class="count">(%s)</span>',
     385                                                $count
     386                                        );
     387                                        break;
     388                                case 'auto-update-disabled':
     389                                        /* translators: %s: Number of themes. */
     390                                        $text = _n(
     391                                                'Auto-updates Disabled <span class="count">(%s)</span>',
     392                                                'Auto-updates Disabled <span class="count">(%s)</span>',
     393                                                $count
     394                                        );
     395                                        break;
    347396                        }
    348397
     
    389438                        }
    390439                }
     440
     441                if ( $this->show_autoupdates ) {
     442                        if ( 'auto-update-enabled' !== $status ) {
     443                                $actions['enable-auto-update-selected'] = __( 'Enable Auto-updates' );
     444                        }
     445
     446                        if ( 'auto-update-disabled' !== $status ) {
     447                                $actions['disable-auto-update-selected'] = __( 'Disable Auto-updates' );
     448                        }
     449                }
     450
    391451                return $actions;
    392452        }
     
    641701
    642702        /**
     703         * Handles the auto-updates column output.
     704         *
     705         * @since 5.5.0
     706         *
     707         * @global string $status
     708         * @global int  $page
     709         *
     710         * @param WP_Theme $theme The current WP_Theme object.
     711         */
     712        public function column_autoupdates( $theme ) {
     713                global $status, $page;
     714
     715                static $auto_updates, $available_updates;
     716
     717                if ( ! $auto_updates ) {
     718                        $auto_updates = (array) get_site_option( 'auto_update_themes', array() );
     719                }
     720                if ( ! $available_updates ) {
     721                        $available_updates = get_site_transient( 'update_themes' );
     722                }
     723
     724                $stylesheet = $theme->get_stylesheet();
     725
     726                if ( in_array( $stylesheet, $auto_updates, true ) ) {
     727                        $text       = __( 'Disable auto-updates' );
     728                        $action     = 'disable';
     729                        $time_class = '';
     730                } else {
     731                        $text       = __( 'Enable auto-updates' );
     732                        $action     = 'enable';
     733                        $time_class = ' hidden';
     734                }
     735
     736                $query_args = array(
     737                        'action'       => "{$action}-auto-update",
     738                        'theme'        => $stylesheet,
     739                        'paged'        => $page,
     740                        'theme_status' => $status,
     741                );
     742
     743                $url = add_query_arg( $query_args, 'themes.php' );
     744
     745                printf(
     746                        '<a href="%s" class="toggle-auto-update" data-wp-action="%s">',
     747                        wp_nonce_url( $url, 'updates' ),
     748                        $action
     749                );
     750
     751                echo '<span class="dashicons dashicons-update spin hidden"></span>';
     752                echo '<span class="label">' . $text . '</span>';
     753                echo '</a>';
     754
     755                $available_updates = get_site_transient( 'update_themes' );
     756                if ( isset( $available_updates->response[ $stylesheet ] ) ) {
     757                        printf(
     758                                '<div class="auto-update-time%s">%s</div>',
     759                                $time_class,
     760                                wp_get_auto_update_message()
     761                        );
     762                }
     763                echo '<div class="auto-updates-error inline notice error hidden"><p></p></div>';
     764        }
     765
     766        /**
    643767         * Handles default column output.
    644768         *
     
    722846                                        break;
    723847
     848                                case 'auto-updates':
     849                                        echo "<td class='column-auto-updates{$extra_classes}'>";
     850
     851                                        $this->column_autoupdates( $item );
     852
     853                                        echo '</td>';
     854                                        break;
    724855                                default:
    725856                                        echo "<td class='$column_name column-$column_name{$extra_classes}'>";
Note: See TracChangeset for help on using the changeset viewer.