Skip to content

Commit d4b30ab

Browse files
Block Supports: Add the translate feature to the transform block support.
An offset is saved inline like the rotation. It replaces the global styles offset whole, and an axis it leaves out is 0, as in CSS. Transform tools, including translate, are switched on by appearanceTools.
1 parent 855ca45 commit d4b30ab

9 files changed

Lines changed: 708 additions & 17 deletions

File tree

‎src/wp-includes/class-wp-theme-json.php‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class WP_Theme_JSON {
261261
* Added `text-indent` property.
262262
* @since 7.1.0 Added `min-width` and `text-shadow`.
263263
* @since 7.2.0 Added `background-clip`.
264-
* @since 7.3.0 Added `rotate`.
264+
* @since 7.3.0 Added `rotate` and `translate`.
265265
* @var array
266266
*/
267267
const PROPERTIES_METADATA = array(
@@ -320,6 +320,7 @@ class WP_Theme_JSON {
320320
'padding-bottom' => array( 'spacing', 'padding', 'bottom' ),
321321
'padding-left' => array( 'spacing', 'padding', 'left' ),
322322
'rotate' => array( 'transform', 'rotate' ),
323+
'translate' => array( 'transform', 'translate' ),
323324
'--wp--style--root--padding' => array( 'spacing', 'padding' ),
324325
'--wp--style--root--padding-top' => array( 'spacing', 'padding', 'top' ),
325326
'--wp--style--root--padding-right' => array( 'spacing', 'padding', 'right' ),
@@ -437,7 +438,7 @@ class WP_Theme_JSON {
437438
* Added support for `background.gradient`, `dimensions.minWidth` and `blockVisibility.allowEditing`.
438439
* @since 7.2.0 Added support for `typography.textShadow`, `typography.textShadowPresets`,
439440
* and `typography.defaultTextShadowPresets`.
440-
* @since 7.3.0 Added support for `transform.rotate`.
441+
* @since 7.3.0 Added support for `transform.rotate` and `transform.translate`.
441442
* @var array
442443
*/
443444
const VALID_SETTINGS = array(
@@ -516,7 +517,8 @@ class WP_Theme_JSON {
516517
'defaultPresets' => null,
517518
),
518519
'transform' => array(
519-
'rotate' => null,
520+
'rotate' => null,
521+
'translate' => null,
520522
),
521523
'typography' => array(
522524
'fluid' => null,
@@ -594,7 +596,7 @@ class WP_Theme_JSON {
594596
* @since 7.1.0 Added support for `background.gradient`,`dimensions.minWidth`,
595597
* and `typography.textShadow`.
596598
* @since 7.2.0 Added support for `background.backgroundClip`.
597-
* @since 7.3.0 Added support for `transform.rotate`, for blocks and block style variations only.
599+
* @since 7.3.0 Added support for `transform.rotate` and `transform.translate`, for blocks and block style variations only.
598600
* @var array
599601
*/
600602
const VALID_STYLES = array(
@@ -645,7 +647,11 @@ class WP_Theme_JSON {
645647
'blockGap' => null,
646648
),
647649
'transform' => array(
648-
'rotate' => null,
650+
'rotate' => null,
651+
'translate' => array(
652+
'x' => null,
653+
'y' => null,
654+
),
649655
),
650656
'typography' => array(
651657
'fontFamily' => null,
@@ -1097,7 +1103,7 @@ public static function get_element_class_name( $element ) {
10971103
* @since 7.0.0 Added `dimensions.width` and `dimensions.height`.
10981104
* @since 7.1.0 Added `background.gradient`.
10991105
* Added `dimensions.minWidth`.
1100-
* @since 7.3.0 Added `transform.rotate`.
1106+
* @since 7.3.0 Added `transform.rotate` and `transform.translate`.
11011107
* @var array
11021108
*/
11031109
const APPEARANCE_TOOLS_OPT_INS = array(
@@ -1122,6 +1128,7 @@ public static function get_element_class_name( $element ) {
11221128
array( 'spacing', 'margin' ),
11231129
array( 'spacing', 'padding' ),
11241130
array( 'transform', 'rotate' ),
1131+
array( 'transform', 'translate' ),
11251132
array( 'typography', 'lineHeight' ),
11261133
array( 'typography', 'textColumns' ),
11271134
);
@@ -5410,6 +5417,7 @@ protected static function remove_insecure_settings( $input, $is_root = false ) {
54105417
* without the insecure styles.
54115418
*
54125419
* @since 5.9.0
5420+
* @since 7.3.0 Keep the `transform.translate` axes when their declaration is safe.
54135421
*
54145422
* @param array $input Node to process.
54155423
* @return array
@@ -5429,6 +5437,19 @@ protected static function remove_insecure_styles( $input ) {
54295437
$value = _wp_array_get( $input, $path, array() );
54305438
if ( ! is_array( $value ) ) {
54315439
_wp_array_set( $output, $path, $value );
5440+
} elseif ( 'translate' === $declaration['name'] ) {
5441+
// The declaration only exists when every axis is valid.
5442+
_wp_array_set(
5443+
$output,
5444+
$path,
5445+
array_intersect_key(
5446+
$value,
5447+
array(
5448+
'x' => true,
5449+
'y' => true,
5450+
)
5451+
)
5452+
);
54325453
}
54335454
}
54345455
}

‎src/wp-includes/kses.php‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,7 +3727,8 @@ function kses_init() {
37273727
* and URLs in the SVG element reference properties.
37283728
* @since 7.2.0 Added support for CSS anchor positioning properties and `text-shadow`.
37293729
* Added support for `background-clip`.
3730-
* @since 7.3.0 Added support for `rotate`, limited to a single angle.
3730+
* @since 7.3.0 Added support for `rotate`, limited to a single angle, and `translate`,
3731+
* limited to one or two lengths or percentages.
37313732
*
37323733
* @param string $css A string of CSS rules, decoded from an HTML `style` attribute.
37333734
* @param string $deprecated Not used.
@@ -3978,6 +3979,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
39783979
'transform',
39793980
'transform-origin',
39803981
'rotate',
3982+
'translate',
39813983

39823984
'pointer-events',
39833985
'visibility',
@@ -4050,6 +4052,7 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
40504052
$url_attr = false;
40514053
$gradient_attr = false;
40524054
$angle_attr = false;
4055+
$translate_attr = false;
40534056
$is_custom_var = false;
40544057

40554058
if ( ! str_contains( $css_item, ':' ) ) {
@@ -4065,10 +4068,11 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
40654068
}
40664069

40674070
if ( in_array( $css_selector, $allowed_attr, true ) ) {
4068-
$found = true;
4069-
$url_attr = in_array( $css_selector, $css_url_data_types, true );
4070-
$gradient_attr = in_array( $css_selector, $css_gradient_data_types, true );
4071-
$angle_attr = 'rotate' === $css_selector;
4071+
$found = true;
4072+
$url_attr = in_array( $css_selector, $css_url_data_types, true );
4073+
$gradient_attr = in_array( $css_selector, $css_gradient_data_types, true );
4074+
$angle_attr = 'rotate' === $css_selector;
4075+
$translate_attr = 'translate' === $css_selector;
40724076
}
40734077

40744078
if ( $is_custom_var ) {
@@ -4155,6 +4159,17 @@ function safecss_filter_attr( $css, $deprecated = '' ) {
41554159
$allow_css = 1 === preg_match( WP_Style_Engine::VALID_ANGLE_PATTERN, trim( $parts[1] ) );
41564160
}
41574161

4162+
// Only allow one or two lengths or percentages for `translate`, mirroring the style engine.
4163+
if ( $allow_css && $translate_attr ) {
4164+
$axes = preg_split( '/\s+/', trim( $parts[1] ) );
4165+
$allow_css = count( $axes ) <= 2;
4166+
foreach ( $axes as $axis ) {
4167+
if ( $allow_css && 1 !== preg_match( WP_Style_Engine::VALID_LENGTH_PERCENTAGE_PATTERN, $axis ) ) {
4168+
$allow_css = false;
4169+
}
4170+
}
4171+
}
4172+
41584173
/**
41594174
* Filters the check for unsafe CSS in `safecss_filter_attr`.
41604175
*

‎src/wp-includes/style-engine/class-wp-style-engine.php‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @since 6.7.0 Added support for typography.writingMode.
2929
* @since 7.0.0 Added support for typography.textIndent.
3030
* @since 7.2.0 Added support for typography.textShadow.
31-
* @since 7.3.0 Added support for transform.rotate.
31+
* @since 7.3.0 Added support for transform.rotate and transform.translate.
3232
*/
3333
#[AllowDynamicProperties]
3434
final class WP_Style_Engine {
@@ -48,6 +48,14 @@ final class WP_Style_Engine {
4848
*/
4949
const VALID_ANGLE_PATTERN = '/^(?:[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:deg|grad|rad|turn)|0)$/iD';
5050

51+
/**
52+
* Matches a single CSS length or percentage: a number with a length unit or `%`, or `0`.
53+
*
54+
* @since 7.3.0
55+
* @var string
56+
*/
57+
const VALID_LENGTH_PERCENTAGE_PATTERN = '/^(?:[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:%|px|cm|mm|q|in|pt|pc|r?em|r?ex|r?ch|r?cap|r?ic|r?lh|[sld]?v(?:w|h|i|b|min|max)|cq(?:w|h|i|b|min|max))|0)$/iD';
58+
5159
/**
5260
* Style definitions that contain the instructions to parse/output valid Gutenberg styles from a block's attributes.
5361
*
@@ -68,7 +76,7 @@ final class WP_Style_Engine {
6876
* @since 6.1.0
6977
* @since 7.1.0 Added `background.gradient` property.
7078
* @since 7.2.0 Added `background.backgroundClip` property.
71-
* @since 7.3.0 Added `transform.rotate` property.
79+
* @since 7.3.0 Added `transform.rotate` and `transform.translate` properties.
7280
* @var array
7381
*/
7482
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
@@ -314,13 +322,20 @@ final class WP_Style_Engine {
314322
),
315323
),
316324
'transform' => array(
317-
'rotate' => array(
325+
'rotate' => array(
318326
'property_keys' => array(
319327
'default' => 'rotate',
320328
),
321329
'value_func' => array( self::class, 'get_angle_css_declarations' ),
322330
'path' => array( 'transform', 'rotate' ),
323331
),
332+
'translate' => array(
333+
'property_keys' => array(
334+
'default' => 'translate',
335+
),
336+
'value_func' => array( self::class, 'get_translate_css_declarations' ),
337+
'path' => array( 'transform', 'translate' ),
338+
),
324339
),
325340
'typography' => array(
326341
'fontSize' => array(
@@ -859,6 +874,36 @@ protected static function get_angle_css_declarations( $style_value, $style_defin
859874
return array( $style_definition['property_keys']['default'] => $style_value );
860875
}
861876

877+
/**
878+
* Style value parser that returns a CSS definition array for the `translate` property.
879+
*
880+
* The value is an object with `x` and `y` axes, each a single length or
881+
* percentage. A missing axis outputs `0`. Any invalid axis outputs nothing,
882+
* and other keys are ignored.
883+
*
884+
* @since 7.3.0
885+
*
886+
* @param array $style_value A single raw style value from $block_styles array.
887+
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
888+
* @return array<string, string> An associative array of CSS definitions, mapping properties to values.
889+
*/
890+
protected static function get_translate_css_declarations( $style_value, $style_definition ) {
891+
if ( ! is_array( $style_value ) || ( ! isset( $style_value['x'] ) && ! isset( $style_value['y'] ) ) ) {
892+
return array();
893+
}
894+
895+
$axes = array();
896+
foreach ( array( 'x', 'y' ) as $axis ) {
897+
$axis_value = $style_value[ $axis ] ?? '0';
898+
if ( ! is_string( $axis_value ) || ! preg_match( static::VALID_LENGTH_PERCENTAGE_PATTERN, $axis_value ) ) {
899+
return array();
900+
}
901+
$axes[] = $axis_value;
902+
}
903+
904+
return array( $style_definition['property_keys']['default'] => implode( ' ', $axes ) );
905+
}
906+
862907
/**
863908
* Returns compiled CSS from CSS declarations.
864909
*

0 commit comments

Comments
 (0)