Нема описа

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

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