| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Colors block support flag. 4 * 5 * @package WordPress 6 * @since 5.6.0 7 */ 8 9 /** 10 * Registers the style and colors block attributes for block types that support it. 11 * 12 * @since 5.6.0 13 * @since 6.1.0 Improved $color_support assignment optimization. 14 * @access private 15 * 16 * @param WP_Block_Type $block_type Block Type. 17 */ 18 function wp_register_colors_support( $block_type ) { 19 $color_support = false; 20 if ( $block_type instanceof WP_Block_Type ) { 21 $color_support = $block_type->supports['color'] ?? false; 22 } 23 $has_text_colors_support = true === $color_support || 24 ( isset( $color_support['text'] ) && $color_support['text'] ) || 25 ( is_array( $color_support ) && ! isset( $color_support['text'] ) ); 26 $has_background_colors_support = true === $color_support || 27 ( isset( $color_support['background'] ) && $color_support['background'] ) || 28 ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); 29 $has_gradients_support = $color_support['gradients'] ?? false; 30 $has_link_colors_support = $color_support['link'] ?? false; 31 $has_button_colors_support = $color_support['button'] ?? false; 32 $has_heading_colors_support = $color_support['heading'] ?? false; 33 $has_color_support = $has_text_colors_support || 34 $has_background_colors_support || 35 $has_gradients_support || 36 $has_link_colors_support || 37 $has_button_colors_support || 38 $has_heading_colors_support; 39 40 if ( ! $block_type->attributes ) { 41 $block_type->attributes = array(); 42 } 43 44 if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) { 45 $block_type->attributes['style'] = array( 46 'type' => 'object', 47 ); 48 } 49 50 if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) { 51 $block_type->attributes['backgroundColor'] = array( 52 'type' => 'string', 53 ); 54 } 55 56 if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) { 57 $block_type->attributes['textColor'] = array( 58 'type' => 'string', 59 ); 60 } 61 62 if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) { 63 $block_type->attributes['gradient'] = array( 64 'type' => 'string', 65 ); 66 } 67 } 68 69 70 /** 71 * Adds CSS classes and inline styles for colors to the incoming attributes array. 72 * This will be applied to the block markup in the front-end. 73 * 74 * @since 5.6.0 75 * @since 6.1.0 Implemented the style engine to generate CSS and classnames. 76 * @since 7.1.0 Suppresses color.gradient when background.gradient is supported and set. 77 * @since 7.2.0 Skips `has-background` for a background color clipped to the text. 78 * @access private 79 * 80 * @param WP_Block_Type $block_type Block type. 81 * @param array<string, mixed> $block_attributes Block attributes. 82 * @return array<string, mixed> Colors CSS classes and inline styles. 83 */ 84 function wp_apply_colors_support( WP_Block_Type $block_type, array $block_attributes ): array { 85 $color_support = $block_type->supports['color'] ?? false; 86 87 if ( 88 is_array( $color_support ) && 89 wp_should_skip_block_supports_serialization( $block_type, 'color' ) 90 ) { 91 return array(); 92 } 93 94 $has_text_colors_support = true === $color_support || 95 ( isset( $color_support['text'] ) && $color_support['text'] ) || 96 ( is_array( $color_support ) && ! isset( $color_support['text'] ) ); 97 $has_background_colors_support = true === $color_support || 98 ( isset( $color_support['background'] ) && $color_support['background'] ) || 99 ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); 100 $has_gradients_support = $color_support['gradients'] ?? false; 101 $skip_text = wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ); 102 $skip_background = wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ); 103 $skip_gradients = wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ); 104 105 // background.php owns the CSS when a background gradient is set, so skip color.gradient. 106 $has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false ); 107 $has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] ); 108 $has_background_gradient = $has_background_gradient_support && $has_background_gradient_value; 109 110 /* 111 * Mirror of the background support: a color clipped to the text paints the 112 * glyphs, not the box, so only a gradient still adds `has-background`. 113 * Read before the attributes are filtered for the helper. 114 */ 115 $clips_to_text = 'text' === ( $block_attributes['style']['background']['backgroundClip'] ?? null ) && 116 block_has_support( $block_type, array( 'background', 'backgroundClip' ), false ) && 117 ! wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundClip' ); 118 119 // The helper reads only `style.color`, so a non-array value counts as no color styles. 120 if ( ! isset( $block_attributes['style']['color'] ) || ! is_array( $block_attributes['style']['color'] ) ) { 121 unset( $block_attributes['style'] ); 122 } 123 124 if ( ! $has_text_colors_support || $skip_text ) { 125 unset( $block_attributes['textColor'], $block_attributes['style']['color']['text'] ); 126 } 127 128 if ( ! $has_background_colors_support || $skip_background ) { 129 unset( $block_attributes['backgroundColor'], $block_attributes['style']['color']['background'] ); 130 } 131 132 if ( ! $has_gradients_support || $skip_gradients || $has_background_gradient ) { 133 unset( $block_attributes['gradient'], $block_attributes['style']['color']['gradient'] ); 134 } 135 136 $classes_and_styles = wp_get_color_classes_and_styles( $block_attributes ); 137 $has_gradient = ! empty( $block_attributes['gradient'] ) || ! empty( $block_attributes['style']['color']['gradient'] ); 138 139 if ( $clips_to_text && ! $has_gradient && ! empty( $classes_and_styles['class'] ) ) { 140 $classes = array_diff( explode( ' ', $classes_and_styles['class'] ), array( 'has-background' ) ); 141 if ( empty( $classes ) ) { 142 unset( $classes_and_styles['class'] ); 143 } else { 144 $classes_and_styles['class'] = implode( ' ', $classes ); 145 } 146 } 147 148 return $classes_and_styles; 149 } 150 151 /** 152 * Returns color classes and inline styles for block attributes, like the JS 153 * `getColorClassesAndStyles()`. Does not check block support or skipped 154 * serialization. 155 * 156 * @since 7.3.0 157 * 158 * @param array<string, mixed> $block_attributes Block attributes. 159 * @return array<string, string> Array with `class` and `style` keys, each present only when non-empty. 160 * 161 * @phpstan-param array{ style?: array{ color?: array<string, mixed>, ...<string, mixed> }, ...<string, mixed> } $block_attributes 162 * @phpstan-return array{ class?: non-empty-string, style?: non-empty-string } 163 */ 164 function wp_get_color_classes_and_styles( $block_attributes ) { 165 if ( ! is_array( $block_attributes ) ) { 166 return array(); 167 } 168 169 $color_block_styles = array(); 170 $preset_attributes = array( 171 'text' => array( 'textColor', 'color' ), 172 'background' => array( 'backgroundColor', 'color' ), 173 'gradient' => array( 'gradient', 'gradient' ), 174 ); 175 176 foreach ( $preset_attributes as $feature => list( $preset_attribute, $preset_type ) ) { 177 $preset_slug = $block_attributes[ $preset_attribute ] ?? null; 178 179 // Preset slugs are strings in theme.json and the editor. 180 $preset_value = is_string( $preset_slug ) && '' !== $preset_slug ? "var:preset|{$preset_type}|{$preset_slug}" : null; 181 $custom_value = $block_attributes['style']['color'][ $feature ] ?? null; 182 $color_block_styles[ $feature ] = $preset_value ?? $custom_value; 183 } 184 185 $attributes = array(); 186 $styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); 187 188 if ( ! empty( $styles['classnames'] ) ) { 189 $attributes['class'] = $styles['classnames']; 190 } 191 192 if ( ! empty( $styles['css'] ) ) { 193 $attributes['style'] = $styles['css']; 194 } 195 196 return $attributes; 197 } 198 199 // Register the block support. 200 WP_Block_Supports::get_instance()->register( 201 'colors', 202 array( 203 'register_attribute' => 'wp_register_colors_support', 204 'apply' => 'wp_apply_colors_support', 205 ) 206 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Nov 27 08:20:01 2026 | Cross-referenced by PHPXref |