Skip to content

Commit

Permalink
Update PHP block support serialization checks
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Nov 15, 2021
1 parent 9ab7a74 commit ddc289f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 38 deletions.
30 changes: 20 additions & 10 deletions lib/block-supports/border.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
// Border radius.
if (
gutenberg_has_border_feature_support( $block_type, 'radius' ) &&
isset( $block_attributes['style']['border']['radius'] )
isset( $block_attributes['style']['border']['radius'] ) &&
! gutenberg_skip_border_serialization( $block_type, 'radius' )
) {
$border_radius = $block_attributes['style']['border']['radius'];

Expand All @@ -78,7 +79,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
// Border style.
if (
gutenberg_has_border_feature_support( $block_type, 'style' ) &&
isset( $block_attributes['style']['border']['style'] )
isset( $block_attributes['style']['border']['style'] ) &&
! gutenberg_skip_border_serialization( $block_type, 'style' )
) {
$border_style = $block_attributes['style']['border']['style'];
$styles[] = sprintf( 'border-style: %s;', $border_style );
Expand All @@ -87,7 +89,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
// Border width.
if (
gutenberg_has_border_feature_support( $block_type, 'width' ) &&
isset( $block_attributes['style']['border']['width'] )
isset( $block_attributes['style']['border']['width'] ) &&
! gutenberg_skip_border_serialization( $block_type, 'width' )
) {
$border_width = $block_attributes['style']['border']['width'];

Expand All @@ -100,7 +103,10 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
}

// Border color.
if ( gutenberg_has_border_feature_support( $block_type, 'color' ) ) {
if (
gutenberg_has_border_feature_support( $block_type, 'color' ) &&
! gutenberg_skip_border_serialization( $block_type, 'color' )
) {
$has_named_border_color = array_key_exists( 'borderColor', $block_attributes );
$has_custom_border_color = isset( $block_attributes['style']['border']['color'] );

Expand Down Expand Up @@ -134,16 +140,20 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
* Checks whether serialization of the current block's border properties should
* occur.
*
* @param WP_Block_type $block_type Block type.
* @param WP_Block_type $block_type Block type.
* @param string $feature Optional name of individual feature to check.
*
* @return boolean
*/
function gutenberg_skip_border_serialization( $block_type ) {
$border_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), false );
function gutenberg_skip_border_serialization( $block_type, $feature = null ) {
$path = array( '__experimentalBorder', '__experimentalSkipSerialization' );
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );

if ( is_array( $skip_serialization ) ) {
return in_array( $feature, $skip_serialization, true );
}

return is_array( $border_support ) &&
array_key_exists( '__experimentalSkipSerialization', $border_support ) &&
$border_support['__experimentalSkipSerialization'];
return $skip_serialization;
}

/**
Expand Down
30 changes: 25 additions & 5 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {

if (
is_array( $color_support ) &&
array_key_exists( '__experimentalSkipSerialization', $color_support ) &&
$color_support['__experimentalSkipSerialization']
gutenberg_skip_color_serialization( $block_type )
) {
return array();
}
Expand All @@ -82,7 +81,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {

// Text colors.
// Check support for text colors.
if ( $has_text_colors_support ) {
if ( $has_text_colors_support && ! gutenberg_skip_color_serialization( $block_type, 'text' ) ) {
$has_named_text_color = array_key_exists( 'textColor', $block_attributes );
$has_custom_text_color = isset( $block_attributes['style']['color']['text'] );

Expand All @@ -99,7 +98,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
}

// Background colors.
if ( $has_background_colors_support ) {
if ( $has_background_colors_support && ! gutenberg_skip_color_serialization( $block_type, 'background' ) ) {
$has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes );
$has_custom_background_color = isset( $block_attributes['style']['color']['background'] );

Expand All @@ -116,7 +115,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
}

// Gradients.
if ( $has_gradients_support ) {
if ( $has_gradients_support && ! gutenberg_skip_color_serialization( $block_type, 'gradients' ) ) {
$has_named_gradient = array_key_exists( 'gradient', $block_attributes );
$has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );

Expand All @@ -142,6 +141,27 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
return $attributes;
}

/**
* Checks whether serialization of the current block's color properties
* should occur.
*
* @param WP_Block_type $block_type Block type.
* @param string $feature Optional name of individual feature to check.
*
* @return boolean Whether to serialize color support styles & classes.
*/
function gutenberg_skip_color_serialization( $block_type, $feature = null ) {
$path = array( 'color', '__experimentalSkipSerialization' );
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );

if ( is_array( $skip_serialization ) ) {
return in_array( $feature, $skip_serialization, true );
}

return $skip_serialization;
}


// Register the block support.
WP_Block_Supports::get_instance()->register(
'colors',
Expand Down
17 changes: 11 additions & 6 deletions lib/block-supports/dimensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) {
* should occur.
*
* @param WP_Block_type $block_type Block type.
* @param string $feature Optional name of individual feature to check.
*
* @return boolean Whether to serialize spacing support styles & classes.
* @return boolean Whether to serialize dimensions support styles & classes.
*/
function gutenberg_skip_dimensions_serialization( $block_type ) {
$dimensions_support = _wp_array_get( $block_type->supports, array( '__experimentalDimensions' ), false );
return is_array( $dimensions_support ) &&
array_key_exists( '__experimentalSkipSerialization', $dimensions_support ) &&
$dimensions_support['__experimentalSkipSerialization'];
function gutenberg_skip_dimensions_serialization( $block_type, $feature = null ) {
$path = array( '__experimentalDimensions', '__experimentalSkipSerialization' );
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );

if ( is_array( $skip_serialization ) ) {
return in_array( $feature, $skip_serialization, true );
}

return $skip_serialization;
}

// Register the block support.
Expand Down
18 changes: 11 additions & 7 deletions lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
$has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false );
$styles = array();

if ( $has_padding_support ) {
if ( $has_padding_support && ! gutenberg_skip_spacing_serialization( $block_type, 'padding' ) ) {
$padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );

if ( is_array( $padding_value ) ) {
Expand All @@ -59,7 +59,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
}
}

if ( $has_margin_support ) {
if ( $has_margin_support && ! gutenberg_skip_spacing_serialization( $block_type, 'margin' ) ) {
$margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );

if ( is_array( $margin_value ) ) {
Expand All @@ -79,15 +79,19 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {
* occur.
*
* @param WP_Block_type $block_type Block type.
* @param string $feature Optional name of individual feature to check.
*
* @return boolean Whether to serialize spacing support styles & classes.
*/
function gutenberg_skip_spacing_serialization( $block_type ) {
$spacing_support = _wp_array_get( $block_type->supports, array( 'spacing' ), false );
function gutenberg_skip_spacing_serialization( $block_type, $feature = null ) {
$path = array( 'spacing', '__experimentalSkipSerialization' );
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );

return is_array( $spacing_support ) &&
array_key_exists( '__experimentalSkipSerialization', $spacing_support ) &&
$spacing_support['__experimentalSkipSerialization'];
if ( is_array( $skip_serialization ) ) {
return in_array( $feature, $skip_serialization, true );
}

return $skip_serialization;
}


Expand Down
39 changes: 29 additions & 10 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
return array();
}

$skip_typography_serialization = _wp_array_get( $typography_supports, array( '__experimentalSkipSerialization' ), false );
if ( $skip_typography_serialization ) {
if ( gutenberg_skip_typography_serialization( $block_type ) ) {
return array();
}

Expand All @@ -93,7 +92,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
$has_text_decoration_support = _wp_array_get( $typography_supports, array( '__experimentalTextDecoration' ), false );
$has_text_transform_support = _wp_array_get( $typography_supports, array( '__experimentalTextTransform' ), false );

if ( $has_font_size_support ) {
if ( $has_font_size_support && ! gutenberg_skip_typography_serialization( $block_type, 'fontSize' ) ) {
$has_named_font_size = array_key_exists( 'fontSize', $block_attributes );
$has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] );

Expand All @@ -104,7 +103,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
}
}

if ( $has_font_family_support ) {
if ( $has_font_family_support && ! gutenberg_skip_typography_serialization( $block_type, 'fontFamily' ) ) {
$has_named_font_family = array_key_exists( 'fontFamily', $block_attributes );
$has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] );

Expand All @@ -123,42 +122,42 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
}
}

