| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block level presets support. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Update the block content with block level presets class name. |
| 10 |
* |
| 11 |
* @access private |
| 12 |
* |
| 13 |
* @param string $block_content Rendered block content. |
| 14 |
* @param array $block Block object. |
| 15 |
* @return string Filtered block content. |
| 16 |
*/ |
| 17 |
function _gutenberg_add_block_level_presets_class( $block_content, $block ) { |
| 18 |
if ( ! $block_content ) { |
| 19 |
return $block_content; |
| 20 |
} |
| 21 |
|
| 22 |
// return early if the block doesn't have support for settings. |
| 23 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 24 |
if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) { |
| 25 |
return $block_content; |
| 26 |
} |
| 27 |
|
| 28 |
// return early if no settings are found on the block attributes. |
| 29 |
$block_settings = $block['attrs']['settings'] ?? null; |
| 30 |
if ( empty( $block_settings ) ) { |
| 31 |
return $block_content; |
| 32 |
} |
| 33 |
|
| 34 |
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper. |
| 35 |
// Add the class name to the first element, presuming it's the wrapper, if it exists. |
| 36 |
$tags = new WP_HTML_Tag_Processor( $block_content ); |
| 37 |
if ( $tags->next_tag() ) { |
| 38 |
$tags->add_class( _wp_get_presets_class_name( $block ) ); |
| 39 |
} |
| 40 |
|
| 41 |
return $tags->get_updated_html(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Render the block level presets stylesheet. |
| 46 |
* |
| 47 |
* @access private |
| 48 |
* |
| 49 |
* @param string|null $pre_render The pre-rendered content. Default null. |
| 50 |
* @param array $block The block being rendered. |
| 51 |
* |
| 52 |
* @return null |
| 53 |
*/ |
| 54 |
function _gutenberg_add_block_level_preset_styles( $pre_render, $block ) { |
| 55 |
// Return early if the block has not support for descendent block styles. |
| 56 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 57 |
if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
// return early if no settings are found on the block attributes. |
| 62 |
$block_settings = $block['attrs']['settings'] ?? null; |
| 63 |
if ( empty( $block_settings ) ) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
$class_name = '.' . _wp_get_presets_class_name( $block ); |
| 68 |
|
| 69 |
// the root selector for preset variables needs to target every possible block selector |
| 70 |
// in order for the general setting to override any bock specific setting of a parent block or |
| 71 |
// the site root. |
| 72 |
$variables_root_selector = '*,[class*="wp-block"]'; |
| 73 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 74 |
$blocks = $registry->get_all_registered(); |
| 75 |
foreach ( $blocks as $block_type ) { |
| 76 |
// We only want to append selectors for block's using custom selectors |
| 77 |
// i.e. not `wp-block-<name>`. |
| 78 |
$has_custom_selector = |
| 79 |
( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) || |
| 80 |
( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) ); |
| 81 |
|
| 82 |
if ( $has_custom_selector ) { |
| 83 |
$variables_root_selector .= ',' . wp_get_block_css_selector( $block_type ); |
| 84 |
} |
| 85 |
} |
| 86 |
$variables_root_selector = WP_Theme_JSON_Gutenberg::scope_selector( $class_name, $variables_root_selector ); |
| 87 |
|
| 88 |
// Remove any potentially unsafe styles. |
| 89 |
$theme_json_shape = WP_Theme_JSON_Gutenberg::remove_insecure_properties( |
| 90 |
array( |
| 91 |
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA, |
| 92 |
'settings' => $block_settings, |
| 93 |
) |
| 94 |
); |
| 95 |
$theme_json_object = new WP_Theme_JSON_Gutenberg( $theme_json_shape ); |
| 96 |
|
| 97 |
$styles = ''; |
| 98 |
|
| 99 |
// include preset css variables declaration on the stylesheet. |
| 100 |
$styles .= $theme_json_object->get_stylesheet( |
| 101 |
array( 'variables' ), |
| 102 |
null, |
| 103 |
array( |
| 104 |
'root_selector' => $variables_root_selector, |
| 105 |
'scope' => $class_name, |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
// include preset css classes on the stylesheet. |
| 110 |
$styles .= $theme_json_object->get_stylesheet( |
| 111 |
array( 'presets' ), |
| 112 |
null, |
| 113 |
array( |
| 114 |
'root_selector' => $class_name . ',' . $class_name . ' *', |
| 115 |
'scope' => $class_name, |
| 116 |
) |
| 117 |
); |
| 118 |
|
| 119 |
if ( ! empty( $styles ) ) { |
| 120 |
/* |
| 121 |
* This method is deprecated since WordPress 6.2. |
| 122 |
* We could enqueue these styles separately, |
| 123 |
* or print them out with other settings presets. |
| 124 |
*/ |
| 125 |
wp_enqueue_block_support_styles( $styles ); |
| 126 |
} |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
// Remove WordPress core filter to avoid rendering duplicate settings style blocks. |
| 131 |
remove_filter( 'render_block', '_wp_add_block_level_presets_class', 10 ); |
| 132 |
remove_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10 ); |
| 133 |
add_filter( 'render_block', '_gutenberg_add_block_level_presets_class', 10, 2 ); |
| 134 |
add_filter( 'pre_render_block', '_gutenberg_add_block_level_preset_styles', 10, 2 ); |
| 135 |
|