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