wp_apply_colors_support( WP_Block_Type $block_type, array $block_attributes ): array
- Since
- 5.6.0, 6.1.0, 7.1.0
- Source
wp-includes/block-supports/colors.php:84
Description
This will be applied to the block markup in the front-end.
Compatibility
- WordPress
- since 7.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$block_typeWP_Block_Type- Block type.
$block_attributesarray- Block attributes.
Return value
array- Colors CSS classes and inline styles.
Performance profile
How much work a call to wp_apply_colors_support() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 43–117
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 150.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_apply_colors_support() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_array($color_support) && $color_support === true && !$color_support | 43–49 | block_has_support(), color() |
!is_array($color_support) && $color_support === true && !$color_support | 49–74 | block_has_support(), wp_should_skip_block_supports_serialization(), color() |
!is_array($color_support) && $color_support === true && !$color_support | 49–72 | wp_should_skip_block_supports_serialization(), block_has_support(), color() |
!is_array($color_support) && $color_support === true && !$color_support | 55–97 | wp_should_skip_block_supports_serialization(), block_has_support(), wp_should_skip_block_supports_serialization(), color() |
!is_array($color_support) && $color_support === true && !$color_support | 55–92 | wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), block_has_support(), color() |
!is_array($color_support) && $color_support === true && !$color_support | 61–117 | wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), block_has_support(), wp_should_skip_block_supports_serialization(), color() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 150 | 43–117 | 31 | |
| 8.5 | 150 | 43–117 | 31 | |
| 8.4 | 150 | 43–117 | 31 | |
| 8.3 | 150 | 43–117 | 31 | |
| 8.2 | 150 | 43–117 | 31 | |
| 8.1 | 150 | 43–117 | 31 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 153 | 43–119 | 31 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- wp_should_skip_block_supports_serialization()Checks whether serialization of the current block's supported properties should occur.
- block_has_support()Checks whether the current block type supports the feature requested.
- wp_style_engine_get_styles()Global public interface method to generate styles from a single style object, e.g. the value of a block's attributes.style object or the top level styles in theme.json.
Used by · 1
- render_block_core_navigation_submenu()Renders the `core/navigation-submenu` block.
Source code
function wp_apply_colors_support( $block_type, $block_attributes ) { $color_support = $block_type->supports['color'] ?? false; if ( is_array( $color_support ) && wp_should_skip_block_supports_serialization( $block_type, 'color' ) ) { return array(); } $has_text_colors_support = true === $color_support || ( isset( $color_support['text'] ) && $color_support['text'] ) || ( is_array( $color_support ) && ! isset( $color_support['text'] ) ); $has_background_colors_support = true === $color_support || ( isset( $color_support['background'] ) && $color_support['background'] ) || ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); $has_gradients_support = $color_support['gradients'] ?? false; $color_block_styles = array(); // Text colors. if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { $preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; $custom_text_color = $block_attributes['style']['color']['text'] ?? null; $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; } // Background colors. if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { $preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; $custom_background_color = $block_attributes['style']['color']['background'] ?? null; $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; } // Gradients. // Suppress color.gradient CSS when background.gradient is supported and // explicitly set. background.php owns CSS generation in that case, and // emitting the background shorthand here would conflict with it. $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false ); $has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] ); if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) && ! ( $has_background_gradient_support && $has_background_gradient_value ) ) { $preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; $custom_gradient_color = $block_attributes['style']['color']['gradient'] ?? null; $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; } $attributes = array(); $styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); if ( ! empty( $styles['classnames'] ) ) { $attributes['class'] = $styles['classnames']; } if ( ! empty( $styles['css'] ) ) { $attributes['style'] = $styles['css']; } return $attributes;}Changelog
Introduced in 5.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/block-supports/colors.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.