WordPress/wp-includes/style-engine/class-wp-style-engine-css-rules-store.php

165 lines
3.1 KiB
PHP
Raw Normal View History

<?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
*/
Code Modernization: Add `AllowDynamicProperties` attribute to recently introduced classes. This commit is a follow-up to [54133] for new classes introduced in WordPress 6.1 since the previous commit. Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0. There are a number of ways to mitigate this: * If it is an accidental typo for a declared property: fix the typo. * For known properties: declare them on the class. * For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in. * For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes. Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1. To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, the `#[AllowDynamicProperties]` attribute has been added to all “parent” classes in WP. Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties]. Follow-up to [53922], [54133]. Props jrf. See #56513, #56034. Built from https://develop.svn.wordpress.org/trunk@54481 git-svn-id: http://core.svn.wordpress.org/trunk@54040 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-10-11 17:00:12 +02:00
#[AllowDynamicProperties]
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 ] );
}
}