No Description

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

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