| 1 |
<?php |
| 2 |
/** |
| 3 |
* WP_Style_Engine_CSS_Declarations_Gutenberg |
| 4 |
* |
| 5 |
* Holds, sanitizes and prints CSS rules declarations |
| 6 |
* |
| 7 |
* @package gutenberg |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations_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_Declarations_Gutenberg { |
| 18 |
/** |
| 19 |
* An array of CSS declarations (property => value pairs). |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
protected $declarations = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Constructor for this object. |
| 27 |
* |
| 28 |
* If a `$declarations` array is passed, it will be used to populate |
| 29 |
* the initial $declarations prop of the object by calling add_declarations(). |
| 30 |
* |
| 31 |
* @param string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). |
| 32 |
*/ |
| 33 |
public function __construct( $declarations = array() ) { |
| 34 |
$this->add_declarations( $declarations ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Adds a single declaration. |
| 39 |
* |
| 40 |
* @param string $property The CSS property. |
| 41 |
* @param string $value The CSS value. |
| 42 |
* |
| 43 |
* @return WP_Style_Engine_CSS_Declarations_Gutenberg Returns the object to allow chaining methods. |
| 44 |
*/ |
| 45 |
public function add_declaration( $property, $value ) { |
| 46 |
// Sanitizes the property. |
| 47 |
$property = $this->sanitize_property( $property ); |
| 48 |
// Bails early if the property is empty. |
| 49 |
if ( empty( $property ) ) { |
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
// Bail early if value is not a string. Prevents fatal errors from malformed block markup. |
| 54 |
if ( ! is_string( $value ) ) { |
| 55 |
return $this; |
| 56 |
} |
| 57 |
$value = trim( $value ); |
| 58 |
if ( '' === $value ) { |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
// Adds the declaration property/value pair. |
| 63 |
$this->declarations[ $property ] = $value; |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Removes a single declaration. |
| 70 |
* |
| 71 |
* @param string $property The CSS property. |
| 72 |
* |
| 73 |
* @return WP_Style_Engine_CSS_Declarations_Gutenberg Returns the object to allow chaining methods. |
| 74 |
*/ |
| 75 |
public function remove_declaration( $property ) { |
| 76 |
unset( $this->declarations[ $property ] ); |
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Adds multiple declarations. |
| 82 |
* |
| 83 |
* @param array $declarations An array of declarations. |
| 84 |
* |
| 85 |
* @return WP_Style_Engine_CSS_Declarations_Gutenberg Returns the object to allow chaining methods. |
| 86 |
*/ |
| 87 |
public function add_declarations( $declarations ) { |
| 88 |
foreach ( $declarations as $property => $value ) { |
| 89 |
$this->add_declaration( $property, $value ); |
| 90 |
} |
| 91 |
return $this; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Removes multiple declarations. |
| 96 |
* |
| 97 |
* @param array $properties An array of properties. |
| 98 |
* |
| 99 |
* @return WP_Style_Engine_CSS_Declarations_Gutenberg Returns the object to allow chaining methods. |
| 100 |
*/ |
| 101 |
public function remove_declarations( $properties = array() ) { |
| 102 |
foreach ( $properties as $property ) { |
| 103 |
$this->remove_declaration( $property ); |
| 104 |
} |
| 105 |
return $this; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets the declarations array. |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_declarations() { |
| 114 |
return $this->declarations; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Filters a CSS property + value pair. |
| 119 |
* |
| 120 |
* @param string $property The CSS property. |
| 121 |
* @param string $value The value to be filtered. |
| 122 |
* @param string $spacer The spacer between the colon and the value. Defaults to an empty string. |
| 123 |
* |
| 124 |
* @return string The filtered declaration or an empty string. |
| 125 |
*/ |
| 126 |
protected static function filter_declaration( $property, $value, $spacer = '' ) { |
| 127 |
$filtered_value = wp_strip_all_tags( $value, true ); |
| 128 |
|
| 129 |
if ( '' !== $filtered_value ) { |
| 130 |
return safecss_filter_attr( "{$property}:{$spacer}{$filtered_value}" ); |
| 131 |
} |
| 132 |
return ''; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Filters and compiles the CSS declarations. |
| 137 |
* |
| 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`. |
| 140 |
* |
| 141 |
* @return string The CSS declarations. |
| 142 |
*/ |
| 143 |
public function get_declarations_string( $should_prettify = false, $indent_count = 0 ) { |
| 144 |
$declarations_array = $this->get_declarations(); |
| 145 |
$declarations_output = ''; |
| 146 |
$indent = $should_prettify ? str_repeat( "\t", $indent_count ) : ''; |
| 147 |
$suffix = $should_prettify ? ' ' : ''; |
| 148 |
$suffix = $should_prettify && $indent_count > 0 ? "\n" : $suffix; |
| 149 |
$spacer = $should_prettify ? ' ' : ''; |
| 150 |
|
| 151 |
foreach ( $declarations_array as $property => $value ) { |
| 152 |
$filtered_declaration = static::filter_declaration( $property, $value, $spacer ); |
| 153 |
if ( $filtered_declaration ) { |
| 154 |
$declarations_output .= "{$indent}{$filtered_declaration};$suffix"; |
| 155 |
} |
| 156 |
} |
| 157 |
return rtrim( $declarations_output ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Sanitizes property names. |
| 162 |
* |
| 163 |
* @param string $property The CSS property. |
| 164 |
* |
| 165 |
* @return string The sanitized property name. |
| 166 |
*/ |
| 167 |
protected function sanitize_property( $property ) { |
| 168 |
return sanitize_key( $property ); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|