class-wp-style-engine-css-rules-store.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * WP_Style_Engine_CSS_Rules_Store
  4. *
  5. * A store for WP_Style_Engine_CSS_Rule objects.
  6. *
  7. * @package WordPress
  8. * @subpackage StyleEngine
  9. * @since 6.1.0
  10. */
  11. /**
  12. * Class WP_Style_Engine_CSS_Rules_Store.
  13. *
  14. * Holds, sanitizes, processes and prints CSS declarations for the style engine.
  15. *
  16. * @since 6.1.0
  17. */
  18. #[AllowDynamicProperties]
  19. class WP_Style_Engine_CSS_Rules_Store {
  20. /**
  21. * An array of named WP_Style_Engine_CSS_Rules_Store objects.
  22. *
  23. * @static
  24. *
  25. * @since 6.1.0
  26. * @var WP_Style_Engine_CSS_Rules_Store[]
  27. */
  28. protected static $stores = array();
  29. /**
  30. * The store name.
  31. *
  32. * @since 6.1.0
  33. * @var string
  34. */
  35. protected $name = '';
  36. /**
  37. * An array of CSS Rules objects assigned to the store.
  38. *
  39. * @since 6.1.0
  40. * @var WP_Style_Engine_CSS_Rule[]
  41. */
  42. protected $rules = array();
  43. /**
  44. * Gets an instance of the store.
  45. *
  46. * @since 6.1.0
  47. *
  48. * @param string $store_name The name of the store.
  49. *
  50. * @return WP_Style_Engine_CSS_Rules_Store|void
  51. */
  52. public static function get_store( $store_name = 'default' ) {
  53. if ( ! is_string( $store_name ) || empty( $store_name ) ) {
  54. return;
  55. }
  56. if ( ! isset( static::$stores[ $store_name ] ) ) {
  57. static::$stores[ $store_name ] = new static();
  58. // Set the store name.
  59. static::$stores[ $store_name ]->set_name( $store_name );
  60. }
  61. return static::$stores[ $store_name ];
  62. }
  63. /**
  64. * Gets an array of all available stores.
  65. *
  66. * @since 6.1.0
  67. *
  68. * @return WP_Style_Engine_CSS_Rules_Store[]
  69. */
  70. public static function get_stores() {
  71. return static::$stores;
  72. }
  73. /**
  74. * Clears all stores from static::$stores.
  75. *
  76. * @since 6.1.0
  77. *
  78. * @return void
  79. */
  80. public static function remove_all_stores() {
  81. static::$stores = array();
  82. }
  83. /**
  84. * Sets the store name.
  85. *
  86. * @since 6.1.0
  87. *
  88. * @param string $name The store name.
  89. *
  90. * @return void
  91. */
  92. public function set_name( $name ) {
  93. $this->name = $name;
  94. }
  95. /**
  96. * Gets the store name.
  97. *
  98. * @since 6.1.0
  99. *
  100. * @return string
  101. */
  102. public function get_name() {
  103. return $this->name;
  104. }
  105. /**
  106. * Gets an array of all rules.
  107. *
  108. * @since 6.1.0
  109. *
  110. * @return WP_Style_Engine_CSS_Rule[]
  111. */
  112. public function get_all_rules() {
  113. return $this->rules;
  114. }
  115. /**
  116. * Gets a WP_Style_Engine_CSS_Rule object by its selector.
  117. * If the rule does not exist, it will be created.
  118. *
  119. * @since 6.1.0
  120. *
  121. * @param string $selector The CSS selector.
  122. *
  123. * @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
  124. */
  125. public function add_rule( $selector ) {
  126. $selector = trim( $selector );
  127. // Bail early if there is no selector.
  128. if ( empty( $selector ) ) {
  129. return;
  130. }
  131. // Create the rule if it doesn't exist.
  132. if ( empty( $this->rules[ $selector ] ) ) {
  133. $this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
  134. }
  135. return $this->rules[ $selector ];
  136. }
  137. /**
  138. * Removes a selector from the store.
  139. *
  140. * @since 6.1.0
  141. *
  142. * @param string $selector The CSS selector.
  143. *
  144. * @return void
  145. */
  146. public function remove_rule( $selector ) {
  147. unset( $this->rules[ $selector ] );
  148. }
  149. }