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