| 1 |
<?php |
| 2 |
/** |
| 3 |
* Spacing block support flag. |
| 4 |
* |
| 5 |
* For backwards compatibility with core, this remains separate to the |
| 6 |
* dimensions.php block support despite both belonging under a single panel in |
| 7 |
* the editor. |
| 8 |
* |
| 9 |
* @package gutenberg |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Registers the style block attribute for block types that support it. |
| 14 |
* |
| 15 |
* @param WP_Block_Type $block_type Block Type. |
| 16 |
*/ |
| 17 |
function gutenberg_register_spacing_support( $block_type ) { |
| 18 |
$has_spacing_support = block_has_support( $block_type, array( 'spacing' ), false ); |
| 19 |
|
| 20 |
// Setup attributes and styles within that if needed. |
| 21 |
if ( ! $block_type->attributes ) { |
| 22 |
$block_type->attributes = array(); |
| 23 |
} |
| 24 |
|
| 25 |
if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) { |
| 26 |
$block_type->attributes['style'] = array( |
| 27 |
'type' => 'object', |
| 28 |
); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Add CSS classes for block spacing to the incoming attributes array. |
| 34 |
* This will be applied to the block markup in the front-end. |
| 35 |
* |
| 36 |
* @param WP_Block_Type $block_type Block Type. |
| 37 |
* @param array $block_attributes Block attributes. |
| 38 |
* |
| 39 |
* @return array Block spacing CSS classes and inline styles. |
| 40 |
*/ |
| 41 |
function gutenberg_apply_spacing_support( $block_type, $block_attributes ) { |
| 42 |
if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) { |
| 43 |
return array(); |
| 44 |
} |
| 45 |
|
| 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 = $block_attributes['style'] ?? null; |
| 50 |
|
| 51 |
if ( ! $block_styles ) { |
| 52 |
return $attributes; |
| 53 |
} |
| 54 |
|
| 55 |
$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); |
| 56 |
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); |
| 57 |
$spacing_block_styles = array( |
| 58 |
'padding' => null, |
| 59 |
'margin' => null, |
| 60 |
); |
| 61 |
if ( $has_padding_support && ! $skip_padding ) { |
| 62 |
$spacing_block_styles['padding'] = $block_styles['spacing']['padding'] ?? null; |
| 63 |
} |
| 64 |
if ( $has_margin_support && ! $skip_margin ) { |
| 65 |
$spacing_block_styles['margin'] = $block_styles['spacing']['margin'] ?? null; |
| 66 |
} |
| 67 |
$styles = gutenberg_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) ); |
| 68 |
|
| 69 |
if ( ! empty( $styles['css'] ) ) { |
| 70 |
$attributes['style'] = $styles['css']; |
| 71 |
} |
| 72 |
|
| 73 |
return $attributes; |
| 74 |
} |
| 75 |
|
| 76 |
// Register the block support. |
| 77 |
WP_Block_Supports::get_instance()->register( |
| 78 |
'spacing', |
| 79 |
array( |
| 80 |
'register_attribute' => 'gutenberg_register_spacing_support', |
| 81 |
'apply' => 'gutenberg_apply_spacing_support', |
| 82 |
) |
| 83 |
); |
| 84 |
|