Make WordPress Core

Ticket #51129: 51129.2.diff

File 51129.2.diff, 5.1 KB (added by pbiron, 5 years ago)
  • src/wp-admin/includes/class-wp-ms-themes-list-table.php

    From 6c6ad879b2aad4a8f46ec6532289c502fc7fa511 Mon Sep 17 00:00:00 2001
    From: Paul Biron <paul@sparrowhawkcomputing.com>
    Date: Wed, 26 Aug 2020 13:29:44 -0600
    Subject: [PATCH] Upgrade/Install: Enhance auto-updates to be disabled for
     multisite themes that don't support 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.
    ---
     .../class-wp-ms-themes-list-table.php         | 82 ++++++++++++++++---
     1 file changed, 69 insertions(+), 13 deletions(-)
    
    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 f67c7bd693..e2167edaca 100644
    a b class WP_MS_Themes_List_Table extends WP_List_Table { 
    151151                        $filter                    = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
    152152                        $themes[ $filter ][ $key ] = $themes['all'][ $key ];
    153153
     154                        $theme_data = array();
     155                        // Extra info if known. array_merge() ensures $theme_data has precedence if keys collide.
     156                        if ( isset( $current->response[ $key ] ) ) {
     157                                $theme_data = array_merge( (array) $current->response[ $key ], array( 'update_supported' => isset( $theme->update_supported ) ? $theme->update_supported : true ) );
     158                        } elseif ( isset( $current->no_update[ $key ] ) ) {
     159                                $theme_data = array_merge( (array) $current->no_update[ $key ], array( 'update_supported' => isset( $theme->update_supported ) ? $theme->update_supported : true ) );
     160                        } else {
     161                                $theme_data['update_supported'] = false;
     162                        }
     163                        $theme->update_supported = $theme_data['update_supported'];
     164
     165                        /*
     166                         * Create the expected payload for the auto_update_theme filter, this is the same data
     167                         * as contained within $updates or $no_updates but used when the Theme is not known.
     168                         */
     169                        $filter_payload = array(
     170                                'theme'            => $key,
     171                                'new_version'      => '',
     172                                'url'              => '',
     173                                'package'          => '',
     174                                'requires'         => '',
     175                                'requires_php'     => '',
     176                        );
     177
     178                        $filter_payload = array_merge( $filter_payload, array_intersect_key( $theme_data, $filter_payload ) );
     179
     180                        $type = 'theme';
     181                        /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
     182                        $auto_update_forced = apply_filters( "auto_update_{$type}", null, (object) $filter_payload );
     183
     184                        if ( ! is_null( $auto_update_forced ) ) {
     185                                $theme->auto_update_forced = $auto_update_forced;
     186                        }
     187
    154188                        if ( $this->show_autoupdates ) {
    155                                 if ( in_array( $key, $auto_updates, true ) ) {
    156                                         $themes['auto-update-enabled'][ $key ] = $themes['all'][ $key ];
     189                                $enabled = in_array( $key, $auto_updates, true ) && $theme->update_supported;
     190                                if ( isset( $theme->auto_update_forced ) ) {
     191                                        $enabled = (bool) $theme->auto_update_forced;
     192                                }
     193
     194                                if ( $enabled ) {
     195                                        $themes['auto-update-enabled'][ $key ] = $theme;
    157196                                } else {
    158                                         $themes['auto-update-disabled'][ $key ] = $themes['all'][ $key ];
     197                                        $themes['auto-update-disabled'][ $key ] = $theme;
    159198                                }
    160199                        }
    161200                }
    class WP_MS_Themes_List_Table extends WP_List_Table { 
    728767
    729768                $stylesheet = $theme->get_stylesheet();
    730769
    731                 if ( in_array( $stylesheet, $auto_updates, true ) ) {
     770                if ( isset( $theme->auto_update_forced ) ) {
     771                        if ( $theme->auto_update_forced ) {
     772                                // Forced on.
     773                                $text = __( 'Auto-updates enabled' );
     774                        } else {
     775                                $text = __( 'Auto-updates disabled' );
     776                        }
     777                        $action     = 'unavailable';
     778                        $time_class = ' hidden';
     779                } elseif ( empty( $theme->update_supported ) ) {
     780                        $text       = '';
     781                        $action     = 'unavailable';
     782                        $time_class = ' hidden';
     783                } elseif ( in_array( $stylesheet, $auto_updates, true ) ) {
    732784                        $text       = __( 'Disable auto-updates' );
    733785                        $action     = 'disable';
    734786                        $time_class = '';
    class WP_MS_Themes_List_Table extends WP_List_Table { 
    747799
    748800                $url = add_query_arg( $query_args, 'themes.php' );
    749801
    750                 $html[] = sprintf(
    751                         '<a href="%s" class="toggle-auto-update aria-button-if-js" data-wp-action="%s">',
    752                         wp_nonce_url( $url, 'updates' ),
    753                         $action
    754                 );
     802                if ( 'unavailable' == $action ) {
     803                        $html[] = '<span class="label">' . $text . '</span>';
     804                } else {
     805                        $html[] = sprintf(
     806                                '<a href="%s" class="toggle-auto-update aria-button-if-js" data-wp-action="%s">',
     807                                wp_nonce_url( $url, 'updates' ),
     808                                $action
     809                        );
    755810
    756                 $html[] = '<span class="dashicons dashicons-update spin hidden" aria-hidden="true"></span>';
    757                 $html[] = '<span class="label">' . $text . '</span>';
    758                 $html[] = '</a>';
     811                        $html[] = '<span class="dashicons dashicons-update spin hidden" aria-hidden="true"></span>';
     812                        $html[] = '<span class="label">' . $text . '</span>';
     813                        $html[] = '</a>';
     814
     815                }
    759816
    760                 $available_updates = get_site_transient( 'update_themes' );
    761817                if ( isset( $available_updates->response[ $stylesheet ] ) ) {
    762818                        $html[] = sprintf(
    763819                                '<div class="auto-update-time%s">%s</div>',