Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Stabilize experimental Typography block support features within the block type class #7069

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/wp-includes/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ function wp_register_typography_support( $block_type ) {
return;
}

$has_font_family_support = isset( $typography_supports['__experimentalFontFamily'] ) ? $typography_supports['__experimentalFontFamily'] : false;
$has_font_family_support = isset( $typography_supports['fontFamily'] ) ? $typography_supports['fontFamily'] : false;
$has_font_size_support = isset( $typography_supports['fontSize'] ) ? $typography_supports['fontSize'] : false;
$has_font_style_support = isset( $typography_supports['__experimentalFontStyle'] ) ? $typography_supports['__experimentalFontStyle'] : false;
$has_font_weight_support = isset( $typography_supports['__experimentalFontWeight'] ) ? $typography_supports['__experimentalFontWeight'] : false;
$has_letter_spacing_support = isset( $typography_supports['__experimentalLetterSpacing'] ) ? $typography_supports['__experimentalLetterSpacing'] : false;
$has_font_style_support = isset( $typography_supports['fontStyle'] ) ? $typography_supports['fontStyle'] : false;
$has_font_weight_support = isset( $typography_supports['fontWeight'] ) ? $typography_supports['fontWeight'] : false;
$has_letter_spacing_support = isset( $typography_supports['letterSpacing'] ) ? $typography_supports['letterSpacing'] : false;
$has_line_height_support = isset( $typography_supports['lineHeight'] ) ? $typography_supports['lineHeight'] : false;
$has_text_align_support = isset( $typography_supports['textAlign'] ) ? $typography_supports['textAlign'] : false;
$has_text_columns_support = isset( $typography_supports['textColumns'] ) ? $typography_supports['textColumns'] : false;
$has_text_decoration_support = isset( $typography_supports['__experimentalTextDecoration'] ) ? $typography_supports['__experimentalTextDecoration'] : false;
$has_text_transform_support = isset( $typography_supports['__experimentalTextTransform'] ) ? $typography_supports['__experimentalTextTransform'] : false;
$has_text_decoration_support = isset( $typography_supports['textDecoration'] ) ? $typography_supports['textDecoration'] : false;
$has_text_transform_support = isset( $typography_supports['textTransform'] ) ? $typography_supports['textTransform'] : false;
$has_writing_mode_support = isset( $typography_supports['__experimentalWritingMode'] ) ? $typography_supports['__experimentalWritingMode'] : false;

$has_typography_support = $has_font_family_support
Expand Down Expand Up @@ -102,16 +102,16 @@ function wp_apply_typography_support( $block_type, $block_attributes ) {
return array();
}

$has_font_family_support = isset( $typography_supports['__experimentalFontFamily'] ) ? $typography_supports['__experimentalFontFamily'] : false;
$has_font_family_support = isset( $typography_supports['fontFamily'] ) ? $typography_supports['fontFamily'] : false;
$has_font_size_support = isset( $typography_supports['fontSize'] ) ? $typography_supports['fontSize'] : false;
$has_font_style_support = isset( $typography_supports['__experimentalFontStyle'] ) ? $typography_supports['__experimentalFontStyle'] : false;
$has_font_weight_support = isset( $typography_supports['__experimentalFontWeight'] ) ? $typography_supports['__experimentalFontWeight'] : false;
$has_letter_spacing_support = isset( $typography_supports['__experimentalLetterSpacing'] ) ? $typography_supports['__experimentalLetterSpacing'] : false;
$has_font_style_support = isset( $typography_supports['fontStyle'] ) ? $typography_supports['fontStyle'] : false;
$has_font_weight_support = isset( $typography_supports['fontWeight'] ) ? $typography_supports['fontWeight'] : false;
$has_letter_spacing_support = isset( $typography_supports['letterSpacing'] ) ? $typography_supports['letterSpacing'] : false;
$has_line_height_support = isset( $typography_supports['lineHeight'] ) ? $typography_supports['lineHeight'] : false;
$has_text_align_support = isset( $typography_supports['textAlign'] ) ? $typography_supports['textAlign'] : false;
$has_text_columns_support = isset( $typography_supports['textColumns'] ) ? $typography_supports['textColumns'] : false;
$has_text_decoration_support = isset( $typography_supports['__experimentalTextDecoration'] ) ? $typography_supports['__experimentalTextDecoration'] : false;
$has_text_transform_support = isset( $typography_supports['__experimentalTextTransform'] ) ? $typography_supports['__experimentalTextTransform'] : false;
$has_text_decoration_support = isset( $typography_supports['textDecoration'] ) ? $typography_supports['textDecoration'] : false;
$has_text_transform_support = isset( $typography_supports['textTransform'] ) ? $typography_supports['textTransform'] : false;
$has_writing_mode_support = isset( $typography_supports['__experimentalWritingMode'] ) ? $typography_supports['__experimentalWritingMode'] : false;

// Whether to skip individual block support features.
Expand Down
56 changes: 56 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ class WP_Block_Type {
'style',
);

