PluginProbe
Gutenberg / 23.2.0
Gutenberg v23.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 / settings.php
lib/block-supports
anchor.php 1.4 KB 7 months ago aria-label.php 1.6 KB 6 months ago background.php 4.6 KB 5 months ago block-style-variations.php 10.3 KB 7 months ago block-visibility.php 5.4 KB 5 months ago border.php 6.0 KB 1 year ago colors.php 5.7 KB 5 months ago custom-css.php 8.6 KB 4 months ago dimensions.php 5.4 KB 4 months ago duotone.php 12.3 KB 5 months ago elements.php 8.8 KB 7 months ago layout.php 45.7 KB 4 months ago position.php 4.2 KB 2 years ago settings.php 4.5 KB 1 year ago shadow.php 2.0 KB 2 years ago spacing.php 2.6 KB 7 months ago typography.php 27.5 KB 6 months ago

settings.php in Gutenberg 23.2.0, at lib/block-supports/settings.php

135 lines 4.5 KB Raw Download Zip
1 <?php
2 /**
3 * Block level presets support.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Update the block content with block level presets class name.
10 *
11 * @access private
12 *
13 * @param string $block_content Rendered block content.
14 * @param array $block Block object.
15 * @return string Filtered block content.
16 */
17 function _gutenberg_add_block_level_presets_class( $block_content, $block ) {
18 if ( ! $block_content ) {
19 return $block_content;
20 }
21
22 // return early if the block doesn't have support for settings.
23 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
24 if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) {
25 return $block_content;
26 }
27
28 // return early if no settings are found on the block attributes.
29 $block_settings = $block['attrs']['settings'] ?? null;
30 if ( empty( $block_settings ) ) {
31 return $block_content;
32 }
33
34 // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
35 // Add the class name to the first element, presuming it's the wrapper, if it exists.
36 $tags = new WP_HTML_Tag_Processor( $block_content );
37 if ( $tags->next_tag() ) {
38 $tags->add_class( _wp_get_presets_class_name( $block ) );
39 }
40
41 return $tags->get_updated_html();
42 }
43
44 /**
45 * Render the block level presets stylesheet.
46 *
47 * @access private
48 *
49 * @param string|null $pre_render The pre-rendered content. Default null.
50 * @param array $block The block being rendered.
51 *
52 * @return null
53 */
54 function _gutenberg_add_block_level_preset_styles( $pre_render, $block ) {
55 // Return early if the block has not support for descendent block styles.
56 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
57 if ( ! block_has_support( $block_type, array( '__experimentalSettings' ), false ) ) {
58 return null;
59 }
60
61 // return early if no settings are found on the block attributes.
62 $block_settings = $block['attrs']['settings'] ?? null;
63 if ( empty( $block_settings ) ) {
64 return null;
65 }
66
67 $class_name = '.' . _wp_get_presets_class_name( $block );
68
69 // the root selector for preset variables needs to target every possible block selector
70 // in order for the general setting to override any bock specific setting of a parent block or
71 // the site root.
72 $variables_root_selector = '*,[class*="wp-block"]';
73 $registry = WP_Block_Type_Registry::get_instance();
74 $blocks = $registry->get_all_registered();
75 foreach ( $blocks as $block_type ) {
76 // We only want to append selectors for block's using custom selectors
77 // i.e. not `wp-block-<name>`.
78 $has_custom_selector =
79 ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) ||
80 ( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) );
81
82 if ( $has_custom_selector ) {
83 $variables_root_selector .= ',' . wp_get_block_css_selector( $block_type );
84 }
85 }
86 $variables_root_selector = WP_Theme_JSON_Gutenberg::scope_selector( $class_name, $variables_root_selector );
87
88 // Remove any potentially unsafe styles.
89 $theme_json_shape = WP_Theme_JSON_Gutenberg::remove_insecure_properties(
90 array(
91 'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
92 'settings' => $block_settings,
93 )
94 );
95 $theme_json_object = new WP_Theme_JSON_Gutenberg( $theme_json_shape );
96
97 $styles = '';
98
99 // include preset css variables declaration on the stylesheet.
100 $styles .= $theme_json_object->get_stylesheet(
101 array( 'variables' ),
102 null,
103 array(
104 'root_selector' => $variables_root_selector,
105 'scope' => $class_name,
106 )
107 );
108
109 // include preset css classes on the stylesheet.
110 $styles .= $theme_json_object->get_stylesheet(
111 array( 'presets' ),
112 null,
113 array(
114 'root_selector' => $class_name . ',' . $class_name . ' *',
115 'scope' => $class_name,
116 )
117 );
118
119 if ( ! empty( $styles ) ) {
120 /*
121 * This method is deprecated since WordPress 6.2.
122 * We could enqueue these styles separately,
123 * or print them out with other settings presets.
124 */
125 wp_enqueue_block_support_styles( $styles );
126 }
127
128 return null;
129 }
130 // Remove WordPress core filter to avoid rendering duplicate settings style blocks.
131 remove_filter( 'render_block', '_wp_add_block_level_presets_class', 10 );
132 remove_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10 );
133 add_filter( 'render_block', '_gutenberg_add_block_level_presets_class', 10, 2 );
134 add_filter( 'pre_render_block', '_gutenberg_add_block_level_preset_styles', 10, 2 );
135