[ 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_link_colors_support       = _wp_array_get( $color_support, array( 'link' ), false );
  87      $has_gradients_support         = _wp_array_get( $color_support, array( 'gradients' ), false );
  88      $classes                       = array();
  89      $styles                        = array();
  90  
  91      // Text Colors.
  92      // Check support for text colors.
  93      if ( $has_text_colors_support ) {
  94          $has_named_text_color  = array_key_exists( 'textColor', $block_attributes );
  95          $has_custom_text_color = isset( $block_attributes['style']['color']['text'] );
  96  
  97          // Apply required generic class.
  98          if ( $has_custom_text_color || $has_named_text_color ) {
  99              $classes[] = 'has-text-color';
 100          }
 101          // Apply color class or inline style.
 102          if ( $has_named_text_color ) {
 103              $classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
 104          } elseif ( $has_custom_text_color ) {
 105              $styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
 106          }
 107      }
 108  
 109      // Link Colors.
 110      if ( $has_link_colors_support ) {
 111          $has_link_color = isset( $block_attributes['style']['color']['link'] );
 112          // Apply required class and style.
 113          if ( $has_link_color ) {
 114              $classes[] = 'has-link-color';
 115              // If link is a named color.
 116              if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) {
 117                  // Get the name from the string and add proper styles.
 118                  $index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1;
 119                  $link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice );
 120                  $styles[]        = sprintf( '--wp--style--color--link: var(--wp--preset--color--%s);', $link_color_name );
 121              } else {
 122                  $styles[] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] );
 123              }
 124          }
 125      }
 126  
 127      // Background Colors.
 128      if ( $has_background_colors_support ) {
 129          $has_named_background_color  = array_key_exists( 'backgroundColor', $block_attributes );
 130          $has_custom_background_color = isset( $block_attributes['style']['color']['background'] );
 131  
 132          // Apply required background class.
 133          if ( $has_custom_background_color || $has_named_background_color ) {
 134              $classes[] = 'has-background';
 135          }
 136          // Apply background color classes or styles.
 137          if ( $has_named_background_color ) {
 138              $classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
 139          } elseif ( $has_custom_background_color ) {
 140              $styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
 141          }
 142      }
 143  
 144      // Gradients.
 145      if ( $has_gradients_support ) {
 146          $has_named_gradient  = array_key_exists( 'gradient', $block_attributes );
 147          $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );
 148  
 149          if ( $has_named_gradient || $has_custom_gradient ) {
 150              $classes[] = 'has-background';
 151          }
 152          // Apply required background class.
 153          if ( $has_named_gradient ) {
 154              $classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
 155          } elseif ( $has_custom_gradient ) {
 156              $styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
 157          }
 158      }
 159  
 160      $attributes = array();
 161      if ( ! empty( $classes ) ) {
 162          $attributes['class'] = implode( ' ', $classes );
 163      }
 164      if ( ! empty( $styles ) ) {
 165          $attributes['style'] = implode( ' ', $styles );
 166      }
 167  
 168      return $attributes;
 169  }
 170  
 171  // Register the block support.
 172  WP_Block_Supports::get_instance()->register(
 173      'colors',
 174      array(
 175          'register_attribute' => 'wp_register_colors_support',
 176          'apply'              => 'wp_apply_colors_support',
 177      )
 178  );


Generated: Wed Apr 21 01:00:05 2021 Cross-referenced by PHPXref 0.7.1