class-wp-block-styles-registry.php 5.7 KB

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