| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP_Style_Engine_CSS_Rule_Gutenberg |
| 4 |
* |
| 5 |
* An object for CSS rules. |
| 6 |
* |
| 7 |
* @package gutenberg |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! class_exists( 'WP_Style_Engine_CSS_Rule_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_Rule_Gutenberg { |
| 18 |
|
| 19 |
/** |
| 20 |
* The selector. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $selector; |
| 25 |
|
| 26 |
/** |
| 27 |
* The selector declarations. |
| 28 |
* |
| 29 |
* Contains a WP_Style_Engine_CSS_Declarations_Gutenberg object. |
| 30 |
* |
| 31 |
* @var WP_Style_Engine_CSS_Declarations_Gutenberg |
| 32 |
*/ |
| 33 |
protected $declarations; |
| 34 |
|
| 35 |
/** |
| 36 |
* A parent CSS selector in the case of nested CSS, or a CSS nested @rule, such as `@media (min-width: 80rem)` or `@layer module`.. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $rules_group; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
* |
| 45 |
* @param string $selector The CSS selector. |
| 46 |
* @param string[]|WP_Style_Engine_CSS_Declarations_Gutenberg $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ), |
| 47 |
* or a WP_Style_Engine_CSS_Declarations_Gutenberg object. |
| 48 |
* @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`. |
| 49 |
* |
| 50 |
*/ |
| 51 |
public function __construct( $selector = '', $declarations = array(), $rules_group = '' ) { |
| 52 |
$this->set_selector( $selector ); |
| 53 |
$this->add_declarations( $declarations ); |
| 54 |
$this->set_rules_group( $rules_group ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Sets the selector. |
| 59 |
* |
| 60 |
* @param string $selector The CSS selector. |
| 61 |
* |
| 62 |
* @return WP_Style_Engine_CSS_Rule_Gutenberg Returns the object to allow chaining of methods. |
| 63 |
*/ |
| 64 |
public function set_selector( $selector ) { |
| 65 |
$this->selector = $selector; |
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Sets the declarations. |
| 71 |
* |
| 72 |
* @param array|WP_Style_Engine_CSS_Declarations_Gutenberg $declarations An array of declarations (property => value pairs), |
| 73 |
* or a WP_Style_Engine_CSS_Declarations_Gutenberg object. |
| 74 |
* |
| 75 |
* @return WP_Style_Engine_CSS_Rule_Gutenberg Returns the object to allow chaining of methods. |
| 76 |
*/ |
| 77 |
public function add_declarations( $declarations ) { |
| 78 |
$is_declarations_object = ! is_array( $declarations ); |
| 79 |
$declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations; |
| 80 |
$declaration_options = $is_declarations_object ? $declarations->get_declaration_options() : array(); |
| 81 |
|
| 82 |
if ( null === $this->declarations ) { |
| 83 |
if ( $is_declarations_object ) { |
| 84 |
$this->declarations = $declarations; |
| 85 |
return $this; |
| 86 |
} |
| 87 |
$this->declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg(); |
| 88 |
} |
| 89 |
|
| 90 |
foreach ( $declarations_array as $property => $value ) { |
| 91 |
$this->declarations->add_declaration( |
| 92 |
$property, |
| 93 |
$value, |
| 94 |
$declaration_options[ $property ] ?? array() |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Sets the rules group. |
| 103 |
* |
| 104 |
* @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`. |
| 105 |
* |
| 106 |
* @return WP_Style_Engine_CSS_Rule_Gutenberg Returns the object to allow chaining of methods. |
| 107 |
*/ |
| 108 |
public function set_rules_group( $rules_group ) { |
| 109 |
$this->rules_group = $rules_group; |
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Gets the rules group. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function get_rules_group() { |
| 119 |
return $this->rules_group; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Gets the declarations object. |
| 124 |
* |
| 125 |
* @return WP_Style_Engine_CSS_Declarations_Gutenberg The declarations object. |
| 126 |
*/ |
| 127 |
public function get_declarations() { |
| 128 |
return $this->declarations; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Gets the full selector. |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function get_selector() { |
| 137 |
return $this->selector; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Gets the CSS. |
| 142 |
* |
| 143 |
* @param bool $should_prettify Whether to add spacing, new lines and indents. |
| 144 |
* @param int $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`. |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public function get_css( $should_prettify = false, $indent_count = 0 ) { |
| 149 |
$rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : ''; |
| 150 |
$nested_rule_indent = $should_prettify ? str_repeat( "\t", $indent_count + 1 ) : ''; |
| 151 |
$declarations_indent = $should_prettify ? $indent_count + 1 : 0; |
| 152 |
$nested_declarations_indent = $should_prettify ? $indent_count + 2 : 0; |
| 153 |
$suffix = $should_prettify ? "\n" : ''; |
| 154 |
$spacer = $should_prettify ? ' ' : ''; |
| 155 |
// Trims any multiple selectors strings. |
| 156 |
$selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector(); |
| 157 |
$selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector; |
| 158 |
$rules_group = $this->get_rules_group(); |
| 159 |
$has_rules_group = ! empty( $rules_group ); |
| 160 |
$css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_rules_group ? $nested_declarations_indent : $declarations_indent ); |
| 161 |
|
| 162 |
if ( empty( $css_declarations ) ) { |
| 163 |
return ''; |
| 164 |
} |
| 165 |
|
| 166 |
if ( $has_rules_group ) { |
| 167 |
$selector = "{$rule_indent}{$rules_group}{$spacer}{{$suffix}{$nested_rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$nested_rule_indent}}{$suffix}{$rule_indent}}"; |
| 168 |
return $selector; |
| 169 |
} |
| 170 |
|
| 171 |
return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}"; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|