[ 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 = isset( $block_type->supports['color'] ) ? $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 = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false; 30 $has_link_colors_support = isset( $color_support['link'] ) ? $color_support['link'] : false; 31 $has_button_colors_support = isset( $color_support['button'] ) ? $color_support['button'] : false; 32 $has_heading_colors_support = isset( $color_support['heading'] ) ? $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 * @access private 77 * 78 * @param WP_Block_Type $block_type Block type. 79 * @param array $block_attributes Block attributes. 80 * 81 * @return array Colors CSS classes and inline styles. 82 */ 83 function wp_apply_colors_support( $block_type, $block_attributes ) { 84 $color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false; 85 86 if ( 87 is_array( $color_support ) && 88 wp_should_skip_block_supports_serialization( $block_type, 'color' ) 89 ) { 90 return array(); 91 } 92 93 $has_text_colors_support = true === $color_support || 94 ( isset( $color_support['text'] ) && $color_support['text'] ) || 95 ( is_array( $color_support ) && ! isset( $color_support['text'] ) ); 96 $has_background_colors_support = true === $color_support || 97 ( isset( $color_support['background'] ) && $color_support['background'] ) || 98 ( is_array( $color_support ) && ! isset( $color_support['background'] ) ); 99 $has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false; 100 $color_block_styles = array(); 101 102 // Text colors. 103 if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { 104 $preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; 105 $custom_text_color = isset( $block_attributes['style']['color']['text'] ) ? $block_attributes['style']['color']['text'] : null; 106 $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; 107 } 108 109 // Background colors. 110 if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { 111 $preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; 112 $custom_background_color = isset( $block_attributes['style']['color']['background'] ) ? $block_attributes['style']['color']['background'] : null; 113 $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; 114 } 115 116 // Gradients. 117 if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) { 118 $preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; 119 $custom_gradient_color = isset( $block_attributes['style']['color']['gradient'] ) ? $block_attributes['style']['color']['gradient'] : null; 120 $color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; 121 } 122 123 $attributes = array(); 124 $styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); 125 126 if ( ! empty( $styles['classnames'] ) ) { 127 $attributes['class'] = $styles['classnames']; 128 } 129 130 if ( ! empty( $styles['css'] ) ) { 131 $attributes['style'] = $styles['css']; 132 } 133 134 return $attributes; 135 } 136 137 // Register the block support. 138 WP_Block_Supports::get_instance()->register( 139 'colors', 140 array( 141 'register_attribute' => 'wp_register_colors_support', 142 'apply' => 'wp_apply_colors_support', 143 ) 144 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Nov 23 08:20:01 2024 | Cross-referenced by PHPXref |