class-wp-block-styles-registry.php (4715B)
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 final class WP_Block_Styles_Registry { 16 /** 17 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays. 18 * 19 * @since 5.3.0 20 * @var array 21 */ 22 private $registered_block_styles = array(); 23 24 /** 25 * Container for the main instance of the class. 26 * 27 * @since 5.3.0 28 * @var WP_Block_Styles_Registry|null 29 */ 30 private static $instance = null; 31 32 /** 33 * Registers a block style. 34 * 35 * @since 5.3.0 36 * 37 * @param string $block_name Block type name including namespace. 38 * @param array $style_properties Array containing the properties of the style name, label, 39 * is_default, style_handle (name of the stylesheet to be enqueued), 40 * inline_style (string containing the CSS to be added). 41 * @return bool True if the block style was registered with success and false otherwise. 42 */ 43 public function register( $block_name, $style_properties ) { 44 45 if ( ! isset( $block_name ) || ! is_string( $block_name ) ) { 46 _doing_it_wrong( 47 __METHOD__, 48 __( 'Block name must be a string.' ), 49 '5.3.0' 50 ); 51 return false; 52 } 53 54 if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) { 55 _doing_it_wrong( 56 __METHOD__, 57 __( 'Block style name must be a string.' ), 58 '5.3.0' 59 ); 60 return false; 61 } 62 63 $block_style_name = $style_properties['name']; 64 65 if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) { 66 $this->registered_block_styles[ $block_name ] = array(); 67 } 68 $this->registered_block_styles[ $block_name ][ $block_style_name ] = $style_properties; 69 70 return true; 71 } 72 73 /** 74 * Unregisters a block style. 75 * 76 * @param string $block_name Block type name including namespace. 77 * @param string $block_style_name Block style name. 78 * @return bool True if the block style was unregistered with success and false otherwise. 79 */ 80 public function unregister( $block_name, $block_style_name ) { 81 if ( ! $this->is_registered( $block_name, $block_style_name ) ) { 82 _doing_it_wrong( 83 __METHOD__, 84 /* translators: 1: Block name, 2: Block style name. */ 85 sprintf( __( 'Block "%1$s" does not contain a style named "%2$s".' ), $block_name, $block_style_name ), 86 '5.3.0' 87 ); 88 return false; 89 } 90 91 unset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); 92 93 return true; 94 } 95 96 /** 97 * Retrieves an array containing the properties of a registered block style. 98 * 99 * @since 5.3.0 100 * 101 * @param string $block_name Block type name including namespace. 102 * @param string $block_style_name Block style name. 103 * @return array Registered block style properties. 104 */ 105 public function get_registered( $block_name, $block_style_name ) { 106 if ( ! $this->is_registered( $block_name, $block_style_name ) ) { 107 return null; 108 } 109 110 return $this->registered_block_styles[ $block_name ][ $block_style_name ]; 111 } 112 113 /** 114 * Retrieves all registered block styles. 115 * 116 * @since 5.3.0 117 * 118 * @return array Array of arrays containing the registered block styles properties grouped per block, 119 * and per style. 120 */ 121 public function get_all_registered() { 122 return $this->registered_block_styles; 123 } 124 125 /** 126 * Retrieves registered block styles for a specific block. 127 * 128 * @since 5.3.0 129 * 130 * @param string $block_name Block type name including namespace. 131 * @return array Array whose keys are block style names and whose value are block style properties. 132 */ 133 public function get_registered_styles_for_block( $block_name ) { 134 if ( isset( $this->registered_block_styles[ $block_name ] ) ) { 135 return $this->registered_block_styles[ $block_name ]; 136 } 137 return array(); 138 } 139 140 /** 141 * Checks if a block style is registered. 142 * 143 * @since 5.3.0 144 * 145 * @param string $block_name Block type name including namespace. 146 * @param string $block_style_name Block style name. 147 * @return bool True if the block style is registered, false otherwise. 148 */ 149 public function is_registered( $block_name, $block_style_name ) { 150 return isset( $this->registered_block_styles[ $block_name ][ $block_style_name ] ); 151 } 152 153 /** 154 * Utility method to retrieve the main instance of the class. 155 * 156 * The instance will be created if it does not exist yet. 157 * 158 * @since 5.3.0 159 * 160 * @return WP_Block_Styles_Registry The main instance. 161 */ 162 public static function get_instance() { 163 if ( null === self::$instance ) { 164 self::$instance = new self(); 165 } 166 167 return self::$instance; 168 } 169 }