[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Typography block support flag.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Registers the style and typography 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_typography_support( $block_type ) {
  16      $has_font_size_support = false;
  17      if ( property_exists( $block_type, 'supports' ) ) {
  18          $has_font_size_support = _wp_array_get( $block_type->supports, array( '__experimentalFontSize' ), false );
  19      }
  20  
  21      $has_line_height_support = false;
  22      if ( property_exists( $block_type, 'supports' ) ) {
  23          $has_line_height_support = _wp_array_get( $block_type->supports, array( '__experimentalLineHeight' ), false );
  24      }
  25  
  26      if ( ! $block_type->attributes ) {
  27          $block_type->attributes = array();
  28      }
  29  
  30      if ( ( $has_font_size_support || $has_line_height_support ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
  31          $block_type->attributes['style'] = array(
  32              'type' => 'object',
  33          );
  34      }
  35  
  36      if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) {
  37          $block_type->attributes['fontSize'] = array(
  38              'type' => 'string',
  39          );
  40      }
  41  }
  42  
  43  /**
  44   * Add CSS classes and inline styles for font sizes to the incoming attributes array.
  45   * This will be applied to the block markup in the front-end.
  46   *
  47   * @access private
  48   *
  49   * @param  WP_Block_Type $block_type       Block type.
  50   * @param  array         $block_attributes Block attributes.
  51   *
  52   * @return array Font size CSS classes and inline styles.
  53   */
  54  function wp_apply_typography_support( $block_type, $block_attributes ) {
  55      $has_font_size_support = false;
  56      $classes               = array();
  57      $styles                = array();
  58      if ( property_exists( $block_type, 'supports' ) ) {
  59          $has_font_size_support = _wp_array_get( $block_type->supports, array( 'fontSize' ), false );
  60      }
  61  
  62      $has_line_height_support = false;
  63      if ( property_exists( $block_type, 'supports' ) ) {
  64          $has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
  65      }
  66  
  67      // Font Size.
  68      if ( $has_font_size_support ) {
  69          $has_named_font_size  = array_key_exists( 'fontSize', $block_attributes );
  70          $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
  71  
  72          // Apply required class or style.
  73          if ( $has_named_font_size ) {
  74              $classes[] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
  75          } elseif ( $has_custom_font_size ) {
  76              $styles[] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] );
  77          }
  78      }
  79  
  80      // Line Height.
  81      if ( $has_line_height_support ) {
  82          $has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
  83          // Add the style (no classes for line-height).
  84          if ( $has_line_height ) {
  85              $styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
  86          }
  87      }
  88  
  89      $attributes = array();
  90      if ( ! empty( $classes ) ) {
  91          $attributes['class'] = implode( ' ', $classes );
  92      }
  93      if ( ! empty( $styles ) ) {
  94          $attributes['style'] = implode( ' ', $styles );
  95      }
  96  
  97      return $attributes;
  98  }
  99  
 100  WP_Block_Supports::get_instance()->register(
 101      'typography',
 102      array(
 103          'register_attribute' => 'wp_register_typography_support',
 104          'apply'              => 'wp_apply_typography_support',
 105      )
 106  );


Generated: Wed Dec 2 01:00:04 2020 Cross-referenced by PHPXref 0.7.1