wp/wp-includes/block-supports/colors.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    73 function wp_apply_colors_support( $block_type, $block_attributes ) {
    73 function wp_apply_colors_support( $block_type, $block_attributes ) {
    74 	$color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
    74 	$color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
    75 
    75 
    76 	if (
    76 	if (
    77 		is_array( $color_support ) &&
    77 		is_array( $color_support ) &&
    78 		array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
    78 		wp_should_skip_block_supports_serialization( $block_type, 'color' )
    79 		$color_support['__experimentalSkipSerialization']
       
    80 	) {
    79 	) {
    81 		return array();
    80 		return array();
    82 	}
    81 	}
    83 
    82 
    84 	$has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
    83 	$has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
    87 	$classes                       = array();
    86 	$classes                       = array();
    88 	$styles                        = array();
    87 	$styles                        = array();
    89 
    88 
    90 	// Text colors.
    89 	// Text colors.
    91 	// Check support for text colors.
    90 	// Check support for text colors.
    92 	if ( $has_text_colors_support ) {
    91 	if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
    93 		$has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
    92 		$has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
    94 		$has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
    93 		$has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
    95 
    94 
    96 		// Apply required generic class.
    95 		// Apply required generic class.
    97 		if ( $has_custom_text_color || $has_named_text_color ) {
    96 		if ( $has_custom_text_color || $has_named_text_color ) {
    98 			$classes[] = 'has-text-color';
    97 			$classes[] = 'has-text-color';
    99 		}
    98 		}
   100 		// Apply color class or inline style.
    99 		// Apply color class or inline style.
   101 		if ( $has_named_text_color ) {
   100 		if ( $has_named_text_color ) {
   102 			$classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
   101 			$classes[] = sprintf( 'has-%s-color', _wp_to_kebab_case( $block_attributes['textColor'] ) );
   103 		} elseif ( $has_custom_text_color ) {
   102 		} elseif ( $has_custom_text_color ) {
   104 			$styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
   103 			$styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
   105 		}
   104 		}
   106 	}
   105 	}
   107 
   106 
   108 	// Background colors.
   107 	// Background colors.
   109 	if ( $has_background_colors_support ) {
   108 	if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
   110 		$has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
   109 		$has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
   111 		$has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
   110 		$has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
   112 
   111 
   113 		// Apply required background class.
   112 		// Apply required background class.
   114 		if ( $has_custom_background_color || $has_named_background_color ) {
   113 		if ( $has_custom_background_color || $has_named_background_color ) {
   115 			$classes[] = 'has-background';
   114 			$classes[] = 'has-background';
   116 		}
   115 		}
   117 		// Apply background color classes or styles.
   116 		// Apply background color classes or styles.
   118 		if ( $has_named_background_color ) {
   117 		if ( $has_named_background_color ) {
   119 			$classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
   118 			$classes[] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $block_attributes['backgroundColor'] ) );
   120 		} elseif ( $has_custom_background_color ) {
   119 		} elseif ( $has_custom_background_color ) {
   121 			$styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
   120 			$styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
   122 		}
   121 		}
   123 	}
   122 	}
   124 
   123 
   125 	// Gradients.
   124 	// Gradients.
   126 	if ( $has_gradients_support ) {
   125 	if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
   127 		$has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
   126 		$has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
   128 		$has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
   127 		$has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
   129 
   128 
   130 		if ( $has_named_gradient || $has_custom_gradient ) {
   129 		if ( $has_named_gradient || $has_custom_gradient ) {
   131 			$classes[] = 'has-background';
   130 			$classes[] = 'has-background';
   132 		}
   131 		}
   133 		// Apply required background class.
   132 		// Apply required background class.
   134 		if ( $has_named_gradient ) {
   133 		if ( $has_named_gradient ) {
   135 			$classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
   134 			$classes[] = sprintf( 'has-%s-gradient-background', _wp_to_kebab_case( $block_attributes['gradient'] ) );
   136 		} elseif ( $has_custom_gradient ) {
   135 		} elseif ( $has_custom_gradient ) {
   137 			$styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
   136 			$styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
   138 		}
   137 		}
   139 	}
   138 	}
   140 
   139