| 1 |
<?php |
| 2 |
/** |
| 3 |
* Colors block support flag. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers the style and colors block attributes for block types that support it. |
| 10 |
* |
| 11 |
* @param WP_Block_Type $block_type Block Type. |
| 12 |
*/ |
| 13 |
function gutenberg_register_colors_support( $block_type ) { |
| 14 |
$color_support = false; |
| 15 |
if ( $block_type instanceof WP_Block_Type ) { |
| 16 |
$color_support = $block_type->supports['color'] ?? false; |
| 17 |
} |
| 18 |
$has_text_colors_support = true === $color_support || |
| 19 |
( isset( $color_support['text'] ) && $color_support['text'] ) || |
| 20 |
( is_array( $color_support ) && ! isset( $color_support['text'] ) ); |
| 21 |
$has_background_colors_support = true === $color_support || |
| 22 |
( isset( $color_support['background'] ) && $color_support['background'] ) || |
| 23 |
( is_array( $color_support ) && ! isset( $color_support['background'] ) ); |
| 24 |
$has_gradients_support = $color_support['gradients'] ?? false; |
| 25 |
$has_link_colors_support = $color_support['link'] ?? false; |
| 26 |
$has_button_colors_support = $color_support['button'] ?? false; |
| 27 |
$has_heading_colors_support = $color_support['heading'] ?? false; |
| 28 |
$has_color_support = $has_text_colors_support || |
| 29 |
$has_background_colors_support || |
| 30 |
$has_gradients_support || |
| 31 |
$has_link_colors_support || |
| 32 |
$has_button_colors_support || |
| 33 |
$has_heading_colors_support; |
| 34 |
|
| 35 |
if ( ! $block_type->attributes ) { |
| 36 |
$block_type->attributes = array(); |
| 37 |
} |
| 38 |
|
| 39 |
if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) { |
| 40 |
$block_type->attributes['style'] = array( |
| 41 |
'type' => 'object', |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) { |
| 46 |
$block_type->attributes['backgroundColor'] = array( |
| 47 |
'type' => 'string', |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) { |
| 52 |
$block_type->attributes['textColor'] = array( |
| 53 |
'type' => 'string', |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) { |
| 58 |
$block_type->attributes['gradient'] = array( |
| 59 |
'type' => 'string', |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
/** |
| 66 |
* Add CSS classes and inline styles for colors to the incoming attributes array. |
| 67 |
* This will be applied to the block markup in the front-end. |
| 68 |
* |
| 69 |
* @param WP_Block_Type $block_type Block type. |
| 70 |
* @param array $block_attributes Block attributes. |
| 71 |
* |
| 72 |
* @return array Colors CSS classes and inline styles. |
| 73 |
*/ |
| 74 |
function gutenberg_apply_colors_support( $block_type, $block_attributes ) { |
| 75 |
$color_support = $block_type->supports['color'] ?? false; |
| 76 |
|
| 77 |
if ( |
| 78 |
is_array( $color_support ) && |
| 79 |
wp_should_skip_block_supports_serialization( $block_type, 'color' ) |
| 80 |
) { |
| 81 |
return array(); |
| 82 |
} |
| 83 |
|
| 84 |
$has_text_colors_support = true === $color_support || |
| 85 |
( isset( $color_support['text'] ) && $color_support['text'] ) || |
| 86 |
( is_array( $color_support ) && ! isset( $color_support['text'] ) ); |
| 87 |
$has_background_colors_support = true === $color_support || |
| 88 |
( isset( $color_support['background'] ) && $color_support['background'] ) || |
| 89 |
( is_array( $color_support ) && ! isset( $color_support['background'] ) ); |
| 90 |
$has_gradients_support = $color_support['gradients'] ?? false; |
| 91 |
$color_block_styles = array(); |
| 92 |
|
| 93 |
// Text colors. |
| 94 |
// Check support for text colors. |
| 95 |
if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { |
| 96 |
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null; |
| 97 |
$custom_text_color = $block_attributes['style']['color']['text'] ?? null; |
| 98 |
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color; |
| 99 |
} |
| 100 |
|
| 101 |
// Background colors. |
| 102 |
if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { |
| 103 |
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null; |
| 104 |
$custom_background_color = $block_attributes['style']['color']['background'] ?? null; |
| 105 |
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color; |
| 106 |
} |
| 107 |
|
| 108 |
// Gradients. |
| 109 |
// Suppress color.gradient CSS when background.gradient is supported and |
| 110 |
// explicitly set. background.php owns CSS generation in that case, and |
| 111 |
// emitting the background shorthand here would conflict with it. |
| 112 |
$has_background_gradient_support = block_has_support( $block_type, array( 'background', 'gradient' ), false ); |
| 113 |
$has_background_gradient_value = ! empty( $block_attributes['style']['background']['gradient'] ); |
| 114 |
|
| 115 |
if ( |
| 116 |
$has_gradients_support && |
| 117 |
! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) && |
| 118 |
! ( $has_background_gradient_support && $has_background_gradient_value ) |
| 119 |
) { |
| 120 |
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null; |
| 121 |
$custom_gradient_color = $block_attributes['style']['color']['gradient'] ?? null; |
| 122 |
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color; |
| 123 |
} |
| 124 |
|
| 125 |
$attributes = array(); |
| 126 |
$styles = gutenberg_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) ); |
| 127 |
|
| 128 |
if ( ! empty( $styles['classnames'] ) ) { |
| 129 |
$attributes['class'] = $styles['classnames']; |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! empty( $styles['css'] ) ) { |
| 133 |
$attributes['style'] = $styles['css']; |
| 134 |
} |
| 135 |
|
| 136 |
return $attributes; |
| 137 |
} |
| 138 |
|
| 139 |
// Register the block support. |
| 140 |
WP_Block_Supports::get_instance()->register( |
| 141 |
'colors', |
| 142 |
array( |
| 143 |
'register_attribute' => 'gutenberg_register_colors_support', |
| 144 |
'apply' => 'gutenberg_apply_colors_support', |
| 145 |
) |
| 146 |
); |
| 147 |
|