PluginProbe
Gutenberg / 23.5.0
Gutenberg v23.5.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / style-engine / class-wp-style-engine-css-rule-gutenberg.php

class-wp-style-engine-css-rule-gutenberg.php in Gutenberg 23.5.0, at build/scripts/style-engine/class-wp-style-engine-css-rule-gutenberg.php

167 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
81 if ( null === $this->declarations ) {
82 if ( $is_declarations_object ) {
83 $this->declarations = $declarations;
84 return $this;
85 }
86 $this->declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $declarations_array );
87 }
88 $this->declarations->add_declarations( $declarations_array );
89
90 return $this;
91 }
92
93 /**
94 * Sets the rules group.
95 *
96 * @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`.
97 *
98 * @return WP_Style_Engine_CSS_Rule_Gutenberg Returns the object to allow chaining of methods.
99 */
100 public function set_rules_group( $rules_group ) {
101 $this->rules_group = $rules_group;
102 return $this;
103 }
104
105 /**
106 * Gets the rules group.
107 *
108 * @return string
109 */
110 public function get_rules_group() {
111 return $this->rules_group;
112 }
113
114 /**
115 * Gets the declarations object.
116 *
117 * @return WP_Style_Engine_CSS_Declarations_Gutenberg The declarations object.
118 */
119 public function get_declarations() {
120 return $this->declarations;
121 }
122
123 /**
124 * Gets the full selector.
125 *
126 * @return string
127 */
128 public function get_selector() {
129 return $this->selector;
130 }
131
132 /**
133 * Gets the CSS.
134 *
135 * @param bool $should_prettify Whether to add spacing, new lines and indents.
136 * @param int $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
137 *
138 * @return string
139 */
140 public function get_css( $should_prettify = false, $indent_count = 0 ) {
141 $rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
142 $nested_rule_indent = $should_prettify ? str_repeat( "\t", $indent_count + 1 ) : '';
143 $declarations_indent = $should_prettify ? $indent_count + 1 : 0;
144 $nested_declarations_indent = $should_prettify ? $indent_count + 2 : 0;
145 $suffix = $should_prettify ? "\n" : '';
146 $spacer = $should_prettify ? ' ' : '';
147 // Trims any multiple selectors strings.
148 $selector = $should_prettify ? implode( ',', array_map( 'trim', explode( ',', $this->get_selector() ) ) ) : $this->get_selector();
149 $selector = $should_prettify ? str_replace( array( ',' ), ",\n", $selector ) : $selector;
150 $rules_group = $this->get_rules_group();
151 $has_rules_group = ! empty( $rules_group );
152 $css_declarations = $this->declarations->get_declarations_string( $should_prettify, $has_rules_group ? $nested_declarations_indent : $declarations_indent );
153
154 if ( empty( $css_declarations ) ) {
155 return '';
156 }
157
158 if ( $has_rules_group ) {
159 $selector = "{$rule_indent}{$rules_group}{$spacer}{{$suffix}{$nested_rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$nested_rule_indent}}{$suffix}{$rule_indent}}";
160 return $selector;
161 }
162
163 return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
164 }
165 }
166 }
167