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

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