Skip to content

Fix ability to preview Additional CSS changes in the Customizer in Block Themes #9000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
16 changes: 14 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,23 @@
/**
* 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;
// TODO: If none of the logic in this function resulted in a CSS update (e.g. due to some optimizer plugin that concatenates/minifies CSS), then a message should be sent to the controls to initiate a reload.
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 + value + 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
15 changes: 14 additions & 1 deletion src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2541,8 +2541,21 @@ 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();
$custom_css = wp_get_custom_css();
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 . $custom_css . $after_milestone;
}
$stylesheet .= $custom_css;

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