[ 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          array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
  79          $color_support['__experimentalSkipSerialization']
  80      ) {
  81          return array();
  82      }
  83  
  84      $has_text_colors_support       = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'text' ), true ) );
  85      $has_background_colors_support = true === $color_support || ( is_array( $color_support ) && _wp_array_get( $color_support, array( 'background' ), true ) );
  86      $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
  87      $classes                       = array();
  88      $styles                        = array();
  89  
  90      // Text colors.
  91      // Check support for text colors.
  92      if ( $has_text_colors_support ) {
  93          $has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
  94          $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
  95  
  96          // Apply required generic class.
  97          if ( $has_custom_text_color || $has_named_text_color ) {
  98              $classes[] = 'has-text-color';
  99          }
 100          // Apply color class or inline style.
 101          if ( $has_named_text_color ) {
 102              $classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
 103          } elseif ( $has_custom_text_color ) {
 104              $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
 105          }
 106      }
 107  
 108      // Background colors.
 109      if ( $has_background_colors_support ) {
 110          $has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
 111          $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
 112  
 113          // Apply required background class.
 114          if ( $has_custom_background_color || $has_named_background_color ) {
 115              $classes[] = 'has-background';
 116          }
 117          // Apply background color classes or styles.
 118          if ( $has_named_background_color ) {
 119              $classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
 120          } elseif ( $has_custom_background_color ) {
 121              $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
 122          }
 123      }
 124  
 125      // Gradients.
 126      if ( $has_gradients_support ) {
 127          $has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
 128          $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
 129  
 130          if ( $has_named_gradient || $has_custom_gradient ) {
 131              $classes[] = 'has-background';
 132          }
 133          // Apply required background class.
 134          if ( $has_named_gradient ) {
 135              $classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
 136          } elseif ( $has_custom_gradient ) {
 137              $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
 138          }
 139      }
 140  
 141      $attributes = array();
 142      if ( ! empty( $classes ) ) {
 143          $attributes['class'] = implode( ' ', $classes );
 144      }
 145      if ( ! empty( $styles ) ) {
 146          $attributes['style'] = implode( ' ', $styles );
 147      }
 148  
 149      return $attributes;
 150  }
 151  
 152  // Register the block support.
 153  WP_Block_Supports::get_instance()->register(
 154      'colors',
 155      array(
 156          'register_attribute' => 'wp_register_colors_support',
 157          'apply'              => 'wp_apply_colors_support',
 158      )
 159  );


Generated: Tue Sep 21 01:00:05 2021 Cross-referenced by PHPXref 0.7.1