aristath /
kirki
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Handles CSS output for fields. |
||||
| 4 | * |
||||
| 5 | * @package Kirki |
||||
| 6 | * @subpackage Controls |
||||
| 7 | * @copyright Copyright (c) 2017, Aristeides Stathopoulos |
||||
| 8 | * @license https://opensource.org/licenses/MIT |
||||
| 9 | * @since 2.2.0 |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * Handles field CSS output. |
||||
| 14 | */ |
||||
| 15 | class Kirki_Output { |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * The Kirki configuration used in the field. |
||||
| 19 | * |
||||
| 20 | * @access protected |
||||
| 21 | * @var string |
||||
| 22 | */ |
||||
| 23 | protected $config_id = 'global'; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * The field's `output` argument. |
||||
| 27 | * |
||||
| 28 | * @access protected |
||||
| 29 | * @var array |
||||
| 30 | */ |
||||
| 31 | protected $output = array(); |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * An array of the generated styles. |
||||
| 35 | * |
||||
| 36 | * @access protected |
||||
| 37 | * @var array |
||||
| 38 | */ |
||||
| 39 | protected $styles = array(); |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * The field. |
||||
| 43 | * |
||||
| 44 | * @access protected |
||||
| 45 | * @var array |
||||
| 46 | */ |
||||
| 47 | protected $field = array(); |
||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * The value. |
||||
| 51 | * |
||||
| 52 | * @access protected |
||||
| 53 | * @var string|array |
||||
| 54 | */ |
||||
| 55 | protected $value; |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * The class constructor. |
||||
| 59 | * |
||||
| 60 | * @access public |
||||
| 61 | * @param string $config_id The config ID. |
||||
| 62 | * @param array $output The output argument. |
||||
| 63 | * @param string|array $value The value. |
||||
| 64 | * @param array $field The field. |
||||
| 65 | */ |
||||
| 66 | public function __construct( $config_id, $output, $value, $field ) { |
||||
| 67 | $this->config_id = $config_id; |
||||
| 68 | $this->value = $value; |
||||
| 69 | $this->output = $output; |
||||
| 70 | $this->field = $field; |
||||
| 71 | |||||
| 72 | $this->parse_output(); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | /** |
||||
| 76 | * If we have a sanitize_callback defined, apply it to the value. |
||||
| 77 | * |
||||
| 78 | * @param array $output The output args. |
||||
| 79 | * @param string|array $value The value. |
||||
| 80 | * |
||||
| 81 | * @return string|array |
||||
| 82 | */ |
||||
| 83 | protected function apply_sanitize_callback( $output, $value ) { |
||||
| 84 | if ( isset( $output['sanitize_callback'] ) && null !== $output['sanitize_callback'] ) { |
||||
| 85 | |||||
| 86 | // If the sanitize_callback is invalid, return the value. |
||||
| 87 | if ( ! is_callable( $output['sanitize_callback'] ) ) { |
||||
| 88 | return $value; |
||||
| 89 | } |
||||
| 90 | return call_user_func( $output['sanitize_callback'], $this->value ); |
||||
| 91 | } |
||||
| 92 | return $value; |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * If we have a value_pattern defined, apply it to the value. |
||||
| 97 | * |
||||
| 98 | * @param array $output The output args. |
||||
| 99 | * @param string|array $value The value. |
||||
| 100 | * @return string|array |
||||
| 101 | */ |
||||
| 102 | protected function apply_value_pattern( $output, $value ) { |
||||
| 103 | if ( isset( $output['value_pattern'] ) && ! empty( $output['value_pattern'] ) && is_string( $output['value_pattern'] ) ) { |
||||
| 104 | if ( ! is_array( $value ) ) { |
||||
| 105 | $value = str_replace( '$', $value, $output['value_pattern'] ); |
||||
| 106 | } |
||||
| 107 | if ( is_array( $value ) ) { |
||||
| 108 | foreach ( array_keys( $value ) as $value_k ) { |
||||
| 109 | if ( ! is_string( $value[ $value_k ] ) ) { |
||||
| 110 | continue; |
||||
| 111 | } |
||||
| 112 | if ( isset( $output['choice'] ) ) { |
||||
| 113 | if ( $output['choice'] === $value_k ) { |
||||
| 114 | $value[ $output['choice'] ] = str_replace( '$', $value[ $output['choice'] ], $output['value_pattern'] ); |
||||
| 115 | } |
||||
| 116 | continue; |
||||
| 117 | } |
||||
| 118 | $value[ $value_k ] = str_replace( '$', $value[ $value_k ], $output['value_pattern'] ); |
||||
| 119 | } |
||||
| 120 | } |
||||
| 121 | $value = $this->apply_pattern_replace( $output, $value ); |
||||
| 122 | } |
||||
| 123 | return $value; |
||||
| 124 | } |
||||
| 125 | |||||
| 126 | /** |
||||
| 127 | * If we have a value_pattern defined, apply it to the value. |
||||
| 128 | * |
||||
| 129 | * @param array $output The output args. |
||||
| 130 | * @param string|array $value The value. |
||||
| 131 | * @return string|array |
||||
| 132 | */ |
||||
| 133 | protected function apply_pattern_replace( $output, $value ) { |
||||
| 134 | if ( isset( $output['pattern_replace'] ) && is_array( $output['pattern_replace'] ) ) { |
||||
| 135 | $option_type = ( '' !== Kirki::get_config_param( $this->config_id, 'option_type' ) ) ? Kirki::get_config_param( $this->config_id, 'option_type' ) : 'theme_mod'; |
||||
| 136 | $option_name = Kirki::get_config_param( $this->config_id, 'option_name' ); |
||||
| 137 | $options = array(); |
||||
| 138 | if ( $option_name ) { |
||||
| 139 | $options = ( 'site_option' === $option_type ) ? get_site_option( $option_name ) : get_option( $option_name ); |
||||
| 140 | } |
||||
| 141 | foreach ( $output['pattern_replace'] as $search => $replace ) { |
||||
| 142 | $replacement = ''; |
||||
| 143 | switch ( $option_type ) { |
||||
| 144 | case 'option': |
||||
| 145 | if ( is_array( $options ) ) { |
||||
| 146 | if ( $option_name ) { |
||||
| 147 | $subkey = str_replace( array( $option_name, '[', ']' ), '', $replace ); |
||||
| 148 | $replacement = ( isset( $options[ $subkey ] ) ) ? $options[ $subkey ] : ''; |
||||
| 149 | break; |
||||
| 150 | } |
||||
| 151 | $replacement = ( isset( $options[ $replace ] ) ) ? $options[ $replace ] : ''; |
||||
| 152 | break; |
||||
| 153 | } |
||||
| 154 | $replacement = get_option( $replace ); |
||||
| 155 | break; |
||||
| 156 | case 'site_option': |
||||
| 157 | $replacement = ( is_array( $options ) && isset( $options[ $replace ] ) ) ? $options[ $replace ] : get_site_option( $replace ); |
||||
| 158 | break; |
||||
| 159 | case 'user_meta': |
||||
| 160 | $user_id = get_current_user_id(); |
||||
| 161 | if ( $user_id ) { |
||||
| 162 | $replacement = get_user_meta( $user_id, $replace, true ); |
||||
| 163 | } |
||||
| 164 | break; |
||||
| 165 | default: |
||||
| 166 | $replacement = get_theme_mod( $replace ); |
||||
| 167 | } |
||||
| 168 | $replacement = ( false === $replacement ) ? '' : $replacement; |
||||
| 169 | if ( is_array( $value ) ) { |
||||
| 170 | foreach ( $value as $k => $v ) { |
||||
| 171 | $_val = ( isset( $value[ $v ] ) ) ? $value[ $v ] : $v; |
||||
| 172 | $value[ $k ] = str_replace( $search, $replacement, $_val ); |
||||
| 173 | } |
||||
| 174 | return $value; |
||||
| 175 | } |
||||
| 176 | $value = str_replace( $search, $replacement, $value ); |
||||
| 177 | } |
||||
| 178 | } |
||||
| 179 | return $value; |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | /** |
||||
| 183 | * Parses the output arguments. |
||||
| 184 | * Calls the process_output method for each of them. |
||||
| 185 | * |
||||
| 186 | * @access protected |
||||
| 187 | */ |
||||
| 188 | protected function parse_output() { |
||||
| 189 | foreach ( $this->output as $output ) { |
||||
| 190 | $skip = false; |
||||
| 191 | |||||
| 192 | // Apply any sanitization callbacks defined. |
||||
| 193 | $value = $this->apply_sanitize_callback( $output, $this->value ); |
||||
| 194 | |||||
| 195 | // Skip if value is empty. |
||||
| 196 | if ( '' === $this->value ) { |
||||
| 197 | $skip = true; |
||||
| 198 | } |
||||
| 199 | |||||
| 200 | // No need to proceed this if the current value is the same as in the "exclude" value. |
||||
| 201 | if ( isset( $output['exclude'] ) && is_array( $output['exclude'] ) ) { |
||||
| 202 | foreach ( $output['exclude'] as $exclude ) { |
||||
| 203 | if ( is_array( $value ) ) { |
||||
| 204 | if ( is_array( $exclude ) ) { |
||||
| 205 | $diff1 = array_diff( $value, $exclude ); |
||||
| 206 | $diff2 = array_diff( $exclude, $value ); |
||||
| 207 | |||||
| 208 | if ( empty( $diff1 ) && empty( $diff2 ) ) { |
||||
| 209 | $skip = true; |
||||
| 210 | } |
||||
| 211 | } |
||||
| 212 | // If 'choice' is defined check for sub-values too. |
||||
| 213 | // Fixes https://github.com/aristath/kirki/issues/1416. |
||||
| 214 | if ( isset( $output['choice'] ) && isset( $value[ $output['choice'] ] ) && $exclude == $value[ $output['choice'] ] ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 215 | $skip = true; |
||||
| 216 | } |
||||
| 217 | } |
||||
| 218 | if ( $skip ) { |
||||
| 219 | continue; |
||||
| 220 | } |
||||
| 221 | |||||
| 222 | // Skip if value is defined as excluded. |
||||
| 223 | if ( $exclude === $value || ( '' === $exclude && empty( $value ) ) ) { |
||||
| 224 | $skip = true; |
||||
| 225 | } |
||||
| 226 | } |
||||
| 227 | } |
||||
| 228 | if ( $skip ) { |
||||
| 229 | continue; |
||||
| 230 | } |
||||
| 231 | |||||
| 232 | // Apply any value patterns defined. |
||||
| 233 | $value = $this->apply_value_pattern( $output, $value ); |
||||
| 234 | |||||
| 235 | if ( isset( $output['element'] ) && is_array( $output['element'] ) ) { |
||||
| 236 | $output['element'] = array_unique( $output['element'] ); |
||||
| 237 | sort( $output['element'] ); |
||||
| 238 | $output['element'] = implode( ',', $output['element'] ); |
||||
| 239 | } |
||||
| 240 | |||||
| 241 | $value = $this->process_value( $value, $output ); |
||||
| 242 | |||||
| 243 | if ( is_admin() && ! is_customize_preview() ) { |
||||
| 244 | |||||
| 245 | // Check if this is an admin style. |
||||
| 246 | if ( ! isset( $output['context'] ) || ! in_array( 'editor', $output['context'] ) ) { |
||||
|
0 ignored issues
–
show
|
|||||
| 247 | continue; |
||||
| 248 | } |
||||
| 249 | } elseif ( isset( $output['context'] ) && ! in_array( 'front', $output['context'] ) ) { |
||||
|
0 ignored issues
–
show
|
|||||
| 250 | |||||
| 251 | // Check if this is a frontend style. |
||||
| 252 | continue; |
||||
| 253 | } |
||||
| 254 | $this->process_output( $output, $value ); |
||||
| 255 | } |
||||
| 256 | } |
||||
| 257 | |||||
| 258 | /** |
||||
| 259 | * Parses an output and creates the styles array for it. |
||||
| 260 | * |
||||
| 261 | * @access protected |
||||
| 262 | * @param array $output The field output. |
||||
| 263 | * @param string|array $value The value. |
||||
| 264 | * |
||||
| 265 | * @return null |
||||
| 266 | */ |
||||
| 267 | protected function process_output( $output, $value ) { |
||||
| 268 | if ( ! isset( $output['element'] ) || ! isset( $output['property'] ) ) { |
||||
| 269 | return; |
||||
| 270 | } |
||||
| 271 | $output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global'; |
||||
| 272 | $output['prefix'] = ( isset( $output['prefix'] ) ) ? $output['prefix'] : ''; |
||||
| 273 | $output['units'] = ( isset( $output['units'] ) ) ? $output['units'] : ''; |
||||
| 274 | $output['suffix'] = ( isset( $output['suffix'] ) ) ? $output['suffix'] : ''; |
||||
| 275 | |||||
| 276 | // Properties that can accept multiple values. |
||||
| 277 | // Useful for example for gradients where all browsers use the "background-image" property |
||||
| 278 | // and the browser prefixes go in the value_pattern arg. |
||||
| 279 | $accepts_multiple = array( |
||||
| 280 | 'background-image', |
||||
| 281 | 'background', |
||||
| 282 | ); |
||||
| 283 | if ( in_array( $output['property'], $accepts_multiple, true ) ) { |
||||
| 284 | if ( isset( $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] ) && ! is_array( $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] ) ) { |
||||
| 285 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = (array) $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ]; |
||||
| 286 | } |
||||
| 287 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ][] = $output['prefix'] . $value . $output['units'] . $output['suffix']; |
||||
|
0 ignored issues
–
show
Are you sure
$value of type array|string can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 288 | return; |
||||
| 289 | } |
||||
| 290 | if ( is_string( $value ) || is_numeric( $value ) ) { |
||||
| 291 | $this->styles[ $output['media_query'] ][ $output['element'] ][ $output['property'] ] = $output['prefix'] . $this->process_property_value( $output['property'], $value ) . $output['units'] . $output['suffix']; |
||||
|
0 ignored issues
–
show
Are you sure
$this->process_property_...ut['property'], $value) of type array can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 292 | } |
||||
| 293 | } |
||||
| 294 | |||||
| 295 | /** |
||||
| 296 | * Some CSS properties are unique. |
||||
| 297 | * We need to tweak the value to make everything works as expected. |
||||
| 298 | * |
||||
| 299 | * @access protected |
||||
| 300 | * @param string $property The CSS property. |
||||
| 301 | * @param string|array $value The value. |
||||
| 302 | * |
||||
| 303 | * @return array |
||||
| 304 | */ |
||||
| 305 | protected function process_property_value( $property, $value ) { |
||||
| 306 | $properties = apply_filters( |
||||
| 307 | "kirki_{$this->config_id}_output_property_classnames", array( |
||||
|
0 ignored issues
–
show
|
|||||
| 308 | 'font-family' => 'Kirki_Output_Property_Font_Family', |
||||
| 309 | 'background-image' => 'Kirki_Output_Property_Background_Image', |
||||
| 310 | 'background-position' => 'Kirki_Output_Property_Background_Position', |
||||
| 311 | ) |
||||
| 312 | ); |
||||
| 313 | if ( array_key_exists( $property, $properties ) ) { |
||||
| 314 | $classname = $properties[ $property ]; |
||||
| 315 | $obj = new $classname( $property, $value ); |
||||
| 316 | return $obj->get_value(); |
||||
| 317 | } |
||||
| 318 | return $value; |
||||
| 319 | } |
||||
| 320 | |||||
| 321 | /** |
||||
| 322 | * Returns the value. |
||||
| 323 | * |
||||
| 324 | * @access protected |
||||
| 325 | * @param string|array $value The value. |
||||
| 326 | * @param array $output The field "output". |
||||
| 327 | * @return string|array |
||||
| 328 | */ |
||||
| 329 | protected function process_value( $value, $output ) { |
||||
| 330 | if ( isset( $output['property'] ) ) { |
||||
| 331 | return $this->process_property_value( $output['property'], $value ); |
||||
| 332 | } |
||||
| 333 | return $value; |
||||
| 334 | } |
||||
| 335 | |||||
| 336 | /** |
||||
| 337 | * Exploses the private $styles property to the world |
||||
| 338 | * |
||||
| 339 | * @access protected |
||||
| 340 | * @return array |
||||
| 341 | */ |
||||
| 342 | public function get_styles() { |
||||
| 343 | return $this->styles; |
||||
| 344 | } |
||||
| 345 | } |
||||
| 346 |