| [ 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 if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) { 55 return array(); 56 } 57 58 $attributes = array(); 59 60 // Width support to be added in near future. 61 62 $has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false ); 63 $block_styles = $block_attributes['style'] ?? null; 64 65 if ( ! $block_styles ) { 66 return $attributes; 67 } 68 69 $skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' ); 70 $dimensions_block_styles = array(); 71 $dimensions_block_styles['minHeight'] = null; 72 if ( $has_min_height_support && ! $skip_min_height ) { 73 $dimensions_block_styles['minHeight'] = $block_styles['dimensions']['minHeight'] ?? null; 74 } 75 $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); 76 77 if ( ! empty( $styles['css'] ) ) { 78 $attributes['style'] = $styles['css']; 79 } 80 81 return $attributes; 82 } 83 84 /** 85 * Renders server-side dimensions styles to the block wrapper. 86 * This block support uses the `render_block` hook to ensure that 87 * it is also applied to non-server-rendered blocks. 88 * 89 * @since 6.5.0 90 * @access private 91 * 92 * @param string $block_content Rendered block content. 93 * @param array $block Block object. 94 * @return string Filtered block content. 95 */ 96 function wp_render_dimensions_support( $block_content, $block ) { 97 $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); 98 $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array(); 99 $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false ); 100 101 if ( 102 ! $has_aspect_ratio_support || 103 wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' ) 104 ) { 105 return $block_content; 106 } 107 108 $dimensions_block_styles = array(); 109 $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null; 110 111 // To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule. 112 if ( 113 isset( $dimensions_block_styles['aspectRatio'] ) 114 ) { 115 $dimensions_block_styles['minHeight'] = 'unset'; 116 } elseif ( 117 isset( $block_attributes['style']['dimensions']['minHeight'] ) || 118 isset( $block_attributes['minHeight'] ) 119 ) { 120 $dimensions_block_styles['aspectRatio'] = 'unset'; 121 } 122 123 $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) ); 124 125 if ( ! empty( $styles['css'] ) ) { 126 // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists. 127 $tags = new WP_HTML_Tag_Processor( $block_content ); 128 129 if ( $tags->next_tag() ) { 130 $existing_style = $tags->get_attribute( 'style' ); 131 $updated_style = ''; 132 133 if ( ! empty( $existing_style ) ) { 134 $updated_style = $existing_style; 135 if ( ! str_ends_with( $existing_style, ';' ) ) { 136 $updated_style .= ';'; 137 } 138 } 139 140 $updated_style .= $styles['css']; 141 $tags->set_attribute( 'style', $updated_style ); 142 143 if ( ! empty( $styles['classnames'] ) ) { 144 foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) { 145 if ( 146 str_contains( $class_name, 'aspect-ratio' ) && 147 ! isset( $block_attributes['style']['dimensions']['aspectRatio'] ) 148 ) { 149 continue; 150 } 151 $tags->add_class( $class_name ); 152 } 153 } 154 } 155 156 return $tags->get_updated_html(); 157 } 158 159 return $block_content; 160 } 161 162 add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 ); 163 164 // Register the block support. 165 WP_Block_Supports::get_instance()->register( 166 'dimensions', 167 array( 168 'register_attribute' => 'wp_register_dimensions_support', 169 'apply' => 'wp_apply_dimensions_support', 170 ) 171 );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Apr 23 08:20:11 2026 | Cross-referenced by PHPXref |