| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Style Engine: WP_Style_Engine class 4 * 5 * @package WordPress 6 * @subpackage StyleEngine 7 * @since 6.1.0 8 */ 9 10 /** 11 * The main class integrating all other WP_Style_Engine_* classes. 12 * 13 * The Style Engine aims to provide a consistent API for rendering styling for blocks 14 * across both client-side and server-side applications. 15 * 16 * This class is final and should not be extended. 17 * 18 * This class is for internal Core usage and is not supposed to be used by extenders 19 * (plugins and/or themes). This is a low-level API that may need to do breaking changes. 20 * Please, use wp_style_engine_get_styles() instead. 21 * 22 * @access private 23 * @since 6.1.0 24 * @since 6.3.0 Added support for text-columns. 25 * @since 6.4.0 Added support for background.backgroundImage. 26 * @since 6.5.0 Added support for background.backgroundPosition, 27 * background.backgroundRepeat and dimensions.aspectRatio. 28 * @since 6.7.0 Added support for typography.writingMode. 29 * @since 7.0.0 Added support for typography.textIndent. 30 */ 31 #[AllowDynamicProperties] 32 final class WP_Style_Engine { 33 /** 34 * Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes. 35 * 36 * For every style definition, the following properties are valid: 37 * 38 * - classnames => (array) an array of classnames to be returned for block styles. The key is a classname or pattern. 39 * A value of `true` means the classname should be applied always. Otherwise, a valid CSS property (string) 40 * to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug. 41 * - css_vars => (array) an array of key value pairs used to generate CSS var values. 42 * The key should be the CSS property name that matches the second element of the preset string value, 43 * i.e., "color" in var:preset|color|somePresetSlug. The value is a CSS var pattern (e.g. `--wp--preset--color--$slug`), 44 * whose `$slug` fragment will be replaced with the preset slug, which is the third element of the preset string value, 45 * i.e., `somePresetSlug` in var:preset|color|somePresetSlug. 46 * - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border". 47 * - path => (array) a path that accesses the corresponding style value in the block style object. 48 * - value_func => (string) the name of a function to generate a CSS definition array for a particular style object. The output of this function should be `array( "$property" => "$value", ... )`. 49 * 50 * @since 6.1.0 51 * @var array 52 */ 53 const BLOCK_STYLE_DEFINITIONS_METADATA = array( 54 'background' => array( 55 'backgroundImage' => array( 56 'property_keys' => array( 57 'default' => 'background-image', 58 ), 59 'value_func' => array( self::class, 'get_url_or_value_css_declaration' ), 60 'path' => array( 'background', 'backgroundImage' ), 61 ), 62 'backgroundPosition' => array( 63 'property_keys' => array( 64 'default' => 'background-position', 65 ), 66 'path' => array( 'background', 'backgroundPosition' ), 67 ), 68 'backgroundRepeat' => array( 69 'property_keys' => array( 70 'default' => 'background-repeat', 71 ), 72 'path' => array( 'background', 'backgroundRepeat' ), 73 ), 74 'backgroundSize' => array( 75 'property_keys' => array( 76 'default' => 'background-size', 77 ), 78 'path' => array( 'background', 'backgroundSize' ), 79 ), 80 'backgroundAttachment' => array( 81 'property_keys' => array( 82 'default' => 'background-attachment', 83 ), 84 'path' => array( 'background', 'backgroundAttachment' ), 85 ), 86 ), 87 'color' => array( 88 'text' => array( 89 'property_keys' => array( 90 'default' => 'color', 91 ), 92 'path' => array( 'color', 'text' ), 93 'css_vars' => array( 94 'color' => '--wp--preset--color--$slug', 95 ), 96 'classnames' => array( 97 'has-text-color' => true, 98 'has-$slug-color' => 'color', 99 ), 100 ), 101 'background' => array( 102 'property_keys' => array( 103 'default' => 'background-color', 104 ), 105 'path' => array( 'color', 'background' ), 106 'css_vars' => array( 107 'color' => '--wp--preset--color--$slug', 108 ), 109 'classnames' => array( 110 'has-background' => true, 111 'has-$slug-background-color' => 'color', 112 ), 113 ), 114 'gradient' => array( 115 'property_keys' => array( 116 'default' => 'background', 117 ), 118 'path' => array( 'color', 'gradient' ), 119 'css_vars' => array( 120 'gradient' => '--wp--preset--gradient--$slug', 121 ), 122 'classnames' => array( 123 'has-background' => true, 124 'has-$slug-gradient-background' => 'gradient', 125 ), 126 ), 127 ), 128 'border' => array( 129 'color' => array( 130 'property_keys' => array( 131 'default' => 'border-color', 132 'individual' => 'border-%s-color', 133 ), 134 'path' => array( 'border', 'color' ), 135 'classnames' => array( 136 'has-border-color' => true, 137 'has-$slug-border-color' => 'color', 138 ), 139 ), 140 'radius' => array( 141 'property_keys' => array( 142 'default' => 'border-radius', 143 'individual' => 'border-%s-radius', 144 ), 145 'path' => array( 'border', 'radius' ), 146 'css_vars' => array( 147 'border-radius' => '--wp--preset--border-radius--$slug', 148 ), 149 ), 150 'style' => array( 151 'property_keys' => array( 152 'default' => 'border-style', 153 'individual' => 'border-%s-style', 154 ), 155 'path' => array( 'border', 'style' ), 156 ), 157 'width' => array( 158 'property_keys' => array( 159 'default' => 'border-width', 160 'individual' => 'border-%s-width', 161 ), 162 'path' => array( 'border', 'width' ), 163 ), 164 'top' => array( 165 'value_func' => array( self::class, 'get_individual_property_css_declarations' ), 166 'path' => array( 'border', 'top' ), 167 'css_vars' => array( 168 'color' => '--wp--preset--color--$slug', 169 ), 170 ), 171 'right' => array( 172 'value_func' => array( self::class, 'get_individual_property_css_declarations' ), 173 'path' => array( 'border', 'right' ), 174 'css_vars' => array( 175 'color' => '--wp--preset--color--$slug', 176 ), 177 ), 178 'bottom' => array( 179 'value_func' => array( self::class, 'get_individual_property_css_declarations' ), 180 'path' => array( 'border', 'bottom' ), 181 'css_vars' => array( 182 'color' => '--wp--preset--color--$slug', 183 ), 184 ), 185 'left' => array( 186 'value_func' => array( self::class, 'get_individual_property_css_declarations' ), 187 'path' => array( 'border', 'left' ), 188 'css_vars' => array( 189 'color' => '--wp--preset--color--$slug', 190 ), 191 ), 192 ), 193 'shadow' => array( 194 'shadow' => array( 195 'property_keys' => array( 196 'default' => 'box-shadow', 197 ), 198 'path' => array( 'shadow' ), 199 'css_vars' => array( 200 'shadow' => '--wp--preset--shadow--$slug', 201 ), 202 ), 203 ), 204 'dimensions' => array( 205 'aspectRatio' => array( 206 'property_keys' => array( 207 'default' => 'aspect-ratio', 208 ), 209 'path' => array( 'dimensions', 'aspectRatio' ), 210 'classnames' => array( 211 'has-aspect-ratio' => true, 212 ), 213 ), 214 'height' => array( 215 'property_keys' => array( 216 'default' => 'height', 217 ), 218 'path' => array( 'dimensions', 'height' ), 219 'css_vars' => array( 220 'dimension' => '--wp--preset--dimension--$slug', 221 ), 222 ), 223 'minHeight' => array( 224 'property_keys' => array( 225 'default' => 'min-height', 226 ), 227 'path' => array( 'dimensions', 'minHeight' ), 228 'css_vars' => array( 229 'dimension' => '--wp--preset--dimension--$slug', 230 ), 231 ), 232 'objectFit' => array( 233 'property_keys' => array( 234 'default' => 'object-fit', 235 ), 236 'path' => array( 'dimensions', 'objectFit' ), 237 ), 238 'width' => array( 239 'property_keys' => array( 240 'default' => 'width', 241 ), 242 'path' => array( 'dimensions', 'width' ), 243 'css_vars' => array( 244 'dimension' => '--wp--preset--dimension--$slug', 245 ), 246 ), 247 ), 248 'spacing' => array( 249 'padding' => array( 250 'property_keys' => array( 251 'default' => 'padding', 252 'individual' => 'padding-%s', 253 ), 254 'path' => array( 'spacing', 'padding' ), 255 'css_vars' => array( 256 'spacing' => '--wp--preset--spacing--$slug', 257 ), 258 ), 259 'margin' => array( 260 'property_keys' => array( 261 'default' => 'margin', 262 'individual' => 'margin-%s', 263 ), 264 'path' => array( 'spacing', 'margin' ), 265 'css_vars' => array( 266 'spacing' => '--wp--preset--spacing--$slug', 267 ), 268 ), 269 ), 270 'typography' => array( 271 'fontSize' => array( 272 'property_keys' => array( 273 'default' => 'font-size', 274 ), 275 'path' => array( 'typography', 'fontSize' ), 276 'css_vars' => array( 277 'font-size' => '--wp--preset--font-size--$slug', 278 ), 279 'classnames' => array( 280 'has-$slug-font-size' => 'font-size', 281 ), 282 ), 283 'fontFamily' => array( 284 'property_keys' => array( 285 'default' => 'font-family', 286 ), 287 'css_vars' => array( 288 'font-family' => '--wp--preset--font-family--$slug', 289 ), 290 'path' => array( 'typography', 'fontFamily' ), 291 'classnames' => array( 292 'has-$slug-font-family' => 'font-family', 293 ), 294 ), 295 'fontStyle' => array( 296 'property_keys' => array( 297 'default' => 'font-style', 298 ), 299 'path' => array( 'typography', 'fontStyle' ), 300 ), 301 'fontWeight' => array( 302 'property_keys' => array( 303 'default' => 'font-weight', 304 ), 305 'path' => array( 'typography', 'fontWeight' ), 306 ), 307 'lineHeight' => array( 308 'property_keys' => array( 309 'default' => 'line-height', 310 ), 311 'path' => array( 'typography', 'lineHeight' ), 312 ), 313 'textColumns' => array( 314 'property_keys' => array( 315 'default' => 'column-count', 316 ), 317 'path' => array( 'typography', 'textColumns' ), 318 ), 319 'textDecoration' => array( 320 'property_keys' => array( 321 'default' => 'text-decoration', 322 ), 323 'path' => array( 'typography', 'textDecoration' ), 324 ), 325 'textIndent' => array( 326 'property_keys' => array( 327 'default' => 'text-indent', 328 ), 329 'path' => array( 'typography', 'textIndent' ), 330 ), 331 'textTransform' => array( 332 'property_keys' => array( 333 'default' => 'text-transform', 334 ), 335 'path' => array( 'typography', 'textTransform' ), 336 ), 337 'letterSpacing' => array( 338 'property_keys' => array( 339 'default' => 'letter-spacing', 340 ), 341 'path' => array( 'typography', 'letterSpacing' ), 342 ), 343 'writingMode' => array( 344 'property_keys' => array( 345 'default' => 'writing-mode', 346 ), 347 'path' => array( 'typography', 'writingMode' ), 348 ), 349 ), 350 ); 351 352 /** 353 * Util: Extracts the slug in kebab case from a preset string, 354 * e.g. `heavenly-blue` from `var:preset|color|heavenlyBlue`. 355 * 356 * @since 6.1.0 357 * 358 * @param string $style_value A single CSS preset value. 359 * @param string $property_key The CSS property that is the second element of the preset string. 360 * Used for matching. 361 * @return string The slug, or empty string if not found. 362 */ 363 protected static function get_slug_from_preset_value( $style_value, $property_key ) { 364 if ( is_string( $style_value ) && is_string( $property_key ) 365 && str_contains( $style_value, "var:preset|{$property_key}|" ) 366 ) { 367 $index_to_splice = strrpos( $style_value, '|' ) + 1; 368 return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) ); 369 } 370 return ''; 371 } 372 373 /** 374 * Util: Generates a CSS var string, e.g. `var(--wp--preset--color--background)` 375 * from a preset string such as `var:preset|space|50`. 376 * 377 * @since 6.1.0 378 * 379 * @param string $style_value A single CSS preset value. 380 * @param string[] $css_vars An associate array of CSS var patterns 381 * used to generate the var string. 382 * @return string The CSS var, or an empty string if no match for slug found. 383 */ 384 protected static function get_css_var_value( $style_value, $css_vars ) { 385 foreach ( $css_vars as $property_key => $css_var_pattern ) { 386 $slug = static::get_slug_from_preset_value( $style_value, $property_key ); 387 if ( static::is_valid_style_value( $slug ) ) { 388 $var = strtr( 389 $css_var_pattern, 390 array( '$slug' => $slug ) 391 ); 392 return "var($var)"; 393 } 394 } 395 return ''; 396 } 397 398 /** 399 * Util: Checks whether an incoming block style value is valid. 400 * 401 * @since 6.1.0 402 * 403 * @param string $style_value A single CSS preset value. 404 * @return bool 405 */ 406 protected static function is_valid_style_value( $style_value ) { 407 return '0' === $style_value || ! empty( $style_value ); 408 } 409 410 /** 411 * Stores a CSS rule using the provided CSS selector and CSS declarations. 412 * 413 * @since 6.1.0 414 * @since 6.6.0 Added the `$rules_group` parameter. 415 * 416 * @param string $store_name A valid store key. 417 * @param string $css_selector When a selector is passed, the function will return 418 * a full CSS rule `$selector { ...rules }` 419 * otherwise a concatenated string of properties and values. 420 * @param string[] $css_declarations An associative array of CSS definitions, 421 * e.g. `array( "$property" => "$value", "$property" => "$value" )`. 422 * @param string $rules_group Optional. A parent CSS selector in the case of nested CSS, or a CSS nested @rule, 423 * such as `@media (min-width: 80rem)` or `@layer module`. 424 */ 425 public static function store_css_rule( $store_name, $css_selector, $css_declarations, $rules_group = '' ) { 426 if ( empty( $store_name ) || empty( $css_selector ) || empty( $css_declarations ) ) { 427 return; 428 } 429 static::get_store( $store_name )->add_rule( $css_selector, $rules_group )->add_declarations( $css_declarations ); 430 } 431 432 /** 433 * Returns a store by store key. 434 * 435 * @since 6.1.0 436 * 437 * @param string $store_name A store key. 438 * @return WP_Style_Engine_CSS_Rules_Store|null 439 */ 440 public static function get_store( $store_name ) { 441 return WP_Style_Engine_CSS_Rules_Store::get_store( $store_name ); 442 } 443 444 /** 445 * Returns classnames and CSS based on the values in a styles object. 446 * 447 * Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA. 448 * 449 * @since 6.1.0 450 * 451 * @param array $block_styles The style object. 452 * @param array $options { 453 * Optional. An array of options. Default empty array. 454 * 455 * @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, 456 * e.g. `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, 457 * to `var( --wp--preset--* )` values. Default false. 458 * @type string $selector Optional. When a selector is passed, 459 * the value of `$css` in the return value will comprise 460 * a full CSS rule `$selector { ...$css_declarations }`, 461 * otherwise, the value will be a concatenated string 462 * of CSS declarations. 463 * } 464 * @return array { 465 * @type string[] $classnames Array of class names. 466 * @type string[] $declarations An associative array of CSS definitions, 467 * e.g. `array( "$property" => "$value", "$property" => "$value" )`. 468 * } 469 */ 470 public static function parse_block_styles( $block_styles, $options ) { 471 $parsed_styles = array( 472 'classnames' => array(), 473 'declarations' => array(), 474 ); 475 if ( empty( $block_styles ) || ! is_array( $block_styles ) ) { 476 return $parsed_styles; 477 } 478 479 // Collect CSS and classnames. 480 foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) { 481 if ( empty( $block_styles[ $definition_group_key ] ) ) { 482 continue; 483 } 484 foreach ( $definition_group_style as $style_definition ) { 485 $style_value = _wp_array_get( $block_styles, $style_definition['path'], null ); 486 487 if ( ! static::is_valid_style_value( $style_value ) ) { 488 continue; 489 } 490 491 $classnames = static::get_classnames( $style_value, $style_definition ); 492 if ( ! empty( $classnames ) ) { 493 $parsed_styles['classnames'] = array_merge( $parsed_styles['classnames'], $classnames ); 494 } 495 496 $css_declarations = static::get_css_declarations( $style_value, $style_definition, $options ); 497 if ( ! empty( $css_declarations ) ) { 498 $parsed_styles['declarations'] = array_merge( $parsed_styles['declarations'], $css_declarations ); 499 } 500 } 501 } 502 503 return $parsed_styles; 504 } 505 506 /** 507 * Returns classnames, and generates classname(s) from a CSS preset property pattern, 508 * e.g. `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`. 509 * 510 * @since 6.1.0 511 * 512 * @param string $style_value A single raw style value or CSS preset property 513 * from the `$block_styles` array. 514 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA. 515 * @return string[] An array of CSS classnames, or empty array if there are none. 516 */ 517 protected static function get_classnames( $style_value, $style_definition ) { 518 if ( empty( $style_value ) ) { 519 return array(); 520 } 521 522 $classnames = array(); 523 if ( ! empty( $style_definition['classnames'] ) ) { 524 foreach ( $style_definition['classnames'] as $classname => $property_key ) { 525 if ( true === $property_key ) { 526 $classnames[] = $classname; 527 continue; 528 } 529 530 $slug = static::get_slug_from_preset_value( $style_value, $property_key ); 531 532 if ( $slug ) { 533 /* 534 * Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA. 535 * One day, if there are no stored schemata, we could allow custom patterns or 536 * generate classnames based on other properties 537 * such as a path or a value or a prefix passed in options. 538 */ 539 $classnames[] = strtr( $classname, array( '$slug' => $slug ) ); 540 } 541 } 542 } 543 544 return $classnames; 545 } 546 547 /** 548 * Returns an array of CSS declarations based on valid block style values. 549 * 550 * @since 6.1.0 551 * 552 * @param mixed $style_value A single raw style value from $block_styles array. 553 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA. 554 * @param array $options { 555 * Optional. An array of options. Default empty array. 556 * 557 * @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, 558 * e.g. `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, 559 * to `var( --wp--preset--* )` values. Default false. 560 * } 561 * @return string[] An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. 562 */ 563 protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) { 564 if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) { 565 return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options ); 566 } 567 568 $css_declarations = array(); 569 $style_property_keys = $style_definition['property_keys']; 570 $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames']; 571 572 /* 573 * Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`. 574 * Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition. 575 */ 576 if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) { 577 if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) { 578 $css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] ); 579 if ( static::is_valid_style_value( $css_var ) ) { 580 $css_declarations[ $style_property_keys['default'] ] = $css_var; 581 } 582 } 583 return $css_declarations; 584 } 585 586 /* 587 * Default rule builder. 588 * If the input contains an array, assume box model-like properties 589 * for styles such as margins and padding. 590 */ 591 if ( is_array( $style_value ) ) { 592 // Bail out early if the `'individual'` property is not defined. 593 if ( ! isset( $style_property_keys['individual'] ) ) { 594 return $css_declarations; 595 } 596 597 foreach ( $style_value as $key => $value ) { 598 if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) { 599 $value = static::get_css_var_value( $value, $style_definition['css_vars'] ); 600 } 601 602 $individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) ); 603 604 if ( $individual_property && static::is_valid_style_value( $value ) ) { 605 $css_declarations[ $individual_property ] = $value; 606 } 607 } 608 609 return $css_declarations; 610 } 611 612 $css_declarations[ $style_property_keys['default'] ] = $style_value; 613 return $css_declarations; 614 } 615 616 /** 617 * Style value parser that returns a CSS definition array comprising style properties 618 * that have keys representing individual style properties, otherwise known as longhand CSS properties. 619 * 620 * Example: 621 * 622 * "$style_property-$individual_feature: $value;" 623 * 624 * Which could represent the following: 625 * 626 * "border-{top|right|bottom|left}-{color|width|style}: {value};" 627 * 628 * or: 629 * 630 * "border-image-{outset|source|width|repeat|slice}: {value};" 631 * 632 * @since 6.1.0 633 * 634 * @param array $style_value A single raw style value from `$block_styles` array. 635 * @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA 636 * representing an individual property of a CSS property, 637 * e.g. 'top' in 'border-top'. 638 * @param array $options { 639 * Optional. An array of options. Default empty array. 640 * 641 * @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, 642 * e.g. `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, 643 * to `var( --wp--preset--* )` values. Default false. 644 * } 645 * @return string[] An associative array of CSS definitions, e.g. `array( "$property" => "$value", "$property" => "$value" )`. 646 */ 647 protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options = array() ) { 648 if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) { 649 return array(); 650 } 651 652 /* 653 * The first item in $individual_property_definition['path'] array 654 * tells us the style property, e.g. "border". We use this to get a corresponding 655 * CSS style definition such as "color" or "width" from the same group. 656 * 657 * The second item in $individual_property_definition['path'] array 658 * refers to the individual property marker, e.g. "top". 659 */ 660 $definition_group_key = $individual_property_definition['path'][0]; 661 $individual_property_key = $individual_property_definition['path'][1]; 662 $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames']; 663 $css_declarations = array(); 664 665 foreach ( $style_value as $css_property => $value ) { 666 if ( empty( $value ) ) { 667 continue; 668 } 669 670 // Build a path to the individual rules in definitions. 671 $style_definition_path = array( $definition_group_key, $css_property ); 672 $style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null ); 673 674 if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) { 675 // Set a CSS var if there is a valid preset value. 676 if ( is_string( $value ) && str_contains( $value, 'var:' ) 677 && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) 678 ) { 679 $value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] ); 680 } 681 682 $individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key ); 683 684 $css_declarations[ $individual_css_property ] = $value; 685 } 686 } 687 return $css_declarations; 688 } 689 690 /** 691 * Style value parser that constructs a CSS definition array comprising a single CSS property and value. 692 * If the provided value is an array containing a `url` property, the function will return a CSS definition array 693 * with a single property and value, with `url` escaped and injected into a CSS `url()` function, 694 * e.g., array( 'background-image' => "url( '...' )" ). 695 * 696 * @since 6.4.0 697 * 698 * @param array $style_value A single raw style value from $block_styles array. 699 * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA. 700 * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). 701 */ 702 protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) { 703 if ( empty( $style_value ) ) { 704 return array(); 705 } 706 707 $css_declarations = array(); 708 709 if ( isset( $style_definition['property_keys']['default'] ) ) { 710 $value = null; 711 712 if ( ! empty( $style_value['url'] ) ) { 713 $value = "url('" . $style_value['url'] . "')"; 714 } elseif ( is_string( $style_value ) ) { 715 $value = $style_value; 716 } 717 718 if ( null !== $value ) { 719 $css_declarations[ $style_definition['property_keys']['default'] ] = $value; 720 } 721 } 722 723 return $css_declarations; 724 } 725 726 /** 727 * Returns compiled CSS from CSS declarations. 728 * 729 * @since 6.1.0 730 * 731 * @param string[] $css_declarations An associative array of CSS definitions, 732 * e.g. `array( "$property" => "$value", "$property" => "$value" )`. 733 * @param string $css_selector When a selector is passed, the function will return 734 * a full CSS rule `$selector { ...rules }`, 735 * otherwise a concatenated string of properties and values. 736 * @return string A compiled CSS string. 737 */ 738 public static function compile_css( $css_declarations, $css_selector ) { 739 if ( empty( $css_declarations ) || ! is_array( $css_declarations ) ) { 740 return ''; 741 } 742 743 // Return an entire rule if there is a selector. 744 if ( $css_selector ) { 745 $css_rule = new WP_Style_Engine_CSS_Rule( $css_selector, $css_declarations ); 746 return $css_rule->get_css(); 747 } 748 749 $css_declarations = new WP_Style_Engine_CSS_Declarations( $css_declarations ); 750 return $css_declarations->get_declarations_string(); 751 } 752 753 /** 754 * Returns a compiled stylesheet from stored CSS rules. 755 * 756 * @since 6.1.0 757 * 758 * @param WP_Style_Engine_CSS_Rule[] $css_rules An array of WP_Style_Engine_CSS_Rule objects 759 * from a store or otherwise. 760 * @param array $options { 761 * Optional. An array of options. Default empty array. 762 * 763 * @type string|null $context An identifier describing the origin of the style object, 764 * e.g. 'block-supports' or 'global-styles'. Default 'block-supports'. 765 * When set, the style engine will attempt to store the CSS rules. 766 * @type bool $optimize Whether to optimize the CSS output, e.g. combine rules. 767 * Default false. 768 * @type bool $prettify Whether to add new lines and indents to output. 769 * Defaults to whether the `SCRIPT_DEBUG` constant is defined. 770 * } 771 * @return string A compiled stylesheet from stored CSS rules. 772 */ 773 public static function compile_stylesheet_from_css_rules( $css_rules, $options = array() ) { 774 $processor = new WP_Style_Engine_Processor(); 775 $processor->add_rules( $css_rules ); 776 return $processor->get_css( $options ); 777 } 778 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jul 1 08:20:12 2026 | Cross-referenced by PHPXref |