[ 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 * @since 5.3.0 14 */ 15 #[AllowDynamicProperties] 16 final class WP_Block_Styles_Registry { 17 /** 18 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays. 19 * 20 * @since 5.3.0 21 * 22 * @var array[] 23 */ 24 private $registered_block_styles = array(); 25 26 /** 27 * Container for the main instance of the class. 28 * 29 * @since 5.3.0 30 * 31 * @var WP_Block_Styles_Registry|null 32 */ 33 private static $instance = null; 34 35 /** 36 * Registers a block style for the given block type. 37 * 38 * If the block styles are present in a standalone stylesheet, register it and pass 39 * its handle as the `style_handle` argument. If the block styles should be inline, 40 * use the `inline_style` argument. Usually, one of them would be used to pass CSS 41 * styles. However, you could also skip them and provide CSS styles in any stylesheet 42 * or with an inline tag. 43 * 44 * @since 5.3.0 45 * 46 * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/ 47 * 48 * @param string $block_name Block type name including namespace. 49 * @param array $style_properties { 50 * Array containing the properties of the style. 51 * 52 * @type string $name The identifier of the style used to compute a CSS class. 53 * @type string $label A human-readable label for the style. 54 * @type string $inline_style Inline CSS code that registers the CSS class required 55 * for the style. 56 * @type string $style_handle The handle to an already registered style that should be 57 * enqueued in places where block styles are needed. 58 * @type bool $is_default Whether this is the default style for the block type. 59 * } 60 * @return bool True if the block style was registered with success and false otherwise. 61 */ 62 public function register( $block_name, $style_properties ) { 63 64 if ( ! isset( $block_name ) || ! is_string( $block_name ) ) { 65 _doing_it_wrong( 66 __METHOD__, 67 __( 'Block name must be a string.' ), 68 '5.3.0' 69 ); 70 return false; 71 } 72 73 if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) { 74 _doing_it_wrong( 75 __METHOD__, 76 __( 'Block style name must be a string.' ), 77 '5.3.0' 78 ); 79 return false; 80 } 81 82 if ( str_contains( $style_properties['name'], ' ' ) ) { 83 _doing_it_wrong( 84 __METHOD__, 85 __( 'Block style name must not contain any spaces.' ), 86 '5.9.0' 87 ); 88 return false; 89 } 90 91 $block_style_name = $style_properties['name']; 92 93 if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) { 94 $this->registered_block_styles[ $block_name ] = array(); 95 } 96 $this->registered_block_styles[ $block_name ][ $block_style_name ] = $style_properties; 97 98 return true; 99 } 100 101 /** 102 * Unregisters a block style of the given block type. 103 * 104 * @since 5.3.0 105 * 106 * @param string $block_name Block type name including namespace. 107 * @param string $block_style_name Block style name. 108 * @return bool True if the block style was unregistered with success and false otherwise. 109 */ 110 public function unregister( $block_name, $block_style_name ) { 111 if ( ! $this->is_registered( $block_name, $block_style_name ) ) { 112 _doing_it_wrong( 113 __METHOD__, 114 /* translators: 1: Block name, 2: Block style name. */ 115 sprintf( __( 'Block "%1$s" does not contain a style named "%2$s".' ), $block_name, $block_style_name ), 116 '5.3.0' 117 ); 118 return false; 119 } 120 121 unset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); 122 123 return true; 124 } 125 126 /** 127 * Retrieves the properties of a registered block style for the given block type. 128 * 129 * @since 5.3.0 130 * 131 * @param string $block_name Block type name including namespace. 132 * @param string $block_style_name Block style name. 133 * @return array Registered block style properties. 134 */ 135 public function get_registered( $block_name, $block_style_name ) { 136 if ( ! $this->is_registered( $block_name, $block_style_name ) ) { 137 return null; 138 } 139 140 return $this->registered_block_styles[ $block_name ][ $block_style_name ]; 141 } 142 143 /** 144 * Retrieves all registered block styles. 145 * 146 * @since 5.3.0 147 * 148 * @return array[] Array of arrays containing the registered block styles properties grouped by block type. 149 */ 150 public function get_all_registered() { 151 return $this->registered_block_styles; 152 } 153 154 /** 155 * Retrieves registered block styles for a specific block type. 156 * 157 * @since 5.3.0 158 * 159 * @param string $block_name Block type name including namespace. 160 * @return array[] Array whose keys are block style names and whose values are block style properties. 161 */ 162 public function get_registered_styles_for_block( $block_name ) { 163 if ( isset( $this->registered_block_styles[ $block_name ] ) ) { 164 return $this->registered_block_styles[ $block_name ]; 165 } 166 return array(); 167 } 168 169 /** 170 * Checks if a block style is registered for the given block type. 171 * 172 * @since 5.3.0 173 * 174 * @param string $block_name Block type name including namespace. 175 * @param string $block_style_name Block style name. 176 * @return bool True if the block style is registered, false otherwise. 177 */ 178 public function is_registered( $block_name, $block_style_name ) { 179 return isset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); 180 } 181 182 /** 183 * Utility method to retrieve the main instance of the class. 184 * 185 * The instance will be created if it does not exist yet. 186 * 187 * @since 5.3.0 188 * 189 * @return WP_Block_Styles_Registry The main instance. 190 */ 191 public static function get_instance() { 192 if ( null === self::$instance ) { 193 self::$instance = new self(); 194 } 195 196 return self::$instance; 197 } 198 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Apr 18 08:20:02 2024 | Cross-referenced by PHPXref |