woocommerce
/
packages
/
email-editor
/
src
/
Engine
/
Renderer
/
ContentRenderer
/
class-rendering-context.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-rendering-context.php
303 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 | use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper; |
| 12 | use WP_Theme_JSON; |
| 13 | |
| 14 | /** |
| 15 | * Class Rendering_Context |
| 16 | */ |
| 17 | class Rendering_Context { |
| 18 | /** |
| 19 | * Instance of the WP Theme. |
| 20 | * |
| 21 | * @var WP_Theme_JSON |
| 22 | */ |
| 23 | private WP_Theme_JSON $theme_json; |
| 24 | |
| 25 | /** |
| 26 | * Email-specific context data. |
| 27 | * |
| 28 | * This array contains email-specific information that can be used during rendering, |
| 29 | * such as: |
| 30 | * - 'user_id': The ID of the user receiving the email |
| 31 | * - 'recipient_email': The recipient's email address |
| 32 | * - Additional context can be added by extensions using the generic get() method |
| 33 | * |
| 34 | * @var array<string, mixed> |
| 35 | */ |
| 36 | private array $email_context; |
| 37 | |
| 38 | /** |
| 39 | * Email language used for deterministic direction fallback. |
| 40 | * |
| 41 | * @var string|null |
| 42 | */ |
| 43 | private ?string $language; |
| 44 | |
| 45 | /** |
| 46 | * Rendering_Context constructor. |
| 47 | * |
| 48 | * @param WP_Theme_JSON $theme_json Theme Json used in the email. |
| 49 | * @param array<string, mixed> $email_context Email-specific context data. |
| 50 | * @param string|null $language Email language. |
| 51 | */ |
| 52 | public function __construct( WP_Theme_JSON $theme_json, array $email_context = array(), ?string $language = null ) { |
| 53 | $this->theme_json = $theme_json; |
| 54 | $this->email_context = $email_context; |
| 55 | $this->language = $language; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns WP_Theme_JSON instance that should be used during the email rendering. |
| 60 | * |
| 61 | * @return WP_Theme_JSON |
| 62 | */ |
| 63 | public function get_theme_json(): WP_Theme_JSON { |
| 64 | return $this->theme_json; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the email theme styles. |
| 69 | * |
| 70 | * @return array{ |
| 71 | * spacing: array{ |
| 72 | * blockGap: string, |
| 73 | * padding: array{bottom: string, left: string, right: string, top: string} |
| 74 | * }, |
| 75 | * color: array{ |
| 76 | * background: string, |
| 77 | * text: string |
| 78 | * }, |
| 79 | * typography: array{ |
| 80 | * fontFamily: string |
| 81 | * } |
| 82 | * } |
| 83 | */ |
| 84 | public function get_theme_styles(): array { |
| 85 | $theme = $this->get_theme_json(); |
| 86 | return $theme->get_data()['styles'] ?? array(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get settings from the theme. |
| 91 | * |
| 92 | * @return array |
| 93 | */ |
| 94 | public function get_theme_settings() { |
| 95 | return $this->get_theme_json()->get_settings(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the width of the layout without padding. |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function get_layout_width_without_padding(): string { |
| 104 | $styles = $this->get_theme_styles(); |
| 105 | $layout_settings = $this->get_theme_settings()['layout'] ?? array(); |
| 106 | $width = Styles_Helper::parse_value( $layout_settings['contentSize'] ?? '0px' ); |
| 107 | $padding = $styles['spacing']['padding'] ?? array(); |
| 108 | $width -= Styles_Helper::parse_value( $padding['left'] ?? '0px' ); |
| 109 | $width -= Styles_Helper::parse_value( $padding['right'] ?? '0px' ); |
| 110 | return "{$width}px"; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Translate color slug to color. |
| 115 | * |
| 116 | * @param string $color_slug Color slug. |
| 117 | * @return string |
| 118 | */ |
| 119 | public function translate_slug_to_color( string $color_slug ): string { |
| 120 | $settings = $this->get_theme_settings(); |
| 121 | |
| 122 | $color_definitions = array_merge( |
| 123 | $settings['color']['palette']['theme'] ?? array(), |
| 124 | $settings['color']['palette']['default'] ?? array() |
| 125 | ); |
| 126 | foreach ( $color_definitions as $color_definition ) { |
| 127 | if ( $color_definition['slug'] === $color_slug ) { |
| 128 | return strtolower( $color_definition['color'] ); |
| 129 | } |
| 130 | } |
| 131 | return $color_slug; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the email-specific context data. |
| 136 | * |
| 137 | * @return array<string, mixed> |
| 138 | */ |
| 139 | public function get_email_context(): array { |
| 140 | return $this->email_context; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the email language. |
| 145 | * |
| 146 | * @return string|null |
| 147 | */ |
| 148 | public function get_language(): ?string { |
| 149 | return $this->language; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Get the user ID from the email context. |
| 154 | * |
| 155 | * @return int|null The user ID if available, null otherwise. |
| 156 | */ |
| 157 | public function get_user_id(): ?int { |
| 158 | return isset( $this->email_context['user_id'] ) && is_numeric( $this->email_context['user_id'] ) ? (int) $this->email_context['user_id'] : null; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get the recipient email address from the email context. |
| 163 | * |
| 164 | * @return string|null The email address if available, null otherwise. |
| 165 | */ |
| 166 | public function get_recipient_email(): ?string { |
| 167 | return isset( $this->email_context['recipient_email'] ) && is_string( $this->email_context['recipient_email'] ) ? $this->email_context['recipient_email'] : null; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get a specific value from the email context. |
| 172 | * |
| 173 | * This method allows extensions to access custom context data that may be |
| 174 | * specific to their implementation (e.g., order IDs, email types, etc.). |
| 175 | * |
| 176 | * @param string $key The context key. |
| 177 | * @param mixed $default_value Default value if key is not found. |
| 178 | * @return mixed The context value or default. |
| 179 | */ |
| 180 | public function get( string $key, $default_value = null ) { |
| 181 | return $this->email_context[ $key ] ?? $default_value; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Whether this render should use right-to-left direction. |
| 186 | * |
| 187 | * @return bool |
| 188 | */ |
| 189 | public function is_rtl(): bool { |
| 190 | if ( isset( $this->email_context['is_rtl'] ) && is_bool( $this->email_context['is_rtl'] ) ) { |
| 191 | return $this->email_context['is_rtl']; |
| 192 | } |
| 193 | |
| 194 | $primary_language = $this->get_primary_language_subtag( $this->language ); |
| 195 | if ( null === $primary_language ) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | return in_array( |
| 200 | $primary_language, |
| 201 | array( |
| 202 | 'ar', |
| 203 | 'arc', |
| 204 | 'azb', |
| 205 | 'ckb', |
| 206 | 'dv', |
| 207 | 'fa', |
| 208 | 'he', |
| 209 | 'ku', |
| 210 | 'nqo', |
| 211 | 'ps', |
| 212 | 'sd', |
| 213 | 'ug', |
| 214 | 'ur', |
| 215 | 'yi', |
| 216 | ), |
| 217 | true |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the HTML/CSS text direction. |
| 223 | * |
| 224 | * @return string |
| 225 | */ |
| 226 | public function get_text_direction(): string { |
| 227 | return $this->is_rtl() ? 'rtl' : 'ltr'; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get the default physical text alignment for this render. |
| 232 | * |
| 233 | * @return string |
| 234 | */ |
| 235 | public function get_default_text_align(): string { |
| 236 | return $this->is_rtl() ? 'right' : 'left'; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get the physical start side. |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | public function get_start_side(): string { |
| 245 | return $this->is_rtl() ? 'right' : 'left'; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Get the physical end side. |
| 250 | * |
| 251 | * @return string |
| 252 | */ |
| 253 | public function get_end_side(): string { |
| 254 | return $this->is_rtl() ? 'left' : 'right'; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Sanitize a text alignment value. |
| 259 | * |
| 260 | * @param mixed $alignment Alignment candidate. |
| 261 | * @return string|null |
| 262 | */ |
| 263 | public function sanitize_text_align( $alignment ): ?string { |
| 264 | if ( ! is_string( $alignment ) ) { |
| 265 | return null; |
| 266 | } |
| 267 | return in_array( $alignment, array( 'left', 'center', 'right' ), true ) ? $alignment : null; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Resolve a text alignment value with direction-aware default. |
| 272 | * |
| 273 | * @param mixed $alignment Alignment candidate. |
| 274 | * @return string |
| 275 | */ |
| 276 | public function resolve_text_align( $alignment ): string { |
| 277 | return $this->sanitize_text_align( $alignment ) ?? $this->get_default_text_align(); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Get the primary language subtag from a locale/language value. |
| 282 | * |
| 283 | * @param string|null $language Language or locale. |
| 284 | * @return string|null |
| 285 | */ |
| 286 | private function get_primary_language_subtag( ?string $language ): ?string { |
| 287 | if ( null === $language || '' === trim( $language ) ) { |
| 288 | return null; |
| 289 | } |
| 290 | |
| 291 | $language = strtolower( str_replace( '_', '-', trim( $language ) ) ); |
| 292 | $parts = explode( '-', $language ); |
| 293 | $primary = $parts[0] ?? ''; |
| 294 | $length = strlen( $primary ); |
| 295 | |
| 296 | if ( $length < 2 || $length > 3 ) { |
| 297 | return null; |
| 298 | } |
| 299 | |
| 300 | return strspn( $primary, 'abcdefghijklmnopqrstuvwxyz' ) === $length ? $primary : null; |
| 301 | } |
| 302 | } |
| 303 |