| @@ -14,9 +14,9 @@ | ||
| 14 | 14 | * |
| 15 | 15 | * @param WP_Block_Type $block_type Block Type. |
| 16 | 16 | */ |
| 17 | 17 | function gutenberg_register_spacing_support( $block_type ) { |
| 18 | - $has_spacing_support = gutenberg_block_has_support( $block_type, array( 'spacing' ), false ); | |
| 18 | + $has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false ); | |
| 19 | 19 | |
| 20 | 20 | // Setup attributes and styles within that if needed. |
| 21 | 21 | if ( ! $block_type->attributes ) { |
| 22 | 22 | $block_type->attributes = array(); |
| @@ -38,57 +38,33 @@ | ||
| 38 | 38 | * |
| 39 | 39 | * @return array Block spacing CSS classes and inline styles. |
| 40 | 40 | */ |
| 41 | 41 | function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { |
| 42 | - if ( gutenberg_skip_spacing_serialization( $block_type ) ) { | |
| 42 | + if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) { | |
| 43 | 43 | return array(); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - $has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false ); | |
| 47 | - $has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false ); | |
| 48 | - $styles = array(); | |
| 46 | + $attributes = array(); | |
| 47 | + $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); | |
| 48 | + $has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); | |
| 49 | + $block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; | |
| 49 | 50 | |
| 50 | - if ( $has_padding_support ) { | |
| 51 | - $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null ); | |
| 52 | - | |
| 53 | - if ( is_array( $padding_value ) ) { | |
| 54 | - foreach ( $padding_value as $key => $value ) { | |
| 55 | - $styles[] = sprintf( 'padding-%s: %s;', $key, $value ); | |
| 56 | - } | |
| 57 | - } elseif ( null !== $padding_value ) { | |
| 58 | - $styles[] = sprintf( 'padding: %s;', $padding_value ); | |
| 59 | - } | |
| 51 | + if ( ! $block_styles ) { | |
| 52 | + return $attributes; | |
| 60 | 53 | } |
| 61 | 54 | |
| 62 | - if ( $has_margin_support ) { | |
| 63 | - $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null ); | |
| 55 | + $skip_padding = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); | |
| 56 | + $skip_margin = gutenberg_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); | |
| 57 | + $spacing_block_styles = array(); | |
| 58 | + $spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null; | |
| 59 | + $spacing_block_styles['margin'] = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : null; | |
| 60 | + $styles = gutenberg_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) ); | |
| 64 | 61 | |
| 65 | - if ( is_array( $margin_value ) ) { | |
| 66 | - foreach ( $margin_value as $key => $value ) { | |
| 67 | - $styles[] = sprintf( 'margin-%s: %s;', $key, $value ); | |
| 68 | - } | |
| 69 | - } elseif ( null !== $margin_value ) { | |
| 70 | - $styles[] = sprintf( 'margin: %s;', $margin_value ); | |
| 71 | - } | |
| 62 | + if ( ! empty( $styles['css'] ) ) { | |
| 63 | + $attributes['style'] = $styles['css']; | |
| 72 | 64 | } |
| 73 | 65 | |
| 74 | - return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); | |
| 75 | -} | |
| 76 | - | |
| 77 | -/** | |
| 78 | - * Checks whether serialization of the current block's spacing properties should | |
| 79 | - * occur. | |
| 80 | - * | |
| 81 | - * @param WP_Block_type $block_type Block type. | |
| 82 | - * | |
| 83 | - * @return boolean Whether to serialize spacing support styles & classes. | |
| 84 | - */ | |
| 85 | -function gutenberg_skip_spacing_serialization( $block_type ) { | |
| 86 | - $spacing_support = _wp_array_get( $block_type->supports, array( 'spacing' ), false ); | |
| 87 | - | |
| 88 | - return is_array( $spacing_support ) && | |
| 89 | - array_key_exists( '__experimentalSkipSerialization', $spacing_support ) && | |
| 90 | - $spacing_support['__experimentalSkipSerialization']; | |
| 66 | + return $attributes; | |
| 91 | 67 | } |
| 92 | 68 | |
| 93 | 69 | // Register the block support. |
| 94 | 70 | WP_Block_Supports::get_instance()->register( |