@@ -26,6 +26,15 @@ class WP_Style_Engine_CSS_Declarations {
2626 */
2727 protected $ declarations = array ();
2828
29+ /**
30+ * CSS declaration options keyed by property name.
31+ *
32+ * @since 7.1.0
33+ *
34+ * @var array
35+ */
36+ protected $ declaration_options = array ();
37+
2938 /**
3039 * Constructor for this object.
3140 *
@@ -46,12 +55,18 @@ public function __construct( $declarations = array() ) {
4655 * Adds a single declaration.
4756 *
4857 * @since 6.1.0
58+ * @since 7.1.0 Added the `$options` parameter.
4959 *
5060 * @param string $property The CSS property.
5161 * @param string $value The CSS value.
62+ * @param array $options {
63+ * Optional. An array of options. Default empty array.
64+ *
65+ * @type bool $important Whether to output the declaration with !important. Default false.
66+ * }
5267 * @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
5368 */
54- public function add_declaration ( $ property , $ value ) {
69+ public function add_declaration ( $ property , $ value, $ options = array () ) {
5570 // Sanitizes the property.
5671 $ property = $ this ->sanitize_property ( $ property );
5772 // Bails early if the property is empty.
@@ -70,8 +85,21 @@ public function add_declaration( $property, $value ) {
7085 return $ this ;
7186 }
7287
88+ $ options = wp_parse_args (
89+ $ options ,
90+ array (
91+ 'important ' => false ,
92+ )
93+ );
94+ $ options = array_filter ( $ options );
95+
7396 // Adds the declaration property/value pair.
7497 $ this ->declarations [ $ property ] = $ value ;
98+ if ( $ options ) {
99+ $ this ->declaration_options [ $ property ] = $ options ;
100+ } else {
101+ unset( $ this ->declaration_options [ $ property ] );
102+ }
75103
76104 return $ this ;
77105 }
@@ -86,6 +114,7 @@ public function add_declaration( $property, $value ) {
86114 */
87115 public function remove_declaration ( $ property ) {
88116 unset( $ this ->declarations [ $ property ] );
117+ unset( $ this ->declaration_options [ $ property ] );
89118 return $ this ;
90119 }
91120
@@ -130,21 +159,52 @@ public function get_declarations() {
130159 return $ this ->declarations ;
131160 }
132161
162+ /**
163+ * Gets declaration options keyed by property name.
164+ *
165+ * @since 7.1.0
166+ *
167+ * @return array Declaration options keyed by property name.
168+ */
169+ public function get_declaration_options () {
170+ return $ this ->declaration_options ;
171+ }
172+
133173 /**
134174 * Filters a CSS property + value pair.
135175 *
136176 * @since 6.1.0
177+ * @since 7.1.0 Added the `$options` parameter.
137178 *
138179 * @param string $property The CSS property.
139180 * @param string $value The value to be filtered.
140181 * @param string $spacer Optional. The spacer between the colon and the value.
141182 * Default empty string.
183+ * @param array $options {
184+ * Optional. An array of options. Default empty array.
185+ *
186+ * @type bool $important Whether to output the declaration with !important. Default false.
187+ * }
142188 * @return string The filtered declaration or an empty string.
143189 */
144- protected static function filter_declaration ( $ property , $ value , $ spacer = '' ) {
190+ protected static function filter_declaration ( $ property , $ value , $ spacer = '' , $ options = array () ) {
145191 $ filtered_value = wp_strip_all_tags ( $ value , true );
146192 if ( '' !== $ filtered_value ) {
147- return safecss_filter_attr ( "{$ property }: {$ spacer }{$ filtered_value }" );
193+ $ options = wp_parse_args (
194+ $ options ,
195+ array (
196+ 'important ' => false ,
197+ )
198+ );
199+
200+ $ filtered_declaration = safecss_filter_attr ( "{$ property }: {$ spacer }{$ filtered_value }" );
201+
202+ // Only append !important in the presence of an option value and when sanitization returns a single declaration.
203+ if ( true === $ options ['important ' ] && '' !== $ filtered_declaration && ! str_contains ( $ filtered_declaration , '; ' ) ) {
204+ return "$ filtered_declaration !important " ;
205+ }
206+
207+ return $ filtered_declaration ;
148208 }
149209 return '' ;
150210 }
@@ -169,7 +229,7 @@ public function get_declarations_string( $should_prettify = false, $indent_count
169229 $ spacer = $ should_prettify ? ' ' : '' ;
170230
171231 foreach ( $ declarations_array as $ property => $ value ) {
172- $ filtered_declaration = static ::filter_declaration ( $ property , $ value , $ spacer );
232+ $ filtered_declaration = static ::filter_declaration ( $ property , $ value , $ spacer, $ this -> declaration_options [ $ property ] ?? array () );
173233 if ( $ filtered_declaration ) {
174234 $ declarations_output .= "{$ indent }{$ filtered_declaration }; $ suffix " ;
175235 }
0 commit comments