| 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 = block_has_support( $block_type, array( 'dimensions' ), false ); |
| 29 |
|
| 30 |
if ( $has_dimensions_support ) { |
| 31 |
$block_type->attributes['style'] = array( |
| 32 |
'type' => 'object', |
| 33 |
); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Add CSS classes for block dimensions to the incoming attributes array. |
| 39 |
* This will be applied to the block markup in the front-end. |
| 40 |
* |
| 41 |
* @param WP_Block_Type $block_type Block Type. |
| 42 |
* @param array $block_attributes Block attributes. |
| 43 |
* |
| 44 |
* @return array Block dimensions CSS classes and inline styles. |
| 45 |
*/ |
| 46 |
function gutenberg_apply_dimensions_support( $block_type, $block_attributes ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 47 |
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) { |
| 48 |
return array(); |
| 49 |
} |
| 50 |
|
| 51 |
$attributes = array(); |
| 52 |
|
| 53 |
// Width support to be added in near future. |
| 54 |
|
| 55 |
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false ); |
| 56 |
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null; |
| 57 |
|
| 58 |
if ( ! $block_styles ) { |
| 59 |
return $attributes; |
| 60 |
} |
| 61 |
|
| 62 |
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' ); |
| 63 |
$dimensions_block_styles = array(); |
| 64 |
$dimensions_block_styles['minHeight'] = null; |
| 65 |
if ( $has_min_height_support && ! $skip_min_height ) { |
| 66 |
$dimensions_block_styles['minHeight'] = $block_styles['dimensions']['minHeight'] ?? null; |
| 67 |
} |
| 68 |
$styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); |
| 69 |
|
| 70 |
if ( ! empty( $styles['css'] ) ) { |
| 71 |
$attributes['style'] = $styles['css']; |
| 72 |
} |
| 73 |
|
| 74 |
return $attributes; |
| 75 |
} |
| 76 |
|
| 77 |
// Register the block support. |
| 78 |
WP_Block_Supports::get_instance()->register( |
| 79 |
'dimensions', |
| 80 |
array( |
| 81 |
'register_attribute' => 'gutenberg_register_dimensions_support', |
| 82 |
'apply' => 'gutenberg_apply_dimensions_support', |
| 83 |
) |
| 84 |
); |
| 85 |
|