WordPress/wp-includes/style-engine/class-wp-style-engine-css-rules-store.php
audrasjb 1e791f041d Editor: Backport Style Engine API functions, classes and tests.
This PR migrates the Style Engine PHP functions, classes and tests into Core for 6.1. It backports the original [WordPress/gutenberg#40260 PR #40260] from Gutenberg repository.

Props ramonopoly, bernhard-reiter, costdev, azaozz, andrewserong, mukesh27, aristath.
See #56467.

Built from https://develop.svn.wordpress.org/trunk@54156


git-svn-id: http://core.svn.wordpress.org/trunk@53715 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-14 12:48:16 +00:00

164 lines
3.0 KiB
PHP

<?php
/**
* WP_Style_Engine_CSS_Rules_Store
*
* A store for WP_Style_Engine_CSS_Rule objects.
*
* @package WordPress
* @subpackage StyleEngine
* @since 6.1.0
*/
/**
* Class WP_Style_Engine_CSS_Rules_Store.
*
* Holds, sanitizes, processes and prints CSS declarations for the style engine.
*
* @since 6.1.0
*/
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
*
* @return void
*/
public static function remove_all_stores() {
static::$stores = array();
}
/**
* Sets the store name.
*
* @since 6.1.0
*
* @param string $name The store name.
*
* @return void
*/
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
*
* @param string $selector The CSS selector.
*
* @return WP_Style_Engine_CSS_Rule|void Returns a WP_Style_Engine_CSS_Rule object, or null if the selector is empty.
*/
public function add_rule( $selector ) {
$selector = trim( $selector );
// Bail early if there is no selector.
if ( empty( $selector ) ) {
return;
}
// 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.
*
* @return void
*/
public function remove_rule( $selector ) {
unset( $this->rules[ $selector ] );
}
}