summaryrefslogtreecommitdiffstats
path: root/wp-includes/block-supports
diff options
context:
space:
mode:
Diffstat (limited to 'wp-includes/block-supports')
-rw-r--r--wp-includes/block-supports/background.php26
-rw-r--r--wp-includes/block-supports/dimensions.php80
-rw-r--r--wp-includes/block-supports/elements.php4
-rw-r--r--wp-includes/block-supports/layout.php54
-rw-r--r--wp-includes/block-supports/shadow.php5
-rw-r--r--wp-includes/block-supports/typography.php9
6 files changed, 158 insertions, 20 deletions
diff --git a/wp-includes/block-supports/background.php b/wp-includes/block-supports/background.php
index a8de0c6..9b82e6a 100644
--- a/wp-includes/block-supports/background.php
+++ b/wp-includes/block-supports/background.php
@@ -40,6 +40,7 @@ function wp_register_background_support( $block_type ) {
* it is also applied to non-server-rendered blocks.
*
* @since 6.4.0
+ * @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output.
* @access private
*
* @param string $block_content Rendered block content.
@@ -64,9 +65,20 @@ function wp_render_background_support( $block_content, $block ) {
$background_image_url = isset( $block_attributes['style']['background']['backgroundImage']['url'] )
? $block_attributes['style']['background']['backgroundImage']['url']
: null;
- $background_size = isset( $block_attributes['style']['background']['backgroundSize'] )
+
+ if ( ! $background_image_source && ! $background_image_url ) {
+ return $block_content;
+ }
+
+ $background_size = isset( $block_attributes['style']['background']['backgroundSize'] )
? $block_attributes['style']['background']['backgroundSize']
: 'cover';
+ $background_position = isset( $block_attributes['style']['background']['backgroundPosition'] )
+ ? $block_attributes['style']['background']['backgroundPosition']
+ : null;
+ $background_repeat = isset( $block_attributes['style']['background']['backgroundRepeat'] )
+ ? $block_attributes['style']['background']['backgroundRepeat']
+ : null;
$background_block_styles = array();
@@ -76,8 +88,15 @@ function wp_render_background_support( $block_content, $block ) {
) {
// Set file based background URL.
$background_block_styles['backgroundImage']['url'] = $background_image_url;
- // Only output the background size when an image url is set.
- $background_block_styles['backgroundSize'] = $background_size;
+ // Only output the background size and repeat when an image url is set.
+ $background_block_styles['backgroundSize'] = $background_size;
+ $background_block_styles['backgroundRepeat'] = $background_repeat;
+ $background_block_styles['backgroundPosition'] = $background_position;
+
+ // If the background size is set to `contain` and no position is set, set the position to `center`.
+ if ( 'contain' === $background_size && ! isset( $background_position ) ) {
+ $background_block_styles['backgroundPosition'] = 'center';
+ }
}
$styles = wp_style_engine_get_styles( array( 'background' => $background_block_styles ) );
@@ -99,6 +118,7 @@ function wp_render_background_support( $block_content, $block ) {
$updated_style .= $styles['css'];
$tags->set_attribute( 'style', $updated_style );
+ $tags->add_class( 'has-background' );
}
return $tags->get_updated_html();
diff --git a/wp-includes/block-supports/dimensions.php b/wp-includes/block-supports/dimensions.php
index a889e78..da68f18 100644
--- a/wp-includes/block-supports/dimensions.php
+++ b/wp-includes/block-supports/dimensions.php
@@ -83,6 +83,86 @@ function wp_apply_dimensions_support( $block_type, $block_attributes ) {
return $attributes;
}
+/**
+ * Renders server-side dimensions styles to the block wrapper.
+ * This block support uses the `render_block` hook to ensure that
+ * it is also applied to non-server-rendered blocks.
+ *
+ * @since 6.5.0
+ * @access private
+ *
+ * @param string $block_content Rendered block content.
+ * @param array $block Block object.
+ * @return string Filtered block content.
+ */
+function wp_render_dimensions_support( $block_content, $block ) {
+ $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
+ $block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
+ $has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
+
+ if (
+ ! $has_aspect_ratio_support ||
+ wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
+ ) {
+ return $block_content;
+ }
+
+ $dimensions_block_styles = array();
+ $dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
+
+ // To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
+ if (
+ isset( $dimensions_block_styles['aspectRatio'] )
+ ) {
+ $dimensions_block_styles['minHeight'] = 'unset';
+ } elseif (
+ isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
+ isset( $block_attributes['minHeight'] )
+ ) {
+ $dimensions_block_styles['aspectRatio'] = 'unset';
+ }
+
+ $styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
+
+ if ( ! empty( $styles['css'] ) ) {
+ // Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
+ $tags = new WP_HTML_Tag_Processor( $block_content );
+
+ if ( $tags->next_tag() ) {
+ $existing_style = $tags->get_attribute( 'style' );
+ $updated_style = '';
+
+ if ( ! empty( $existing_style ) ) {
+ $updated_style = $existing_style;
+ if ( ! str_ends_with( $existing_style, ';' ) ) {
+ $updated_style .= ';';
+ }
+ }
+
+ $updated_style .= $styles['css'];
+ $tags->set_attribute( 'style', $updated_style );
+
+ if ( ! empty( $styles['classnames'] ) ) {
+ foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
+ if (
+ str_contains( $class_name, 'aspect-ratio' ) &&
+ ! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
+ ) {
+ continue;
+ }
+ $tags->add_class( $class_name );
+ }
+ }
+ }
+
+ return $tags->get_updated_html();
+ }
+
+ return $block_content;
+}
+
+add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 );
+
// Register the block support.
WP_Block_Supports::get_instance()->register(
'dimensions',
diff --git a/wp-includes/block-supports/elements.php b/wp-includes/block-supports/elements.php
index 4f3de8c..e7fa76a 100644
--- a/wp-includes/block-supports/elements.php
+++ b/wp-includes/block-supports/elements.php
@@ -166,8 +166,8 @@ function wp_render_elements_support_styles( $pre_render, $block ) {
'skip' => $skip_button_color_serialization,
),
'link' => array(
- 'selector' => ".$class_name a",
- 'hover_selector' => ".$class_name a:hover",
+ 'selector' => ".$class_name a:where(:not(.wp-element-button))",
+ 'hover_selector' => ".$class_name a:where(:not(.wp-element-button)):hover",
'skip' => $skip_link_color_serialization,
),
'heading' => array(
diff --git a/wp-includes/block-supports/layout.php b/wp-includes/block-supports/layout.php
index 0e22dde..f5acd75 100644
--- a/wp-includes/block-supports/layout.php
+++ b/wp-includes/block-supports/layout.php
@@ -615,6 +615,9 @@ function wp_render_layout_support_flag( $block_content, $block ) {
$processor->add_class( $class_name );
}
return $processor->get_updated_html();
+ } elseif ( ! $block_supports_layout ) {
+ // Ensure layout classnames are not injected if there is no layout support.
+ return $block_content;
}
$global_settings = wp_get_global_settings();
@@ -638,7 +641,7 @@ function wp_render_layout_support_flag( $block_content, $block ) {
* for features like the enhanced pagination of the Query block.
*/
$container_class = wp_unique_prefixed_id(
- 'wp-container-' . sanitize_title( $block['blockName'] ) . '-layout-'
+ 'wp-container-' . sanitize_title( $block['blockName'] ) . '-is-layout-'
);
// Set the correct layout type for blocks using legacy content width.
@@ -796,12 +799,12 @@ function wp_render_layout_support_flag( $block_content, $block ) {
* are still present in the wrapper as they are in this example. Frequently, additional classes
* will also be present; rarely should classes be removed.
*
- * @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?
+ * @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?
*
* @var string|null
*/
@@ -834,7 +837,8 @@ function wp_render_layout_support_flag( $block_content, $block ) {
break;
}
- if ( false !== strpos( $processor->get_attribute( 'class' ), $inner_block_wrapper_classes ) ) {
+ $class_attribute = $processor->get_attribute( 'class' );
+ if ( is_string( $class_attribute ) && str_contains( $class_attribute, $inner_block_wrapper_classes ) ) {
break;
}
} while ( $processor->next_tag() );
@@ -883,17 +887,45 @@ function wp_restore_group_inner_container( $block_content, $block ) {
return $block_content;
}
- $replace_regex = sprintf(
+ /*
+ * 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(
'/(^\s*<%1$s\b[^>]*wp-block-group[^>]*>)(.*)(<\/%1$s>\s*$)/ms',
preg_quote( $tag_name, '/' )
);
- $updated_content = preg_replace_callback(
+ $updated_content = preg_replace_callback(
$replace_regex,
static function ( $matches ) {
return $matches[1] . '<div class="wp-block-group__inner-container">' . $matches[2] . '</div>' . $matches[3];
},
- $block_content
+ $content_without_layout_classes
);
+
+ // 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();
+ }
return $updated_content;
}
diff --git a/wp-includes/block-supports/shadow.php b/wp-includes/block-supports/shadow.php
index 6fa05b2..0ccaf3f 100644
--- a/wp-includes/block-supports/shadow.php
+++ b/wp-includes/block-supports/shadow.php
@@ -58,9 +58,8 @@ function wp_apply_shadow_support( $block_type, $block_attributes ) {
$shadow_block_styles = array();
- $preset_shadow = array_key_exists( 'shadow', $block_attributes ) ? "var:preset|shadow|{$block_attributes['shadow']}" : null;
- $custom_shadow = isset( $block_attributes['style']['shadow'] ) ? $block_attributes['style']['shadow'] : null;
- $shadow_block_styles['shadow'] = $preset_shadow ? $preset_shadow : $custom_shadow;
+ $custom_shadow = $block_attributes['style']['shadow'] ?? null;
+ $shadow_block_styles['shadow'] = $custom_shadow;
$attributes = array();
$styles = wp_style_engine_get_styles( $shadow_block_styles );
diff --git a/wp-includes/block-supports/typography.php b/wp-includes/block-supports/typography.php
index bccde4f..e7d081c 100644
--- a/wp-includes/block-supports/typography.php
+++ b/wp-includes/block-supports/typography.php
@@ -398,6 +398,7 @@ function wp_get_typography_value_and_unit( $raw_value, $options = array() ) {
*
* @since 6.1.0
* @since 6.3.0 Checks for unsupported min/max viewport values that cause invalid clamp values.
+ * @since 6.5.0 Returns early when min and max viewport subtraction is zero to avoid division by zero.
* @access private
*
* @param array $args {
@@ -468,12 +469,18 @@ function wp_get_computed_fluid_typography_value( $args = array() ) {
return null;
}
+ // Calculates the linear factor denominator. If it's 0, we cannot calculate a fluid value.
+ $linear_factor_denominator = $maximum_viewport_width['value'] - $minimum_viewport_width['value'];
+ if ( empty( $linear_factor_denominator ) ) {
+ return null;
+ }
+
/*
* Build CSS rule.
* Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
*/
$view_port_width_offset = round( $minimum_viewport_width['value'] / 100, 3 ) . $font_size_unit;
- $linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $maximum_viewport_width['value'] - $minimum_viewport_width['value'] ) );
+ $linear_factor = 100 * ( ( $maximum_font_size['value'] - $minimum_font_size['value'] ) / ( $linear_factor_denominator ) );
$linear_factor_scaled = round( $linear_factor * $scale_factor, 3 );
$linear_factor_scaled = empty( $linear_factor_scaled ) ? 1 : $linear_factor_scaled;
$fluid_target_font_size = implode( '', $minimum_font_size_rem ) . " + ((1vw - $view_port_width_offset) * $linear_factor_scaled)";