WordPress.org

Make WordPress Core

Ticket #39221: 39221.2.diff

File 39221.2.diff, 10.1 KB (added by westonruter, 4 years ago)

https://github.com/xwp/wordpress-develop/pull/231

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

    diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
    index 9330d2789b..dd3d23c66d 100644
    final class WP_Customize_Manager { 
    193193        protected $messenger_channel;
    194194
    195195        /**
     196         * If settings should be previewed.
     197         *
     198         * @since 4.9.0
     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
    final class WP_Customize_Manager { 
    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
    final class WP_Customize_Manager { 
    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' );
    final class WP_Customize_Manager { 
    645660        }
    646661
    647662        /**
     663         * If should preview settings.
     664         *
     665         * @since 4.9.0
     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
    final class WP_Customize_Manager { 
    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 src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index 485c29761a..b89ffc6df9 100644
    final class WP_Customize_Nav_Menus { 
    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 src/wp-includes/class-wp-customize-widgets.php src/wp-includes/class-wp-customize-widgets.php
    index ecf0c4a97b..99777c5770 100644
    final class WP_Customize_Widgets { 
    223223
    224224                $settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) );
    225225
    226                 /*
    227                  * Preview settings right away so that widgets and sidebars will get registered properly.
    228                  * But don't do this if a customize_save because this will cause WP to think there is nothing
    229                  * changed that needs to be saved.
    230                  */
    231                 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) {
     226                if ( $this->manager->settings_previewed() ) {
    232227                        foreach ( $settings as $setting ) {
    233228                                $setting->preview();
    234229                        }
    final class WP_Customize_Widgets { 
    526521                        }
    527522                }
    528523
    529                 if ( ! $this->manager->doing_ajax( 'customize_save' ) ) {
     524                if ( $this->manager->settings_previewed() ) {
    530525                        foreach ( $new_setting_ids as $new_setting_id ) {
    531526                                $this->manager->get_setting( $new_setting_id )->preview();
    532527                        }
  • src/wp-includes/theme.php

    diff --git src/wp-includes/theme.php src/wp-includes/theme.php
    index e5cdd952b8..960d0cebcc 100644
    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/**
    function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_p 
    28432859
    28442860        if ( empty( $wp_customize ) ) {
    28452861                require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
    2846                 $wp_customize = new WP_Customize_Manager( array( 'changeset_uuid' => $changeset_post->post_name ) );
     2862                $wp_customize = new WP_Customize_Manager( array(
     2863                        'changeset_uuid' => $changeset_post->post_name,
     2864                        'settings_previewed' => false,
     2865                ) );
    28472866        }
    28482867
    28492868        if ( ! did_action( 'customize_register' ) ) {
  • tests/phpunit/tests/customize/manager.php

    diff --git tests/phpunit/tests/customize/manager.php tests/phpunit/tests/customize/manager.php
    index 056dd2398f..2aaafb6278 100644
    class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    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
    class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    12921306        }
    12931307
    12941308        /**
     1309         * Test saving settings by publishing a changeset outside of Customizer entirely.
     1310         *
     1311         * Widgets get their settings registered and previewed early in the admin,
     1312         * so this ensures that the previewing is bypassed when in the context of
     1313         * publishing
     1314         *
     1315         * @ticket 39221
     1316         * @covers _wp_customize_publish_changeset()
     1317         * @see WP_Customize_Widgets::schedule_customize_register()
     1318         * @see WP_Customize_Widgets::customize_register()
     1319         */
     1320        function test_wp_customize_publish_changeset() {
     1321                global $wp_customize;
     1322                $wp_customize = null;
     1323
     1324                // Set the admin current screen to cause WP_Customize_Widgets::schedule_customize_register() to do early setting registration.
     1325                set_current_screen( 'edit' );
     1326                $this->assertTrue( is_admin() );
     1327
     1328                $old_sidebars_widgets = get_option( 'sidebars_widgets' );
     1329                $new_sidebars_widgets = $old_sidebars_widgets;
     1330                $this->assertGreaterThan( 2, count( $new_sidebars_widgets['sidebar-1'] ) );
     1331                $new_sidebar_1 = array_reverse( $new_sidebars_widgets['sidebar-1'] );
     1332
     1333                $post_id = $this->factory()->post->create( array(
     1334                        'post_type' => 'customize_changeset',
     1335                        'post_status' => 'draft',
     1336                        'post_name' => wp_generate_uuid4(),
     1337                        'post_content' => wp_json_encode( array(
     1338                                'sidebars_widgets[sidebar-1]' => array(
     1339                                        'value' => $new_sidebar_1,
     1340                                ),
     1341                        ) ),
     1342                ) );
     1343
     1344                // Save the updated sidebar widgets into the options table by publishing the changeset.
     1345                wp_publish_post( $post_id );
     1346
     1347                // Make sure previewing filters were never added, since WP_Customize_Manager should be constructed with settings_previewed=false.
     1348                $this->assertFalse( has_filter( 'option_sidebars_widgets' ) );
     1349                $this->assertFalse( has_filter( 'default_option_sidebars_widgets' ) );
     1350
     1351                // Ensure that the value has actually been written to the DB.
     1352                $updated_sidebars_widgets = get_option( 'sidebars_widgets' );
     1353                $this->assertEquals( $new_sidebar_1, $updated_sidebars_widgets['sidebar-1'] );
     1354        }
     1355
     1356        /**
    12951357         * Ensure that save_changeset_post method bails updating an underlying changeset which is invalid.
    12961358         *
    12971359         * @ticket 41252