wp/wp-includes/style-engine/class-wp-style-engine-css-rules-store.php
changeset 21 48c4eec2b7e6
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
       
     1 <?php
       
     2 /**
       
     3  * Style Engine: WP_Style_Engine_CSS_Rules_Store class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage StyleEngine
       
     7  * @since 6.1.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class used as a store for WP_Style_Engine_CSS_Rule objects.
       
    12  *
       
    13  * Holds, sanitizes, processes, and prints CSS declarations for the style engine.
       
    14  *
       
    15  * @since 6.1.0
       
    16  */
       
    17 #[AllowDynamicProperties]
       
    18 class WP_Style_Engine_CSS_Rules_Store {
       
    19 
       
    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 	/**
       
    31 	 * The store name.
       
    32 	 *
       
    33 	 * @since 6.1.0
       
    34 	 * @var string
       
    35 	 */
       
    36 	protected $name = '';
       
    37 
       
    38 	/**
       
    39 	 * An array of CSS Rules objects assigned to the store.
       
    40 	 *
       
    41 	 * @since 6.1.0
       
    42 	 * @var WP_Style_Engine_CSS_Rule[]
       
    43 	 */
       
    44 	protected $rules = array();
       
    45 
       
    46 	/**
       
    47 	 * Gets an instance of the store.
       
    48 	 *
       
    49 	 * @since 6.1.0
       
    50 	 *
       
    51 	 * @param string $store_name The name of the store.
       
    52 	 * @return WP_Style_Engine_CSS_Rules_Store|void
       
    53 	 */
       
    54 	public static function get_store( $store_name = 'default' ) {
       
    55 		if ( ! is_string( $store_name ) || empty( $store_name ) ) {
       
    56 			return;
       
    57 		}
       
    58 		if ( ! isset( static::$stores[ $store_name ] ) ) {
       
    59 			static::$stores[ $store_name ] = new static();
       
    60 			// Set the store name.
       
    61 			static::$stores[ $store_name ]->set_name( $store_name );
       
    62 		}
       
    63 		return static::$stores[ $store_name ];
       
    64 	}
       
    65 
       
    66 	/**
       
    67 	 * Gets an array of all available stores.
       
    68 	 *
       
    69 	 * @since 6.1.0
       
    70 	 *
       
    71 	 * @return WP_Style_Engine_CSS_Rules_Store[]
       
    72 	 */
       
    73 	public static function get_stores() {
       
    74 		return static::$stores;
       
    75 	}
       
    76 
       
    77 	/**
       
    78 	 * Clears all stores from static::$stores.
       
    79 	 *
       
    80 	 * @since 6.1.0
       
    81 	 */
       
    82 	public static function remove_all_stores() {
       
    83 		static::$stores = array();
       
    84 	}
       
    85 
       
    86 	/**
       
    87 	 * Sets the store name.
       
    88 	 *
       
    89 	 * @since 6.1.0
       
    90 	 *
       
    91 	 * @param string $name The store name.
       
    92 	 */
       
    93 	public function set_name( $name ) {
       
    94 		$this->name = $name;
       
    95 	}
       
    96 
       
    97 	/**
       
    98 	 * Gets the store name.
       
    99 	 *
       
   100 	 * @since 6.1.0
       
   101 	 *
       
   102 	 * @return string
       
   103 	 */
       
   104 	public function get_name() {
       
   105 		return $this->name;
       
   106 	}
       
   107 
       
   108 	/**
       
   109 	 * Gets an array of all rules.
       
   110 	 *
       
   111 	 * @since 6.1.0
       
   112 	 *
       
   113 	 * @return WP_Style_Engine_CSS_Rule[]
       
   114 	 */
       
   115 	public function get_all_rules() {
       
   116 		return $this->rules;
       
   117 	}
       
   118 
       
   119 	/**
       
   120 	 * Gets a WP_Style_Engine_CSS_Rule object by its selector.
       
   121 	 * If the rule does not exist, it will be created.
       
   122 	 *
       
   123 	 * @since 6.1.0
       
   124 	 * @since 6.6.0 Added the $rules_group parameter.
       
   125 	 *
       
   126 	 * @param string $selector The CSS selector.
       
   127 	 * @param string $rules_group A parent CSS selector in the case of nested CSS, or a CSS nested @rule,
       
   128 	 *                            such as `@media (min-width: 80rem)` or `@layer module`.
       
   129 	 * @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object,
       
   130 	 *                                       or void if the selector is empty.
       
   131 	 */
       
   132 	public function add_rule( $selector, $rules_group = '' ) {
       
   133 		$selector    = $selector ? trim( $selector ) : '';
       
   134 		$rules_group = $rules_group ? trim( $rules_group ) : '';
       
   135 
       
   136 		// Bail early if there is no selector.
       
   137 		if ( empty( $selector ) ) {
       
   138 			return;
       
   139 		}
       
   140 
       
   141 		if ( ! empty( $rules_group ) ) {
       
   142 			if ( empty( $this->rules[ "$rules_group $selector" ] ) ) {
       
   143 				$this->rules[ "$rules_group $selector" ] = new WP_Style_Engine_CSS_Rule( $selector, array(), $rules_group );
       
   144 			}
       
   145 			return $this->rules[ "$rules_group $selector" ];
       
   146 		}
       
   147 
       
   148 		// Create the rule if it doesn't exist.
       
   149 		if ( empty( $this->rules[ $selector ] ) ) {
       
   150 			$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
       
   151 		}
       
   152 
       
   153 		return $this->rules[ $selector ];
       
   154 	}
       
   155 
       
   156 	/**
       
   157 	 * Removes a selector from the store.
       
   158 	 *
       
   159 	 * @since 6.1.0
       
   160 	 *
       
   161 	 * @param string $selector The CSS selector.
       
   162 	 */
       
   163 	public function remove_rule( $selector ) {
       
   164 		unset( $this->rules[ $selector ] );
       
   165 	}
       
   166 }