| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 WordPress 10 * @since 5.9.0 11 */ 12 13 /** 14 * Registers the style block attribute for block types that support it. 15 * 16 * @since 5.9.0 17 * @access private 18 * 19 * @param WP_Block_Type $block_type Block Type. 20 */ 21 function wp_register_dimensions_support( $block_type ) { 22 // Setup attributes and styles within that if needed. 23 if ( ! $block_type->attributes ) { 24 $block_type->attributes = array(); 25 } 26 27 // Check for existing style attribute definition e.g. from block.json. 28 if ( array_key_exists( 'style', $block_type->attributes ) ) { 29 return; 30 } 31 32 $has_dimensions_support = block_has_support( $block_type, 'dimensions', false ); 33 34 if ( $has_dimensions_support ) { 35 $block_type->attributes['style'] = array( 36 'type' => 'object', 37 ); 38 } 39 } 40 41 /** 42 * Adds CSS classes for block dimensions to the incoming attributes array. 43 * This will be applied to the block markup in the front-end. 44 * 45 * @since 5.9.0 46 * @since 6.2.0 Added `minHeight` support. 47 * @access private 48 * 49 * @param WP_Block_Type $block_type Block Type. 50 * @param array $block_attributes Block attributes. 51 * @return array Block dimensions CSS classes and inline styles. 52 */ 53 function wp_apply_dimensions_support( $block_type, $block_attributes ) { 54 $attributes = array(); 55 56 if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) { 57 return $attributes; 58 } 59 60 $block_styles = $block_attributes['style'] ?? null; 61 62 if ( ! $block_styles ) { 63 return $attributes; 64 } 65 66 $dimensions_block_styles = array(); 67 $supported_features = array( 'minHeight', 'height', 'width' ); 68 69 foreach ( $supported_features as $feature ) { 70 $has_support = block_has_support( $block_type, array( 'dimensions', $feature ), false ); 71 $skip_serialization = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', $feature ); 72 73 $dimensions_block_styles[ $feature ] = null; 74 75 if ( $has_support && ! $skip_serialization ) { 76 $dimensions_block_styles[ $feature ] = $block_styles['dimensions'][ $feature ] ?? null; 77 } 78 } 79 80 $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); 81 82 if ( ! empty( $styles['css'] ) ) { 83 $attributes['style'] = $styles['css']; 84 } 85 86 return $attributes; 87 } 88 89 /** 90 * Checks whether an aspectRatio block-support value is explicitly set. 91 * 92 * @since 7.1.0 93 * @access private 94 * 95 * @param mixed $aspect_ratio Aspect-ratio value. 96 * @return bool Whether the value is an explicit aspect ratio. 97 */ 98 function wp_is_explicit_aspect_ratio_value( $aspect_ratio ) { 99 if ( ! is_string( $aspect_ratio ) && ! is_numeric( $aspect_ratio ) ) { 100 return false; 101 } 102 103 $aspect_ratio = strtolower( trim( (string) $aspect_ratio ) ); 104 105 return '' !== $aspect_ratio && 'auto' !== $aspect_ratio; 106 } 107 108 /** 109 * Renders server-side dimensions styles to the block wrapper. 110 * This block support uses the `render_block` hook to ensure that 111 * it is also applied to non-server-rendered blocks. 112 * 113 * @since 6.5.0 114 * @access private 115 * 116 * @param string $block_content Rendered block content. 117 * @param array $block Block object. 118 * @return string Filtered block content. 119 */ 120 function wp_render_dimensions_support( $block_content, $block ) { 121 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 122 $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array(); 123 $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false ); 124 125 if ( 126 ! $has_aspect_ratio_support || 127 wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' ) 128 ) { 129 return $block_content; 130 } 131 132 $dimensions_block_styles = array(); 133 $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null; 134 135 // To ensure the aspect ratio does not get overridden by `minHeight` or `height` unset any existing rule. 136 if ( 137 wp_is_explicit_aspect_ratio_value( $dimensions_block_styles['aspectRatio'] ) 138 ) { 139 $dimensions_block_styles['minHeight'] = 'unset'; 140 $dimensions_block_styles['height'] = 'unset'; 141 } elseif ( 142 isset( $block_attributes['style']['dimensions']['minHeight'] ) || 143 isset( $block_attributes['minHeight'] ) 144 ) { 145 $dimensions_block_styles['aspectRatio'] = 'unset'; 146 } 147 148 $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); 149 150 if ( ! empty( $styles['css'] ) ) { 151 // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists. 152 $tags = new WP_HTML_Tag_Processor( $block_content ); 153 154 if ( $tags->next_tag() ) { 155 $existing_style = $tags->get_attribute( 'style' ); 156 $updated_style = ''; 157 158 if ( ! empty( $existing_style ) ) { 159 $updated_style = $existing_style; 160 if ( ! str_ends_with( $existing_style, ';' ) ) { 161 $updated_style .= ';'; 162 } 163 } 164 165 $updated_style .= $styles['css']; 166 $tags->set_attribute( 'style', $updated_style ); 167 168 if ( ! empty( $styles['classnames'] ) ) { 169 foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) { 170 if ( 171 str_contains( $class_name, 'aspect-ratio' ) && 172 ! wp_is_explicit_aspect_ratio_value( $block_attributes['style']['dimensions']['aspectRatio'] ?? null ) 173 ) { 174 continue; 175 } 176 $tags->add_class( $class_name ); 177 } 178 } 179 } 180 181 return $tags->get_updated_html(); 182 } 183 184 return $block_content; 185 } 186 187 add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 ); 188 189 // Register the block support. 190 WP_Block_Supports::get_instance()->register( 191 'dimensions', 192 array( 193 'register_attribute' => 'wp_register_dimensions_support', 194 'apply' => 'wp_apply_dimensions_support', 195 ) 196 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Jun 24 08:20:11 2026 | Cross-referenced by PHPXref |