PluginProbe
Gutenberg / trunk
Gutenberg vtrunk
24.1.0 24.0.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 All 404 releases
← All changes | build/scripts/style-engine/class-wp-style-engine-css-declarations-gutenberg.php +66 -6 23.0.1 → trunk View file →
@@ -22,8 +22,15 @@
22 22 */
23 23 protected $declarations = array();
24 24
25 25 /**
26 + * CSS declaration options keyed by property name.
27 + *
28 + * @var array
29 + */
30 + protected $declaration_options = array();
31 +
32 + /**
26 33 * Constructor for this object.
27 34 *
28 35 * If a `$declarations` array is passed, it will be used to populate
29 36 * the initial $declarations prop of the object by calling add_declarations().
@@ -38,12 +45,17 @@
38 45 * Adds a single declaration.
39 46 *
40 47 * @param string $property The CSS property.
41 48 * @param string $value The CSS value.
49 + * @param array $options {
50 + * Optional. An array of options. Default empty array.
42 51 *
52 + * @type bool $important Whether to output the declaration with !important. Default false.
53 + * }
54 + *
43 55 * @return WP_Style_Engine_CSS_Declarations_Gutenberg Returns the object to allow chaining methods.
44 56 */
45 - public function add_declaration( $property, $value ) {
57 + public function add_declaration( $property, $value, $options = array() ) {
46 58 // Sanitizes the property.
47 59 $property = $this->sanitize_property( $property );
48 60 // Bails early if the property is empty.
49 61 if ( empty( $property ) ) {
@@ -58,10 +70,24 @@
58 70 if ( '' === $value ) {
59 71 return $this;
60 72 }
61 73
74 + $options = wp_parse_args(
75 + $options,
76 + array(
77 + 'important' => false,
78 + )
79 + );
80 +
81 + $options = array_filter( $options );
82 +
62 83 // Adds the declaration property/value pair.
63 84 $this->declarations[ $property ] = $value;
85 + if ( $options ) {
86 + $this->declaration_options[ $property ] = $options;
87 + } else {
88 + unset( $this->declaration_options[ $property ] );
89 + }
64 90
65 91 return $this;
66 92 }
67 93
@@ -73,8 +99,9 @@
73 99 * @return WP_Style_Engine_CSS_Declarations_Gutenberg Returns the object to allow chaining methods.
74 100 */
75 101 public function remove_declaration( $property ) {
76 102 unset( $this->declarations[ $property ] );
103 + unset( $this->declaration_options[ $property ] );
77 104 return $this;
78 105 }
79 106
80 107 /**
@@ -114,21 +141,49 @@
114 141 return $this->declarations;
115 142 }
116 143
117 144 /**
145 + * Gets declaration options keyed by property name.
146 + *
147 + * @return array
148 + */
149 + public function get_declaration_options() {
150 + return $this->declaration_options;
151 + }
152 +
153 + /**
118 154 * Filters a CSS property + value pair.
119 155 *
120 156 * @param string $property The CSS property.
121 157 * @param string $value The value to be filtered.
122 158 * @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
159 + * @param array $options {
160 + * Optional. An array of options. Default empty array.
123 161 *
162 + * @type bool $important Whether to output the declaration with !important. Default false.
163 + * }
164 + *
124 165 * @return string The filtered declaration or an empty string.
125 166 */
126 - protected static function filter_declaration( $property, $value, $spacer = '' ) {
167 + protected static function filter_declaration( $property, $value, $spacer = '', $options = array() ) {
127 168 $filtered_value = wp_strip_all_tags( $value, true );
128 169
129 170 if ( '' !== $filtered_value ) {
130 - return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
171 + $options = wp_parse_args(
172 + $options,
173 + array(
174 + 'important' => false,
175 + )
176 + );
177 +
178 + $filtered_declaration = safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" );
179 +
180 + // Only append !important in the presence of an option value and when sanitization returns a single declaration.
181 + if ( true === $options['important'] && '' !== $filtered_declaration && ! str_contains( $filtered_declaration, ';' ) ) {
182 + return "$filtered_declaration !important";
183 + }
184 +
185 + return $filtered_declaration;
131 186 }
132 187 return '';
133 188 }
134 189
@@ -134,10 +189,10 @@
134 189
135 190 /**
136 191 * Filters and compiles the CSS declarations.
137 192 *
138 - * @param bool $should_prettify Whether to add spacing, new lines and indents.
139 - * @param number $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
193 + * @param bool $should_prettify Whether to add spacing, new lines and indents.
194 + * @param int $indent_count The number of tab indents to apply to the rule. Applies if `prettify` is `true`.
140 195 *
141 196 * @return string The CSS declarations.
142 197 */
143 198 public function get_declarations_string( $should_prettify = false, $indent_count = 0 ) {
@@ -148,9 +203,14 @@
148 203 $suffix = $should_prettify && $indent_count > 0 ? "\n" : $suffix;
149 204 $spacer = $should_prettify ? ' ' : '';
150 205
151 206 foreach ( $declarations_array as $property => $value ) {
152 - $filtered_declaration = static::filter_declaration( $property, $value, $spacer );
207 + $filtered_declaration = static::filter_declaration(
208 + $property,
209 + $value,
210 + $spacer,
211 + $this->declaration_options[ $property ] ?? array()
212 + );
153 213 if ( $filtered_declaration ) {
154 214 $declarations_output .= "{$indent}{$filtered_declaration};$suffix";
155 215 }
156 216 }