Changeset 52069 for trunk/src/wp-includes/block-supports/spacing.php
- Timestamp:
- 11/09/2021 02:15:23 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/spacing.php
r51298 r52069 2 2 /** 3 3 * Spacing block support flag. 4 5 * For backwards compatibility, this remains separate to the dimensions.php 6 * block support despite both belonging under a single panel in the editor. 4 7 * 5 8 * @package WordPress … … 43 46 */ 44 47 function wp_apply_spacing_support( $block_type, $block_attributes ) { 45 $has_padding_support = wp_has_spacing_feature_support( $block_type, 'padding' ); 46 $has_margin_support = wp_has_spacing_feature_support( $block_type, 'margin' ); 48 if ( wp_skip_spacing_serialization( $block_type ) ) { 49 return array(); 50 } 51 52 $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); 53 $has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); 47 54 $styles = array(); 48 55 49 56 if ( $has_padding_support ) { 50 57 $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null ); 51 if ( null !== $padding_value) {58 if ( is_array( $padding_value ) ) { 52 59 foreach ( $padding_value as $key => $value ) { 53 60 $styles[] = sprintf( 'padding-%s: %s;', $key, $value ); 54 61 } 62 } elseif ( null !== $padding_value ) { 63 $styles[] = sprintf( 'padding: %s;', $padding_value ); 55 64 } 56 65 } … … 58 67 if ( $has_margin_support ) { 59 68 $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null ); 60 if ( null !== $margin_value) {69 if ( is_array( $margin_value ) ) { 61 70 foreach ( $margin_value as $key => $value ) { 62 71 $styles[] = sprintf( 'margin-%s: %s;', $key, $value ); 63 72 } 73 } elseif ( null !== $margin_value ) { 74 $styles[] = sprintf( 'margin: %s;', $margin_value ); 64 75 } 65 76 } … … 69 80 70 81 /** 71 * Checks whether the current block type supports the spacing feature requested. 82 * Checks whether serialization of the current block's spacing properties should 83 * occur. 72 84 * 73 * @since 5. 8.085 * @since 5.9.0 74 86 * @access private 75 87 * 76 * @param WP_Block_Type $block_type Block type to check for support. 77 * @param string $feature Name of the feature to check support for. 78 * @param mixed $default Fallback value for feature support. Default false. 79 * @return bool Whether the feature is supported. 88 * @param WP_Block_Type $block_type Block type. 89 * 90 * @return boolean Whether to serialize spacing support styles & classes. 80 91 */ 81 function wp_has_spacing_feature_support( $block_type, $feature, $default = false ) { 82 // Check if the specific feature has been opted into individually 83 // via nested flag under `spacing`. 84 return block_has_support( $block_type, array( 'spacing', $feature ), $default ); 92 function wp_skip_spacing_serialization( $block_type ) { 93 $spacing_support = _wp_array_get( $block_type->supports, array( 'spacing' ), false ); 94 95 return is_array( $spacing_support ) && 96 array_key_exists( '__experimentalSkipSerialization', $spacing_support ) && 97 $spacing_support['__experimentalSkipSerialization']; 98 } 99 100 /** 101 * Renders the spacing gap support to the block wrapper, to ensure 102 * that the CSS variable is rendered in all environments. 103 * 104 * @since 5.9.0 105 * @access private 106 * 107 * @param string $block_content Rendered block content. 108 * @param array $block Block object. 109 * @return string Filtered block content. 110 */ 111 function wp_render_spacing_gap_support( $block_content, $block ) { 112 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 113 $has_gap_support = block_has_support( $block_type, array( 'spacing', 'blockGap' ), false ); 114 if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) { 115 return $block_content; 116 } 117 118 $gap_value = $block['attrs']['style']['spacing']['blockGap']; 119 120 // Skip if gap value contains unsupported characters. 121 // Regex for CSS value borrowed from `safecss_filter_attr`, and used here 122 // because we only want to match against the value, not the CSS attribute. 123 if ( preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ) { 124 return $block_content; 125 } 126 127 $style = sprintf( 128 '--wp--style--block-gap: %s', 129 esc_attr( $gap_value ) 130 ); 131 132 // Attempt to update an existing style attribute on the wrapper element. 133 $injected_style = preg_replace( 134 '/^([^>.]+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/', 135 '$1$2' . $style . '; ', 136 $block_content, 137 1 138 ); 139 140 // If there is no existing style attribute, add one to the wrapper element. 141 if ( $injected_style === $block_content ) { 142 $injected_style = preg_replace( 143 '/<([a-zA-Z0-9]+)([ >])/', 144 '<$1 style="' . $style . '"$2', 145 $block_content, 146 1 147 ); 148 }; 149 150 return $injected_style; 85 151 } 86 152 … … 93 159 ) 94 160 ); 161 162 add_filter( 'render_block', 'wp_render_spacing_gap_support', 10, 2 );
Note: See TracChangeset
for help on using the changeset viewer.