| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elements styles block support. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Render the elements stylesheet. |
| 10 |
* |
| 11 |
* @param string $block_content Rendered block content. |
| 12 |
* @param array $block Block object. |
| 13 |
* @return string Filtered block content. |
| 14 |
*/ |
| 15 |
function gutenberg_render_elements_support( $block_content, $block ) { |
| 16 |
|
| 17 |
if ( ! $block_content ) { |
| 18 |
return $block_content; |
| 19 |
} |
| 20 |
|
| 21 |
$link_color = null; |
| 22 |
if ( ! empty( $block['attrs'] ) ) { |
| 23 |
$link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null ); |
| 24 |
} |
| 25 |
|
| 26 |
/* |
| 27 |
* For now we only care about link color. |
| 28 |
* This code in the future when we have a public API |
| 29 |
* should take advantage of WP_Theme_JSON_Gutenberg::compute_style_properties |
| 30 |
* and work for any element and style. |
| 31 |
*/ |
| 32 |
if ( null === $link_color ) { |
| 33 |
return $block_content; |
| 34 |
} |
| 35 |
|
| 36 |
$class_name = 'wp-elements-' . uniqid(); |
| 37 |
|
| 38 |
if ( strpos( $link_color, 'var:preset|color|' ) !== false ) { |
| 39 |
// Get the name from the string and add proper styles. |
| 40 |
$index_to_splice = strrpos( $link_color, '|' ) + 1; |
| 41 |
$link_color_name = substr( $link_color, $index_to_splice ); |
| 42 |
$link_color = "var(--wp--preset--color--$link_color_name)"; |
| 43 |
} |
| 44 |
$link_color_declaration = esc_html( safecss_filter_attr( "color: $link_color" ) ); |
| 45 |
|
| 46 |
$style = "<style>.$class_name a{" . $link_color_declaration . ";}</style>\n"; |
| 47 |
|
| 48 |
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper. |
| 49 |
// Retrieve the opening tag of the first HTML element. |
| 50 |
$html_element_matches = array(); |
| 51 |
preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE ); |
| 52 |
$first_element = $html_element_matches[0][0]; |
| 53 |
// If the first HTML element has a class attribute just add the new class |
| 54 |
// as we do on layout and duotone. |
| 55 |
if ( strpos( $first_element, 'class="' ) !== false ) { |
| 56 |
$content = preg_replace( |
| 57 |
'/' . preg_quote( 'class="', '/' ) . '/', |
| 58 |
'class="' . $class_name . ' ', |
| 59 |
$block_content, |
| 60 |
1 |
| 61 |
); |
| 62 |
} else { |
| 63 |
// If the first HTML element has no class attribute we should inject the attribute before the attribute at the end. |
| 64 |
$first_element_offset = $html_element_matches[0][1]; |
| 65 |
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 ); |
| 66 |
} |
| 67 |
|
| 68 |
// Ideally styles should be loaded in the head, but blocks may be parsed |
| 69 |
// after that, so loading in the footer for now. |
| 70 |
// See https://core.trac.wordpress.org/ticket/53494. |
| 71 |
add_action( |
| 72 |
'wp_footer', |
| 73 |
function () use ( $style ) { |
| 74 |
echo $style; |
| 75 |
} |
| 76 |
); |
| 77 |
|
| 78 |
return $content; |
| 79 |
} |
| 80 |
|
| 81 |
// Remove WordPress core filter to avoid rendering duplicate elements stylesheet. |
| 82 |
remove_filter( 'render_block', 'wp_render_elements_support', 10, 2 ); |
| 83 |
add_filter( 'render_block', 'gutenberg_render_elements_support', 10, 2 ); |
| 84 |
|