PluginProbe
Gutenberg / 16.2.0
Gutenberg v16.2.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 16.2.0, at lib/block-supports/elements.php

120 lines 3.9 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 = wp_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 $hover_link_color = null;
43 if ( ! empty( $block['attrs'] ) ) {
44 $hover_link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', ':hover', 'color', 'text' ), null );
45 }
46
47 /*
48 * For now we only care about link colors.
49 * This code in the future when we have a public API
50 * should take advantage of WP_Theme_JSON_Gutenberg::compute_style_properties
51 * and work for any element and style.
52 */
53 if ( null === $link_color && null === $hover_link_color ) {
54 return $block_content;
55 }
56
57 // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
58 // Add the class name to the first element, presuming it's the wrapper, if it exists.
59 $tags = new WP_HTML_Tag_Processor( $block_content );
60 if ( $tags->next_tag() ) {
61 $tags->add_class( gutenberg_get_elements_class_name( $block ) );
62 }
63
64 return $tags->get_updated_html();
65 }
66
67 /**
68 * Render the elements stylesheet.
69 *
70 * In the case of nested blocks we want the parent element styles to be rendered before their descendants.
71 * This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
72 * we want the descendant style to take priority, and this is done by loading it after, in DOM order.
73 *
74 * @param string|null $pre_render The pre-rendered content. Default null.
75 * @param array $block The block being rendered.
76 *
77 * @return null
78 */
79 function gutenberg_render_elements_support_styles( $pre_render, $block ) {
80 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
81 $element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
82
83 /*
84 * For now we only care about link color.
85 */
86 $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
87
88 if ( $skip_link_color_serialization ) {
89 return null;
90 }
91 $class_name = gutenberg_get_elements_class_name( $block );
92 $link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null;
93
94 gutenberg_style_engine_get_styles(
95 $link_block_styles,
96 array(
97 'selector' => ".$class_name a",
98 'context' => 'block-supports',
99 )
100 );
101
102 if ( isset( $link_block_styles[':hover'] ) ) {
103 gutenberg_style_engine_get_styles(
104 $link_block_styles[':hover'],
105 array(
106 'selector' => ".$class_name a:hover",
107 'context' => 'block-supports',
108 )
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