2021-04-15 11:19:43 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Layout block support flag.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
|
2023-06-27 03:25:22 -04:00
|
|
|
/**
|
|
|
|
* Returns layout definitions, keyed by layout type.
|
|
|
|
*
|
|
|
|
* Provides a common definition of slugs, classnames, base styles, and spacing styles for each layout type.
|
|
|
|
* When making changes or additions to layout definitions, the corresponding JavaScript definitions should
|
|
|
|
* also be updated.
|
|
|
|
*
|
|
|
|
* @since 6.3.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @return array[] Layout definitions.
|
|
|
|
*/
|
|
|
|
function wp_get_layout_definitions() {
|
|
|
|
$layout_definitions = array(
|
|
|
|
'default' => array(
|
|
|
|
'name' => 'default',
|
|
|
|
'slug' => 'flow',
|
|
|
|
'className' => 'is-layout-flow',
|
|
|
|
'baseStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => ' > .alignleft',
|
|
|
|
'rules' => array(
|
|
|
|
'float' => 'left',
|
|
|
|
'margin-inline-start' => '0',
|
|
|
|
'margin-inline-end' => '2em',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > .alignright',
|
|
|
|
'rules' => array(
|
|
|
|
'float' => 'right',
|
|
|
|
'margin-inline-start' => '2em',
|
|
|
|
'margin-inline-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > .aligncenter',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-left' => 'auto !important',
|
|
|
|
'margin-right' => 'auto !important',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'spacingStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => ' > :first-child:first-child',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-block-start' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > :last-child:last-child',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > *',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-block-start' => null,
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'constrained' => array(
|
|
|
|
'name' => 'constrained',
|
|
|
|
'slug' => 'constrained',
|
|
|
|
'className' => 'is-layout-constrained',
|
|
|
|
'baseStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => ' > .alignleft',
|
|
|
|
'rules' => array(
|
|
|
|
'float' => 'left',
|
|
|
|
'margin-inline-start' => '0',
|
|
|
|
'margin-inline-end' => '2em',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > .alignright',
|
|
|
|
'rules' => array(
|
|
|
|
'float' => 'right',
|
|
|
|
'margin-inline-start' => '2em',
|
|
|
|
'margin-inline-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > .aligncenter',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-left' => 'auto !important',
|
|
|
|
'margin-right' => 'auto !important',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > :where(:not(.alignleft):not(.alignright):not(.alignfull))',
|
|
|
|
'rules' => array(
|
|
|
|
'max-width' => 'var(--wp--style--global--content-size)',
|
|
|
|
'margin-left' => 'auto !important',
|
|
|
|
'margin-right' => 'auto !important',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > .alignwide',
|
|
|
|
'rules' => array(
|
|
|
|
'max-width' => 'var(--wp--style--global--wide-size)',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'spacingStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => ' > :first-child:first-child',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-block-start' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > :last-child:last-child',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > *',
|
|
|
|
'rules' => array(
|
|
|
|
'margin-block-start' => null,
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'flex' => array(
|
|
|
|
'name' => 'flex',
|
|
|
|
'slug' => 'flex',
|
|
|
|
'className' => 'is-layout-flex',
|
|
|
|
'displayMode' => 'flex',
|
|
|
|
'baseStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => '',
|
|
|
|
'rules' => array(
|
|
|
|
'flex-wrap' => 'wrap',
|
|
|
|
'align-items' => 'center',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => ' > *',
|
|
|
|
'rules' => array(
|
|
|
|
'margin' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'spacingStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => '',
|
|
|
|
'rules' => array(
|
|
|
|
'gap' => null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'grid' => array(
|
|
|
|
'name' => 'grid',
|
|
|
|
'slug' => 'grid',
|
|
|
|
'className' => 'is-layout-grid',
|
|
|
|
'displayMode' => 'grid',
|
|
|
|
'baseStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => ' > *',
|
|
|
|
'rules' => array(
|
|
|
|
'margin' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'spacingStyles' => array(
|
|
|
|
array(
|
|
|
|
'selector' => '',
|
|
|
|
'rules' => array(
|
|
|
|
'gap' => null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $layout_definitions;
|
|
|
|
}
|
|
|
|
|
2021-05-25 03:36:58 -04:00
|
|
|
/**
|
|
|
|
* Registers the layout block attribute for block types that support it.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2023-06-27 03:25:22 -04:00
|
|
|
* @since 6.3.0 Check for layout support via the `layout` key with fallback to `__experimentalLayout`.
|
2021-05-25 03:36:58 -04:00
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param WP_Block_Type $block_type Block Type.
|
|
|
|
*/
|
|
|
|
function wp_register_layout_support( $block_type ) {
|
2023-08-10 12:48:19 -04:00
|
|
|
$support_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
|
2021-05-25 03:36:58 -04:00
|
|
|
if ( $support_layout ) {
|
|
|
|
if ( ! $block_type->attributes ) {
|
|
|
|
$block_type->attributes = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! array_key_exists( 'layout', $block_type->attributes ) ) {
|
|
|
|
$block_type->attributes['layout'] = array(
|
|
|
|
'type' => 'object',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 21:17:17 -05:00
|
|
|
/**
|
|
|
|
* Generates the CSS corresponding to the provided layout.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
* @since 6.1.0 Added `$block_spacing` param, use style engine to enqueue styles.
|
2023-06-22 00:06:26 -04:00
|
|
|
* @since 6.3.0 Added grid layout type.
|
2021-11-08 21:17:17 -05:00
|
|
|
* @access private
|
|
|
|
*
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
* @param string $selector CSS selector.
|
|
|
|
* @param array $layout Layout object. The one that is passed has already checked
|
|
|
|
* the existence of default block layout.
|
2022-09-21 09:42:09 -04:00
|
|
|
* @param bool $has_block_gap_support Optional. Whether the theme has support for the block gap. Default false.
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
* @param string|string[]|null $gap_value Optional. The block gap value to apply. Default null.
|
2022-09-21 09:42:09 -04:00
|
|
|
* @param bool $should_skip_gap_serialization Optional. Whether to skip applying the user-defined value set in the editor. Default false.
|
|
|
|
* @param string $fallback_gap_value Optional. The block gap value to apply. Default '0.5em'.
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
* @param array|null $block_spacing Optional. Custom spacing set on the block. Default null.
|
2022-09-21 09:42:09 -04:00
|
|
|
* @return string CSS styles on success. Else, empty string.
|
2021-11-08 21:17:17 -05:00
|
|
|
*/
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
function wp_get_layout_style( $selector, $layout, $has_block_gap_support = false, $gap_value = null, $should_skip_gap_serialization = false, $fallback_gap_value = '0.5em', $block_spacing = null ) {
|
|
|
|
$layout_type = isset( $layout['type'] ) ? $layout['type'] : 'default';
|
|
|
|
$layout_styles = array();
|
2021-11-08 21:17:17 -05:00
|
|
|
|
|
|
|
if ( 'default' === $layout_type ) {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if ( $has_block_gap_support ) {
|
|
|
|
if ( is_array( $gap_value ) ) {
|
|
|
|
$gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
|
|
|
|
}
|
|
|
|
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
|
2022-09-26 10:58:12 -04:00
|
|
|
// Get spacing CSS variable from preset value if provided.
|
|
|
|
if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
|
|
|
|
$index_to_splice = strrpos( $gap_value, '|' ) + 1;
|
|
|
|
$slug = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
|
|
|
|
$gap_value = "var(--wp--preset--spacing--$slug)";
|
|
|
|
}
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
array_push(
|
|
|
|
$layout_styles,
|
|
|
|
array(
|
|
|
|
'selector' => "$selector > *",
|
|
|
|
'declarations' => array(
|
|
|
|
'margin-block-start' => '0',
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => "$selector$selector > * + *",
|
|
|
|
'declarations' => array(
|
|
|
|
'margin-block-start' => $gap_value,
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( 'constrained' === $layout_type ) {
|
|
|
|
$content_size = isset( $layout['contentSize'] ) ? $layout['contentSize'] : '';
|
|
|
|
$wide_size = isset( $layout['wideSize'] ) ? $layout['wideSize'] : '';
|
|
|
|
$justify_content = isset( $layout['justifyContent'] ) ? $layout['justifyContent'] : 'center';
|
2021-11-08 21:17:17 -05:00
|
|
|
|
|
|
|
$all_max_width_value = $content_size ? $content_size : $wide_size;
|
|
|
|
$wide_max_width_value = $wide_size ? $wide_size : $content_size;
|
|
|
|
|
|
|
|
// Make sure there is a single CSS rule, and all tags are stripped for security.
|
2022-03-11 19:29:04 -05:00
|
|
|
$all_max_width_value = safecss_filter_attr( explode( ';', $all_max_width_value )[0] );
|
|
|
|
$wide_max_width_value = safecss_filter_attr( explode( ';', $wide_max_width_value )[0] );
|
2021-11-08 21:17:17 -05:00
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$margin_left = 'left' === $justify_content ? '0 !important' : 'auto !important';
|
|
|
|
$margin_right = 'right' === $justify_content ? '0 !important' : 'auto !important';
|
|
|
|
|
2021-11-08 21:17:17 -05:00
|
|
|
if ( $content_size || $wide_size ) {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
array_push(
|
|
|
|
$layout_styles,
|
|
|
|
array(
|
|
|
|
'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
|
|
|
|
'declarations' => array(
|
|
|
|
'max-width' => $all_max_width_value,
|
|
|
|
'margin-left' => $margin_left,
|
|
|
|
'margin-right' => $margin_right,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => "$selector > .alignwide",
|
|
|
|
'declarations' => array( 'max-width' => $wide_max_width_value ),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => "$selector .alignfull",
|
|
|
|
'declarations' => array( 'max-width' => 'none' ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $block_spacing ) ) {
|
|
|
|
$block_spacing_values = wp_style_engine_get_styles(
|
|
|
|
array(
|
|
|
|
'spacing' => $block_spacing,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle negative margins for alignfull children of blocks with custom padding set.
|
|
|
|
* They're added separately because padding might only be set on one side.
|
|
|
|
*/
|
|
|
|
if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
|
|
|
|
$padding_right = $block_spacing_values['declarations']['padding-right'];
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => "$selector > .alignfull",
|
|
|
|
'declarations' => array( 'margin-right' => "calc($padding_right * -1)" ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( isset( $block_spacing_values['declarations']['padding-left'] ) ) {
|
|
|
|
$padding_left = $block_spacing_values['declarations']['padding-left'];
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => "$selector > .alignfull",
|
|
|
|
'declarations' => array( 'margin-left' => "calc($padding_left * -1)" ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'left' === $justify_content ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
|
|
|
|
'declarations' => array( 'margin-left' => '0 !important' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'right' === $justify_content ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => "$selector > :where(:not(.alignleft):not(.alignright):not(.alignfull))",
|
|
|
|
'declarations' => array( 'margin-right' => '0 !important' ),
|
|
|
|
);
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_block_gap_support ) {
|
2022-04-06 07:41:05 -04:00
|
|
|
if ( is_array( $gap_value ) ) {
|
|
|
|
$gap_value = isset( $gap_value['top'] ) ? $gap_value['top'] : null;
|
|
|
|
}
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
|
|
|
|
// Get spacing CSS variable from preset value if provided.
|
|
|
|
if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
|
|
|
|
$index_to_splice = strrpos( $gap_value, '|' ) + 1;
|
|
|
|
$slug = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
|
|
|
|
$gap_value = "var(--wp--preset--spacing--$slug)";
|
|
|
|
}
|
|
|
|
|
|
|
|
array_push(
|
|
|
|
$layout_styles,
|
|
|
|
array(
|
|
|
|
'selector' => "$selector > *",
|
|
|
|
'declarations' => array(
|
|
|
|
'margin-block-start' => '0',
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'selector' => "$selector$selector > * + *",
|
|
|
|
'declarations' => array(
|
|
|
|
'margin-block-start' => $gap_value,
|
|
|
|
'margin-block-end' => '0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
} elseif ( 'flex' === $layout_type ) {
|
|
|
|
$layout_orientation = isset( $layout['orientation'] ) ? $layout['orientation'] : 'horizontal';
|
|
|
|
|
|
|
|
$justify_content_options = array(
|
|
|
|
'left' => 'flex-start',
|
|
|
|
'right' => 'flex-end',
|
|
|
|
'center' => 'center',
|
|
|
|
);
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$vertical_alignment_options = array(
|
|
|
|
'top' => 'flex-start',
|
|
|
|
'center' => 'center',
|
|
|
|
'bottom' => 'flex-end',
|
|
|
|
);
|
|
|
|
|
2021-11-08 21:17:17 -05:00
|
|
|
if ( 'horizontal' === $layout_orientation ) {
|
2023-02-02 19:36:17 -05:00
|
|
|
$justify_content_options += array( 'space-between' => 'space-between' );
|
|
|
|
$vertical_alignment_options += array( 'stretch' => 'stretch' );
|
|
|
|
} else {
|
|
|
|
$justify_content_options += array( 'stretch' => 'stretch' );
|
|
|
|
$vertical_alignment_options += array( 'space-between' => 'space-between' );
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if ( ! empty( $layout['flexWrap'] ) && 'nowrap' === $layout['flexWrap'] ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'flex-wrap' => 'nowrap' ),
|
|
|
|
);
|
|
|
|
}
|
2021-11-08 21:17:17 -05:00
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if ( $has_block_gap_support && isset( $gap_value ) ) {
|
|
|
|
$combined_gap_value = '';
|
|
|
|
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
|
|
|
|
|
|
|
|
foreach ( $gap_sides as $gap_side ) {
|
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
|
|
|
$process_value = $gap_value;
|
|
|
|
if ( is_array( $gap_value ) ) {
|
|
|
|
$process_value = isset( $gap_value[ $gap_side ] ) ? $gap_value[ $gap_side ] : $fallback_gap_value;
|
|
|
|
}
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
// Get spacing CSS variable from preset value if provided.
|
|
|
|
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
|
|
|
|
$index_to_splice = strrpos( $process_value, '|' ) + 1;
|
|
|
|
$slug = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
|
|
|
|
$process_value = "var(--wp--preset--spacing--$slug)";
|
|
|
|
}
|
|
|
|
$combined_gap_value .= "$process_value ";
|
|
|
|
}
|
|
|
|
$gap_value = trim( $combined_gap_value );
|
|
|
|
|
|
|
|
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'gap' => $gap_value ),
|
|
|
|
);
|
2022-04-06 07:41:05 -04:00
|
|
|
}
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
2022-04-06 07:41:05 -04:00
|
|
|
|
2021-11-08 21:17:17 -05:00
|
|
|
if ( 'horizontal' === $layout_orientation ) {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
/*
|
2021-11-08 21:17:17 -05:00
|
|
|
* Add this style only if is not empty for backwards compatibility,
|
|
|
|
* since we intend to convert blocks that had flex layout implemented
|
|
|
|
* by custom css.
|
|
|
|
*/
|
|
|
|
if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'justify-content' => $justify_content_options[ $layout['justifyContent'] ] ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'align-items' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
|
|
|
|
);
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
} else {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'flex-direction' => 'column' ),
|
|
|
|
);
|
2021-11-08 21:17:17 -05:00
|
|
|
if ( ! empty( $layout['justifyContent'] ) && array_key_exists( $layout['justifyContent'], $justify_content_options ) ) {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'align-items' => $justify_content_options[ $layout['justifyContent'] ] ),
|
|
|
|
);
|
2022-04-06 07:41:05 -04:00
|
|
|
} else {
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'align-items' => 'flex-start' ),
|
|
|
|
);
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
2023-02-02 19:36:17 -05:00
|
|
|
if ( ! empty( $layout['verticalAlignment'] ) && array_key_exists( $layout['verticalAlignment'], $vertical_alignment_options ) ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'justify-content' => $vertical_alignment_options[ $layout['verticalAlignment'] ] ),
|
|
|
|
);
|
|
|
|
}
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
2023-06-22 00:06:26 -04:00
|
|
|
} elseif ( 'grid' === $layout_type ) {
|
|
|
|
if ( ! empty( $layout['columnCount'] ) ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'grid-template-columns' => 'repeat(' . $layout['columnCount'] . ', minmax(0, 1fr))' ),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$minimum_column_width = ! empty( $layout['minimumColumnWidth'] ) ? $layout['minimumColumnWidth'] : '12rem';
|
|
|
|
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'grid-template-columns' => 'repeat(auto-fill, minmax(min(' . $minimum_column_width . ', 100%), 1fr))' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $has_block_gap_support && isset( $gap_value ) ) {
|
|
|
|
$combined_gap_value = '';
|
|
|
|
$gap_sides = is_array( $gap_value ) ? array( 'top', 'left' ) : array( 'top' );
|
|
|
|
|
|
|
|
foreach ( $gap_sides as $gap_side ) {
|
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
|
|
|
$process_value = $gap_value;
|
|
|
|
if ( is_array( $gap_value ) ) {
|
|
|
|
$process_value = isset( $gap_value[ $gap_side ] ) ? $gap_value[ $gap_side ] : $fallback_gap_value;
|
|
|
|
}
|
2023-06-22 00:06:26 -04:00
|
|
|
// Get spacing CSS variable from preset value if provided.
|
|
|
|
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
|
|
|
|
$index_to_splice = strrpos( $process_value, '|' ) + 1;
|
|
|
|
$slug = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
|
|
|
|
$process_value = "var(--wp--preset--spacing--$slug)";
|
|
|
|
}
|
|
|
|
$combined_gap_value .= "$process_value ";
|
|
|
|
}
|
|
|
|
$gap_value = trim( $combined_gap_value );
|
|
|
|
|
|
|
|
if ( null !== $gap_value && ! $should_skip_gap_serialization ) {
|
|
|
|
$layout_styles[] = array(
|
|
|
|
'selector' => $selector,
|
|
|
|
'declarations' => array( 'gap' => $gap_value ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
}
|
2021-11-08 21:17:17 -05:00
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if ( ! empty( $layout_styles ) ) {
|
|
|
|
/*
|
|
|
|
* Add to the style engine store to enqueue and render layout styles.
|
|
|
|
* Return compiled layout styles to retain backwards compatibility.
|
|
|
|
* Since https://github.com/WordPress/gutenberg/pull/42452,
|
|
|
|
* wp_enqueue_block_support_styles is no longer called in this block supports file.
|
|
|
|
*/
|
|
|
|
return wp_style_engine_get_stylesheet_from_css_rules(
|
|
|
|
$layout_styles,
|
|
|
|
array(
|
|
|
|
'context' => 'block-supports',
|
|
|
|
'prettify' => false,
|
|
|
|
)
|
|
|
|
);
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
return '';
|
2021-11-08 21:17:17 -05:00
|
|
|
}
|
|
|
|
|
2021-05-25 03:36:58 -04:00
|
|
|
/**
|
|
|
|
* Renders the layout config to the block wrapper.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
2023-06-21 01:32:19 -04:00
|
|
|
* @since 6.3.0 Adds compound class to layout wrapper for global spacing styles.
|
2023-06-27 03:25:22 -04:00
|
|
|
* @since 6.3.0 Check for layout support via the `layout` key with fallback to `__experimentalLayout`.
|
2021-05-25 03:36:58 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2021-07-01 17:29:56 -04:00
|
|
|
* @param string $block_content Rendered block content.
|
|
|
|
* @param array $block Block object.
|
|
|
|
* @return string Filtered block content.
|
2021-05-25 03:36:58 -04:00
|
|
|
*/
|
|
|
|
function wp_render_layout_support_flag( $block_content, $block ) {
|
2023-09-26 01:04:22 -04:00
|
|
|
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
|
|
|
$block_supports_layout = block_has_support( $block_type, 'layout', false ) || block_has_support( $block_type, '__experimentalLayout', false );
|
2023-09-26 02:55:12 -04:00
|
|
|
$layout_from_parent = isset( $block['attrs']['style']['layout']['selfStretch'] ) ? $block['attrs']['style']['layout']['selfStretch'] : null;
|
2021-11-08 21:17:17 -05:00
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
if ( ! $block_supports_layout && ! $layout_from_parent ) {
|
2021-05-25 03:36:58 -04:00
|
|
|
return $block_content;
|
|
|
|
}
|
2023-09-26 01:04:22 -04:00
|
|
|
|
2023-02-07 12:44:21 -05:00
|
|
|
$outer_class_names = array();
|
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
if ( 'fixed' === $layout_from_parent || 'fill' === $layout_from_parent ) {
|
2023-02-07 12:44:21 -05:00
|
|
|
$container_content_class = wp_unique_id( 'wp-container-content-' );
|
|
|
|
|
|
|
|
$child_layout_styles = array();
|
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
if ( 'fixed' === $layout_from_parent && isset( $block['attrs']['style']['layout']['flexSize'] ) ) {
|
2023-02-07 12:44:21 -05:00
|
|
|
$child_layout_styles[] = array(
|
|
|
|
'selector' => ".$container_content_class",
|
|
|
|
'declarations' => array(
|
|
|
|
'flex-basis' => $block['attrs']['style']['layout']['flexSize'],
|
|
|
|
'box-sizing' => 'border-box',
|
|
|
|
),
|
|
|
|
);
|
2023-09-26 01:04:22 -04:00
|
|
|
} elseif ( 'fill' === $layout_from_parent ) {
|
2023-02-07 12:44:21 -05:00
|
|
|
$child_layout_styles[] = array(
|
|
|
|
'selector' => ".$container_content_class",
|
|
|
|
'declarations' => array(
|
|
|
|
'flex-grow' => '1',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_style_engine_get_stylesheet_from_css_rules(
|
|
|
|
$child_layout_styles,
|
|
|
|
array(
|
|
|
|
'context' => 'block-supports',
|
|
|
|
'prettify' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$outer_class_names[] = $container_content_class;
|
|
|
|
}
|
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
// Prep the processor for modifying the block output.
|
|
|
|
$processor = new WP_HTML_Tag_Processor( $block_content );
|
|
|
|
|
|
|
|
// Having no tags implies there are no tags onto which to add class names.
|
|
|
|
if ( ! $processor->next_tag() ) {
|
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A block may not support layout but still be affected by a parent block's layout.
|
|
|
|
*
|
|
|
|
* In these cases add the appropriate class names and then return early; there's
|
|
|
|
* no need to investigate on this block whether additional layout constraints apply.
|
|
|
|
*/
|
|
|
|
if ( ! $block_supports_layout && ! empty( $outer_class_names ) ) {
|
|
|
|
foreach ( $outer_class_names as $class_name ) {
|
|
|
|
$processor->add_class( $class_name );
|
|
|
|
}
|
|
|
|
return $processor->get_updated_html();
|
2024-01-23 00:38:13 -05:00
|
|
|
} elseif ( ! $block_supports_layout ) {
|
|
|
|
// Ensure layout classnames are not injected if there is no layout support.
|
|
|
|
return $block_content;
|
2023-02-07 12:44:21 -05:00
|
|
|
}
|
2021-05-25 03:36:58 -04:00
|
|
|
|
2023-06-27 03:25:22 -04:00
|
|
|
$global_settings = wp_get_global_settings();
|
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
|
|
|
$fallback_layout = isset( $block_type->supports['layout']['default'] )
|
|
|
|
? $block_type->supports['layout']['default']
|
|
|
|
: array();
|
|
|
|
if ( empty( $fallback_layout ) ) {
|
|
|
|
$fallback_layout = isset( $block_type->supports['__experimentalLayout']['default'] )
|
|
|
|
? $block_type->supports['__experimentalLayout']['default']
|
|
|
|
: array();
|
|
|
|
}
|
|
|
|
$used_layout = isset( $block['attrs']['layout'] ) ? $block['attrs']['layout'] : $fallback_layout;
|
2021-05-25 03:36:58 -04:00
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$class_names = array();
|
2023-06-27 03:25:22 -04:00
|
|
|
$layout_definitions = wp_get_layout_definitions();
|
Blocks: Fix layout support to be compatible with enhanced pagination.
Make layout support compatible with enhanced pagination by ensuring that generated class names are stable across pagination, even when the number of rendered posts is different.
With the previous implementation of enhanced pagination, the CSS corresponding to each block was not detected. Therefore, for enhanced pagination to work correctly, the CSS of the blocks present in the Post Template must be stable on all pages.
The number of posts rendered by the Query block is always the same, except in the last page, where it can be only a fraction. If any of the blocks rendered by the Post Template used the `wp_unique_id` function, the ID (which is incremental) would have been different than in the previous pages and the class names would have varied.
This is remediated by this changeset by replacing the usage of `wp_unique_id` in the layout support (which is used by the Query block) with an implementation that uses IDs that are incremental only for that block. That way, the generated class names are never affected by the number of times `wp_unique_id` runs.
Props luisherranz, andrewserong, isabel_brison, costdev, mukesh27, cbravobernal, hellofromTonya, jorbin.
Fixes #59681.
Built from https://develop.svn.wordpress.org/trunk@56994
git-svn-id: http://core.svn.wordpress.org/trunk@56505 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-24 04:51:24 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Uses an incremental ID that is independent per prefix to make sure that
|
|
|
|
* rendering different numbers of blocks doesn't affect the IDs of other
|
|
|
|
* blocks. Makes the CSS class names stable across paginations
|
|
|
|
* for features like the enhanced pagination of the Query block.
|
|
|
|
*/
|
|
|
|
$container_class = wp_unique_prefixed_id(
|
2024-01-08 01:13:20 -05:00
|
|
|
'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
|
Blocks: Fix layout support to be compatible with enhanced pagination.
Make layout support compatible with enhanced pagination by ensuring that generated class names are stable across pagination, even when the number of rendered posts is different.
With the previous implementation of enhanced pagination, the CSS corresponding to each block was not detected. Therefore, for enhanced pagination to work correctly, the CSS of the blocks present in the Post Template must be stable on all pages.
The number of posts rendered by the Query block is always the same, except in the last page, where it can be only a fraction. If any of the blocks rendered by the Post Template used the `wp_unique_id` function, the ID (which is incremental) would have been different than in the previous pages and the class names would have varied.
This is remediated by this changeset by replacing the usage of `wp_unique_id` in the layout support (which is used by the Query block) with an implementation that uses IDs that are incremental only for that block. That way, the generated class names are never affected by the number of times `wp_unique_id` runs.
Props luisherranz, andrewserong, isabel_brison, costdev, mukesh27, cbravobernal, hellofromTonya, jorbin.
Fixes #59681.
Built from https://develop.svn.wordpress.org/trunk@56994
git-svn-id: http://core.svn.wordpress.org/trunk@56505 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-10-24 04:51:24 -04:00
|
|
|
);
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
|
|
|
|
// Set the correct layout type for blocks using legacy content width.
|
|
|
|
if ( isset( $used_layout['inherit'] ) && $used_layout['inherit'] || isset( $used_layout['contentSize'] ) && $used_layout['contentSize'] ) {
|
|
|
|
$used_layout['type'] = 'constrained';
|
|
|
|
}
|
2022-06-23 19:09:09 -04:00
|
|
|
|
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
|
|
|
$root_padding_aware_alignments = isset( $global_settings['useRootPaddingAwareAlignments'] )
|
|
|
|
? $global_settings['useRootPaddingAwareAlignments']
|
|
|
|
: false;
|
2023-03-13 12:32:20 -04:00
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if (
|
Update: Improve performance of gutenberg_render_layout_support_flag.
Backports https://github.com/WordPress/gutenberg/pull/46074 into the core.
render_layout_support_flag is run per block, and inside we called get_global_settings three times. get_global_settings calls get_merged_data, which is costly. render_layout_support_flag is a filter called during the block render. When the blocks start rendering, there is no expectation that the theme.json settings change during the block render, so the settings and their derived information should all be static information of this function.
This simple change removes 3*NUMBER_OF_BLOCKS calls of get_merged_data to just one call.
Props oandregal, aristath, felixarntz, tellthemachines, andrewserong, aaronrobertshaw, aaronrobertshaw.
Built from https://develop.svn.wordpress.org/trunk@55167
git-svn-id: http://core.svn.wordpress.org/trunk@54700 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-31 10:24:14 -05:00
|
|
|
$root_padding_aware_alignments &&
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
isset( $used_layout['type'] ) &&
|
|
|
|
'constrained' === $used_layout['type']
|
|
|
|
) {
|
|
|
|
$class_names[] = 'has-global-padding';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following section was added to reintroduce a small set of layout classnames that were
|
|
|
|
* removed in the 5.9 release (https://github.com/WordPress/gutenberg/issues/38719). It is
|
|
|
|
* not intended to provide an extended set of classes to match all block layout attributes
|
|
|
|
* here.
|
|
|
|
*/
|
2022-06-23 19:09:09 -04:00
|
|
|
if ( ! empty( $block['attrs']['layout']['orientation'] ) ) {
|
|
|
|
$class_names[] = 'is-' . sanitize_title( $block['attrs']['layout']['orientation'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $block['attrs']['layout']['justifyContent'] ) ) {
|
|
|
|
$class_names[] = 'is-content-justification-' . sanitize_title( $block['attrs']['layout']['justifyContent'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $block['attrs']['layout']['flexWrap'] ) && 'nowrap' === $block['attrs']['layout']['flexWrap'] ) {
|
|
|
|
$class_names[] = 'is-nowrap';
|
|
|
|
}
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
// Get classname for layout type.
|
|
|
|
if ( isset( $used_layout['type'] ) ) {
|
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
|
|
|
$layout_classname = isset( $layout_definitions[ $used_layout['type'] ]['className'] )
|
|
|
|
? $layout_definitions[ $used_layout['type'] ]['className']
|
|
|
|
: '';
|
2022-04-06 07:41:05 -04:00
|
|
|
} else {
|
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
|
|
|
$layout_classname = isset( $layout_definitions['default']['className'] )
|
|
|
|
? $layout_definitions['default']['className']
|
|
|
|
: '';
|
2022-04-06 07:41:05 -04:00
|
|
|
}
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
if ( $layout_classname && is_string( $layout_classname ) ) {
|
|
|
|
$class_names[] = sanitize_title( $layout_classname );
|
|
|
|
}
|
2022-05-20 10:32:40 -04:00
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
/*
|
|
|
|
* Only generate Layout styles if the theme has not opted-out.
|
|
|
|
* Attribute-based Layout classnames are output in all cases.
|
|
|
|
*/
|
|
|
|
if ( ! current_theme_supports( 'disable-layout-styles' ) ) {
|
|
|
|
|
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
|
|
|
$gap_value = isset( $block['attrs']['style']['spacing']['blockGap'] )
|
|
|
|
? $block['attrs']['style']['spacing']['blockGap']
|
|
|
|
: null;
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
/*
|
|
|
|
* Skip if gap value contains unsupported characters.
|
|
|
|
* Regex for CSS value borrowed from `safecss_filter_attr`, and used here
|
|
|
|
* to only match against the value, not the CSS attribute.
|
|
|
|
*/
|
|
|
|
if ( is_array( $gap_value ) ) {
|
|
|
|
foreach ( $gap_value as $key => $value ) {
|
|
|
|
$gap_value[ $key ] = $value && preg_match( '%[\\\(&=}]|/\*%', $value ) ? null : $value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$gap_value = $gap_value && preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ? null : $gap_value;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
$fallback_gap_value = isset( $block_type->supports['spacing']['blockGap']['__experimentalDefault'] )
|
|
|
|
? $block_type->supports['spacing']['blockGap']['__experimentalDefault']
|
|
|
|
: '0.5em';
|
|
|
|
$block_spacing = isset( $block['attrs']['style']['spacing'] )
|
|
|
|
? $block['attrs']['style']['spacing']
|
|
|
|
: null;
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If a block's block.json skips serialization for spacing or spacing.blockGap,
|
|
|
|
* don't apply the user-defined value to the styles.
|
|
|
|
*/
|
|
|
|
$should_skip_gap_serialization = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'blockGap' );
|
|
|
|
|
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
|
|
|
$block_gap = isset( $global_settings['spacing']['blockGap'] )
|
|
|
|
? $global_settings['spacing']['blockGap']
|
|
|
|
: null;
|
2023-03-13 12:32:20 -04:00
|
|
|
$has_block_gap_support = isset( $block_gap );
|
|
|
|
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$style = wp_get_layout_style(
|
2023-02-07 12:44:21 -05:00
|
|
|
".$container_class.$container_class",
|
Editor: Improves layout block support in `wp_get_layout_style()`.
This commit merges the remaining changes from [https://github.com/WordPress/gutenberg/pull/40875 Gutenberg PR 40875]. It's Part 2 (see [54162] for Part 1) of a layout improvement initiative and targets `wp_get_layout_style()` in `layout.php`.
Context:
The overall initiative is to improve layout block support:
>to use centralised layout definitions, output base layout styles from global styles, introduce a layout type semantic classname, reduce duplication of container class and style tag output, and fix blockGap at the block level in global styles.
Changes include:
* Adding an optional parameter `$block_spacing` to `wp_get_layout_style()` for setting a custom spacing on the block.
* Adding handle for the block spacing.
* Using the style engine to to enqueue and render layout styles via `wp_style_engine_get_stylesheet_from_css_rules()`.
* Introduces a new test file for `wp_get_layout_style()`.
Follow-up to [54162], [54160], [54159], [53421], [52380], [53085], [52069].
Props andrewserong, isabel_brison, costdev, hellofromTonya.
See #56467.
Built from https://develop.svn.wordpress.org/trunk@54274
git-svn-id: http://core.svn.wordpress.org/trunk@53833 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-21 09:02:09 -04:00
|
|
|
$used_layout,
|
|
|
|
$has_block_gap_support,
|
|
|
|
$gap_value,
|
|
|
|
$should_skip_gap_serialization,
|
|
|
|
$fallback_gap_value,
|
|
|
|
$block_spacing
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only add container class and enqueue block support styles if unique styles were generated.
|
|
|
|
if ( ! empty( $style ) ) {
|
|
|
|
$class_names[] = $container_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 01:32:19 -04:00
|
|
|
// Add combined layout and block classname for global styles to hook onto.
|
|
|
|
$block_name = explode( '/', $block['blockName'] );
|
|
|
|
$class_names[] = 'wp-block-' . end( $block_name ) . '-' . $layout_classname;
|
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
// Add classes to the outermost HTML tag if necessary.
|
2023-02-07 12:44:21 -05:00
|
|
|
if ( ! empty( $outer_class_names ) ) {
|
|
|
|
foreach ( $outer_class_names as $outer_class_name ) {
|
2023-09-26 01:04:22 -04:00
|
|
|
$processor->add_class( $outer_class_name );
|
2023-02-07 12:44:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-09-26 01:04:22 -04:00
|
|
|
* Attempts to refer to the inner-block wrapping element by its class attribute.
|
|
|
|
*
|
|
|
|
* When examining a block's inner content, if a block has inner blocks, then
|
|
|
|
* the first content item will likely be a text (HTML) chunk immediately
|
|
|
|
* preceding the inner blocks. The last HTML tag in that chunk would then be
|
|
|
|
* an opening tag for an element that wraps the inner blocks.
|
|
|
|
*
|
|
|
|
* There's no reliable way to associate this wrapper in $block_content because
|
|
|
|
* it may have changed during the rendering pipeline (as inner contents is
|
|
|
|
* provided before rendering) and through previous filters. In many cases,
|
|
|
|
* however, the `class` attribute will be a good-enough identifier, so this
|
|
|
|
* code finds the last tag in that chunk and stores the `class` attribute
|
|
|
|
* so that it can be used later when working through the rendered block output
|
|
|
|
* to identify the wrapping element and add the remaining class names to it.
|
|
|
|
*
|
|
|
|
* It's also possible that no inner block wrapper even exists. If that's the
|
|
|
|
* case this code could apply the class names to an invalid element.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* $block['innerBlocks'] = array( $list_item );
|
|
|
|
* $block['innerContent'] = array( '<ul class="list-wrapper is-unordered">', null, '</ul>' );
|
|
|
|
*
|
|
|
|
* // After rendering, the initial contents may have been modified by other renderers or filters.
|
|
|
|
* $block_content = <<<HTML
|
|
|
|
* <figure>
|
|
|
|
* <ul class="annotated-list list-wrapper is-unordered">
|
|
|
|
* <li>Code</li>
|
|
|
|
* </ul><figcaption>It's a list!</figcaption>
|
|
|
|
* </figure>
|
|
|
|
* HTML;
|
|
|
|
*
|
|
|
|
* Although it is possible that the original block-wrapper classes are changed in $block_content
|
|
|
|
* from how they appear in $block['innerContent'], it's likely that the original class attributes
|
|
|
|
* are still present in the wrapper as they are in this example. Frequently, additional classes
|
|
|
|
* will also be present; rarely should classes be removed.
|
|
|
|
*
|
2023-11-06 19:14:23 -05:00
|
|
|
* @todo Find a better way to match the first inner block. If it's possible to identify where the
|
|
|
|
* first inner block starts, then it will be possible to find the last tag before it starts
|
|
|
|
* and then that tag, if an opening tag, can be solidly identified as a wrapping element.
|
|
|
|
* Can some unique value or class or ID be added to the inner blocks when they process
|
|
|
|
* so that they can be extracted here safely without guessing? Can the block rendering function
|
|
|
|
* return information about where the rendered inner blocks start?
|
2023-09-26 01:04:22 -04:00
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
$inner_block_wrapper_classes = null;
|
2023-09-26 02:55:12 -04:00
|
|
|
$first_chunk = isset( $block['innerContent'][0] ) ? $block['innerContent'][0] : null;
|
2023-09-26 01:04:22 -04:00
|
|
|
if ( is_string( $first_chunk ) && count( $block['innerContent'] ) > 1 ) {
|
|
|
|
$first_chunk_processor = new WP_HTML_Tag_Processor( $first_chunk );
|
|
|
|
while ( $first_chunk_processor->next_tag() ) {
|
|
|
|
$class_attribute = $first_chunk_processor->get_attribute( 'class' );
|
|
|
|
if ( is_string( $class_attribute ) && ! empty( $class_attribute ) ) {
|
|
|
|
$inner_block_wrapper_classes = $class_attribute;
|
|
|
|
}
|
2023-02-07 12:44:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
/*
|
|
|
|
* If necessary, advance to what is likely to be an inner block wrapper tag.
|
|
|
|
*
|
|
|
|
* This advances until it finds the first tag containing the original class
|
|
|
|
* attribute from above. If none is found it will scan to the end of the block
|
|
|
|
* and fail to add any class names.
|
|
|
|
*
|
|
|
|
* If there is no block wrapper it won't advance at all, in which case the
|
|
|
|
* class names will be added to the first and outermost tag of the block.
|
|
|
|
* For cases where this outermost tag is the only tag surrounding inner
|
|
|
|
* blocks then the outer wrapper and inner wrapper are the same.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
if ( ! $inner_block_wrapper_classes ) {
|
|
|
|
break;
|
2023-02-07 12:44:21 -05:00
|
|
|
}
|
2023-09-26 01:04:22 -04:00
|
|
|
|
|
|
|
if ( false !== strpos( $processor->get_attribute( 'class' ), $inner_block_wrapper_classes ) ) {
|
|
|
|
break;
|
2023-02-07 12:44:21 -05:00
|
|
|
}
|
2023-09-26 01:04:22 -04:00
|
|
|
} while ( $processor->next_tag() );
|
|
|
|
|
|
|
|
// Add the remaining class names.
|
|
|
|
foreach ( $class_names as $class_name ) {
|
|
|
|
$processor->add_class( $class_name );
|
2023-02-07 12:44:21 -05:00
|
|
|
}
|
|
|
|
|
2023-09-26 01:04:22 -04:00
|
|
|
return $processor->get_updated_html();
|
2021-05-25 03:36:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register the block support.
|
|
|
|
WP_Block_Supports::get_instance()->register(
|
|
|
|
'layout',
|
|
|
|
array(
|
|
|
|
'register_attribute' => 'wp_register_layout_support',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
add_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
|
|
|
|
|
2021-04-15 11:19:43 -04:00
|
|
|
/**
|
|
|
|
* For themes without theme.json file, make sure
|
|
|
|
* to restore the inner div for the group block
|
|
|
|
* to avoid breaking styles relying on that div.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
* @access private
|
|
|
|
*
|
2021-05-19 11:09:27 -04:00
|
|
|
* @param string $block_content Rendered block content.
|
|
|
|
* @param array $block Block object.
|
|
|
|
* @return string Filtered block content.
|
2021-04-15 11:19:43 -04:00
|
|
|
*/
|
|
|
|
function wp_restore_group_inner_container( $block_content, $block ) {
|
2021-11-11 02:43:31 -05:00
|
|
|
$tag_name = isset( $block['attrs']['tagName'] ) ? $block['attrs']['tagName'] : 'div';
|
|
|
|
$group_with_inner_container_regex = sprintf(
|
2021-11-15 07:50:17 -05:00
|
|
|
'/(^\s*<%1$s\b[^>]*wp-block-group(\s|")[^>]*>)(\s*<div\b[^>]*wp-block-group__inner-container(\s|")[^>]*>)((.|\S|\s)*)/U',
|
2021-11-11 02:43:31 -05:00
|
|
|
preg_quote( $tag_name, '/' )
|
|
|
|
);
|
2021-04-15 11:19:43 -04:00
|
|
|
|
|
|
|
if (
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
wp_theme_has_theme_json() ||
|
2021-11-08 21:17:17 -05:00
|
|
|
1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
|
2022-10-18 10:47:15 -04:00
|
|
|
( isset( $block['attrs']['layout']['type'] ) && 'flex' === $block['attrs']['layout']['type'] )
|
2021-04-15 11:19:43 -04:00
|
|
|
) {
|
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
2024-01-08 01:13:20 -05:00
|
|
|
/*
|
|
|
|
* This filter runs after the layout classnames have been added to the block, so they
|
|
|
|
* have to be removed from the outer wrapper and then added to the inner.
|
|
|
|
*/
|
|
|
|
$layout_classes = array();
|
|
|
|
$processor = new WP_HTML_Tag_Processor( $block_content );
|
|
|
|
|
|
|
|
if ( $processor->next_tag( array( 'class_name' => 'wp-block-group' ) ) ) {
|
|
|
|
foreach ( $processor->class_list() as $class_name ) {
|
|
|
|
if ( str_contains( $class_name, 'is-layout-' ) ) {
|
|
|
|
$layout_classes[] = $class_name;
|
|
|
|
$processor->remove_class( $class_name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$content_without_layout_classes = $processor->get_updated_html();
|
|
|
|
$replace_regex = sprintf(
|
2021-11-11 02:43:31 -05:00
|
|
|
'/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
|
|
|
|
preg_quote( $tag_name, '/' )
|
|
|
|
);
|
2024-01-08 01:13:20 -05:00
|
|
|
$updated_content = preg_replace_callback(
|
2021-04-15 11:19:43 -04:00
|
|
|
$replace_regex,
|
2023-09-12 11:23:18 -04:00
|
|
|
static function ( $matches ) {
|
2021-04-15 11:19:43 -04:00
|
|
|
return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
|
|
|
|
},
|
2024-01-08 01:13:20 -05:00
|
|
|
$content_without_layout_classes
|
2021-04-15 11:19:43 -04:00
|
|
|
);
|
2024-01-08 01:13:20 -05:00
|
|
|
|
|
|
|
// Add layout classes to inner wrapper.
|
|
|
|
if ( ! empty( $layout_classes ) ) {
|
|
|
|
$processor = new WP_HTML_Tag_Processor( $updated_content );
|
|
|
|
if ( $processor->next_tag( array( 'class_name' => 'wp-block-group__inner-container' ) ) ) {
|
|
|
|
foreach ( $layout_classes as $class_name ) {
|
|
|
|
$processor->add_class( $class_name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$updated_content = $processor->get_updated_html();
|
|
|
|
}
|
2021-04-15 11:19:43 -04:00
|
|
|
return $updated_content;
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:41:05 -04:00
|
|
|
add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For themes without theme.json file, make sure
|
|
|
|
* to restore the outer div for the aligned image block
|
|
|
|
* to avoid breaking styles relying on that div.
|
|
|
|
*
|
|
|
|
* @since 6.0.0
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param string $block_content Rendered block content.
|
|
|
|
* @param array $block Block object.
|
|
|
|
* @return string Filtered block content.
|
|
|
|
*/
|
|
|
|
function wp_restore_image_outer_container( $block_content, $block ) {
|
|
|
|
$image_with_align = "
|
|
|
|
/# 1) everything up to the class attribute contents
|
|
|
|
(
|
|
|
|
^\s*
|
|
|
|
<figure\b
|
|
|
|
[^>]*
|
|
|
|
\bclass=
|
|
|
|
[\"']
|
|
|
|
)
|
|
|
|
# 2) the class attribute contents
|
|
|
|
(
|
|
|
|
[^\"']*
|
|
|
|
\bwp-block-image\b
|
|
|
|
[^\"']*
|
|
|
|
\b(?:alignleft|alignright|aligncenter)\b
|
|
|
|
[^\"']*
|
|
|
|
)
|
|
|
|
# 3) everything after the class attribute contents
|
|
|
|
(
|
|
|
|
[\"']
|
|
|
|
[^>]*
|
|
|
|
>
|
|
|
|
.*
|
|
|
|
<\/figure>
|
|
|
|
)/iUx";
|
|
|
|
|
|
|
|
if (
|
Themes: Introduce wp_theme_has_theme_json() for public consumption.
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file.
For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).
Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`.
References:
* [https://github.com/WordPress/gutenberg/pull/45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`.
* [https://github.com/WordPress/gutenberg/pull/45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`.
* [https://github.com/WordPress/gutenberg/pull/46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent.
* [https://github.com/WordPress/gutenberg/pull/45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined.
* [https://github.com/WordPress/gutenberg/pull/45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`.
Follow-up to [54493], [53282], [52744], [52049], [50959].
Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.
Built from https://develop.svn.wordpress.org/trunk@55086
git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-18 06:40:10 -05:00
|
|
|
wp_theme_has_theme_json() ||
|
2022-04-06 07:41:05 -04:00
|
|
|
0 === preg_match( $image_with_align, $block_content, $matches )
|
|
|
|
) {
|
|
|
|
return $block_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
$wrapper_classnames = array( 'wp-block-image' );
|
|
|
|
|
|
|
|
// If the block has a classNames attribute these classnames need to be removed from the content and added back
|
|
|
|
// to the new wrapper div also.
|
|
|
|
if ( ! empty( $block['attrs']['className'] ) ) {
|
|
|
|
$wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) );
|
|
|
|
}
|
|
|
|
$content_classnames = explode( ' ', $matches[2] );
|
|
|
|
$filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames );
|
|
|
|
|
|
|
|
return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );
|