| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Blocks API: WP_Block_Styles_Registry class 4 * 5 * @package WordPress 6 * @subpackage Blocks 7 * @since 5.3.0 8 */ 9 10 /** 11 * Class used for interacting with block styles. 12 * 13 * The properties of a block style as the registry stores them are more than its register() hash 14 * accepts: `name` is required for a style to be registered at all, and `label` is filled in from 15 * the name when the caller omits it, so both are always present on the way back out. The shape 16 * stays open because the array is stored verbatim, so a caller's own keys survive, including 17 * into the REST response, which does not set `additionalProperties` to false. 18 * 19 * @since 5.3.0 20 * 21 * @phpstan-type Block_Style_Properties array{ 22 * name: string, 23 * label: string, 24 * inline_style?: string, 25 * style_handle?: string, 26 * is_default?: bool, 27 * style_data?: array<string, mixed>, 28 * ... 29 * } 30 */ 31 #[AllowDynamicProperties] 32 final class WP_Block_Styles_Registry { 33 /** 34 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays. 35 * 36 * @since 5.3.0 37 * 38 * @var array<string, array<string, array<string, mixed>>> 39 * @phpstan-var array<string, array<string, Block_Style_Properties>> 40 */ 41 private $registered_block_styles = array(); 42 43 /** 44 * Container for the main instance of the class. 45 * 46 * @since 5.3.0 47 * 48 * @var WP_Block_Styles_Registry|null 49 */ 50 private static $instance = null; 51 52 /** 53 * Registers a block style for the given block type. 54 * 55 * If the block styles are present in a standalone stylesheet, register it and pass 56 * its handle as the `style_handle` argument. If the block styles should be inline, 57 * use the `inline_style` argument. Usually, one of them would be used to pass CSS 58 * styles. However, you could also skip them and provide CSS styles in any stylesheet 59 * or with an inline tag. 60 * 61 * @since 5.3.0 62 * @since 6.6.0 Added ability to register style across multiple block types along with theme.json-like style data. 63 * 64 * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/ 65 * 66 * @param string|string[] $block_name Block type name including namespace or array of namespaced block type names. 67 * @param array $style_properties { 68 * Array containing the properties of the style. 69 * 70 * @type string $name The identifier of the style used to compute a CSS class. 71 * @type string $label A human-readable label for the style. 72 * @type string $inline_style Inline CSS code that registers the CSS class required 73 * for the style. 74 * @type string $style_handle The handle to an already registered style that should be 75 * enqueued in places where block styles are needed. 76 * @type bool $is_default Whether this is the default style for the block type. 77 * @type array<string, mixed> $style_data Theme.json-like object to generate CSS from. 78 * } 79 * @return bool True if the block style was registered with success and false otherwise. 80 */ 81 public function register( $block_name, $style_properties ) { 82 83 if ( ! is_string( $block_name ) && ! is_array( $block_name ) ) { 84 _doing_it_wrong( 85 __METHOD__, 86 __( 'Block name must be a string or array.' ), 87 '6.6.0' 88 ); 89 return false; 90 } 91 92 if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) { 93 _doing_it_wrong( 94 __METHOD__, 95 __( 'Block style name must be a string.' ), 96 '5.3.0' 97 ); 98 return false; 99 } 100 101 if ( str_contains( $style_properties['name'], ' ' ) ) { 102 _doing_it_wrong( 103 __METHOD__, 104 __( 'Block style name must not contain any spaces.' ), 105 '5.9.0' 106 ); 107 return false; 108 } 109 110 $block_style_name = $style_properties['name']; 111 $block_names = is_string( $block_name ) ? array( $block_name ) : $block_name; 112 113 // Ensure there is a label defined. 114 if ( empty( $style_properties['label'] ) ) { 115 $style_properties['label'] = $block_style_name; 116 } 117 118 foreach ( $block_names as $name ) { 119 $this->registered_block_styles[ $name ][ $block_style_name ] = $style_properties; 120 } 121 122 return true; 123 } 124 125 /** 126 * Unregisters a block style of the given block type. 127 * 128 * @since 5.3.0 129 * 130 * @param string $block_name Block type name including namespace. 131 * @param string $block_style_name Block style name. 132 * @return bool True if the block style was unregistered with success and false otherwise. 133 */ 134 public function unregister( $block_name, $block_style_name ) { 135 if ( ! $this->is_registered( $block_name, $block_style_name ) ) { 136 _doing_it_wrong( 137 __METHOD__, 138 /* translators: 1: Block name, 2: Block style name. */ 139 sprintf( __( 'Block "%1$s" does not contain a style named "%2$s".' ), $block_name, $block_style_name ), 140 '5.3.0' 141 ); 142 return false; 143 } 144 145 unset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); 146 147 return true; 148 } 149 150 /** 151 * Retrieves the properties of a registered block style for the given block type. 152 * 153 * @since 5.3.0 154 * 155 * @param string $block_name Block type name including namespace. 156 * @param string $block_style_name Block style name. 157 * @return array<string, mixed>|null Registered block style properties or `null` if the block style is not registered. 158 * @phpstan-return Block_Style_Properties|null 159 */ 160 public function get_registered( $block_name, $block_style_name ) { 161 if ( ! $this->is_registered( $block_name, $block_style_name ) ) { 162 return null; 163 } 164 165 return $this->registered_block_styles[ $block_name ][ $block_style_name ]; 166 } 167 168 /** 169 * Retrieves all registered block styles. 170 * 171 * @since 5.3.0 172 * 173 * @return array<string, array<string, array<string, mixed>>> Array of arrays containing the registered block styles 174 * properties grouped by block type. 175 * @phpstan-return array<string, array<string, Block_Style_Properties>> 176 */ 177 public function get_all_registered() { 178 return $this->registered_block_styles; 179 } 180 181 /** 182 * Retrieves registered block styles for a specific block type. 183 * 184 * @since 5.3.0 185 * 186 * @param string $block_name Block type name including namespace. 187 * @return array<string, array<string, mixed>> Array whose keys are block style names and whose values are 188 * block style properties. 189 * @phpstan-return array<string, Block_Style_Properties> 190 */ 191 public function get_registered_styles_for_block( $block_name ) { 192 return $this->registered_block_styles[ $block_name ] ?? array(); 193 } 194 195 /** 196 * Checks if a block style is registered for the given block type. 197 * 198 * @since 5.3.0 199 * 200 * @param string|null $block_name Block type name including namespace. 201 * @param string|null $block_style_name Block style name. 202 * @return bool True if the block style is registered, false otherwise. 203 */ 204 public function is_registered( $block_name, $block_style_name ) { 205 return isset( $block_name, $block_style_name, $this->registered_block_styles[ $block_name ][ $block_style_name ] ); 206 } 207 208 /** 209 * Utility method to retrieve the main instance of the class. 210 * 211 * The instance will be created if it does not exist yet. 212 * 213 * @since 5.3.0 214 * 215 * @return WP_Block_Styles_Registry The main instance. 216 */ 217 public static function get_instance() { 218 self::$instance ??= new self(); 219 220 return self::$instance; 221 } 222 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Nov 11 08:20:02 2026 | Cross-referenced by PHPXref |