2021-11-08 21:17:17 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Dimensions block support flag.
|
|
|
|
*
|
|
|
|
* This does not include the `spacing` block support even though that visually
|
|
|
|
* appears under the "Dimensions" panel in the editor. It remains in its
|
2022-04-05 08:08:02 -04:00
|
|
|
* original `spacing.php` file for compatibility with core.
|
2021-11-08 21:17:17 -05:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 5.9.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the style block attribute for block types that support it.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param WP_Block_Type $block_type Block Type.
|
|
|
|
*/
|
|
|
|
function wp_register_dimensions_support( $block_type ) {
|
|
|
|
// Setup attributes and styles within that if needed.
|
|
|
|
if ( ! $block_type->attributes ) {
|
|
|
|
$block_type->attributes = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for existing style attribute definition e.g. from block.json.
|
|
|
|
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 12:48:19 -04:00
|
|
|
$has_dimensions_support = block_has_support( $block_type, 'dimensions', false );
|
2021-11-08 21:17:17 -05:00
|
|
|
|
|
|
|
if ( $has_dimensions_support ) {
|
|
|
|
$block_type->attributes['style'] = array(
|
|
|
|
'type' => 'object',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-01 18:16:04 -05:00
|
|
|
* Adds CSS classes for block dimensions to the incoming attributes array.
|
2021-11-08 21:17:17 -05:00
|
|
|
* This will be applied to the block markup in the front-end.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
Editor: Introduce minimum height dimensions block support.
This changeset adds the new dimension feature's PHP code for supporting minimum height in the block editor inspector and in global styles. Minimum height is quite useful for defining the minimum vertical dimensions of a block, while allowing it to expand beyond that size.
In this changeset:
* Adds support in `theme.json`.
* Adds support in the style engine.
* Adds support in `wp_apply_dimensions_support()`.
* Renames the setting from `'__experimentalDimensions'` to `dimensions` in `wp_register_dimensions_support()`.
* Adds PHPUnit tests.
Is renaming `'__experimentalDimensions'` a backwards-compatibility (BC) break?
Though the setting has been in the code since 5.9.0, it was never wired to anything, ie it did not expose any controls or styles. Notice in `wp_register_dimensions_support()` and `wp_apply_dimensions_support()` prior to this changeset, there are inline comments as placeholders for height and width support, but no code.
If a developer opted in to use it, it had no effect.
A search in wp.org's plugin and themes repo showed no instances of this experimental setting.
Given there was no functionality attached to it (until this changeset), no change in behavior or effect from removing it, and no usage found in the plugins and themes repository, it does appear to be a BC break.
References:
* [https://github.com/WordPress/gutenberg/pull/45300 Gutenberg PR 45300]
* [https://github.com/WordPress/gutenberg/pull/45334 Gutenberg PR 45334]
Follow-up to [53076], [52069].
Props andrewserong, aaronrobertshaw , costdev, hellofromTonya, isabel_brison, joen, paaljoachim, mukesh27, ntsekouras, oandregal, ramonopoly.
Fixes #57582.
Built from https://develop.svn.wordpress.org/trunk@55175
git-svn-id: http://core.svn.wordpress.org/trunk@54708 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-02-01 11:15:15 -05:00
|
|
|
* @since 6.2.0 Added `minHeight` support.
|
2021-11-08 21:17:17 -05:00
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param WP_Block_Type $block_type Block Type.
|
|
|
|
* @param array $block_attributes Block attributes.
|
|
|
|
* @return array Block dimensions CSS classes and inline styles.
|
|
|
|
*/
|
Coding Standards: Remove redundant ignore annotations, take 5.
The `VariableAnalysis` standard is not used by WP Core.
Follow-up to [50958], [51003], [52049], [52051], [52069], [53072], [54132], [55132], [56363], [56738], [56743], [56751], [56752].
Props jrf.
See #59161.
Built from https://develop.svn.wordpress.org/trunk@56753
git-svn-id: http://core.svn.wordpress.org/trunk@56265 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-02 07:27:24 -04:00
|
|
|
function wp_apply_dimensions_support( $block_type, $block_attributes ) {
|
Editor: Introduce minimum height dimensions block support.
This changeset adds the new dimension feature's PHP code for supporting minimum height in the block editor inspector and in global styles. Minimum height is quite useful for defining the minimum vertical dimensions of a block, while allowing it to expand beyond that size.
In this changeset:
* Adds support in `theme.json`.
* Adds support in the style engine.
* Adds support in `wp_apply_dimensions_support()`.
* Renames the setting from `'__experimentalDimensions'` to `dimensions` in `wp_register_dimensions_support()`.
* Adds PHPUnit tests.
Is renaming `'__experimentalDimensions'` a backwards-compatibility (BC) break?
Though the setting has been in the code since 5.9.0, it was never wired to anything, ie it did not expose any controls or styles. Notice in `wp_register_dimensions_support()` and `wp_apply_dimensions_support()` prior to this changeset, there are inline comments as placeholders for height and width support, but no code.
If a developer opted in to use it, it had no effect.
A search in wp.org's plugin and themes repo showed no instances of this experimental setting.
Given there was no functionality attached to it (until this changeset), no change in behavior or effect from removing it, and no usage found in the plugins and themes repository, it does appear to be a BC break.
References:
* [https://github.com/WordPress/gutenberg/pull/45300 Gutenberg PR 45300]
* [https://github.com/WordPress/gutenberg/pull/45334 Gutenberg PR 45334]
Follow-up to [53076], [52069].
Props andrewserong, aaronrobertshaw , costdev, hellofromTonya, isabel_brison, joen, paaljoachim, mukesh27, ntsekouras, oandregal, ramonopoly.
Fixes #57582.
Built from https://develop.svn.wordpress.org/trunk@55175
git-svn-id: http://core.svn.wordpress.org/trunk@54708 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-02-01 11:15:15 -05:00
|
|
|
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
|
2021-11-08 21:17:17 -05:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
Editor: Introduce minimum height dimensions block support.
This changeset adds the new dimension feature's PHP code for supporting minimum height in the block editor inspector and in global styles. Minimum height is quite useful for defining the minimum vertical dimensions of a block, while allowing it to expand beyond that size.
In this changeset:
* Adds support in `theme.json`.
* Adds support in the style engine.
* Adds support in `wp_apply_dimensions_support()`.
* Renames the setting from `'__experimentalDimensions'` to `dimensions` in `wp_register_dimensions_support()`.
* Adds PHPUnit tests.
Is renaming `'__experimentalDimensions'` a backwards-compatibility (BC) break?
Though the setting has been in the code since 5.9.0, it was never wired to anything, ie it did not expose any controls or styles. Notice in `wp_register_dimensions_support()` and `wp_apply_dimensions_support()` prior to this changeset, there are inline comments as placeholders for height and width support, but no code.
If a developer opted in to use it, it had no effect.
A search in wp.org's plugin and themes repo showed no instances of this experimental setting.
Given there was no functionality attached to it (until this changeset), no change in behavior or effect from removing it, and no usage found in the plugins and themes repository, it does appear to be a BC break.
References:
* [https://github.com/WordPress/gutenberg/pull/45300 Gutenberg PR 45300]
* [https://github.com/WordPress/gutenberg/pull/45334 Gutenberg PR 45334]
Follow-up to [53076], [52069].
Props andrewserong, aaronrobertshaw , costdev, hellofromTonya, isabel_brison, joen, paaljoachim, mukesh27, ntsekouras, oandregal, ramonopoly.
Fixes #57582.
Built from https://develop.svn.wordpress.org/trunk@55175
git-svn-id: http://core.svn.wordpress.org/trunk@54708 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-02-01 11:15:15 -05:00
|
|
|
$attributes = array();
|
2021-11-08 21:17:17 -05:00
|
|
|
|
|
|
|
// Width support to be added in near future.
|
|
|
|
|
Editor: Introduce minimum height dimensions block support.
This changeset adds the new dimension feature's PHP code for supporting minimum height in the block editor inspector and in global styles. Minimum height is quite useful for defining the minimum vertical dimensions of a block, while allowing it to expand beyond that size.
In this changeset:
* Adds support in `theme.json`.
* Adds support in the style engine.
* Adds support in `wp_apply_dimensions_support()`.
* Renames the setting from `'__experimentalDimensions'` to `dimensions` in `wp_register_dimensions_support()`.
* Adds PHPUnit tests.
Is renaming `'__experimentalDimensions'` a backwards-compatibility (BC) break?
Though the setting has been in the code since 5.9.0, it was never wired to anything, ie it did not expose any controls or styles. Notice in `wp_register_dimensions_support()` and `wp_apply_dimensions_support()` prior to this changeset, there are inline comments as placeholders for height and width support, but no code.
If a developer opted in to use it, it had no effect.
A search in wp.org's plugin and themes repo showed no instances of this experimental setting.
Given there was no functionality attached to it (until this changeset), no change in behavior or effect from removing it, and no usage found in the plugins and themes repository, it does appear to be a BC break.
References:
* [https://github.com/WordPress/gutenberg/pull/45300 Gutenberg PR 45300]
* [https://github.com/WordPress/gutenberg/pull/45334 Gutenberg PR 45334]
Follow-up to [53076], [52069].
Props andrewserong, aaronrobertshaw , costdev, hellofromTonya, isabel_brison, joen, paaljoachim, mukesh27, ntsekouras, oandregal, ramonopoly.
Fixes #57582.
Built from https://develop.svn.wordpress.org/trunk@55175
git-svn-id: http://core.svn.wordpress.org/trunk@54708 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-02-01 11:15:15 -05:00
|
|
|
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
|
|
|
|
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
|
|
|
|
|
|
|
|
if ( ! $block_styles ) {
|
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
|
|
|
|
$dimensions_block_styles = array();
|
Editor: Reduce the use of the `_wp_array_get()` function to improve performance.
`_wp_array_get()` is an expensive function, and it's called thousands of times on each page view on the front end. While the function performance was slightly improved in #58376, it is still called more times than it should be.
This commit aims to further optimize its usage:
* In many cases, `_wp_array_get()` can be replaced with a much simpler and faster `isset()` check.
* The `isset()` function is capable of checking nested arrays, so `isset( $foo['a']['b']['c'] )` will return false even if `$foo['a']` is unset, without throwing any errors or warnings.
* When `_wp_array_get()` cannot be directly replaced with `isset()`, it would be good practice to wrap it in an `isset()` function so that `_wp_array_get()` only runs when it needs to.
Original PR from Gutenberg repository:
* [https://github.com/WordPress/gutenberg/pull/51116 #51116 Performance improvement: Reduce the use of the _wp_array_get() function]
Follow-up to [55851], [56382].
Props aristath, jrf, spacedmonkey, mukesh27, swissspidy, hellofromTonya.
Fixes #59405.
Built from https://develop.svn.wordpress.org/trunk@56709
git-svn-id: http://core.svn.wordpress.org/trunk@56221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 09:47:20 -04:00
|
|
|
$dimensions_block_styles['minHeight'] = null;
|
|
|
|
if ( $has_min_height_support && ! $skip_min_height ) {
|
|
|
|
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
|
|
|
|
? $block_styles['dimensions']['minHeight']
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
|
Editor: Introduce minimum height dimensions block support.
This changeset adds the new dimension feature's PHP code for supporting minimum height in the block editor inspector and in global styles. Minimum height is quite useful for defining the minimum vertical dimensions of a block, while allowing it to expand beyond that size.
In this changeset:
* Adds support in `theme.json`.
* Adds support in the style engine.
* Adds support in `wp_apply_dimensions_support()`.
* Renames the setting from `'__experimentalDimensions'` to `dimensions` in `wp_register_dimensions_support()`.
* Adds PHPUnit tests.
Is renaming `'__experimentalDimensions'` a backwards-compatibility (BC) break?
Though the setting has been in the code since 5.9.0, it was never wired to anything, ie it did not expose any controls or styles. Notice in `wp_register_dimensions_support()` and `wp_apply_dimensions_support()` prior to this changeset, there are inline comments as placeholders for height and width support, but no code.
If a developer opted in to use it, it had no effect.
A search in wp.org's plugin and themes repo showed no instances of this experimental setting.
Given there was no functionality attached to it (until this changeset), no change in behavior or effect from removing it, and no usage found in the plugins and themes repository, it does appear to be a BC break.
References:
* [https://github.com/WordPress/gutenberg/pull/45300 Gutenberg PR 45300]
* [https://github.com/WordPress/gutenberg/pull/45334 Gutenberg PR 45334]
Follow-up to [53076], [52069].
Props andrewserong, aaronrobertshaw , costdev, hellofromTonya, isabel_brison, joen, paaljoachim, mukesh27, ntsekouras, oandregal, ramonopoly.
Fixes #57582.
Built from https://develop.svn.wordpress.org/trunk@55175
git-svn-id: http://core.svn.wordpress.org/trunk@54708 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-02-01 11:15:15 -05:00
|
|
|
|
|
|
|
if ( ! empty( $styles['css'] ) ) {
|
|
|
|
$attributes['style'] = $styles['css'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attributes;
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
2024-01-30 21:54:17 -05:00
|
|
|
/**
|
|
|
|
* Renders server-side dimensions styles to the block wrapper.
|
|
|
|
* This block support uses the `render_block` hook to ensure that
|
|
|
|
* it is also applied to non-server-rendered blocks.
|
|
|
|
*
|
|
|
|
* @since 6.5.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param string $block_content Rendered block content.
|
|
|
|
* @param array $block Block object.
|
|
|
|
* @return string Filtered block content.
|
|
|
|
*/
|
|
|
|
function wp_render_dimensions_support( $block_content, $block ) {
|
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
|
|
|
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
|
|
|
|
$has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
|
|
|
|
|
|
|
|
if (
|
|
|
|
! $has_aspect_ratio_support ||
|
|
|
|
wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
|
|
|
|
) {
|
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dimensions_block_styles = array();
|
|
|
|
$dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
|
|
|
|
|
|
|
|
// To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
|
|
|
|
if (
|
|
|
|
isset( $dimensions_block_styles['aspectRatio'] )
|
|
|
|
) {
|
|
|
|
$dimensions_block_styles['minHeight'] = 'unset';
|
|
|
|
} elseif (
|
|
|
|
isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
|
|
|
|
isset( $block_attributes['minHeight'] )
|
|
|
|
) {
|
|
|
|
$dimensions_block_styles['aspectRatio'] = 'unset';
|
|
|
|
}
|
|
|
|
|
|
|
|
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
|
|
|
|
|
|
|
|
if ( ! empty( $styles['css'] ) ) {
|
|
|
|
// Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
|
|
|
|
$tags = new WP_HTML_Tag_Processor( $block_content );
|
|
|
|
|
|
|
|
if ( $tags->next_tag() ) {
|
|
|
|
$existing_style = $tags->get_attribute( 'style' );
|
|
|
|
$updated_style = '';
|
|
|
|
|
|
|
|
if ( ! empty( $existing_style ) ) {
|
|
|
|
$updated_style = $existing_style;
|
|
|
|
if ( ! str_ends_with( $existing_style, ';' ) ) {
|
|
|
|
$updated_style .= ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$updated_style .= $styles['css'];
|
|
|
|
$tags->set_attribute( 'style', $updated_style );
|
|
|
|
|
|
|
|
if ( ! empty( $styles['classnames'] ) ) {
|
|
|
|
foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
|
|
|
|
if (
|
|
|
|
str_contains( $class_name, 'aspect-ratio' ) &&
|
|
|
|
! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$tags->add_class( $class_name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags->get_updated_html();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 );
|
|
|
|
|
2021-11-08 21:17:17 -05:00
|
|
|
// Register the block support.
|
|
|
|
WP_Block_Supports::get_instance()->register(
|
|
|
|
'dimensions',
|
|
|
|
array(
|
|
|
|
'register_attribute' => 'wp_register_dimensions_support',
|
|
|
|
'apply' => 'wp_apply_dimensions_support',
|
|
|
|
)
|
|
|
|
);
|