From 4bf4fd76a6d2708af8f7c0325fa1843f41663c3a Mon Sep 17 00:00:00 2001 From: ramonjd Date: Fri, 4 Mar 2022 22:55:25 +1100 Subject: [PATCH] Extract `gutenberg_should_skip_block_supports_serialization` to a utils file. --- lib/block-supports/border.php | 30 +++++--------------------- lib/block-supports/colors.php | 33 ++++------------------------ lib/block-supports/dimensions.php | 22 +------------------ lib/block-supports/elements.php | 2 +- lib/block-supports/layout.php | 2 +- lib/block-supports/spacing.php | 26 +++------------------- lib/block-supports/typography.php | 36 ++++++++----------------------- lib/block-supports/utils.php | 31 ++++++++++++++++++++++++++ lib/load.php | 1 + 9 files changed, 56 insertions(+), 127 deletions(-) create mode 100644 lib/block-supports/utils.php diff --git a/lib/block-supports/border.php b/lib/block-supports/border.php index 063f0c29c906c..0ea1b2e88f9b9 100644 --- a/lib/block-supports/border.php +++ b/lib/block-supports/border.php @@ -44,7 +44,7 @@ function gutenberg_register_border_support( $block_type ) { * @return array Border CSS classes and inline styles. */ function gutenberg_apply_border_support( $block_type, $block_attributes ) { - if ( gutenberg_skip_border_serialization( $block_type ) ) { + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'border' ) ) { return array(); } @@ -55,7 +55,7 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { if ( gutenberg_has_border_feature_support( $block_type, 'radius' ) && isset( $block_attributes['style']['border']['radius'] ) && - ! gutenberg_skip_border_serialization( $block_type, 'radius' ) + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' ) ) { $border_radius = $block_attributes['style']['border']['radius']; @@ -80,7 +80,7 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { if ( gutenberg_has_border_feature_support( $block_type, 'style' ) && isset( $block_attributes['style']['border']['style'] ) && - ! gutenberg_skip_border_serialization( $block_type, 'style' ) + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ) { $border_style = $block_attributes['style']['border']['style']; $styles[] = sprintf( 'border-style: %s;', $border_style ); @@ -90,7 +90,7 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { if ( gutenberg_has_border_feature_support( $block_type, 'width' ) && isset( $block_attributes['style']['border']['width'] ) && - ! gutenberg_skip_border_serialization( $block_type, 'width' ) + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ) { $border_width = $block_attributes['style']['border']['width']; @@ -105,7 +105,7 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { // Border color. if ( gutenberg_has_border_feature_support( $block_type, 'color' ) && - ! gutenberg_skip_border_serialization( $block_type, 'color' ) + ! gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ) { $has_named_border_color = array_key_exists( 'borderColor', $block_attributes ); $has_custom_border_color = isset( $block_attributes['style']['border']['color'] ); @@ -136,26 +136,6 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) { return $attributes; } -/** - * Checks whether serialization of the current block's border properties should - * occur. - * - * @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, $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 $skip_serialization; -} - /** * Checks whether the current block type supports the border feature requested. * diff --git a/lib/block-supports/colors.php b/lib/block-supports/colors.php index ba5edf09494a9..266c6716e4817 100644 --- a/lib/block-supports/colors.php +++ b/lib/block-supports/colors.php @@ -68,7 +68,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { if ( is_array( $color_support ) && - gutenberg_skip_color_serialization( $block_type ) + gutenberg_should_skip_block_supports_serialization( $block_type, 'color' ) ) { return array(); } @@ -81,7 +81,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { // Text colors. // Check support for text colors. - if ( $has_text_colors_support && ! gutenberg_skip_color_serialization( $block_type, 'text' ) ) { + if ( $has_text_colors_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) { $has_named_text_color = array_key_exists( 'textColor', $block_attributes ); $has_custom_text_color = isset( $block_attributes['style']['color']['text'] ); @@ -98,7 +98,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { } // Background colors. - if ( $has_background_colors_support && ! gutenberg_skip_color_serialization( $block_type, 'background' ) ) { + if ( $has_background_colors_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) { $has_named_background_color = array_key_exists( 'backgroundColor', $block_attributes ); $has_custom_background_color = isset( $block_attributes['style']['color']['background'] ); @@ -115,7 +115,7 @@ function gutenberg_apply_colors_support( $block_type, $block_attributes ) { } // Gradients. - if ( $has_gradients_support && ! gutenberg_skip_color_serialization( $block_type, 'gradients' ) ) { + if ( $has_gradients_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) { $has_named_gradient = array_key_exists( 'gradient', $block_attributes ); $has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] ); @@ -141,31 +141,6 @@ 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 ) { - if ( ! is_object( $block_type ) ) { - return false; - } - - $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', diff --git a/lib/block-supports/dimensions.php b/lib/block-supports/dimensions.php index 48dfee8b4420c..9f6ac8defe910 100644 --- a/lib/block-supports/dimensions.php +++ b/lib/block-supports/dimensions.php @@ -45,7 +45,7 @@ function gutenberg_register_dimensions_support( $block_type ) { * @return array Block dimensions CSS classes and inline styles. */ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - if ( gutenberg_skip_dimensions_serialization( $block_type ) ) { + if ( gutenberg_should_skip_block_supports_serialization( $block_type, '__experimentalDimensions' ) ) { return array(); } @@ -57,26 +57,6 @@ function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); } -/** - * Checks whether serialization of the current block's dimensions 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 dimensions support styles & classes. - */ -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. WP_Block_Supports::get_instance()->register( 'dimensions', diff --git a/lib/block-supports/elements.php b/lib/block-supports/elements.php index 96bcc4c1163cf..04bada4f39782 100644 --- a/lib/block-supports/elements.php +++ b/lib/block-supports/elements.php @@ -19,7 +19,7 @@ function gutenberg_render_elements_support( $block_content, $block ) { } $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); - $skip_link_color_serialization = gutenberg_skip_color_serialization( $block_type, 'link' ); + $skip_link_color_serialization = gutenberg_should_skip_block_supports_serialization( $block_type, 'color', 'link' ); if ( $skip_link_color_serialization ) { return $block_content; diff --git a/lib/block-supports/layout.php b/lib/block-supports/layout.php index de17711f9a490..e369cd4e4c26f 100644 --- a/lib/block-supports/layout.php +++ b/lib/block-supports/layout.php @@ -158,7 +158,7 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) { $gap_value = preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value; // If a block's block.json skips serialization for spacing or spacing.blockGap, // don't apply the user-defined value to the styles. - $should_skip_gap_serialization = gutenberg_skip_spacing_serialization( $block_type, 'blockGap' ); + $should_skip_gap_serialization = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' ); $style = gutenberg_get_layout_style( ".$class_name", $used_layout, $has_block_gap_support, $gap_value, $should_skip_gap_serialization ); // This assumes the hook only applies to blocks with a single wrapper. // I think this is a reasonable limitation for that particular hook. diff --git a/lib/block-supports/spacing.php b/lib/block-supports/spacing.php index d037dc282bdb3..693a14418e3ed 100644 --- a/lib/block-supports/spacing.php +++ b/lib/block-supports/spacing.php @@ -39,7 +39,7 @@ function gutenberg_register_spacing_support( $block_type ) { * @return array Block spacing CSS classes and inline styles. */ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { - if ( gutenberg_skip_spacing_serialization( $block_type ) ) { + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) { return array(); } @@ -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 && ! gutenberg_skip_spacing_serialization( $block_type, 'padding' ) ) { + if ( $has_padding_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ) ) { $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null ); if ( is_array( $padding_value ) ) { @@ -59,7 +59,7 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { } } - if ( $has_margin_support && ! gutenberg_skip_spacing_serialization( $block_type, 'margin' ) ) { + if ( $has_margin_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ) ) { $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null ); if ( is_array( $margin_value ) ) { @@ -74,26 +74,6 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); } -/** - * Checks whether serialization of the current block's spacing 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 spacing support styles & classes. - */ -function gutenberg_skip_spacing_serialization( $block_type, $feature = null ) { - $path = array( 'spacing', '__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( 'spacing', diff --git a/lib/block-supports/typography.php b/lib/block-supports/typography.php index 5b3836f2c4a4e..9661277c1c7b9 100644 --- a/lib/block-supports/typography.php +++ b/lib/block-supports/typography.php @@ -75,7 +75,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) { return array(); } - if ( gutenberg_skip_typography_serialization( $block_type ) ) { + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'typography' ) ) { return array(); } @@ -92,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 && ! gutenberg_skip_typography_serialization( $block_type, 'fontSize' ) ) { + if ( $has_font_size_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontSize' ) ) { $has_named_font_size = array_key_exists( 'fontSize', $block_attributes ); $has_custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ); @@ -103,7 +103,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) { } } - if ( $has_font_family_support && ! gutenberg_skip_typography_serialization( $block_type, 'fontFamily' ) ) { + if ( $has_font_family_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'fontFamily' ) ) { $has_named_font_family = array_key_exists( 'fontFamily', $block_attributes ); $has_custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] ); @@ -122,42 +122,42 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) { } } - if ( $has_font_style_support && ! gutenberg_skip_typography_serialization( $block_type, 'fontStyle' ) ) { + if ( $has_font_style_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', '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 && ! gutenberg_skip_typography_serialization( $block_type, 'fontWeight' ) ) { + if ( $has_font_weight_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', '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 && ! gutenberg_skip_typography_serialization( $block_type, 'lineHeight' ) ) { + if ( $has_line_height_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', '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 && ! gutenberg_skip_typography_serialization( $block_type, 'textDecoration' ) ) { + if ( $has_text_decoration_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', '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 && ! gutenberg_skip_typography_serialization( $block_type, 'textTransform' ) ) { + if ( $has_text_transform_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', '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 && ! gutenberg_skip_typography_serialization( $block_type, 'letterSpacing' ) ) { + if ( $has_letter_spacing_support && ! gutenberg_should_skip_block_supports_serialization( $block_type, 'typography', 'letterSpacing' ) ) { $letter_spacing_style = gutenberg_typography_get_css_variable_inline_style( $block_attributes, 'letterSpacing', 'letter-spacing' ); if ( $letter_spacing_style ) { $styles[] = $letter_spacing_style; @@ -214,22 +214,4 @@ function gutenberg_typography_get_css_variable_inline_style( $attributes, $featu ) ); -/** - * 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; -} diff --git a/lib/block-supports/utils.php b/lib/block-supports/utils.php new file mode 100644 index 0000000000000..a498d2025b895 --- /dev/null +++ b/lib/block-supports/utils.php @@ -0,0 +1,31 @@ +supports, $path, false ); + + if ( is_array( $skip_serialization ) ) { + return in_array( $feature, $skip_serialization, true ); + } + + return $skip_serialization; +} diff --git a/lib/load.php b/lib/load.php index 26cd50608dbdc..72a00ff2b36b5 100644 --- a/lib/load.php +++ b/lib/load.php @@ -119,6 +119,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/global-styles.php'; require __DIR__ . '/pwa.php'; +require __DIR__ . '/block-supports/utils.php'; require __DIR__ . '/block-supports/elements.php'; require __DIR__ . '/block-supports/colors.php'; require __DIR__ . '/block-supports/typography.php';