2021-05-21 06:14:23 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Elements styles block support.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-11-24 17:36:11 -05:00
|
|
|
* Gets the elements class names.
|
2022-04-25 12:37:08 -04:00
|
|
|
*
|
|
|
|
* @since 6.0.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param array $block Block object.
|
2022-11-24 17:52:11 -05:00
|
|
|
* @return string The unique class name.
|
2022-04-25 12:37:08 -04:00
|
|
|
*/
|
|
|
|
function wp_get_elements_class_name( $block ) {
|
|
|
|
return 'wp-elements-' . md5( serialize( $block ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-24 17:36:11 -05:00
|
|
|
* Updates the block content with elements class names.
|
2021-05-21 06:14:23 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
* @access private
|
|
|
|
*
|
2021-07-01 17:29:56 -04:00
|
|
|
* @param string $block_content Rendered block content.
|
|
|
|
* @param array $block Block object.
|
|
|
|
* @return string Filtered block content.
|
2021-05-21 06:14:23 -04:00
|
|
|
*/
|
|
|
|
function wp_render_elements_support( $block_content, $block ) {
|
2021-11-08 21:17:17 -05:00
|
|
|
if ( ! $block_content ) {
|
|
|
|
return $block_content;
|
2022-04-05 08:08:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
|
|
|
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
|
|
|
|
|
|
|
|
if ( $skip_link_color_serialization ) {
|
|
|
|
return $block_content;
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
2021-06-01 04:10:04 -04:00
|
|
|
$link_color = null;
|
|
|
|
if ( ! empty( $block['attrs'] ) ) {
|
|
|
|
$link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null );
|
|
|
|
}
|
2021-05-21 06:14:23 -04:00
|
|
|
|
2023-06-25 21:15:21 -04:00
|
|
|
$hover_link_color = null;
|
|
|
|
if ( ! empty( $block['attrs'] ) ) {
|
|
|
|
$hover_link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', ':hover', 'color', 'text' ), null );
|
|
|
|
}
|
|
|
|
|
2021-05-21 06:14:23 -04:00
|
|
|
/*
|
2023-06-25 21:15:21 -04:00
|
|
|
* For now we only care about link colors.
|
2021-07-01 17:29:56 -04:00
|
|
|
* This code in the future when we have a public API
|
|
|
|
* should take advantage of WP_Theme_JSON::compute_style_properties
|
|
|
|
* and work for any element and style.
|
|
|
|
*/
|
2023-06-25 21:15:21 -04:00
|
|
|
if ( null === $link_color && null === $hover_link_color ) {
|
2021-05-21 06:14:23 -04:00
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
|
2023-02-06 14:43:16 -05:00
|
|
|
// Add the class name to the first element, presuming it's the wrapper, if it exists.
|
|
|
|
$tags = new WP_HTML_Tag_Processor( $block_content );
|
|
|
|
if ( $tags->next_tag() ) {
|
|
|
|
$tags->add_class( wp_get_elements_class_name( $block ) );
|
2021-05-21 06:14:23 -04:00
|
|
|
}
|
|
|
|
|
2023-02-06 14:43:16 -05:00
|
|
|
return $tags->get_updated_html();
|
2022-04-25 12:37:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-24 17:36:11 -05:00
|
|
|
* Renders the elements stylesheet.
|
2022-04-25 12:37:08 -04:00
|
|
|
*
|
|
|
|
* In the case of nested blocks we want the parent element styles to be rendered before their descendants.
|
|
|
|
* This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
|
|
|
|
* we want the descendant style to take priority, and this is done by loading it after, in DOM order.
|
|
|
|
*
|
|
|
|
* @since 6.0.0
|
2022-09-19 16:14:10 -04:00
|
|
|
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
2022-04-25 12:37:08 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2022-11-24 17:52:11 -05:00
|
|
|
* @param string|null $pre_render The pre-rendered content. Default null.
|
|
|
|
* @param array $block The block being rendered.
|
2022-04-25 12:37:08 -04:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
function wp_render_elements_support_styles( $pre_render, $block ) {
|
2022-09-19 16:14:10 -04:00
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
|
|
|
$element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
|
2022-04-25 12:37:08 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For now we only care about link color.
|
|
|
|
*/
|
2022-09-19 16:14:10 -04:00
|
|
|
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
|
2022-04-25 12:37:08 -04:00
|
|
|
|
2022-09-19 16:14:10 -04:00
|
|
|
if ( $skip_link_color_serialization ) {
|
|
|
|
return null;
|
2022-04-25 12:37:08 -04:00
|
|
|
}
|
2022-09-19 16:14:10 -04:00
|
|
|
$class_name = wp_get_elements_class_name( $block );
|
|
|
|
$link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null;
|
|
|
|
|
|
|
|
wp_style_engine_get_styles(
|
|
|
|
$link_block_styles,
|
|
|
|
array(
|
|
|
|
'selector' => ".$class_name a",
|
|
|
|
'context' => 'block-supports',
|
|
|
|
)
|
|
|
|
);
|
2021-05-21 06:14:23 -04:00
|
|
|
|
2023-06-25 21:15:21 -04:00
|
|
|
if ( isset( $link_block_styles[':hover'] ) ) {
|
|
|
|
wp_style_engine_get_styles(
|
|
|
|
$link_block_styles[':hover'],
|
|
|
|
array(
|
|
|
|
'selector' => ".$class_name a:hover",
|
|
|
|
'context' => 'block-supports',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-25 12:37:08 -04:00
|
|
|
return null;
|
2021-05-21 06:14:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
|
2022-04-25 12:37:08 -04:00
|
|
|
add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
|