WordPress Version: 6.3
/**
* Adds 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.
*
* @since 5.6.0
* @since 6.1.0 Used the style engine to generate CSS and classnames.
* @since 6.3.0 Added support for text-columns.
* @access private
*
* @param WP_Block_Type $block_type Block type.
* @param array $block_attributes Block attributes.
* @return array Typography CSS classes and inline styles.
*/
function wp_apply_typography_support($block_type, $block_attributes)
{
if (!property_exists($block_type, 'supports')) {
return array();
}
$typography_supports = _wp_array_get($block_type->supports, array('typography'), false);
if (!$typography_supports) {
return array();
}
if (wp_should_skip_block_supports_serialization($block_type, 'typography')) {
return array();
}
$has_font_family_support = _wp_array_get($typography_supports, array('__experimentalFontFamily'), false);
$has_font_size_support = _wp_array_get($typography_supports, array('fontSize'), false);
$has_font_style_support = _wp_array_get($typography_supports, array('__experimentalFontStyle'), false);
$has_font_weight_support = _wp_array_get($typography_supports, array('__experimentalFontWeight'), false);
$has_letter_spacing_support = _wp_array_get($typography_supports, array('__experimentalLetterSpacing'), false);
$has_line_height_support = _wp_array_get($typography_supports, array('lineHeight'), false);
$has_text_columns_support = _wp_array_get($typography_supports, array('textColumns'), false);
$has_text_decoration_support = _wp_array_get($typography_supports, array('__experimentalTextDecoration'), false);
$has_text_transform_support = _wp_array_get($typography_supports, array('__experimentalTextTransform'), false);
// Whether to skip individual block support features.
$should_skip_font_size = wp_should_skip_block_supports_serialization($block_type, 'typography', 'fontSize');
$should_skip_font_family = wp_should_skip_block_supports_serialization($block_type, 'typography', 'fontFamily');
$should_skip_font_style = wp_should_skip_block_supports_serialization($block_type, 'typography', 'fontStyle');
$should_skip_font_weight = wp_should_skip_block_supports_serialization($block_type, 'typography', 'fontWeight');
$should_skip_line_height = wp_should_skip_block_supports_serialization($block_type, 'typography', 'lineHeight');
$should_skip_text_columns = wp_should_skip_block_supports_serialization($block_type, 'typography', 'textColumns');
$should_skip_text_decoration = wp_should_skip_block_supports_serialization($block_type, 'typography', 'textDecoration');
$should_skip_text_transform = wp_should_skip_block_supports_serialization($block_type, 'typography', 'textTransform');
$should_skip_letter_spacing = wp_should_skip_block_supports_serialization($block_type, 'typography', 'letterSpacing');
$typography_block_styles = array();
if ($has_font_size_support && !$should_skip_font_size) {
$preset_font_size = array_key_exists('fontSize', $block_attributes) ? "var:preset|font-size|{$block_attributes['fontSize']}" : null;
$custom_font_size = isset($block_attributes['style']['typography']['fontSize']) ? $block_attributes['style']['typography']['fontSize'] : null;
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : wp_get_typography_font_size_value(array('size' => $custom_font_size));
}
if ($has_font_family_support && !$should_skip_font_family) {
$preset_font_family = array_key_exists('fontFamily', $block_attributes) ? "var:preset|font-family|{$block_attributes['fontFamily']}" : null;
$custom_font_family = isset($block_attributes['style']['typography']['fontFamily']) ? wp_typography_get_preset_inline_style_value($block_attributes['style']['typography']['fontFamily'], 'font-family') : null;
$typography_block_styles['fontFamily'] = $preset_font_family ? $preset_font_family : $custom_font_family;
}
if ($has_font_style_support && !$should_skip_font_style && isset($block_attributes['style']['typography']['fontStyle'])) {
$typography_block_styles['fontStyle'] = wp_typography_get_preset_inline_style_value($block_attributes['style']['typography']['fontStyle'], 'font-style');
}
if ($has_font_weight_support && !$should_skip_font_weight && isset($block_attributes['style']['typography']['fontWeight'])) {
$typography_block_styles['fontWeight'] = wp_typography_get_preset_inline_style_value($block_attributes['style']['typography']['fontWeight'], 'font-weight');
}
if ($has_line_height_support && !$should_skip_line_height) {
$typography_block_styles['lineHeight'] = _wp_array_get($block_attributes, array('style', 'typography', 'lineHeight'));
}
if ($has_text_columns_support && !$should_skip_text_columns && isset($block_attributes['style']['typography']['textColumns'])) {
$typography_block_styles['textColumns'] = _wp_array_get($block_attributes, array('style', 'typography', 'textColumns'), null);
}
if ($has_text_decoration_support && !$should_skip_text_decoration && isset($block_attributes['style']['typography']['textDecoration'])) {
$typography_block_styles['textDecoration'] = wp_typography_get_preset_inline_style_value($block_attributes['style']['typography']['textDecoration'], 'text-decoration');
}
if ($has_text_transform_support && !$should_skip_text_transform && isset($block_attributes['style']['typography']['textTransform'])) {
$typography_block_styles['textTransform'] = wp_typography_get_preset_inline_style_value($block_attributes['style']['typography']['textTransform'], 'text-transform');
}
if ($has_letter_spacing_support && !$should_skip_letter_spacing && isset($block_attributes['style']['typography']['letterSpacing'])) {
$typography_block_styles['letterSpacing'] = wp_typography_get_preset_inline_style_value($block_attributes['style']['typography']['letterSpacing'], 'letter-spacing');
}
$attributes = array();
$styles = wp_style_engine_get_styles(array('typography' => $typography_block_styles), array('convert_vars_to_classnames' => true));
if (!empty($styles['classnames'])) {
$attributes['class'] = $styles['classnames'];
}
if (!empty($styles['css'])) {
$attributes['style'] = $styles['css'];
}
return $attributes;
}