Skip to content

Commit 9b3d784

Browse files
westonrutert-hamanopeterwilsoncc
authored
Fix ability to preview Additional CSS changes in the Customizer when using a Block Theme (#70428)
* Fix ability to preview Custom CSS changes in the Customizer * Add backport changelog WordPress/wordpress-develop#9000 #70428 * Undo unnecessary update to deprecated function * Ensure global styles are shown in Customizer preview and override Customizer styles in cascade * Add Customizer details to the comment * Update Customizer Custom CSS inside of global styles for live preview * Make sure saved milestone comments are removed from stored CSS when previewing * Add line breaks before/after Custom CSS for readability/debugging Co-authored-by: Peter Wilson <[email protected]> * Remove erroneous duplicated appending of global styles custom CSS * Simplify adding inline script to customize-preview Co-authored-by: t-hamano <[email protected]> * Improve JS formatting * Add commit reference * Move custom CSS preview logic to 6.9 compat include Co-authored-by: t-hamano <[email protected]> --------- Co-authored-by: westonruter <[email protected]> Co-authored-by: t-hamano <[email protected]> Co-authored-by: Peter Wilson <[email protected]>
1 parent 4c3e4c4 commit 9b3d784

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

backport-changelog/6.9/9000.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/WordPress/wordpress-develop/pull/9000
2+
3+
* https://github.com/WordPress/gutenberg/pull/70428
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Adds inline script to extend customize-preview.js.
4+
*
5+
* @package gutenberg
6+
*/
7+
8+
/**
9+
* Adds JS logic to the Customizer preview for live previewing Custom CSS for Block Themes.
10+
*
11+
* The logic in this function would be back-ported into customize-preview.js.
12+
* This was committed to wordpress-develop trunk in <https://core.trac.wordpress.org/changeset/60522>.
13+
*/
14+
function gutenberg_add_customizer_block_theme_custom_css_preview_js() {
15+
if ( ! wp_is_block_theme() ) {
16+
return;
17+
}
18+
19+
$setting_id = 'custom_css[' . get_stylesheet() . ']';
20+
21+
$js_function = <<<JS
22+
( settingId ) => {
23+
wp.customize.bind( 'preview-ready', () => {
24+
// Skip running logic that is already merged in trunk.
25+
if ( window._wpCustomizeSettings.theme.isBlockTheme ) {
26+
return;
27+
}
28+
29+
wp.customize( settingId, function ( setting ) {
30+
setting.bind( function ( newValue ) {
31+
const style = document.querySelector( 'style#global-styles-inline-css' );
32+
if ( ! style ) {
33+
return;
34+
}
35+
36+
// Forbid milestone comments from appearing in Custom CSS which would break live preview.
37+
newValue = newValue.replace( /\/\*(BEGIN|END)_CUSTOMIZER_CUSTOM_CSS\*\//g, '' );
38+
39+
style.textContent = style.textContent.replace(
40+
/(\/\*BEGIN_CUSTOMIZER_CUSTOM_CSS\*\/)((?:.|\s)*?)(\/\*END_CUSTOMIZER_CUSTOM_CSS\*\/)/,
41+
function ( match, beforeComment, oldValue, afterComment ) {
42+
return beforeComment + newValue + afterComment;
43+
}
44+
);
45+
} );
46+
} );
47+
} );
48+
}
49+
JS;
50+
wp_add_inline_script(
51+
'customize-preview',
52+
sprintf( '( %s )( %s )', $js_function, wp_json_encode( $setting_id ) )
53+
);
54+
}
55+
56+
if ( version_compare( get_bloginfo( 'version' ), '6.9.0', '<' ) ) {
57+
add_action( 'customize_preview_init', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );
58+
}

lib/load.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function gutenberg_is_experiment_enabled( $name ) {
7979
require __DIR__ . '/compat/wordpress-6.8/block-template-utils.php';
8080
require __DIR__ . '/compat/wordpress-6.8/site-preview.php';
8181

82+
// WordPress 6.9 compat.
83+
require __DIR__ . '/compat/wordpress-6.9/customizer-preview-custom-css.php';
84+
8285
// Experimental features.
8386
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
8487
require __DIR__ . '/experimental/blocks.php';

lib/script-loader.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,27 @@ function gutenberg_enqueue_global_styles() {
5050
* and add it before the global styles custom CSS.
5151
*/
5252
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
53-
// Get the custom CSS from the Customizer and add it to the global stylesheet.
54-
$custom_css = wp_get_custom_css();
53+
54+
/*
55+
* Get the custom CSS from the Customizer and add it to the global stylesheet.
56+
* Always do this in Customizer preview for the sake of live preview since it be empty.
57+
*/
58+
$custom_css = trim( wp_get_custom_css() );
59+
if ( $custom_css || is_customize_preview() ) {
60+
if ( is_customize_preview() ) {
61+
/*
62+
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
63+
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
64+
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
65+
* would break live previewing.
66+
*/
67+
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
68+
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
69+
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
70+
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
71+
}
72+
$custom_css = "\n" . $custom_css;
73+
}
5574
$stylesheet .= $custom_css;
5675

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

0 commit comments

Comments
 (0)