lib/block-supports
anchor.php
1.4 KB
7 months ago
aria-label.php
1.6 KB
6 months ago
background.php
4.6 KB
5 months ago
block-style-variations.php
10.3 KB
7 months ago
block-visibility.php
5.4 KB
5 months ago
border.php
6.0 KB
1 year ago
colors.php
5.7 KB
5 months ago
custom-css.php
8.6 KB
4 months ago
dimensions.php
5.4 KB
4 months ago
duotone.php
12.3 KB
5 months ago
elements.php
8.8 KB
7 months ago
layout.php
45.7 KB
4 months ago
position.php
4.2 KB
2 years ago
settings.php
4.5 KB
1 year ago
shadow.php
2.0 KB
2 years ago
spacing.php
2.6 KB
7 months ago
typography.php
27.5 KB
6 months ago
dimensions.php in Gutenberg 23.2.0, at lib/block-supports/dimensions.php
| 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 ) { |
| 47 | $attributes = array(); |
| 48 | |
| 49 | if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) { |
| 50 | return $attributes; |
| 51 | } |
| 52 | |
| 53 | $block_styles = $block_attributes['style'] ?? null; |
| 54 | |
| 55 | if ( ! $block_styles ) { |
| 56 | return $attributes; |
| 57 | } |
| 58 | |
| 59 | $dimensions_block_styles = array(); |
| 60 | $supported_features = array( 'minHeight', 'minWidth', 'height', 'width' ); |
| 61 | |
| 62 | foreach ( $supported_features as $feature ) { |
| 63 | $has_support = block_has_support( $block_type, array( 'dimensions', $feature ), false ); |
| 64 | $skip_serialization = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', $feature ); |
| 65 | |
| 66 | $dimensions_block_styles[ $feature ] = null; |
| 67 | |
| 68 | if ( $has_support && ! $skip_serialization ) { |
| 69 | $dimensions_block_styles[ $feature ] = $block_styles['dimensions'][ $feature ] ?? null; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); |
| 74 | |
| 75 | if ( ! empty( $styles['css'] ) ) { |
| 76 | $attributes['style'] = $styles['css']; |
| 77 | } |
| 78 | |
| 79 | return $attributes; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Renders server-side dimensions styles to the block wrapper. |
| 84 | * This block support uses the `render_block` hook to ensure that |
| 85 | * it is also applied to non-server-rendered blocks. |
| 86 | * |
| 87 | * @param string $block_content Rendered block content. |
| 88 | * @param array $block Block object. |
| 89 | * @return string Filtered block content. |
| 90 | */ |
| 91 | function gutenberg_render_dimensions_support( $block_content, $block ) { |
| 92 | $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 93 | $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array(); |
| 94 | $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false ); |
| 95 | |
| 96 | if ( |
| 97 | ! $has_aspect_ratio_support || |
| 98 | wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' ) |
| 99 | ) { |
| 100 | return $block_content; |
| 101 | } |
| 102 | |
| 103 | $dimensions_block_styles = array(); |
| 104 | $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null; |
| 105 | |
| 106 | // To ensure the aspect ratio does not get overridden by `minHeight` or `height` unset any existing rule. |
| 107 | if ( |
| 108 | isset( $dimensions_block_styles['aspectRatio'] ) |
| 109 | ) { |
| 110 | $dimensions_block_styles['minHeight'] = 'unset'; |
| 111 | $dimensions_block_styles['height'] = 'unset'; |
| 112 | } elseif ( |
| 113 | isset( $block_attributes['style']['dimensions']['minHeight'] ) || |
| 114 | isset( $block_attributes['minHeight'] ) || |
| 115 | isset( $block_attributes['style']['dimensions']['height'] ) || |
| 116 | isset( $block_attributes['height'] ) |
| 117 | ) { |
| 118 | $dimensions_block_styles['aspectRatio'] = 'unset'; |
| 119 | } |
| 120 | |
| 121 | $styles = gutenberg_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); |
| 122 | |
| 123 | if ( ! empty( $styles['css'] ) ) { |
| 124 | // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists. |
| 125 | $tags = new WP_HTML_Tag_Processor( $block_content ); |
| 126 | |
| 127 | if ( $tags->next_tag() ) { |
| 128 | $existing_style = $tags->get_attribute( 'style' ); |
| 129 | $updated_style = ''; |
| 130 | |
| 131 | if ( ! empty( $existing_style ) ) { |
| 132 | $updated_style = $existing_style; |
| 133 | if ( ! str_ends_with( $existing_style, ';' ) ) { |
| 134 | $updated_style .= ';'; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $updated_style .= $styles['css']; |
| 139 | $tags->set_attribute( 'style', $updated_style ); |
| 140 | |
| 141 | if ( ! empty( $styles['classnames'] ) ) { |
| 142 | foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) { |
| 143 | if ( |
| 144 | str_contains( $class_name, 'aspect-ratio' ) && |
| 145 | ! isset( $block_attributes['style']['dimensions']['aspectRatio'] ) |
| 146 | ) { |
| 147 | continue; |
| 148 | } |
| 149 | $tags->add_class( $class_name ); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return $tags->get_updated_html(); |
| 155 | } |
| 156 | |
| 157 | return $block_content; |
| 158 | } |
| 159 | |
| 160 | remove_filter( 'render_block', 'wp_render_dimensions_support', 10 ); |
| 161 | add_filter( 'render_block', 'gutenberg_render_dimensions_support', 10, 2 ); |
| 162 | |
| 163 | // Register the block support. |
| 164 | WP_Block_Supports::get_instance()->register( |
| 165 | 'dimensions', |
| 166 | array( |
| 167 | 'register_attribute' => 'gutenberg_register_dimensions_support', |
| 168 | 'apply' => 'gutenberg_apply_dimensions_support', |
| 169 | ) |
| 170 | ); |
| 171 |