Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions src/js/_enqueues/wp/customize/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,22 @@
/**
* Preview changes to custom css.
*
* @param {string} value Custom CSS..
* @param {string} value Custom CSS.
* @return {void}
*/
custom_css: function( value ) {
$( '#wp-custom-css' ).text( value );
var style;
if ( api.settings.theme.isBlockTheme ) {
style = $( 'style#global-styles-inline-css' );
value = value.replace( /\/\*(BEGIN|END)_CUSTOMIZER_CUSTOM_CSS\*\//g, '' ); // Forbid milestone comments from appearing in Custom CSS which would break live preview.
var textContent = style.text().replace( /(\/\*BEGIN_CUSTOMIZER_CUSTOM_CSS\*\/)((?:.|\s)*?)(\/\*END_CUSTOMIZER_CUSTOM_CSS\*\/)/, function ( match, beforeComment, oldValue, afterComment ) {
return beforeComment + '\n' + value + '\n' + afterComment;
} );
style.text( textContent );
} else {
style = $( 'style#wp-custom-css' );
style.text( value );
}
},

/**
Expand Down
5 changes: 3 additions & 2 deletions src/wp-includes/class-wp-customize-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2161,8 +2161,9 @@ public function customize_preview_settings() {
'keepAliveSend' => 1000,
),
'theme' => array(
'stylesheet' => $this->get_stylesheet(),
'active' => $this->is_theme_active(),
'stylesheet' => $this->get_stylesheet(),
'active' => $this->is_theme_active(),
'isBlockTheme' => wp_is_block_theme(),
),
'url' => array(
'self' => $self_url,
Expand Down
23 changes: 21 additions & 2 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2541,8 +2541,27 @@ function wp_enqueue_global_styles() {
* and add it before the global styles custom CSS.
*/
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
// Get the custom CSS from the Customizer and add it to the global stylesheet.
$custom_css = wp_get_custom_css();

/*
* Get the custom CSS from the Customizer and add it to the global stylesheet.
* Always do this in Customizer preview for the sake of live preview since it be empty.
*/
$custom_css = trim( wp_get_custom_css() );
if ( $custom_css || is_customize_preview() ) {
if ( is_customize_preview() ) {
/*
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
* would break live previewing.
*/
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
}
$custom_css = "\n" . $custom_css;
}
$stylesheet .= $custom_css;

// Add the global styles custom CSS at the end.
Expand Down
Loading