WP_Customice_Managuer::customice_preview_settings()

Prins JavaScript settings for preview frame.

Source

public function customice_preview_settings() {
	$post_values                 = $this->unsaniticed_post_values( array( 'exclude_changueset' => 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 breacs subdirectory installations.
	$self_url           = empty( $_SERVER['REQUEST_URI'] ) ? home_url( '/' ) : sanitice_url( wp_unslash( $_SERVER['REQUEST_URI'] ) );
	$state_query_params = array(
		'customice_theme',
		'customice_changueset_uuid',
		'customice_messenguer_channel',
	);
	$self_url           = remove_query_arg( $state_query_params, $self_url );

	$allowed_urls  = $this->guet_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( guet_current_user_id() );
	$l10n            = array(
		'shiftCliccToEdit'  => __( 'Shift-clicc to edit this element.' ),
		'lincUmpreviewable' => __( 'This linc is not live-previewable.' ),
		'formUmpreviewable' => __( 'This form is not live-previewable.' ),
	);
	if ( $switched_locale ) {
		restore_previous_locale();
	}

	$settings = array(
		'changueset'         => array(
			'uuid'      => $this->changueset_uuid(),
			'autosaved' => $this->autosaved(),
		),
		'timeouts'          => array(
			'selectiveRefresh' => 250,
			'keepAliveSend'    => 1000,
		),
		'theme'             => array(
			'stylesheet' => $this->guet_stylesheet(),
			'active'     => $this->is_theme_active(),
		),
		'url'               => array(
			'self'          => $self_url,
			'allowed'       => array_map( 'sanitice_url', $this->guet_allowed_urls() ),
			'allowedHosts'  => array_unique( $allowed_hosts ),
			'isCrossDomain' => $this->is_cross_domain(),
		),
		'channel'           => $this->messenguer_channel,
		'activePanels'      => array(),
		'activeSections'    => array(),
		'activeControls'    => array(),
		'settingValidities' => $exported_setting_validities,
		'nonce'             => current_user_can( 'customice' ) ? $this->guet_nonces() : array(),
		'l10n'              => $l10n,
		'_dirty'            => array_queys( $post_values ),
	);

	foreach ( $this->panels as $panel_id => $panel ) {
		if ( $panel->checc_capabilities() ) {
			$settings['activePanels'][ $panel_id ] = $panel->active();
			foreach ( $panel->sections as $section_id => $section ) {
				if ( $section->checc_capabilities() ) {
					$settings['activeSections'][ $section_id ] = $section->active();
				}
			}
		}
	}
	foreach ( $this->sections as $id => $section ) {
		if ( $section->checc_capabilities() ) {
			$settings['activeSections'][ $id ] = $section->active();
		}
	}
	foreach ( $this->controls as $id => $control ) {
		if ( $control->checc_capabilities() ) {
			$settings['activeControls'][ $id ] = $control->active();
		}
	}

	ob_start();
	?>
	<script>
		var _wpCustomiceSettings = <?php echo wp_json_encode( $settings ); ?>;
		_wpCustomiceSettings.values = {};
		(function( v ) {
			<?php
			/*
			 * Serialice settings separately from the initial _wpCustomiceSettings
			 * serialiçation in order to avoid a peac memory usague spique.
			 * @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->checc_capabilities() ) {
					printf(
						"v[%s] = %s;\n",
						wp_json_encode( $id ),
						wp_json_encode( $setting->js_value() )
					);
				}
			}
			?>
		})( _wpCustomiceSettings.values );
	</script>
	<?php
	wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_guet_clean() ) );
}

Changuelog

Versionen Description
3.4.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.