WP_Style_Engine_CSS_Rules_Store
云策文档标注
概述
WP_Style_Engine_CSS_Rules_Store 是 WordPress 样式引擎的核心类,用于存储和管理 WP_Style_Engine_CSS_Rule 对象。它提供方法来添加、获取、移除规则,并支持多存储实例。
关键要点
- 作为 WP_Style_Engine_CSS_Rule 对象的存储容器,负责持有、清理、处理和打印 CSS 声明。
- 使用静态属性 $stores 管理多个命名存储实例,支持通过 get_store() 获取或创建实例。
- 提供 add_rule() 方法根据选择器获取或创建规则,支持嵌套 CSS 规则组(如 @media 或 @layer)。
- 包含 get_all_rules()、remove_rule()、set_name()、get_name() 等方法用于规则和存储管理。
- 静态方法 get_stores() 和 remove_all_stores() 用于全局存储操作。
代码示例
// 获取或创建名为 'default' 的存储实例
$store = WP_Style_Engine_CSS_Rules_Store::get_store('default');
// 添加一个 CSS 规则,选择器为 '.my-class'
$rule = $store->add_rule('.my-class');
// 获取所有规则
$all_rules = $store->get_all_rules();
// 移除一个规则
$store->remove_rule('.my-class');注意事项
- add_rule() 方法在 WordPress 6.6.0 版本中新增了 $rules_group 参数,用于处理嵌套 CSS 规则。
- 如果选择器为空,add_rule() 会返回 void,需确保传入有效的选择器字符串。
- 存储名称通过 set_name() 设置,应在创建实例后调用以正确标识存储。
原文内容
Core class used as a store for WP_Style_Engine_CSS_Rule objects.
Description
Holds, sanitizes, processes, and prints CSS declarations for the style engine.
Methods
| Name | Description |
|---|---|
| WP_Style_Engine_CSS_Rules_Store::add_rule | Gets a WP_Style_Engine_CSS_Rule object by its selector. |
| WP_Style_Engine_CSS_Rules_Store::get_all_rules | Gets an array of all rules. |
| WP_Style_Engine_CSS_Rules_Store::get_name | Gets the store name. |
| WP_Style_Engine_CSS_Rules_Store::get_store | Gets an instance of the store. |
| WP_Style_Engine_CSS_Rules_Store::get_stores | Gets an array of all available stores. |
| WP_Style_Engine_CSS_Rules_Store::remove_all_stores | Clears all stores from static::$stores. |
| WP_Style_Engine_CSS_Rules_Store::remove_rule | Removes a selector from the store. |
| WP_Style_Engine_CSS_Rules_Store::set_name | Sets the store name. |
Source
class WP_Style_Engine_CSS_Rules_Store {
/**
* An array of named WP_Style_Engine_CSS_Rules_Store objects.
*
* @static
*
* @since 6.1.0
* @var WP_Style_Engine_CSS_Rules_Store[]
*/
protected static $stores = array();
/**
* The store name.
*
* @since 6.1.0
* @var string
*/
protected $name = '';
/**
* An array of CSS Rules objects assigned to the store.
*
* @since 6.1.0
* @var WP_Style_Engine_CSS_Rule[]
*/
protected $rules = array();
/**
* Gets an instance of the store.
*
* @since 6.1.0
*
* @param string $store_name The name of the store.
* @return WP_Style_Engine_CSS_Rules_Store|void
*/
public static function get_store( $store_name = 'default' ) {
if ( ! is_string( $store_name ) || empty( $store_name ) ) {
return;
}
if ( ! isset( static::$stores[ $store_name ] ) ) {
static::$stores[ $store_name ] = new static();
// Set the store name.
static::$stores[ $store_name ]->set_name( $store_name );
}
return static::$stores[ $store_name ];
}
/**
* Gets an array of all available stores.
*
* @since 6.1.0
*
* @return WP_Style_Engine_CSS_Rules_Store[]
*/
public static function get_stores() {
return static::$stores;
}
/**
* Clears all stores from static::$stores.
*
* @since 6.1.0
*/
public static function remove_all_stores() {
static::$stores = array();
}
/**
* Sets the store name.
*
* @since 6.1.0
*
* @param string $name The store name.
*/
public function set_name( $name ) {
$this->name = $name;
}
/**
* Gets the store name.
*
* @since 6.1.0
*
* @return string
*/
public function get_name() {
return $this->name;
}
/**
* Gets an array of all rules.
*
* @since 6.1.0
*
* @return WP_Style_Engine_CSS_Rule[]
*/
public function get_all_rules() {
return $this->rules;
}
/**
* Gets a WP_Style_Engine_CSS_Rule object by its selector.
* If the rule does not exist, it will be created.
*
* @since 6.1.0
* @since 6.6.0 Added the $rules_group parameter.
*
* @param string $selector The CSS selector.
* @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`.
* @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object,
* or void if the selector is empty.
*/
public function add_rule( $selector, $rules_group = '' ) {
$selector = $selector ? trim( $selector ) : '';
$rules_group = $rules_group ? trim( $rules_group ) : '';
// Bail early if there is no selector.
if ( empty( $selector ) ) {
return;
}
if ( ! empty( $rules_group ) ) {
if ( empty( $this->rules[ "$rules_group $selector" ] ) ) {
$this->rules[ "$rules_group $selector" ] = new WP_Style_Engine_CSS_Rule( $selector, array(), $rules_group );
}
return $this->rules[ "$rules_group $selector" ];
}
// Create the rule if it doesn't exist.
if ( empty( $this->rules[ $selector ] ) ) {
$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
}
return $this->rules[ $selector ];
}
/**
* Removes a selector from the store.
*
* @since 6.1.0
*
* @param string $selector The CSS selector.
*/
public function remove_rule( $selector ) {
unset( $this->rules[ $selector ] );
}
}
Changelog
| Version | Description |
|---|---|
| 6.1.0 | Introduced. |