Make WordPress Core


Ignore:
Timestamp:
01/27/2016 05:54:39 PM (11 years ago)
Author:
westonruter
Message:

Customizer: Export nonce, theme, and url app settings in preview as exported in pane.

  • Introduce WP_Customize_Manager::get_nonces() to consolidate logic for retrieving nonces.
  • Export nonces centrally in wp.customize.settings.nonce with each request and update nav menus preview to utilize.
  • Send updated nonces to preview upon nonce-refresh.
  • Request full preview refresh if Nav Menu selective refresh request fails (e.g. due to bad nonce).
  • Update nav menus and widgets in Customizer to utilize customize_refresh_nonces for exporting nonces and keeping them up to date.

See #27355.
Fixes #35617.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r36261 r36414  
    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         *
     
    400430                $this->assertArrayHasKey( 'save', $data['nonce'] );
    401431                $this->assertArrayHasKey( 'preview', $data['nonce'] );
     432        }
     433
     434        /**
     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'] );
    402464        }
    403465
Note: See TracChangeset for help on using the changeset viewer.