PluginProbe
Gutenberg / 14.7.0
Gutenberg v14.7.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / lib / block-supports / dimensions.php

dimensions.php in Gutenberg 14.7.0, at lib/block-supports/dimensions.php

82 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( gutenberg_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 = gutenberg_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
63 $dimensions_block_styles = array();
64 $dimensions_block_styles['minHeight'] = $has_min_height_support && ! $skip_min_height ? _wp_array_get( $block_styles, array( 'dimensions', 'minHeight' ), null ) : null;
65 $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
66
67 if ( ! empty( $styles['css'] ) ) {
68 $attributes['style'] = $styles['css'];
69 }
70
71 return $attributes;
72 }
73
74 // Register the block support.
75 WP_Block_Supports::get_instance()->register(
76 'dimensions',
77 array(
78 'register_attribute' => 'gutenberg_register_dimensions_support',
79 'apply' => 'gutenberg_apply_dimensions_support',
80 )
81 );
82