WordPress.org

Make WordPress Core

Ticket #39221: 39221.1.diff

File 39221.1.diff, 7.4 KB (added by stubgo, 4 years ago)

Updated with a fix.

  • src/wp-includes/class-wp-customize-manager.php

    diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php
    index 62d587bf6a..a0964e12c1 100644
    a b  
    193193        protected $messenger_channel;
    194194
    195195        /**
     196         * If settings should be previewed.
     197         *
     198         * @since 4.?.?
     199         * @access protected
     200         * @var bool
     201         */
     202        protected $settings_previewed;
     203
     204        /**
    196205         * Unsanitized values for Customize Settings parsed from $_POST['customized'].
    197206         *
    198207         * @var array
     
    235244         * @param array $args {
    236245         *     Args.
    237246         *
    238          *     @type string $changeset_uuid    Changeset UUID, the post_name for the customize_changeset post containing the customized state. Defaults to new UUID.
    239          *     @type string $theme             Theme to be previewed (for theme switch). Defaults to customize_theme or theme query params.
    240          *     @type string $messenger_channel Messenger channel. Defaults to customize_messenger_channel query param.
     247         *     @type string $changeset_uuid     Changeset UUID, the post_name for the customize_changeset post containing the customized state. Defaults to new UUID.
     248         *     @type string $theme              Theme to be previewed (for theme switch). Defaults to customize_theme or theme query params.
     249         *     @type string $messenger_channel  Messenger channel. Defaults to customize_messenger_channel query param.
     250         *     @type bool   $settings_previewed If settings should be previewed. Defaults to true.
    241251         * }
    242252         */
    243253        public function __construct( $args = array() ) {
    244254
    245255                $args = array_merge(
    246                         array_fill_keys( array( 'changeset_uuid', 'theme', 'messenger_channel' ), null ),
     256                        array_fill_keys( array( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed' ), null ),
    247257                        $args
    248258                );
    249259
    public function __construct( $args = array() ) { 
    264274                        $args['messenger_channel'] = sanitize_key( wp_unslash( $_REQUEST['customize_messenger_channel'] ) );
    265275                }
    266276
     277                if ( ! isset( $args['settings_previewed'] ) ) {
     278                    $args['settings_previewed'] = true;
     279        }
     280
    267281                $this->original_stylesheet = get_stylesheet();
    268282                $this->theme = wp_get_theme( $args['theme'] );
    269283                $this->messenger_channel = $args['messenger_channel'];
     284                $this->settings_previewed = ! empty( $args['settings_previewed'] );
    270285                $this->_changeset_uuid = $args['changeset_uuid'];
    271286
    272287                require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
    public function stop_previewing_theme() { 
    645660        }
    646661
    647662        /**
     663         * If should preview settings.
     664         *
     665         * @since 4.?.?
     666         * @access public
     667         *
     668         * @return bool
     669         */
     670        public function settings_previewed() {
     671                return $this->settings_previewed;
     672        }
     673
     674        /**
    648675         * Get the changeset UUID.
    649676         *
    650677         * @since 4.7.0
    public function wp_loaded() { 
    753780                 */
    754781                do_action( 'customize_register', $this );
    755782
    756                 /*
    757                  * Note that settings must be previewed here even outside the customizer preview
    758                  * and also in the customizer pane itself. This is to enable loading an existing
    759                  * changeset into the customizer. Previewing the settings only has to be prevented
    760                  * in the case of a customize_save action because then update_option()
    761                  * may short-circuit because it will detect that there are no changes to
    762                  * make.
    763                  */
    764                 if ( ! $this->doing_ajax( 'customize_save' ) ) {
     783                if ( $this->settings_previewed() ) {
    765784                        foreach ( $this->settings as $setting ) {
    766785                                $setting->preview();
    767786                        }
  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git a/src/wp-includes/class-wp-customize-nav-menus.php b/src/wp-includes/class-wp-customize-nav-menus.php
    index e263e3f391..b056e5ceec 100644
    a b public function customize_register() { 
    538538                                $nav_menus_setting_ids[] = $setting_id;
    539539                        }
    540540                }
    541                 $this->manager->add_dynamic_settings( $nav_menus_setting_ids );
    542                 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) {
    543                         foreach ( $nav_menus_setting_ids as $setting_id ) {
    544                                 $setting = $this->manager->get_setting( $setting_id );
    545                                 if ( $setting ) {
    546                                         $setting->preview();
    547                                 }
     541                $settings = $this->manager->add_dynamic_settings( $nav_menus_setting_ids );
     542                if ( $this->manager->settings_previewed() ) {
     543                        foreach ( $settings as $setting ) {
     544                                $setting->preview();
    548545                        }
    549546                }
    550547
  • src/wp-includes/class-wp-customize-widgets.php

    diff --git a/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php
    index 40590f0818..134591c712 100644
    a b public function register_settings() { 
    207207
    208208                $settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) );
    209209
    210                 /*
    211                  * Preview settings right away so that widgets and sidebars will get registered properly.
    212                  * But don't do this if a customize_save because this will cause WP to think there is nothing
    213                  * changed that needs to be saved.
    214                  */
    215                 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) {
     210                if ( $this->manager->settings_previewed() ) {
    216211                        foreach ( $settings as $setting ) {
    217212                                $setting->preview();
    218213                        }
    public function customize_register() { 
    510505                        }
    511506                }
    512507
    513                 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) {
     508                if ( $this->manager->settings_previewed() ) {
    514509                        foreach ( $new_setting_ids as $new_setting_id ) {
    515510                                $this->manager->get_setting( $new_setting_id )->preview();
    516511                        }
  • src/wp-includes/theme.php

    diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php
    index e5cdd952b8..8ef82d7141 100644
    a b function _wp_customize_include() { 
    28162816                $messenger_channel = sanitize_key( $input_vars['customize_messenger_channel'] );
    28172817        }
    28182818
     2819        /*
     2820         * Note that settings must be previewed even outside the customizer preview
     2821         * and also in the customizer pane itself. This is to enable loading an existing
     2822         * changeset into the customizer. Previewing the settings only has to be prevented
     2823         * here in the case of a customize_save action because this will cause WP to think
     2824         * there is nothing changed that needs to be saved.
     2825         */
     2826        $is_customize_save_action = (
     2827                wp_doing_ajax()
     2828                &&
     2829                isset( $_REQUEST['action'] )
     2830                &&
     2831                'customize_save' === wp_unslash( $_REQUEST['action'] )
     2832        );
     2833        $settings_previewed = ! $is_customize_save_action;
     2834
    28192835        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
    2820         $GLOBALS['wp_customize'] = new WP_Customize_Manager( compact( 'changeset_uuid', 'theme', 'messenger_channel' ) );
     2836        $GLOBALS['wp_customize'] = new WP_Customize_Manager( compact( 'changeset_uuid', 'theme', 'messenger_channel', 'settings_previewed' ) );
    28212837}
    28222838
    28232839/**
  • tests/phpunit/tests/customize/manager.php

    diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php
    index c141e38ff5..e3df13df11 100644
    a b function test_setup_theme_in_frontend() { 
    216216        }
    217217
    218218        /**
     219         * Test WP_Customize_Manager::settings_previewed().
     220         *
     221         * @ticket 39221
     222         * @covers WP_Customize_Manager::settings_previewed()
     223         */
     224        function test_settings_previewed() {
     225                $wp_customize = new WP_Customize_Manager( array( 'settings_previewed' => false ) );
     226                $this->assertSame( false, $wp_customize->settings_previewed() );
     227
     228                $wp_customize = new WP_Customize_Manager();
     229                $this->assertSame( true, $wp_customize->settings_previewed() );
     230        }
     231
     232        /**
    219233         * Test WP_Customize_Manager::changeset_uuid().
    220234         *
    221235         * @ticket 30937