wp/wp-includes/block-supports/typography.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    26 
    26 
    27 	$has_font_family_support     = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
    27 	$has_font_family_support     = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
    28 	$has_font_size_support       = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
    28 	$has_font_size_support       = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
    29 	$has_font_style_support      = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
    29 	$has_font_style_support      = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
    30 	$has_font_weight_support     = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
    30 	$has_font_weight_support     = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
       
    31 	$has_letter_spacing_support  = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false );
    31 	$has_line_height_support     = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
    32 	$has_line_height_support     = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
    32 	$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
    33 	$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
    33 	$has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    34 	$has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    34 
    35 
    35 	$has_typography_support = $has_font_family_support
    36 	$has_typography_support = $has_font_family_support
    36 		|| $has_font_size_support
    37 		|| $has_font_size_support
    37 		|| $has_font_style_support
    38 		|| $has_font_style_support
    38 		|| $has_font_weight_support
    39 		|| $has_font_weight_support
       
    40 		|| $has_letter_spacing_support
    39 		|| $has_line_height_support
    41 		|| $has_line_height_support
    40 		|| $has_text_decoration_support
    42 		|| $has_text_decoration_support
    41 		|| $has_text_transform_support;
    43 		|| $has_text_transform_support;
    42 
    44 
    43 	if ( ! $block_type->attributes ) {
    45 	if ( ! $block_type->attributes ) {
    56 		);
    58 		);
    57 	}
    59 	}
    58 }
    60 }
    59 
    61 
    60 /**
    62 /**
    61  * Add CSS classes and inline styles for typography features such as font sizes
    63  * Adds CSS classes and inline styles for typography features such as font sizes
    62  * to the incoming attributes array. This will be applied to the block markup in
    64  * to the incoming attributes array. This will be applied to the block markup in
    63  * the front-end.
    65  * the front-end.
    64  *
    66  *
    65  * @since 5.6.0
    67  * @since 5.6.0
    66  * @access private
    68  * @access private
    67  *
    69  *
    68  * @param  WP_Block_Type $block_type       Block type.
    70  * @param WP_Block_Type $block_type       Block type.
    69  * @param  array         $block_attributes Block attributes.
    71  * @param array         $block_attributes Block attributes.
    70  *
       
    71  * @return array Typography CSS classes and inline styles.
    72  * @return array Typography CSS classes and inline styles.
    72  */
    73  */
    73 function wp_apply_typography_support( $block_type, $block_attributes ) {
    74 function wp_apply_typography_support( $block_type, $block_attributes ) {
    74 	if ( ! property_exists( $block_type, 'supports' ) ) {
    75 	if ( ! property_exists( $block_type, 'supports' ) ) {
    75 		return array();
    76 		return array();
    78 	$typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
    79 	$typography_supports = _wp_array_get( $block_type->supports, array( 'typography' ), false );
    79 	if ( ! $typography_supports ) {
    80 	if ( ! $typography_supports ) {
    80 		return array();
    81 		return array();
    81 	}
    82 	}
    82 
    83 
    83 	$skip_typography_serialization = _wp_array_get( $typography_supports, array( '__experimentalSkipSerialization' ), false );
    84 	if ( wp_should_skip_block_supports_serialization( $block_type, 'typography' ) ) {
    84 	if ( $skip_typography_serialization ) {
       
    85 		return array();
    85 		return array();
    86 	}
    86 	}
    87 
    87 
    88 	$attributes = array();
    88 	$attributes = array();
    89 	$classes    = array();
    89 	$classes    = array();
    91 
    91 
    92 	$has_font_family_support     = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
    92 	$has_font_family_support     = _wp_array_get( $typography_supports, array( '__experimentalFontFamily' ), false );
    93 	$has_font_size_support       = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
    93 	$has_font_size_support       = _wp_array_get( $typography_supports, array( 'fontSize' ), false );
    94 	$has_font_style_support      = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
    94 	$has_font_style_support      = _wp_array_get( $typography_supports, array( '__experimentalFontStyle' ), false );
    95 	$has_font_weight_support     = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
    95 	$has_font_weight_support     = _wp_array_get( $typography_supports, array( '__experimentalFontWeight' ), false );
       
    96 	$has_letter_spacing_support  = _wp_array_get( $typography_supports, array( '__experimentalLetterSpacing' ), false );
    96 	$has_line_height_support     = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
    97 	$has_line_height_support     = _wp_array_get( $typography_supports, array( 'lineHeight' ), false );
    97 	$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
    98 	$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
    98 	$has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    99 	$has_text_transform_support  = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );
    99 
   100 
   100 	if ( $has_font_size_support ) {
   101 	if ( $has_font_size_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' ) ) {
   101 		$has_named_font_size  = array_key_exists( 'fontSize', $block_attributes );
   102 		$has_named_font_size  = array_key_exists( 'fontSize', $block_attributes );
   102 		$has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
   103 		$has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
   103 
   104 
   104 		if ( $has_named_font_size ) {
   105 		if ( $has_named_font_size ) {
   105 			$classes[] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
   106 			$classes[] = sprintf( 'has-%s-font-size', _wp_to_kebab_case( $block_attributes['fontSize'] ) );
   106 		} elseif ( $has_custom_font_size ) {
   107 		} elseif ( $has_custom_font_size ) {
   107 			$styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] );
   108 			$styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] );
   108 		}
   109 		}
   109 	}
   110 	}
   110 
   111 
   111 	if ( $has_font_family_support ) {
   112 	if ( $has_font_family_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' ) ) {
   112 		$has_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );
   113 		$has_named_font_family  = array_key_exists( 'fontFamily', $block_attributes );
   113 		if ( $has_font_family ) {
   114 		$has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );
   114 			$font_family = $block_attributes['style']['typography']['fontFamily'];
   115 
   115 			if ( strpos( $font_family, 'var:preset|font-family' ) !== false ) {
   116 		if ( $has_named_font_family ) {
   116 				// Get the name from the string and add proper styles.
   117 			$classes[] = sprintf( 'has-%s-font-family', _wp_to_kebab_case( $block_attributes['fontFamily'] ) );
   117 				$index_to_splice  = strrpos( $font_family, '|' ) + 1;
   118 		} elseif ( $has_custom_font_family ) {
   118 				$font_family_name = substr( $font_family, $index_to_splice );
   119 			// Before using classes, the value was serialized as a CSS Custom Property.
   119 				$styles[]         = sprintf( 'font-family: var(--wp--preset--font-family--%s);', $font_family_name );
   120 			// We don't need this code path when it lands in core.
   120 			} else {
   121 			$font_family_custom = $block_attributes['style']['typography']['fontFamily'];
   121 				$styles[] = sprintf( 'font-family: %s;', $block_attributes['style']['typography']['fontFamily'] );
   122 			if ( strpos( $font_family_custom, 'var:preset|font-family' ) !== false ) {
       
   123 				$index_to_splice    = strrpos( $font_family_custom, '|' ) + 1;
       
   124 				$font_family_slug   = _wp_to_kebab_case( substr( $font_family_custom, $index_to_splice ) );
       
   125 				$font_family_custom = sprintf( 'var(--wp--preset--font-family--%s)', $font_family_slug );
   122 			}
   126 			}
   123 		}
   127 			$styles[] = sprintf( 'font-family: %s;', $font_family_custom );
   124 	}
   128 		}
   125 
   129 	}
   126 	if ( $has_font_style_support ) {
   130 
       
   131 	if ( $has_font_style_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontStyle' ) ) {
   127 		$font_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' );
   132 		$font_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' );
   128 		if ( $font_style ) {
   133 		if ( $font_style ) {
   129 			$styles[] = $font_style;
   134 			$styles[] = $font_style;
   130 		}
   135 		}
   131 	}
   136 	}
   132 
   137 
   133 	if ( $has_font_weight_support ) {
   138 	if ( $has_font_weight_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'fontWeight' ) ) {
   134 		$font_weight = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' );
   139 		$font_weight = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' );
   135 		if ( $font_weight ) {
   140 		if ( $font_weight ) {
   136 			$styles[] = $font_weight;
   141 			$styles[] = $font_weight;
   137 		}
   142 		}
   138 	}
   143 	}
   139 
   144 
   140 	if ( $has_line_height_support ) {
   145 	if ( $has_line_height_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'lineHeight' ) ) {
   141 		$has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
   146 		$has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
   142 		if ( $has_line_height ) {
   147 		if ( $has_line_height ) {
   143 			$styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
   148 			$styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
   144 		}
   149 		}
   145 	}
   150 	}
   146 
   151 
   147 	if ( $has_text_decoration_support ) {
   152 	if ( $has_text_decoration_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textDecoration' ) ) {
   148 		$text_decoration_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' );
   153 		$text_decoration_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' );
   149 		if ( $text_decoration_style ) {
   154 		if ( $text_decoration_style ) {
   150 			$styles[] = $text_decoration_style;
   155 			$styles[] = $text_decoration_style;
   151 		}
   156 		}
   152 	}
   157 	}
   153 
   158 
   154 	if ( $has_text_transform_support ) {
   159 	if ( $has_text_transform_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'textTransform' ) ) {
   155 		$text_transform_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
   160 		$text_transform_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
   156 		if ( $text_transform_style ) {
   161 		if ( $text_transform_style ) {
   157 			$styles[] = $text_transform_style;
   162 			$styles[] = $text_transform_style;
   158 		}
   163 		}
   159 	}
   164 	}
   160 
   165 
       
   166 	if ( $has_letter_spacing_support && ! wp_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' ) ) {
       
   167 		$letter_spacing_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' );
       
   168 		if ( $letter_spacing_style ) {
       
   169 			$styles[] = $letter_spacing_style;
       
   170 		}
       
   171 	}
       
   172 
   161 	if ( ! empty( $classes ) ) {
   173 	if ( ! empty( $classes ) ) {
   162 		$attributes['class'] = implode( ' ', $classes );
   174 		$attributes['class'] = implode( ' ', $classes );
   163 	}
   175 	}
   164 	if ( ! empty( $styles ) ) {
   176 	if ( ! empty( $styles ) ) {
   165 		$attributes['style'] = implode( ' ', $styles );
   177 		$attributes['style'] = implode( ' ', $styles );
   176  * @access private
   188  * @access private
   177  *
   189  *
   178  * @param array  $attributes   Block's attributes.
   190  * @param array  $attributes   Block's attributes.
   179  * @param string $feature      Key for the feature within the typography styles.
   191  * @param string $feature      Key for the feature within the typography styles.
   180  * @param string $css_property Slug for the CSS property the inline style sets.
   192  * @param string $css_property Slug for the CSS property the inline style sets.
   181  *
   193  * @return string CSS inline style.
   182  * @return string              CSS inline style.
       
   183  */
   194  */
   184 function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
   195 function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
   185 	// Retrieve current attribute value or skip if not found.
   196 	// Retrieve current attribute value or skip if not found.
   186 	$style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
   197 	$style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
   187 	if ( ! $style_value ) {
   198 	if ( ! $style_value ) {