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


Generated: Fri Dec 4 01:00:03 2020 Cross-referenced by PHPXref 0.7.1