PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / block-supports / elements.php

elements.php in Gutenberg 14.7.0, at lib/block-supports/elements.php

120 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Elements styles block support.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Get the elements class names.
10 *
11 * @param array $block Block object.
12 * @return string The unique class name.
13 */
14 function gutenberg_get_elements_class_name( $block ) {
15 return 'wp-elements-' . md5( serialize( $block ) );
16 }
17
18 /**
19 * Update the block content with elements class names.
20 *
21 * @param string $block_content Rendered block content.
22 * @param array $block Block object.
23 * @return string Filtered block content.
24 */
25 function gutenberg_render_elements_support( $block_content, $block ) {
26 if ( ! $block_content ) {
27 return $block_content;
28 }
29
30 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
31 $skip_link_color_serialization = gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
32
33 if ( $skip_link_color_serialization ) {
34 return $block_content;
35 }
36
37 $link_color = null;
38 if ( ! empty( $block['attrs'] ) ) {
39 $link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null );
40 }
41
42 /*
43 * For now we only care about link color.
44 * This code in the future when we have a public API
45 * should take advantage of WP_Theme_JSON_Gutenberg::compute_style_properties
46 * and work for any element and style.
47 */
48 if ( null === $link_color ) {
49 return $block_content;
50 }
51
52 $class_name = gutenberg_get_elements_class_name( $block );
53
54 // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
55 // Retrieve the opening tag of the first HTML element.
56 $html_element_matches = array();
57 preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
58 $first_element = $html_element_matches[0][0];
59 // If the first HTML element has a class attribute just add the new class
60 // as we do on layout and duotone.
61 if ( str_contains( $first_element, 'class="' ) ) {
62 $content = preg_replace(
63 '/' . preg_quote( 'class="', '/' ) . '/',
64 'class="' . $class_name . ' ',
65 $block_content,
66 1
67 );
68 } else {
69 // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
70 $first_element_offset = $html_element_matches[0][1];
71 $content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
72 }
73
74 return $content;
75 }
76
77 /**
78 * Render the elements stylesheet.
79 *
80 * In the case of nested blocks we want the parent element styles to be rendered before their descendants.
81 * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
82 * we want the descendant style to take priority, and this is done by loading it after, in DOM order.
83 *
84 * @param string|null $pre_render The pre-rendered content. Default null.
85 * @param array $block The block being rendered.
86 *
87 * @return null
88 */
89 function gutenberg_render_elements_support_styles( $pre_render, $block ) {
90 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
91 $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
92
93 /*
94 * For now we only care about link color.
95 */
96 $skip_link_color_serialization = gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
97
98 if ( $skip_link_color_serialization ) {
99 return null;
100 }
101 $class_name = gutenberg_get_elements_class_name( $block );
102 $link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null;
103
104 gutenberg_style_engine_get_styles(
105 $link_block_styles,
106 array(
107 'selector' => ".$class_name a",
108 'context' => 'block-supports',
109 )
110 );
111
112 return null;
113 }
114
115 // Remove WordPress core filters to avoid rendering duplicate elements stylesheet & attaching classes twice.
116 remove_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
117 remove_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
118 add_filter( 'render_block', 'gutenberg_render_elements_support', 10, 2 );
119 add_filter( 'pre_render_block', 'gutenberg_render_elements_support_styles', 10, 2 );
120