| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dimensions block support flag. |
| 4 |
* |
| 5 |
* This does not include the `spacing` block support even though that visually |
| 6 |
* appears under the "Dimensions" panel in the editor. It remains in its |
| 7 |
* original `spacing.php` file for compatibility with core. |
| 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_dimensions_support( $block_type ) { |
| 18 |
// Setup attributes and styles within that if needed. |
| 19 |
if ( ! $block_type->attributes ) { |
| 20 |
$block_type->attributes = array(); |
| 21 |
} |
| 22 |
|
| 23 |
// Check for existing style attribute definition e.g. from block.json. |
| 24 |
if ( array_key_exists( 'style', $block_type->attributes ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$has_dimensions_support = gutenberg_block_has_support( $block_type, array( '__experimentalDimensions' ), false ); |
| 29 |
// Future block supports such as height & width will be added here. |
| 30 |
|
| 31 |
if ( $has_dimensions_support ) { |
| 32 |
$block_type->attributes['style'] = array( |
| 33 |
'type' => 'object', |
| 34 |
); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Add CSS classes for block dimensions to the incoming attributes array. |
| 40 |
* This will be applied to the block markup in the front-end. |
| 41 |
* |
| 42 |
* @param WP_Block_Type $block_type Block Type. |
| 43 |
* @param array $block_attributes Block attributes. |
| 44 |
* |
| 45 |
* @return array Block dimensions CSS classes and inline styles. |
| 46 |
*/ |
| 47 |
function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 48 |
if ( gutenberg_skip_dimensions_serialization( $block_type ) ) { |
| 49 |
return array(); |
| 50 |
} |
| 51 |
|
| 52 |
$styles = array(); |
| 53 |
|
| 54 |
// Height support to be added in near future. |
| 55 |
// Width support to be added in near future. |
| 56 |
|
| 57 |
return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Checks whether serialization of the current block's dimensions properties |
| 62 |
* should occur. |
| 63 |
* |
| 64 |
* @param WP_Block_type $block_type Block type. |
| 65 |
* |
| 66 |
* @return boolean Whether to serialize spacing support styles & classes. |
| 67 |
*/ |
| 68 |
function gutenberg_skip_dimensions_serialization( $block_type ) { |
| 69 |
$dimensions_support = _wp_array_get( $block_type->supports, array( '__experimentalDimensions' ), false ); |
| 70 |
return is_array( $dimensions_support ) && |
| 71 |
array_key_exists( '__experimentalSkipSerialization', $dimensions_support ) && |
| 72 |
$dimensions_support['__experimentalSkipSerialization']; |
| 73 |
} |
| 74 |
|
| 75 |
// Register the block support. |
| 76 |
WP_Block_Supports::get_instance()->register( |
| 77 |
'dimensions', |
| 78 |
array( |
| 79 |
'register_attribute' => 'gutenberg_register_dimensions_support', |
| 80 |
'apply' => 'gutenberg_apply_dimensions_support', |
| 81 |
) |
| 82 |
); |
| 83 |
|