2020-10-13 15:10:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Typography block support flag.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the style and typography block attributes for block types that support it.
|
|
|
|
*
|
2020-10-20 15:36:16 +02:00
|
|
|
* @access private
|
|
|
|
*
|
2020-10-13 15:10:30 +02:00
|
|
|
* @param WP_Block_Type $block_type Block Type.
|
|
|
|
*/
|
|
|
|
function wp_register_typography_support( $block_type ) {
|
|
|
|
$has_font_size_support = false;
|
|
|
|
if ( property_exists( $block_type, 'supports' ) ) {
|
2020-11-12 21:20:12 +01:00
|
|
|
$has_font_size_support = _wp_array_get( $block_type->supports, array( '__experimentalFontSize' ), false );
|
2020-10-13 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$has_line_height_support = false;
|
|
|
|
if ( property_exists( $block_type, 'supports' ) ) {
|
2020-11-12 21:20:12 +01:00
|
|
|
$has_line_height_support = _wp_array_get( $block_type->supports, array( '__experimentalLineHeight' ), false );
|
2020-10-13 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $block_type->attributes ) {
|
|
|
|
$block_type->attributes = array();
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:36:16 +02:00
|
|
|
if ( ( $has_font_size_support || $has_line_height_support ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
2020-10-13 15:10:30 +02: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',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add CSS classes and inline styles for font sizes to the incoming attributes array.
|
|
|
|
* This will be applied to the block markup in the front-end.
|
|
|
|
*
|
2020-10-20 15:36:16 +02:00
|
|
|
* @access private
|
|
|
|
*
|
2020-10-13 15:10:30 +02:00
|
|
|
* @param WP_Block_Type $block_type Block type.
|
2020-10-20 15:36:16 +02:00
|
|
|
* @param array $block_attributes Block attributes.
|
2020-10-13 15:10:30 +02:00
|
|
|
*
|
|
|
|
* @return array Font size CSS classes and inline styles.
|
|
|
|
*/
|
2020-10-20 15:36:16 +02:00
|
|
|
function wp_apply_typography_support( $block_type, $block_attributes ) {
|
2020-10-13 15:10:30 +02:00
|
|
|
$has_font_size_support = false;
|
2020-10-20 15:36:16 +02:00
|
|
|
$classes = array();
|
|
|
|
$styles = array();
|
2020-10-13 15:10:30 +02:00
|
|
|
if ( property_exists( $block_type, 'supports' ) ) {
|
2020-11-12 21:20:12 +01:00
|
|
|
$has_font_size_support = _wp_array_get( $block_type->supports, array( 'fontSize' ), false );
|
2020-10-13 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$has_line_height_support = false;
|
|
|
|
if ( property_exists( $block_type, 'supports' ) ) {
|
2020-11-12 21:20:12 +01:00
|
|
|
$has_line_height_support = _wp_array_get( $block_type->supports, array( 'lineHeight' ), false );
|
2020-10-13 15:10:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Font Size.
|
|
|
|
if ( $has_font_size_support ) {
|
|
|
|
$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 15:36:16 +02:00
|
|
|
$classes[] = sprintf( 'has-%s-font-size', $block_attributes['fontSize'] );
|
2020-10-13 15:10:30 +02:00
|
|
|
} elseif ( $has_custom_font_size ) {
|
2020-10-20 15:36:16 +02:00
|
|
|
$styles[] = sprintf( 'font-size: %spx;', $block_attributes['style']['typography']['fontSize'] );
|
2020-10-13 15:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line Height.
|
|
|
|
if ( $has_line_height_support ) {
|
|
|
|
$has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
|
|
|
|
// Add the style (no classes for line-height).
|
|
|
|
if ( $has_line_height ) {
|
2020-10-20 15:36:16 +02:00
|
|
|
$styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
|
2020-10-13 15:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:36:16 +02:00
|
|
|
$attributes = array();
|
|
|
|
if ( ! empty( $classes ) ) {
|
|
|
|
$attributes['class'] = implode( ' ', $classes );
|
|
|
|
}
|
|
|
|
if ( ! empty( $styles ) ) {
|
|
|
|
$attributes['style'] = implode( ' ', $styles );
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:10:30 +02:00
|
|
|
return $attributes;
|
|
|
|
}
|
2020-10-20 15:36:16 +02:00
|
|
|
|
|
|
|
WP_Block_Supports::get_instance()->register(
|
|
|
|
'typography',
|
|
|
|
array(
|
|
|
|
'register_attribute' => 'wp_register_typography_support',
|
|
|
|
'apply' => 'wp_apply_typography_support',
|
|
|
|
)
|
|
|
|
);
|