PluginProbe
WooCommerce / 11.0.1
WooCommerce v11.0.1
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / packages / email-editor / src / Engine / Renderer / ContentRenderer / class-preset-variable-resolver.php
class-preset-variable-resolver.php
83 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the WooCommerce Email Editor package
4 *
5 * @package Automattic\WooCommerce\EmailEditor
6 */
7
8 declare(strict_types = 1);
9 namespace Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer;
10
11 /**
12 * Resolves WordPress preset variable references to their actual values.
13 *
14 * Block attributes store spacing values as preset references like
15 * "var:preset|spacing|20". This class provides shared methods to:
16 * - Convert these references to CSS variable names (--wp--preset--spacing--20)
17 * - Resolve them to pixel values (e.g. "20px") using a variables map
18 * - Convert them to CSS var() syntax for stylesheet use
19 *
20 * Used by Content_Renderer, Spacing_Preprocessor, and Blocks_Width_Preprocessor
21 * to avoid duplicating the same resolution logic.
22 */
23 class Preset_Variable_Resolver {
24 /**
25 * Convert a preset variable reference to its CSS variable name.
26 *
27 * Transforms "var:preset|spacing|20" to "--wp--preset--spacing--20".
28 *
29 * @param string $value The preset reference string.
30 * @return string The CSS variable name.
31 */
32 private static function to_css_variable_name( string $value ): string {
33 return '--wp--' . str_replace( '|', '--', str_replace( 'var:', '', $value ) );
34 }
35
36 /**
37 * Check if a value is a preset variable reference.
38 *
39 * @param string $value The CSS value to check.
40 * @return bool True if the value starts with "var:preset|".
41 */
42 public static function is_preset_reference( string $value ): bool {
43 return strpos( $value, 'var:preset|' ) === 0;
44 }
45
46 /**
47 * Resolve a preset variable reference to its actual value.
48 *
49 * Converts "var:preset|spacing|20" to the resolved pixel value (e.g. "20px")
50 * using the provided variables map. Returns the original value if not a preset
51 * reference or if the variable is not found in the map.
52 *
53 * @param string $value The CSS value, possibly a preset reference.
54 * @param array $variables_map Map of CSS variable names to resolved values.
55 * @return string The resolved value or the original value.
56 */
57 public static function resolve( string $value, array $variables_map ): string {
58 if ( empty( $variables_map ) || ! self::is_preset_reference( $value ) ) {
59 return $value;
60 }
61
62 $css_var_name = self::to_css_variable_name( $value );
63 return $variables_map[ $css_var_name ] ?? $value;
64 }
65
66 /**
67 * Convert a preset variable reference to CSS var() syntax.
68 *
69 * Transforms "var:preset|spacing|20" to "var(--wp--preset--spacing--20)".
70 * Returns the original value if not a preset reference.
71 *
72 * @param string $value The CSS value, possibly a preset reference.
73 * @return string The CSS var() expression or the original value.
74 */
75 public static function to_css_var( string $value ): string {
76 if ( ! self::is_preset_reference( $value ) ) {
77 return $value;
78 }
79
80 return 'var(' . self::to_css_variable_name( $value ) . ')';
81 }
82 }
83