PluginProbe
Gutenberg / 23.3.0
Gutenberg v23.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-6.9 / customizer-preview-custom-css.php

customizer-preview-custom-css.php in Gutenberg 23.3.0, at lib/compat/wordpress-6.9/customizer-preview-custom-css.php

59 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) )
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 }
59