Make WordPress Core

Ticket #35617: 35617.2.diff

File 35617.2.diff, 10.6 KB (added by westonruter, 10 years ago)
  • src/wp-admin/js/customize-controls.js

    diff --git src/wp-admin/js/customize-controls.js src/wp-admin/js/customize-controls.js
    index 4901b56..caf296a 100644
     
    33753375                api.bind( 'nonce-refresh', function( nonce ) {
    33763376                        $.extend( api.settings.nonce, nonce );
    33773377                        $.extend( api.previewer.nonce, nonce );
     3378                        api.previewer.send( 'nonce-refresh', nonce );
    33783379                });
    33793380
    33803381                // Create Settings
  • 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 aa73ed7..48ccc80 100644
    final class WP_Customize_Manager { 
    801801         */
    802802        public function customize_preview_settings() {
    803803                $settings = array(
     804                        'theme' => array(
     805                                'stylesheet' => $this->get_stylesheet(),
     806                                'active'     => $this->is_theme_active(),
     807                        ),
     808                        'url' => array(
     809                                'self' => empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
     810                        ),
    804811                        'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
    805812                        'activePanels' => array(),
    806813                        'activeSections' => array(),
    807814                        'activeControls' => array(),
     815                        'nonce' => $this->get_nonces(),
    808816                        '_dirty' => array_keys( $this->unsanitized_post_values() ),
    809817                );
    810818
    811                 if ( 2 == $this->nonce_tick ) {
    812                         $settings['nonce'] = array(
    813                                 'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
    814                                 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() )
    815                         );
    816                 }
    817 
    818819                foreach ( $this->panels as $panel_id => $panel ) {
    819820                        if ( $panel->check_capabilities() ) {
    820821                                $settings['activePanels'][ $panel_id ] = $panel->active();
    final class WP_Customize_Manager { 
    10251026                        wp_send_json_error( 'not_preview' );
    10261027                }
    10271028
    1028                 $nonces = array(
    1029                         'save'    => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
    1030                         'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
    1031                 );
    1032 
    1033                 /**
    1034                  * Filter nonces for a customize_refresh_nonces AJAX request.
    1035                  *
    1036                  * @since 4.2.0
    1037                  *
    1038                  * @param array                $nonces Array of refreshed nonces for save and
    1039                  *                                     preview actions.
    1040                  * @param WP_Customize_Manager $this   WP_Customize_Manager instance.
    1041                  */
    1042                 $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
    1043                 wp_send_json_success( $nonces );
     1029                wp_send_json_success( $this->get_nonces() );
    10441030        }
    10451031
    10461032        /**
    final class WP_Customize_Manager { 
    16361622        }
    16371623
    16381624        /**
     1625         * Get nonces for the Customizer.
     1626         *
     1627         * @since 4.5.0
     1628         * @return array Nonces.
     1629         */
     1630        public function get_nonces() {
     1631                $nonces = array(
     1632                        'save' => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
     1633                        'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
     1634                );
     1635
     1636                /**
     1637                 * Filter nonces for Customizer.
     1638                 *
     1639                 * @since 4.2.0
     1640                 *
     1641                 * @param array                $nonces Array of refreshed nonces for save and
     1642                 *                                     preview actions.
     1643                 * @param WP_Customize_Manager $this   WP_Customize_Manager instance.
     1644                 */
     1645                $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
     1646
     1647                return $nonces;
     1648        }
     1649
     1650        /**
    16391651         * Print JavaScript settings for parent window.
    16401652         *
    16411653         * @since 4.4.0
    final class WP_Customize_Manager { 
    16951707                        ),
    16961708                        'panels'   => array(),
    16971709                        'sections' => array(),
    1698                         'nonce'    => array(
    1699                                 'save'    => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
    1700                                 'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
    1701                         ),
     1710                        'nonce'    => $this->get_nonces(),
    17021711                        'autofocus' => array(),
    17031712                        'documentTitleTmpl' => $this->get_document_title_template(),
    17041713                );
  • 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 2c90548..ec92d58 100644
    final class WP_Customize_Nav_Menus { 
    939939                        'renderQueryVar'        => self::RENDER_QUERY_VAR,
    940940                        'renderNonceValue'      => wp_create_nonce( self::RENDER_AJAX_ACTION ),
    941941                        'renderNoncePostKey'    => self::RENDER_NONCE_POST_KEY,
    942                         'requestUri'            => empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ),
    943                         'theme'                 => array(
    944                                 'stylesheet' => $this->manager->get_stylesheet(),
    945                                 'active'     => $this->manager->is_theme_active(),
    946                         ),
    947                         'previewCustomizeNonce' => wp_create_nonce( 'preview-customize_' . $this->manager->get_stylesheet() ),
    948942                        'navMenuInstanceArgs'   => $this->preview_nav_menu_instance_args,
    949943                        'l10n'                  => array(
    950944                                'editNavMenuItemTooltip' => __( 'Shift-click to edit this menu item.' ),
  • src/wp-includes/js/customize-preview-nav-menus.js

    diff --git src/wp-includes/js/customize-preview-nav-menus.js src/wp-includes/js/customize-preview-nav-menus.js
    index fe5f305..5441b86 100644
     
    1313                        renderQueryVar: null,
    1414                        renderNonceValue: null,
    1515                        renderNoncePostKey: null,
    16                         previewCustomizeNonce: null,
    1716                        requestUri: '/',
    18                         theme: {
    19                                 active: false,
    20                                 stylesheet: ''
    21                         },
    2217                        navMenuInstanceArgs: {},
    2318                        l10n: {}
    2419                };
     
    200195                        menuId = parseInt( menuId, 10 );
    201196
    202197                        data = {
    203                                 nonce: settings.previewCustomizeNonce, // for Customize Preview
     198                                nonce: wp.customize.settings.nonce.preview,
    204199                                wp_customize: 'on'
    205200                        };
    206                         if ( ! settings.theme.active ) {
    207                                 data.theme = settings.theme.stylesheet;
     201                        if ( ! wp.customize.settings.theme.active ) {
     202                                data.theme = wp.customize.settings.theme.stylesheet;
    208203                        }
    209204                        data[ settings.renderQueryVar ] = '1';
    210205
     
    239234
    240235                        request = wp.ajax.send( null, {
    241236                                data: data,
    242                                 url: settings.requestUri
     237                                url: api.settings.url.self
    243238                        } );
    244239                        request.done( function( data ) {
    245240                                // If the menu is now not visible, refresh since the page layout may have changed.
     
    263258                                container.removeClass( 'customize-partial-refreshing' );
    264259                                $( document ).trigger( 'customize-preview-menu-refreshed', [ eventParam ] );
    265260                        } );
     261                        request.fail( function() {
     262                                api.preview.send( 'refresh' );
     263                        } );
    266264                },
    267265
    268266                refreshMenuInstanceDebounced : function( instanceNumber ) {
  • src/wp-includes/js/customize-preview.js

    diff --git src/wp-includes/js/customize-preview.js src/wp-includes/js/customize-preview.js
    index 1d18027..b6da775 100644
     
    146146                });
    147147
    148148                api.preview.bind( 'active', function() {
    149                         if ( api.settings.nonce ) {
    150                                 api.preview.send( 'nonce', api.settings.nonce );
    151                         }
     149                        api.preview.send( 'nonce', api.settings.nonce );
    152150
    153151                        api.preview.send( 'documentTitle', document.title );
    154152                });
     
    163161                        } );
    164162                } );
    165163
     164                api.preview.bind( 'nonce-refresh', function( nonce ) {
     165                        $.extend( api.settings.nonce, nonce );
     166                } );
     167
    166168                /*
    167169                 * Send a message to the parent customize frame with a list of which
    168170                 * containers and controls are active.
  • tests/phpunit/tests/customize/manager.php

    diff --git tests/phpunit/tests/customize/manager.php tests/phpunit/tests/customize/manager.php
    index e787cfa..4cf8d01 100644
    class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    369369        }
    370370
    371371        /**
     372         * Test get_nonces() method.
     373         *
     374         * @see WP_Customize_Manager::get_nonces()
     375         */
     376        function test_nonces() {
     377                $nonces = $this->manager->get_nonces();
     378                $this->assertInternalType( 'array', $nonces );
     379                $this->assertArrayHasKey( 'save', $nonces );
     380                $this->assertArrayHasKey( 'preview', $nonces );
     381
     382                add_filter( 'customize_refresh_nonces', array( $this, 'filter_customize_refresh_nonces' ), 10, 2 );
     383                $nonces = $this->manager->get_nonces();
     384                $this->assertArrayHasKey( 'foo', $nonces );
     385                $this->assertEquals( wp_create_nonce( 'foo' ), $nonces['foo'] );
     386        }
     387
     388        /**
     389         * Filter for customize_refresh_nonces.
     390         *
     391         * @param array                $nonces  Nonces.
     392         * @param WP_Customize_Manager $manager Manager.
     393         * @return array Nonces.
     394         */
     395        function filter_customize_refresh_nonces( $nonces, $manager ) {
     396                $this->assertInstanceOf( 'WP_Customize_Manager', $manager );
     397                $nonces['foo'] = wp_create_nonce( 'foo' );
     398                return $nonces;
     399        }
     400
     401        /**
    372402         * Test customize_pane_settings() method.
    373403         *
    374404         * @see WP_Customize_Manager::customize_pane_settings()
    class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    402432        }
    403433
    404434        /**
     435         * Test customize_preview_settings() method.
     436         *
     437         * @see WP_Customize_Manager::customize_preview_settings()
     438         */
     439        function test_customize_preview_settings() {
     440                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     441                $this->manager->register_controls();
     442                $this->manager->prepare_controls();
     443                $this->manager->set_post_value( 'foo', 'bar' );
     444                $_POST['customize_messenger_channel'] = 'preview-0';
     445
     446                ob_start();
     447                $this->manager->customize_preview_settings();
     448                $content = ob_get_clean();
     449
     450                $this->assertEquals( 1, preg_match( '/var _wpCustomizeSettings = ({.+});/', $content, $matches ) );
     451                $settings = json_decode( $matches[1], true );
     452
     453                $this->assertArrayHasKey( 'theme', $settings );
     454                $this->assertArrayHasKey( 'url', $settings );
     455                $this->assertArrayHasKey( 'channel', $settings );
     456                $this->assertArrayHasKey( 'activePanels', $settings );
     457                $this->assertArrayHasKey( 'activeSections', $settings );
     458                $this->assertArrayHasKey( 'activeControls', $settings );
     459                $this->assertArrayHasKey( 'nonce', $settings );
     460                $this->assertArrayHasKey( '_dirty', $settings );
     461
     462                $this->assertArrayHasKey( 'preview', $settings['nonce'] );
     463                $this->assertEquals( array( 'foo' ), $settings['_dirty'] );
     464        }
     465
     466        /**
    405467         * @ticket 33552
    406468         */
    407469        function test_customize_loaded_components_filter() {
  • tests/phpunit/tests/customize/nav-menus.php

    diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
    index dd620c3..2969a2d 100644
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    647647                $this->assertContains( 'renderQueryVar', $data );
    648648                $this->assertContains( 'renderNonceValue', $data );
    649649                $this->assertContains( 'renderNoncePostKey', $data );
    650                 $this->assertContains( 'requestUri', $data );
    651                 $this->assertContains( 'theme', $data );
    652                 $this->assertContains( 'previewCustomizeNonce', $data );
    653650                $this->assertContains( 'navMenuInstanceArgs', $data );
    654                 $this->assertContains( 'requestUri', $data );
    655 
    656651        }
    657 
    658652}