WP_Customize_Manager::customize_preview_settings PHP Method

customize_preview_settings() public method

Print JavaScript settings for preview frame.
Since: 3.4.0
    public function customize_preview_settings()
    {
        $post_values = $this->unsanitized_post_values(array('exclude_changeset' => true));
        $setting_validities = $this->validate_setting_values($post_values);
        $exported_setting_validities = array_map(array($this, 'prepare_setting_validity_for_js'), $setting_validities);
        // Note that the REQUEST_URI is not passed into home_url() since this breaks subdirectory installs.
        $self_url = empty($_SERVER['REQUEST_URI']) ? home_url('/') : esc_url_raw(wp_unslash($_SERVER['REQUEST_URI']));
        $state_query_params = array('customize_theme', 'customize_changeset_uuid', 'customize_messenger_channel');
        $self_url = remove_query_arg($state_query_params, $self_url);
        $allowed_urls = $this->get_allowed_urls();
        $allowed_hosts = array();
        foreach ($allowed_urls as $allowed_url) {
            $parsed = wp_parse_url($allowed_url);
            if (empty($parsed['host'])) {
                continue;
            }
            $host = $parsed['host'];
            if (!empty($parsed['port'])) {
                $host .= ':' . $parsed['port'];
            }
            $allowed_hosts[] = $host;
        }
        $settings = array('changeset' => array('uuid' => $this->_changeset_uuid), 'timeouts' => array('selectiveRefresh' => 250, 'keepAliveSend' => 1000), 'theme' => array('stylesheet' => $this->get_stylesheet(), 'active' => $this->is_theme_active()), 'url' => array('self' => $self_url, 'allowed' => array_map('esc_url_raw', $this->get_allowed_urls()), 'allowedHosts' => array_unique($allowed_hosts), 'isCrossDomain' => $this->is_cross_domain()), 'channel' => $this->messenger_channel, 'activePanels' => array(), 'activeSections' => array(), 'activeControls' => array(), 'settingValidities' => $exported_setting_validities, 'nonce' => current_user_can('customize') ? $this->get_nonces() : array(), 'l10n' => array('shiftClickToEdit' => __('Shift-click to edit this element.'), 'linkUnpreviewable' => __('This link is not live-previewable.'), 'formUnpreviewable' => __('This form is not live-previewable.')), '_dirty' => array_keys($post_values));
        foreach ($this->panels as $panel_id => $panel) {
            if ($panel->check_capabilities()) {
                $settings['activePanels'][$panel_id] = $panel->active();
                foreach ($panel->sections as $section_id => $section) {
                    if ($section->check_capabilities()) {
                        $settings['activeSections'][$section_id] = $section->active();
                    }
                }
            }
        }
        foreach ($this->sections as $id => $section) {
            if ($section->check_capabilities()) {
                $settings['activeSections'][$id] = $section->active();
            }
        }
        foreach ($this->controls as $id => $control) {
            if ($control->check_capabilities()) {
                $settings['activeControls'][$id] = $control->active();
            }
        }
        ?>
		<script type="text/javascript">
			var _wpCustomizeSettings = <?php 
        echo wp_json_encode($settings);
        ?>
;
			_wpCustomizeSettings.values = {};
			(function( v ) {
				<?php 
        /*
         * Serialize settings separately from the initial _wpCustomizeSettings
         * serialization in order to avoid a peak memory usage spike.
         * @todo We may not even need to export the values at all since the pane syncs them anyway.
         */
        foreach ($this->settings as $id => $setting) {
            if ($setting->check_capabilities()) {
                printf("v[%s] = %s;\n", wp_json_encode($id), wp_json_encode($setting->js_value()));
            }
        }
        ?>
			})( _wpCustomizeSettings.values );
		</script>
		<?php 
    }

Usage Example

	/**
	 * Test customize_preview_settings() method.
	 *
	 * @see WP_Customize_Manager::customize_preview_settings()
	 */
	function test_customize_preview_settings() {
		wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
		$this->manager->register_controls();
		$this->manager->prepare_controls();
		$this->manager->set_post_value( 'foo', 'bar' );
		$_POST['customize_messenger_channel'] = 'preview-0';

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

		$this->assertEquals( 1, preg_match( '/var _wpCustomizeSettings = ({.+});/', $content, $matches ) );
		$settings = json_decode( $matches[1], true );

		$this->assertArrayHasKey( 'theme', $settings );
		$this->assertArrayHasKey( 'url', $settings );
		$this->assertArrayHasKey( 'channel', $settings );
		$this->assertArrayHasKey( 'activePanels', $settings );
		$this->assertArrayHasKey( 'activeSections', $settings );
		$this->assertArrayHasKey( 'activeControls', $settings );
		$this->assertArrayHasKey( 'nonce', $settings );
		$this->assertArrayHasKey( '_dirty', $settings );

		$this->assertArrayHasKey( 'preview', $settings['nonce'] );
		$this->assertEquals( array( 'foo' ), $settings['_dirty'] );
	}
WP_Customize_Manager