2020-10-13 09:10:30 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Typography block support flag.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
2021-04-15 11:19:43 -04:00
|
|
|
* @since 5.6.0
|
2020-10-13 09:10:30 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the style and typography block attributes for block types that support it.
|
|
|
|
*
|
2021-04-15 11:19:43 -04:00
|
|
|
* @since 5.6.0
|
2020-10-20 09:36:16 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2020-10-13 09:10:30 -04:00
|
|
|
* @param WP_Block_Type $block_type Block Type.
|
|
|
|
*/
|
|
|
|
function wp_register_typography_support( $block_type ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
if ( ! property_exists( $block_type, 'supports' ) ) {
|
|
|
|
return;
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
$has_font_size_support = _wp_array_get( $block_type->supports, array( 'fontSize' ), false );
|
|
|
|
$has_font_style_support = _wp_array_get( $block_type->supports, array( '__experimentalFontStyle' ), false );
|
|
|
|
$has_font_weight_support = _wp_array_get( $block_type->supports, array( '__experimentalFontWeight' ), false );
|
|
|
|
$has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
|
|
|
|
$has_text_decoration_support = _wp_array_get( $block_type->supports, array( '__experimentalTextDecoration' ), false );
|
|
|
|
$has_text_transform_support = _wp_array_get( $block_type->supports, array( '__experimentalTextTransform' ), false );
|
|
|
|
|
|
|
|
$has_typography_support = $has_font_size_support
|
|
|
|
|| $has_font_weight_support
|
|
|
|
|| $has_font_style_support
|
|
|
|
|| $has_line_height_support
|
|
|
|
|| $has_text_transform_support
|
|
|
|
|| $has_text_decoration_support;
|
2020-10-13 09:10:30 -04:00
|
|
|
|
|
|
|
if ( ! $block_type->attributes ) {
|
|
|
|
$block_type->attributes = array();
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
if ( $has_typography_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
2020-10-13 09:10:30 -04:00
|
|
|
$block_type->attributes['style'] = array(
|
|
|
|
'type' => 'object',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) {
|
|
|
|
$block_type->attributes['fontSize'] = array(
|
|
|
|
'type' => 'string',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-15 11:19:43 -04:00
|
|
|
* Add CSS classes and inline styles for typography features such as font sizes
|
|
|
|
* to the incoming attributes array. This will be applied to the block markup in
|
|
|
|
* the front-end.
|
2020-10-13 09:10:30 -04:00
|
|
|
*
|
2021-04-15 11:19:43 -04:00
|
|
|
* @since 5.6.0
|
2020-10-20 09:36:16 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2020-10-13 09:10:30 -04:00
|
|
|
* @param WP_Block_Type $block_type Block type.
|
2020-10-20 09:36:16 -04:00
|
|
|
* @param array $block_attributes Block attributes.
|
2020-10-13 09:10:30 -04:00
|
|
|
*
|
2021-04-15 11:19:43 -04:00
|
|
|
* @return array Typography CSS classes and inline styles.
|
2020-10-13 09:10:30 -04:00
|
|
|
*/
|
2020-10-20 09:36:16 -04:00
|
|
|
function wp_apply_typography_support( $block_type, $block_attributes ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
if ( ! property_exists( $block_type, 'supports' ) ) {
|
|
|
|
return array();
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
|
|
|
|
2021-05-21 06:14:23 -04:00
|
|
|
$attributes = array();
|
|
|
|
$classes = array();
|
|
|
|
$styles = array();
|
2021-04-15 11:19:43 -04:00
|
|
|
|
|
|
|
$has_font_family_support = _wp_array_get( $block_type->supports, array( '__experimentalFontFamily' ), false );
|
|
|
|
$has_font_style_support = _wp_array_get( $block_type->supports, array( '__experimentalFontStyle' ), false );
|
|
|
|
$has_font_weight_support = _wp_array_get( $block_type->supports, array( '__experimentalFontWeight' ), false );
|
|
|
|
$has_font_size_support = _wp_array_get( $block_type->supports, array( 'fontSize' ), false );
|
|
|
|
$has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
|
|
|
|
$has_text_decoration_support = _wp_array_get( $block_type->supports, array( '__experimentalTextDecoration' ), false );
|
|
|
|
$has_text_transform_support = _wp_array_get( $block_type->supports, array( '__experimentalTextTransform' ), false );
|
2020-10-13 09:10:30 -04:00
|
|
|
|
2021-05-19 11:09:27 -04:00
|
|
|
$skip_font_size_support_serialization = _wp_array_get( $block_type->supports, array( '__experimentalSkipFontSizeSerialization' ), false );
|
|
|
|
|
2021-05-21 06:14:23 -04:00
|
|
|
// Covers all typography features _except_ font size.
|
|
|
|
$skip_typography_serialization = _wp_array_get( $block_type->supports, array( '__experimentalSkipTypographySerialization' ), false );
|
|
|
|
|
2020-10-13 09:10:30 -04:00
|
|
|
// Font Size.
|
2021-05-19 11:09:27 -04:00
|
|
|
if ( $has_font_size_support && ! $skip_font_size_support_serialization ) {
|
2020-10-13 09:10:30 -04:00
|
|
|
$has_named_font_size = array_key_exists( 'fontSize', $block_attributes );
|
|
|
|
$has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );
|
|
|
|
|
|
|
|
// Apply required class or style.
|
|
|
|
if ( $has_named_font_size ) {
|
2020-10-20 09:36:16 -04:00
|
|
|
$classes[] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
|
2020-10-13 09:10:30 -04:00
|
|
|
} elseif ( $has_custom_font_size ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
$styles[] = sprintf( 'font-size: %s;', $block_attributes['style']['typography']['fontSize'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Font Family.
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( $has_font_family_support && ! $skip_typography_serialization ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
$has_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );
|
|
|
|
// Apply required class and style.
|
|
|
|
if ( $has_font_family ) {
|
|
|
|
$font_family = $block_attributes['style']['typography']['fontFamily'];
|
|
|
|
if ( strpos( $font_family, 'var:preset|font-family' ) !== false ) {
|
|
|
|
// Get the name from the string and add proper styles.
|
|
|
|
$index_to_splice = strrpos( $font_family, '|' ) + 1;
|
|
|
|
$font_family_name = substr( $font_family, $index_to_splice );
|
|
|
|
$styles[] = sprintf( 'font-family: var(--wp--preset--font-family--%s);', $font_family_name );
|
|
|
|
} else {
|
2021-05-21 06:14:23 -04:00
|
|
|
$styles[] = sprintf( 'font-family: %s;', $block_attributes['style']['typography']['fontFamily'] );
|
2021-04-15 11:19:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Font style.
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( $has_font_style_support && ! $skip_typography_serialization ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
// Apply font style.
|
|
|
|
$font_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' );
|
|
|
|
if ( $font_style ) {
|
|
|
|
$styles[] = $font_style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Font weight.
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( $has_font_weight_support && ! $skip_typography_serialization ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
// Apply font weight.
|
|
|
|
$font_weight = wp_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' );
|
|
|
|
if ( $font_weight ) {
|
|
|
|
$styles[] = $font_weight;
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line Height.
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( $has_line_height_support && ! $skip_typography_serialization ) {
|
2020-10-13 09:10:30 -04:00
|
|
|
$has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
|
|
|
|
// Add the style (no classes for line-height).
|
|
|
|
if ( $has_line_height ) {
|
2020-10-20 09:36:16 -04:00
|
|
|
$styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
|
2020-10-13 09:10:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
// Text Decoration.
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( $has_text_decoration_support && ! $skip_typography_serialization ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
$text_decoration_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' );
|
|
|
|
if ( $text_decoration_style ) {
|
|
|
|
$styles[] = $text_decoration_style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Text Transform.
|
2021-05-21 06:14:23 -04:00
|
|
|
if ( $has_text_transform_support && ! $skip_typography_serialization ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
$text_transform_style = wp_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
|
|
|
|
if ( $text_transform_style ) {
|
|
|
|
$styles[] = $text_transform_style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 09:36:16 -04:00
|
|
|
if ( ! empty( $classes ) ) {
|
|
|
|
$attributes['class'] = implode( ' ', $classes );
|
|
|
|
}
|
|
|
|
if ( ! empty( $styles ) ) {
|
|
|
|
$attributes['style'] = implode( ' ', $styles );
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:10:30 -04:00
|
|
|
return $attributes;
|
|
|
|
}
|
2020-10-20 09:36:16 -04:00
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
/**
|
|
|
|
* Generates an inline style for a typography feature e.g. text decoration,
|
|
|
|
* text transform, and font style.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param array $attributes Block's attributes.
|
|
|
|
* @param string $feature Key for the feature within the typography styles.
|
|
|
|
* @param string $css_property Slug for the CSS property the inline style sets.
|
|
|
|
*
|
|
|
|
* @return string CSS inline style.
|
|
|
|
*/
|
|
|
|
function wp_typography_get_css_variable_inline_style( $attributes, $feature, $css_property ) {
|
|
|
|
// Retrieve current attribute value or skip if not found.
|
|
|
|
$style_value = _wp_array_get( $attributes, array( 'style', 'typography', $feature ), false );
|
|
|
|
if ( ! $style_value ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
|
|
|
|
if ( strpos( $style_value, "var:preset|{$css_property}|" ) === false ) {
|
|
|
|
return sprintf( '%s:%s;', $css_property, $style_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have a preset CSS variable as the style.
|
|
|
|
// Get the style value from the string and return CSS style.
|
|
|
|
$index_to_splice = strrpos( $style_value, '|' ) + 1;
|
|
|
|
$slug = substr( $style_value, $index_to_splice );
|
|
|
|
|
|
|
|
// Return the actual CSS inline style e.g. `text-decoration:var(--wp--preset--text-decoration--underline);`.
|
|
|
|
return sprintf( '%s:var(--wp--preset--%s--%s);', $css_property, $css_property, $slug );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register the block support.
|
2020-10-20 09:36:16 -04:00
|
|
|
WP_Block_Supports::get_instance()->register(
|
|
|
|
'typography',
|
|
|
|
array(
|
|
|
|
'register_attribute' => 'wp_register_typography_support',
|
|
|
|
'apply' => 'wp_apply_typography_support',
|
|
|
|
)
|
|
|
|
);
|