if ( $has_font_style_support ) {
if ( $has_font_style_support && ! gutenberg_skip_typography_serialization( $block_type, 'fontStyle' ) ) {
$font_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'fontStyle', 'font-style' );
if ( $font_style ) {
$styles[] = $font_style;
}
}

if ( $has_font_weight_support ) {
if ( $has_font_weight_support && ! gutenberg_skip_typography_serialization( $block_type, 'fontWeight' ) ) {
$font_weight = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'fontWeight', 'font-weight' );
if ( $font_weight ) {
$styles[] = $font_weight;
}
}

if ( $has_line_height_support ) {
if ( $has_line_height_support && ! gutenberg_skip_typography_serialization( $block_type, 'lineHeight' ) ) {
$has_line_height = isset( $block_attributes['style']['typography']['lineHeight'] );
if ( $has_line_height ) {
$styles[] = sprintf( 'line-height: %s;', $block_attributes['style']['typography']['lineHeight'] );
}
}

if ( $has_text_decoration_support ) {
if ( $has_text_decoration_support && ! gutenberg_skip_typography_serialization( $block_type, 'textDecoration' ) ) {
$text_decoration_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'textDecoration', 'text-decoration' );
if ( $text_decoration_style ) {
$styles[] = $text_decoration_style;
}
}

if ( $has_text_transform_support ) {
if ( $has_text_transform_support && ! gutenberg_skip_typography_serialization( $block_type, 'textTransform' ) ) {
$text_transform_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'textTransform', 'text-transform' );
if ( $text_transform_style ) {
$styles[] = $text_transform_style;
}
}

if ( $has_letter_spacing_support ) {
if ( $has_letter_spacing_support && ! gutenberg_skip_typography_serialization( $block_type, 'letterSpacing' ) ) {
$letter_spacing_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' );
if ( $letter_spacing_style ) {
$styles[] = $letter_spacing_style;
Expand Down Expand Up @@ -214,3 +213,23 @@ function gutenberg_typography_get_css_variable_inline_style( $attributes, $featu
'apply' => 'gutenberg_apply_typography_support',
)
);

/**
* Checks whether serialization of the current block's typography properties
* should occur.
*
* @param WP_Block_type $block_type Block type.
* @param string $feature Optional name of individual feature to check.
*
* @return boolean Whether to serialize typography support styles & classes.
*/
function gutenberg_skip_typography_serialization( $block_type, $feature = null ) {
$path = array( 'typography', '__experimentalSkipSerialization' );
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );

if ( is_array( $skip_serialization ) ) {
return in_array( $feature, $skip_serialization, true );
}

return $skip_serialization;
}

0 comments on commit ddc289f

Please sign in to comment.