Edit File by line
/home/zeestwma/ceyloniy.../wp-inclu.../block-su...
File: dimensions.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Dimensions block support flag.
[2] Fix | Delete
*
[3] Fix | Delete
* This does not include the `spacing` block support even though that visually
[4] Fix | Delete
* appears under the "Dimensions" panel in the editor. It remains in its
[5] Fix | Delete
* original `spacing.php` file for compatibility with core.
[6] Fix | Delete
*
[7] Fix | Delete
* @package WordPress
[8] Fix | Delete
* @since 5.9.0
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Registers the style block attribute for block types that support it.
[13] Fix | Delete
*
[14] Fix | Delete
* @since 5.9.0
[15] Fix | Delete
* @access private
[16] Fix | Delete
*
[17] Fix | Delete
* @param WP_Block_Type $block_type Block Type.
[18] Fix | Delete
*/
[19] Fix | Delete
function wp_register_dimensions_support( $block_type ) {
[20] Fix | Delete
// Setup attributes and styles within that if needed.
[21] Fix | Delete
if ( ! $block_type->attributes ) {
[22] Fix | Delete
$block_type->attributes = array();
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
// Check for existing style attribute definition e.g. from block.json.
[26] Fix | Delete
if ( array_key_exists( 'style', $block_type->attributes ) ) {
[27] Fix | Delete
return;
[28] Fix | Delete
}
[29] Fix | Delete
[30] Fix | Delete
$has_dimensions_support = block_has_support( $block_type, 'dimensions', false );
[31] Fix | Delete
[32] Fix | Delete
if ( $has_dimensions_support ) {
[33] Fix | Delete
$block_type->attributes['style'] = array(
[34] Fix | Delete
'type' => 'object',
[35] Fix | Delete
);
[36] Fix | Delete
}
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Adds CSS classes for block dimensions to the incoming attributes array.
[41] Fix | Delete
* This will be applied to the block markup in the front-end.
[42] Fix | Delete
*
[43] Fix | Delete
* @since 5.9.0
[44] Fix | Delete
* @since 6.2.0 Added `minHeight` support.
[45] Fix | Delete
* @access private
[46] Fix | Delete
*
[47] Fix | Delete
* @param WP_Block_Type $block_type Block Type.
[48] Fix | Delete
* @param array $block_attributes Block attributes.
[49] Fix | Delete
* @return array Block dimensions CSS classes and inline styles.
[50] Fix | Delete
*/
[51] Fix | Delete
function wp_apply_dimensions_support( $block_type, $block_attributes ) {
[52] Fix | Delete
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
[53] Fix | Delete
return array();
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
$attributes = array();
[57] Fix | Delete
[58] Fix | Delete
// Width support to be added in near future.
[59] Fix | Delete
[60] Fix | Delete
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
[61] Fix | Delete
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
[62] Fix | Delete
[63] Fix | Delete
if ( ! $block_styles ) {
[64] Fix | Delete
return $attributes;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
[68] Fix | Delete
$dimensions_block_styles = array();
[69] Fix | Delete
$dimensions_block_styles['minHeight'] = null;
[70] Fix | Delete
if ( $has_min_height_support && ! $skip_min_height ) {
[71] Fix | Delete
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
[72] Fix | Delete
? $block_styles['dimensions']['minHeight']
[73] Fix | Delete
: null;
[74] Fix | Delete
}
[75] Fix | Delete
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
[76] Fix | Delete
[77] Fix | Delete
if ( ! empty( $styles['css'] ) ) {
[78] Fix | Delete
$attributes['style'] = $styles['css'];
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
return $attributes;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
/**
[85] Fix | Delete
* Renders server-side dimensions styles to the block wrapper.
[86] Fix | Delete
* This block support uses the `render_block` hook to ensure that
[87] Fix | Delete
* it is also applied to non-server-rendered blocks.
[88] Fix | Delete
*
[89] Fix | Delete
* @since 6.5.0
[90] Fix | Delete
* @access private
[91] Fix | Delete
*
[92] Fix | Delete
* @param string $block_content Rendered block content.
[93] Fix | Delete
* @param array $block Block object.
[94] Fix | Delete
* @return string Filtered block content.
[95] Fix | Delete
*/
[96] Fix | Delete
function wp_render_dimensions_support( $block_content, $block ) {
[97] Fix | Delete
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
[98] Fix | Delete
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
[99] Fix | Delete
$has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
[100] Fix | Delete
[101] Fix | Delete
if (
[102] Fix | Delete
! $has_aspect_ratio_support ||
[103] Fix | Delete
wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
[104] Fix | Delete
) {
[105] Fix | Delete
return $block_content;
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
$dimensions_block_styles = array();
[109] Fix | Delete
$dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
[110] Fix | Delete
[111] Fix | Delete
// To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
[112] Fix | Delete
if (
[113] Fix | Delete
isset( $dimensions_block_styles['aspectRatio'] )
[114] Fix | Delete
) {
[115] Fix | Delete
$dimensions_block_styles['minHeight'] = 'unset';
[116] Fix | Delete
} elseif (
[117] Fix | Delete
isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
[118] Fix | Delete
isset( $block_attributes['minHeight'] )
[119] Fix | Delete
) {
[120] Fix | Delete
$dimensions_block_styles['aspectRatio'] = 'unset';
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
[124] Fix | Delete
[125] Fix | Delete
if ( ! empty( $styles['css'] ) ) {
[126] Fix | Delete
// Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
[127] Fix | Delete
$tags = new WP_HTML_Tag_Processor( $block_content );
[128] Fix | Delete
[129] Fix | Delete
if ( $tags->next_tag() ) {
[130] Fix | Delete
$existing_style = $tags->get_attribute( 'style' );
[131] Fix | Delete
$updated_style = '';
[132] Fix | Delete
[133] Fix | Delete
if ( ! empty( $existing_style ) ) {
[134] Fix | Delete
$updated_style = $existing_style;
[135] Fix | Delete
if ( ! str_ends_with( $existing_style, ';' ) ) {
[136] Fix | Delete
$updated_style .= ';';
[137] Fix | Delete
}
[138] Fix | Delete
}
[139] Fix | Delete
[140] Fix | Delete
$updated_style .= $styles['css'];
[141] Fix | Delete
$tags->set_attribute( 'style', $updated_style );
[142] Fix | Delete
[143] Fix | Delete
if ( ! empty( $styles['classnames'] ) ) {
[144] Fix | Delete
foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
[145] Fix | Delete
if (
[146] Fix | Delete
str_contains( $class_name, 'aspect-ratio' ) &&
[147] Fix | Delete
! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
[148] Fix | Delete
) {
[149] Fix | Delete
continue;
[150] Fix | Delete
}
[151] Fix | Delete
$tags->add_class( $class_name );
[152] Fix | Delete
}
[153] Fix | Delete
}
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
return $tags->get_updated_html();
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
return $block_content;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 );
[163] Fix | Delete
[164] Fix | Delete
// Register the block support.
[165] Fix | Delete
WP_Block_Supports::get_instance()->register(
[166] Fix | Delete
'dimensions',
[167] Fix | Delete
array(
[168] Fix | Delete
'register_attribute' => 'wp_register_dimensions_support',
[169] Fix | Delete
'apply' => 'wp_apply_dimensions_support',
[170] Fix | Delete
)
[171] Fix | Delete
);
[172] Fix | Delete
[173] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function