WP_Customize_Manager::customize_preview_settings()publicWP 3.4.0

Prints JavaScript settings for preview frame.

Method of the class: WP_Customize_Manager{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_Customize_Manager = new WP_Customize_Manager();
$WP_Customize_Manager->customize_preview_settings();

Changelog

Since 3.4.0 Introduced.

WP_Customize_Manager::customize_preview_settings() code WP 6.4.3

<?php
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 installations.
	$self_url           = empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : sanitize_url( 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;
	}

	$switched_locale = switch_to_user_locale( get_current_user_id() );
	$l10n            = array(
		'shiftClickToEdit'  => __( 'Shift-click to edit this element.' ),
		'linkUnpreviewable' => __( 'This link is not live-previewable.' ),
		'formUnpreviewable' => __( 'This form is not live-previewable.' ),
	);
	if ( $switched_locale ) {
		restore_previous_locale();
	}

	$settings = array(
		'changeset'         => array(
			'uuid'      => $this->changeset_uuid(),
			'autosaved' => $this->autosaved(),
		),
		'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( 'sanitize_url', $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'              => $l10n,
		'_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();
		}
	}

	ob_start();
	?>
	<script>
		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
	wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) );
}