2021-05-21 06:14:23 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Elements styles block support.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the elements stylesheet.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
|
|
/*
|
2021-07-01 17:29:56 -04:00
|
|
|
* For now we only care about link color.
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( null === $link_color ) {
|
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
2022-03-29 09:06:07 -04:00
|
|
|
$class_name = wp_unique_id( 'wp-elements-' );
|
2021-05-21 06:14:23 -04:00
|
|
|
|
|
|
|
if ( strpos( $link_color, 'var:preset|color|' ) !== false ) {
|
|
|
|
// Get the name from the string and add proper styles.
|
|
|
|
$index_to_splice = strrpos( $link_color, '|' ) + 1;
|
|
|
|
$link_color_name = substr( $link_color, $index_to_splice );
|
|
|
|
$link_color = "var(--wp--preset--color--$link_color_name)";
|
|
|
|
}
|
|
|
|
$link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) );
|
|
|
|
|
2022-02-16 12:28:01 -05:00
|
|
|
$style = ".$class_name a{" . $link_color_declaration . ';}';
|
2021-05-21 06:14:23 -04:00
|
|
|
|
|
|
|
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
|
|
|
|
// Retrieve the opening tag of the first HTML element.
|
|
|
|
$html_element_matches = array();
|
|
|
|
preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
|
|
|
|
$first_element = $html_element_matches[0][0];
|
|
|
|
// If the first HTML element has a class attribute just add the new class
|
|
|
|
// as we do on layout and duotone.
|
|
|
|
if ( strpos( $first_element, 'class="' ) !== false ) {
|
|
|
|
$content = preg_replace(
|
|
|
|
'/' . preg_quote( 'class="', '/' ) . '/',
|
|
|
|
'class="' . $class_name . ' ',
|
|
|
|
$block_content,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
|
|
|
|
$first_element_offset = $html_element_matches[0][1];
|
|
|
|
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
|
|
|
|
}
|
|
|
|
|
2022-02-16 17:20:01 -05:00
|
|
|
wp_enqueue_block_support_styles( $style );
|
2021-05-21 06:14:23 -04:00
|
|
|
|
2021-11-08 21:17:17 -05:00
|
|
|
return $content;
|
2021-05-21 06:14:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
|