| 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 |
|