16 * @access private |
16 * @access private |
17 * |
17 * |
18 * @param WP_Block_Type $block_type Block Type. |
18 * @param WP_Block_Type $block_type Block Type. |
19 */ |
19 */ |
20 function wp_register_spacing_support( $block_type ) { |
20 function wp_register_spacing_support( $block_type ) { |
21 $has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false ); |
21 $has_spacing_support = block_has_support( $block_type, 'spacing', false ); |
22 |
22 |
23 // Setup attributes and styles within that if needed. |
23 // Setup attributes and styles within that if needed. |
24 if ( ! $block_type->attributes ) { |
24 if ( ! $block_type->attributes ) { |
25 $block_type->attributes = array(); |
25 $block_type->attributes = array(); |
26 } |
26 } |
46 function wp_apply_spacing_support( $block_type, $block_attributes ) { |
47 function wp_apply_spacing_support( $block_type, $block_attributes ) { |
47 if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) { |
48 if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) { |
48 return array(); |
49 return array(); |
49 } |
50 } |
50 |
51 |
|
52 $attributes = array(); |
51 $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); |
53 $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); |
52 $has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); |
54 $has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); |
53 $skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); |
55 $block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; |
54 $skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); |
|
55 $styles = array(); |
|
56 |
56 |
57 if ( $has_padding_support && ! $skip_padding ) { |
57 if ( ! $block_styles ) { |
58 $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null ); |
58 return $attributes; |
59 if ( is_array( $padding_value ) ) { |
|
60 foreach ( $padding_value as $key => $value ) { |
|
61 $styles[] = sprintf( 'padding-%s: %s;', $key, $value ); |
|
62 } |
|
63 } elseif ( null !== $padding_value ) { |
|
64 $styles[] = sprintf( 'padding: %s;', $padding_value ); |
|
65 } |
|
66 } |
59 } |
67 |
60 |
|
61 $skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); |
|
62 $skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); |
|
63 $spacing_block_styles = array( |
|
64 'padding' => null, |
|
65 'margin' => null, |
|
66 ); |
|
67 if ( $has_padding_support && ! $skip_padding ) { |
|
68 $spacing_block_styles['padding'] = isset( $block_styles['spacing']['padding'] ) ? $block_styles['spacing']['padding'] : null; |
|
69 } |
68 if ( $has_margin_support && ! $skip_margin ) { |
70 if ( $has_margin_support && ! $skip_margin ) { |
69 $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null ); |
71 $spacing_block_styles['margin'] = isset( $block_styles['spacing']['margin'] ) ? $block_styles['spacing']['margin'] : null; |
70 if ( is_array( $margin_value ) ) { |
72 } |
71 foreach ( $margin_value as $key => $value ) { |
73 $styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) ); |
72 $styles[] = sprintf( 'margin-%s: %s;', $key, $value ); |
74 |
73 } |
75 if ( ! empty( $styles['css'] ) ) { |
74 } elseif ( null !== $margin_value ) { |
76 $attributes['style'] = $styles['css']; |
75 $styles[] = sprintf( 'margin: %s;', $margin_value ); |
|
76 } |
|
77 } |
77 } |
78 |
78 |
79 return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); |
79 return $attributes; |
80 } |
80 } |
81 |
81 |
82 // Register the block support. |
82 // Register the block support. |
83 WP_Block_Supports::get_instance()->register( |
83 WP_Block_Supports::get_instance()->register( |
84 'spacing', |
84 'spacing', |