|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _wp_get_presets_class_name( $block ) { |
|
return 'wp-settings-' . md5( serialize( $block ) ); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _wp_add_block_level_presets_class( $block_content, $block ) { |
|
if ( ! $block_content ) { |
|
return $block_content; |
|
} |
|
|
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
|
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) { |
|
return $block_content; |
|
} |
|
|
|
|
|
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null; |
|
if ( empty( $block_settings ) ) { |
|
return $block_content; |
|
} |
|
|
|
|
|
|
|
$tags = new WP_HTML_Tag_Processor( $block_content ); |
|
if ( $tags->next_tag() ) { |
|
$tags->add_class( _wp_get_presets_class_name( $block ) ); |
|
} |
|
|
|
return $tags->get_updated_html(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _wp_add_block_level_preset_styles( $pre_render, $block ) { |
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
|
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) { |
|
return null; |
|
} |
|
|
|
|
|
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null; |
|
if ( empty( $block_settings ) ) { |
|
return null; |
|
} |
|
|
|
$class_name = '.' . _wp_get_presets_class_name( $block ); |
|
|
|
|
|
|
|
|
|
$variables_root_selector = '*,[class*="wp-block"]'; |
|
$registry = WP_Block_Type_Registry::get_instance(); |
|
$blocks = $registry->get_all_registered(); |
|
foreach ( $blocks as $block_type ) { |
|
|
|
|
|
|
|
|
|
$has_custom_selector = |
|
( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) || |
|
( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) ); |
|
|
|
if ( $has_custom_selector ) { |
|
$variables_root_selector .= ',' . wp_get_block_css_selector( $block_type ); |
|
} |
|
} |
|
$variables_root_selector = WP_Theme_JSON::scope_selector( $class_name, $variables_root_selector ); |
|
|
|
|
|
$theme_json_shape = WP_Theme_JSON::remove_insecure_properties( |
|
array( |
|
'version' => WP_Theme_JSON::LATEST_SCHEMA, |
|
'settings' => $block_settings, |
|
) |
|
); |
|
$theme_json_object = new WP_Theme_JSON( $theme_json_shape ); |
|
|
|
$styles = ''; |
|
|
|
|
|
$styles .= $theme_json_object->get_stylesheet( |
|
array( 'variables' ), |
|
null, |
|
array( |
|
'root_selector' => $variables_root_selector, |
|
'scope' => $class_name, |
|
) |
|
); |
|
|
|
|
|
$styles .= $theme_json_object->get_stylesheet( |
|
array( 'presets' ), |
|
null, |
|
array( |
|
'root_selector' => $class_name . ',' . $class_name . ' *', |
|
'scope' => $class_name, |
|
) |
|
); |
|
|
|
if ( ! empty( $styles ) ) { |
|
wp_enqueue_block_support_styles( $styles ); |
|
} |
|
|
|
return null; |
|
} |
|
|
|
add_filter( 'render_block', '_wp_add_block_level_presets_class', 10, 2 ); |
|
add_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10, 2 ); |
|
|