woocommerce
/
packages
/
email-editor
/
src
/
Engine
/
Renderer
/
ContentRenderer
/
class-preset-variable-resolver.php
Layout
1 month ago
Postprocessors
9 months ago
Preprocessors
1 month ago
class-block-renderer.php
11 months ago
class-blocks-parser.php
1 year ago
class-content-renderer.php
1 month ago
class-preset-variable-resolver.php
2 months ago
class-process-manager.php
1 month ago
class-rendering-context.php
1 month ago
content.css
1 year ago
class-preset-variable-resolver.php
83 lines
| 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 |