Skip to content

Commit 632ead6

Browse files
committed
Customize: Enable Customizer live previews of Custom CSS in Block Themes.
In a Block Theme, the Custom CSS from the Customizer is inserted into the global styles, before the global style's Custom CSS. In order to update the Customizer's Custom CSS inside of the global styles, milestone CSS comments are added in the preview so that the live preview logic can do the replacement. Follow-up to [55192], [58703]. See #57536, #61395. Props westonruter, wildworks, peterwilsoncc, rollybueno, SirLouen, poojapadamad, rafiq91, audrasjb. Fixes #63589. git-svn-id: https://develop.svn.wordpress.org/trunk@60522 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 54a06f8 commit 632ead6

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/js/_enqueues/wp/customize/preview.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,28 @@
635635
/**
636636
* Preview changes to custom css.
637637
*
638-
* @param {string} value Custom CSS..
638+
* @param {string} value Custom CSS.
639639
* @return {void}
640640
*/
641641
custom_css: function( value ) {
642-
$( '#wp-custom-css' ).text( value );
642+
var style;
643+
if ( api.settings.theme.isBlockTheme ) {
644+
style = $( 'style#global-styles-inline-css' );
645+
646+
// Forbid milestone comments from appearing in Custom CSS which would break live preview.
647+
value = value.replace( /\/\*(BEGIN|END)_CUSTOMIZER_CUSTOM_CSS\*\//g, '' );
648+
649+
var textContent = style.text().replace(
650+
/(\/\*BEGIN_CUSTOMIZER_CUSTOM_CSS\*\/)((?:.|\s)*?)(\/\*END_CUSTOMIZER_CUSTOM_CSS\*\/)/,
651+
function ( match, beforeComment, oldValue, afterComment ) {
652+
return beforeComment + '\n' + value + '\n' + afterComment;
653+
}
654+
);
655+
style.text( textContent );
656+
} else {
657+
style = $( 'style#wp-custom-css' );
658+
style.text( value );
659+
}
643660
},
644661

645662
/**

src/wp-includes/class-wp-customize-manager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,8 +2161,9 @@ public function customize_preview_settings() {
21612161
'keepAliveSend' => 1000,
21622162
),
21632163
'theme' => array(
2164-
'stylesheet' => $this->get_stylesheet(),
2165-
'active' => $this->is_theme_active(),
2164+
'stylesheet' => $this->get_stylesheet(),
2165+
'active' => $this->is_theme_active(),
2166+
'isBlockTheme' => wp_is_block_theme(),
21662167
),
21672168
'url' => array(
21682169
'self' => $self_url,

src/wp-includes/script-loader.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,8 +2541,27 @@ function wp_enqueue_global_styles() {
25412541
* and add it before the global styles custom CSS.
25422542
*/
25432543
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
2544-
// Get the custom CSS from the Customizer and add it to the global stylesheet.
2545-
$custom_css = wp_get_custom_css();
2544+
2545+
/*
2546+
* Get the custom CSS from the Customizer and add it to the global stylesheet.
2547+
* Always do this in Customizer preview for the sake of live preview since it be empty.
2548+
*/
2549+
$custom_css = trim( wp_get_custom_css() );
2550+
if ( $custom_css || is_customize_preview() ) {
2551+
if ( is_customize_preview() ) {
2552+
/*
2553+
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2554+
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2555+
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2556+
* would break live previewing.
2557+
*/
2558+
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2559+
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2560+
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2561+
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
2562+
}
2563+
$custom_css = "\n" . $custom_css;
2564+
}
25462565
$stylesheet .= $custom_css;
25472566

25482567
// Add the global styles custom CSS at the end.

0 commit comments

Comments
 (0)