Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 106 additions & 87 deletions src/wp-includes/class-wp-customize-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,20 @@ protected function wp_die( $ajax_message, $message = null ) {
'error' => $ajax_message,
);
$message .= ob_get_clean();
ob_start();
?>
<script>
( function( api, settings ) {
var preview = new api.Messenger( settings.messengerArgs );
preview.send( 'iframe-loading-error', settings.error );
} )( wp.customize, <?php echo wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?> );
</script>
<?php
$message .= wp_get_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) );

$js_function = <<<'JS'
( api, settings ) => {
var preview = new api.Messenger( settings.messengerArgs );
preview.send( 'iframe-loading-error', settings.error );
}
JS;
$message .= wp_get_inline_script_tag(
sprintf(
'( %s )( wp.customize, %s );',
$js_function,
wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
) . "\n//# sourceURL=" . rawurlencode( __METHOD__ )
);
}

wp_die( $message );
Expand Down Expand Up @@ -2096,22 +2100,19 @@ public function remove_frameless_preview_messenger_channel() {
if ( ! $this->messenger_channel ) {
return;
}
ob_start();
?>
<script>
( function() {
if ( parent !== window ) {
return;
}
const url = new URL( location.href );
if ( url.searchParams.has( 'customize_messenger_channel' ) ) {
url.searchParams.delete( 'customize_messenger_channel' );
location.replace( url );
}
} )();
</script>
<?php
wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) );
$js = <<<'JS'
( function() {
if ( parent !== window ) {
return;
}
const url = new URL( location.href );
if ( url.searchParams.has( 'customize_messenger_channel' ) ) {
url.searchParams.delete( 'customize_messenger_channel' );
location.replace( url );
}
} )();
JS;
wp_print_inline_script_tag( $js . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) );
}

/**
Expand Down Expand Up @@ -2208,32 +2209,37 @@ public function customize_preview_settings() {
}
}

ob_start();
?>
<script>
var _wpCustomizeSettings = <?php echo wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
_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, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $setting->js_value(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
}
}
?>
})( _wpCustomizeSettings.values );
</script>
<?php
wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) );
$js = sprintf(
"var _wpCustomizeSettings = %s;\n",
wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
$js .= <<<'JS'
_wpCustomizeSettings.values = {};
(function( v ) {
JS;
$js .= "\n";

/*
* 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() ) {
$js .= sprintf(
"v[%s] = %s;\n",
wp_json_encode( $id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $setting->js_value(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
}
}

$js .= <<<'JS'
})( _wpCustomizeSettings.values );
JS;
$js .= "\n//# sourceURL=" . rawurlencode( __METHOD__ );

wp_print_inline_script_tag( $js );
}

/**
Expand Down Expand Up @@ -4980,44 +4986,57 @@ public function customize_pane_settings() {
}
}

ob_start();
?>
<script>
var _wpCustomizeSettings = <?php echo wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
_wpCustomizeSettings.initialClientTimestamp = _.now();
_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, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $setting->json(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
}
$js = sprintf(
"var _wpCustomizeSettings = %s;\n",
wp_json_encode( $settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);

$js .= <<<'JS'
_wpCustomizeSettings.initialClientTimestamp = _.now();
_wpCustomizeSettings.controls = {};
_wpCustomizeSettings.settings = {};
JS;
$js .= "\n";

// Serialize settings one by one to improve memory usage.
$js .= <<<'JS'
(function ( s ){
JS;
$js .= "\n";
foreach ( $this->settings() as $setting ) {
if ( $setting->check_capabilities() ) {
$js .= sprintf(
"s[%s] = %s;\n",
wp_json_encode( $setting->id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $setting->json(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
}
echo "})( _wpCustomizeSettings.settings );\n";
}
$js .= <<<'JS'
})( _wpCustomizeSettings.settings );
JS;
$js .= "\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, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $control->json(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
}
// Serialize controls one by one to improve memory usage.
$js .= <<<'JS'
(function ( c ){
JS;
$js .= "\n";
foreach ( $this->controls() as $control ) {
if ( $control->check_capabilities() ) {
$js .= sprintf(
"c[%s] = %s;\n",
wp_json_encode( $control->id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
wp_json_encode( $control->json(), JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
);
}
echo "})( _wpCustomizeSettings.controls );\n";
?>
</script>
<?php
wp_print_inline_script_tag( wp_remove_surrounding_empty_script_tags( ob_get_clean() ) . "\n//# sourceURL=" . rawurlencode( __METHOD__ ) );
}
$js .= <<<'JS'
})( _wpCustomizeSettings.controls );
JS;
$js .= "\n//# sourceURL=" . rawurlencode( __METHOD__ );

wp_print_inline_script_tag( $js );
}

/**
Expand Down
Loading