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