| 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 = gutenberg_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 ( gutenberg_skip_spacing_serialization( $block_type ) ) { |
| 43 |
return array(); |
| 44 |
} |
| 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(); |
| 49 |
|
| 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 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ( $has_margin_support ) { |
| 63 |
$margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null ); |
| 64 |
|
| 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 |
} |
| 72 |
} |
| 73 |
|
| 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']; |
| 91 |
} |
| 92 |
|
| 93 |
// Register the block support. |
| 94 |
WP_Block_Supports::get_instance()->register( |
| 95 |
'spacing', |
| 96 |
array( |
| 97 |
'register_attribute' => 'gutenberg_register_spacing_support', |
| 98 |
'apply' => 'gutenberg_apply_spacing_support', |
| 99 |
) |
| 100 |
); |
| 101 |
|