[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-block-styles-registry.php (source)

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


Generated : Sat Sep 12 08:20:32 2026 Cross-referenced by PHPXref