WP_Customize_Manager::customize_pane_settings PHP Method

customize_pane_settings() public method

Print JavaScript settings for parent window.
Since: 4.4.0
    public function customize_pane_settings()
    {
        $login_url = add_query_arg(array('interim-login' => 1, 'customize-login' => 1), wp_login_url());
        // Ensure dirty flags are set for modified settings.
        foreach (array_keys($this->unsanitized_post_values()) as $setting_id) {
            $setting = $this->get_setting($setting_id);
            if ($setting) {
                $setting->dirty = true;
            }
        }
        // Prepare Customizer settings to pass to JavaScript.
        $settings = array('changeset' => array('uuid' => $this->changeset_uuid(), 'status' => $this->changeset_post_id() ? get_post_status($this->changeset_post_id()) : ''), 'timeouts' => array('windowRefresh' => 250, 'changesetAutoSave' => AUTOSAVE_INTERVAL * 1000, 'keepAliveCheck' => 2500, 'reflowPaneContents' => 100, 'previewFrameSensitivity' => 2000), 'theme' => array('stylesheet' => $this->get_stylesheet(), 'active' => $this->is_theme_active()), 'url' => array('preview' => esc_url_raw($this->get_preview_url()), 'parent' => esc_url_raw(admin_url()), 'activated' => esc_url_raw(home_url('/')), 'ajax' => esc_url_raw(admin_url('admin-ajax.php', 'relative')), 'allowed' => array_map('esc_url_raw', $this->get_allowed_urls()), 'isCrossDomain' => $this->is_cross_domain(), 'home' => esc_url_raw(home_url('/')), 'login' => esc_url_raw($login_url)), 'browser' => array('mobile' => wp_is_mobile(), 'ios' => $this->is_ios()), 'panels' => array(), 'sections' => array(), 'nonce' => $this->get_nonces(), 'autofocus' => $this->get_autofocus(), 'documentTitleTmpl' => $this->get_document_title_template(), 'previewableDevices' => $this->get_previewable_devices());
        // Prepare Customize Section objects to pass to JavaScript.
        foreach ($this->sections() as $id => $section) {
            if ($section->check_capabilities()) {
                $settings['sections'][$id] = $section->json();
            }
        }
        // Prepare Customize Panel objects to pass to JavaScript.
        foreach ($this->panels() as $panel_id => $panel) {
            if ($panel->check_capabilities()) {
                $settings['panels'][$panel_id] = $panel->json();
                foreach ($panel->sections as $section_id => $section) {
                    if ($section->check_capabilities()) {
                        $settings['sections'][$section_id] = $section->json();
                    }
                }
            }
        }
        ?>
		<script type="text/javascript">
			var _wpCustomizeSettings = <?php 
        echo wp_json_encode($settings);
        ?>
;
			_wpCustomizeSettings.controls = {};
			_wpCustomizeSettings.settings = {};
			<?php 
        // Serialize settings one by one to improve memory usage.
        echo "(function ( s ){\n";
        foreach ($this->settings() as $setting) {
            if ($setting->check_capabilities()) {
                printf("s[%s] = %s;\n", wp_json_encode($setting->id), wp_json_encode($setting->json()));
            }
        }
        echo "})( _wpCustomizeSettings.settings );\n";
        // Serialize controls one by one to improve memory usage.
        echo "(function ( c ){\n";
        foreach ($this->controls() as $control) {
            if ($control->check_capabilities()) {
                printf("c[%s] = %s;\n", wp_json_encode($control->id), wp_json_encode($control->json()));
            }
        }
        echo "})( _wpCustomizeSettings.controls );\n";
        ?>
		</script>
		<?php 
    }

Usage Example

Exemplo n.º 1
0
	/**
	 * Test customize_pane_settings() method.
	 *
	 * @see WP_Customize_Manager::customize_pane_settings()
	 */
	function test_customize_pane_settings() {
		wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
		$this->manager->register_controls();
		$this->manager->prepare_controls();
		$autofocus = array( 'control' => 'blogname' );
		$this->manager->set_autofocus( $autofocus );

		ob_start();
		$this->manager->customize_pane_settings();
		$content = ob_get_clean();

		$this->assertContains( 'var _wpCustomizeSettings =', $content );
		$this->assertContains( '"blogname"', $content );
		$this->assertContains( '_wpCustomizeSettings.controls', $content );
		$this->assertContains( '_wpCustomizeSettings.settings', $content );
		$this->assertContains( '</script>', $content );

		$this->assertNotEmpty( preg_match( '#var _wpCustomizeSettings\s*=\s*({.*?});\s*\n#', $content, $matches ) );
		$json = $matches[1];
		$data = json_decode( $json, true );
		$this->assertNotEmpty( $data );

		$this->assertEqualSets( array( 'theme', 'url', 'browser', 'panels', 'sections', 'nonce', 'autofocus', 'documentTitleTmpl', 'previewableDevices' ), array_keys( $data ) );
		$this->assertEquals( $autofocus, $data['autofocus'] );
		$this->assertArrayHasKey( 'save', $data['nonce'] );
		$this->assertArrayHasKey( 'preview', $data['nonce'] );
	}
WP_Customize_Manager