/**
* Stabilized block supports.
*
* @since 6.7.0
* @var array
*/
private $stabilized_supports = array(
'typography' => array(
'__experimentalFontFamily' => 'fontFamily',
'__experimentalFontStyle' => 'fontStyle',
'__experimentalFontWeight' => 'fontWeight',
'__experimentalLetterSpacing' => 'letterSpacing',
'__experimentalTextDecoration' => 'textDecoration',
'__experimentalTextTransform' => 'textTransform',
),
);

/**
* Attributes supported by every block.
*
Expand Down Expand Up @@ -572,6 +589,11 @@ public function set_props( $args ) {
*/
$args = apply_filters( 'register_block_type_args', $args, $this->name );

// Stabilize experimental block supports.
if ( isset( $args['supports'] ) && is_array( $args['supports'] ) ) {
$args['supports'] = $this->stabilize_supports( $args['supports'] );
}

foreach ( $args as $property_name => $property_value ) {
$this->$property_name = $property_value;
}
Expand Down Expand Up @@ -634,4 +656,38 @@ public function get_uses_context() {
*/
return apply_filters( 'get_block_type_uses_context', $this->uses_context, $this );
}

/**
* Stabilize experimental block supports. This method transforms experimental
* block supports into their stable counterparts, by renaming the keys to the
* stable versions.
*
* @since 6.7.0
*
* @param array $supports The block supports array from within the block type arguments.
* @return array The stabilized block supports array.
*/
private function stabilize_supports( $supports ) {
foreach ( $this->stabilized_supports as $feature => $mapping ) {
if ( isset( $supports[ $feature ] ) ) {
if ( is_array( $mapping ) ) {
foreach ( $mapping as $old => $new ) {
if (
isset( $supports[ $feature ][ $old ] ) &&
! isset( $supports[ $feature ][ $new ] )
) {
$supports[ $feature ][ $new ] = $supports[ $feature ][ $old ];
}
}
} elseif (
is_string( $mapping ) &&
! isset( $supports[ $mapping ] )
) {
$supports[ $mapping ] = $supports[ $feature ];
}
}
}

return $supports;
}
}
109 changes: 109 additions & 0 deletions tests/phpunit/tests/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,115 @@ public function test_should_generate_classname_for_font_family() {
$this->assertSame( $expected, $actual );
}

/**
* Tests that stabilized typography supports will also apply to blocks using
* the experimental syntax, for backwards compatibility with existing blocks.
*
* @ticket 61728
*
* @covers ::wp_apply_typography_support
*/
public function test_should_apply_to_experimental_typography_supports() {
$this->test_block_name = 'test/experimental-typography-supports';
register_block_type(
$this->test_block_name,
array(
'api_version' => 3,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'typography' => array(
'__experimentalFontFamily' => true,
'__experimentalFontStyle' => true,
'__experimentalFontWeight' => true,
'__experimentalLetterSpacing' => true,
'__experimentalTextDecoration' => true,
'__experimentalTextTransform' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $this->test_block_name );
$block_atts = array(
'fontFamily' => 'serif',
'style' => array(
'typography' => array(
'fontStyle' => 'italic',
'fontWeight' => 'bold',
'letterSpacing' => '1px',
'textDecoration' => 'underline',
'textTransform' => 'uppercase',
),
),
);

$actual = wp_apply_typography_support( $block_type, $block_atts );
$expected = array(
'class' => 'has-serif-font-family',
'style' => 'font-style:italic;font-weight:bold;text-decoration:underline;text-transform:uppercase;letter-spacing:1px;',
);

$this->assertSame( $expected, $actual );
}

/**
* Tests that stabilized typography supports are applied correctly.
*
* @ticket 61728
*
* @covers ::wp_apply_typography_support
*/
public function test_should_apply_to_stabilized_typography_supports() {
$this->test_block_name = 'test/experimental-typography-supports';
register_block_type(
$this->test_block_name,
array(
'api_version' => 3,
'attributes' => array(
'style' => array(
'type' => 'object',
),
),
'supports' => array(
'typography' => array(
'fontFamily' => true,
'fontStyle' => true,
'fontWeight' => true,
'letterSpacing' => true,
'textDecoration' => true,
'textTransform' => true,
),
),
)
);
$registry = WP_Block_Type_Registry::get_instance();
$block_type = $registry->get_registered( $this->test_block_name );
$block_atts = array(
'fontFamily' => 'serif',
'style' => array(
'typography' => array(
'fontStyle' => 'italic',
'fontWeight' => 'bold',
'letterSpacing' => '1px',
'textDecoration' => 'underline',
'textTransform' => 'uppercase',
),
),
);

$actual = wp_apply_typography_support( $block_type, $block_atts );
$expected = array(
'class' => 'has-serif-font-family',
'style' => 'font-style:italic;font-weight:bold;text-decoration:underline;text-transform:uppercase;letter-spacing:1px;',
);

$this->assertSame( $expected, $actual );
}

/**
* Tests generating font size values, including fluid formulae, from fontSizes preset.
*
Expand Down
Loading