[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/block-supports/ -> colors.php (source)

   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   * @access private
  14   *
  15   * @param WP_Block_Type $block_type Block Type.
  16   */
  17  function wp_register_colors_support( $block_type ) {
  18      $color_support = false;
  19      if ( property_exists( $block_type, 'supports' ) ) {
  20          $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
  21      }
  22      $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
  23      $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
  24      $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
  25      $has_link_colors_support       = _wp_array_get( $color_support, array( 'link' ), false );
  26      $has_color_support             = $has_text_colors_support ||
  27          $has_background_colors_support ||
  28          $has_gradients_support ||
  29          $has_link_colors_support;
  30  
  31      if ( ! $block_type->attributes ) {
  32          $block_type->attributes = array();
  33      }
  34  
  35      if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
  36          $block_type->attributes['style'] = array(
  37              'type' => 'object',
  38          );
  39      }
  40  
  41      if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) {
  42          $block_type->attributes['backgroundColor'] = array(
  43              'type' => 'string',
  44          );
  45      }
  46  
  47      if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) {
  48          $block_type->attributes['textColor'] = array(
  49              'type' => 'string',
  50          );
  51      }
  52  
  53      if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) {
  54          $block_type->attributes['gradient'] = array(
  55              'type' => 'string',
  56          );
  57      }
  58  }
  59  
  60  
  61  /**
  62   * Add CSS classes and inline styles for colors to the incoming attributes array.
  63   * This will be applied to the block markup in the front-end.
  64   *
  65   * @since 5.6.0
  66   * @access private
  67   *
  68   * @param  WP_Block_Type $block_type       Block type.
  69   * @param  array         $block_attributes Block attributes.
  70   *
  71   * @return array Colors CSS classes and inline styles.
  72   */
  73  function wp_apply_colors_support( $block_type, $block_attributes ) {
  74      $color_support = _wp_array_get( $block_type->supports, array( 'color' ), false );
  75  
  76      if (
  77          is_array( $color_support ) &&
  78          wp_should_skip_block_supports_serialization( $block_type, 'color' )
  79      ) {
  80          return array();
  81      }
  82  
  83      $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
  84      $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
  85      $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
  86      $classes                       = array();
  87      $styles                        = array();
  88  
  89      // Text colors.
  90      // Check support for text colors.
  91      if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
  92          $has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
  93          $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
  94  
  95          // Apply required generic class.
  96          if ( $has_custom_text_color || $has_named_text_color ) {
  97              $classes[] = 'has-text-color';
  98          }
  99          // Apply color class or inline style.
 100          if ( $has_named_text_color ) {
 101              $classes[] = sprintf( 'has-%s-color', _wp_to_kebab_case( $block_attributes['textColor'] ) );
 102          } elseif ( $has_custom_text_color ) {
 103              $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
 104          }
 105      }
 106  
 107      // Background colors.
 108      if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
 109          $has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
 110          $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
 111  
 112          // Apply required background class.
 113          if ( $has_custom_background_color || $has_named_background_color ) {
 114              $classes[] = 'has-background';
 115          }
 116          // Apply background color classes or styles.
 117          if ( $has_named_background_color ) {
 118              $classes[] = sprintf( 'has-%s-background-color', _wp_to_kebab_case( $block_attributes['backgroundColor'] ) );
 119          } elseif ( $has_custom_background_color ) {
 120              $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
 121          }
 122      }
 123  
 124      // Gradients.
 125      if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
 126          $has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
 127          $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
 128  
 129          if ( $has_named_gradient || $has_custom_gradient ) {
 130              $classes[] = 'has-background';
 131          }
 132          // Apply required background class.
 133          if ( $has_named_gradient ) {
 134              $classes[] = sprintf( 'has-%s-gradient-background', _wp_to_kebab_case( $block_attributes['gradient'] ) );
 135          } elseif ( $has_custom_gradient ) {
 136              $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
 137          }
 138      }
 139  
 140      $attributes = array();
 141      if ( ! empty( $classes ) ) {
 142          $attributes['class'] = implode( ' ', $classes );
 143      }
 144      if ( ! empty( $styles ) ) {
 145          $attributes['style'] = implode( ' ', $styles );
 146      }
 147  
 148      return $attributes;
 149  }
 150  
 151  // Register the block support.
 152  WP_Block_Supports::get_instance()->register(
 153      'colors',
 154      array(
 155          'register_attribute' => 'wp_register_colors_support',
 156          'apply'              => 'wp_apply_colors_support',
 157      )
 158  );


Generated: Fri Apr 19 01:00:02 2024 Cross-referenced by PHPXref 0.7.1