mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Engine
/
Renderer
/
ContentRenderer
/
class-rendering-context.php
mailpoet
/
vendor
/
woocommerce
/
email-editor
/
src
/
Engine
/
Renderer
/
ContentRenderer
Last commit date
Layout
5 days ago
Postprocessors
11 months ago
Preprocessors
2 weeks ago
class-block-renderer.php
11 months ago
class-blocks-parser.php
11 months ago
class-content-renderer.php
5 days ago
class-preset-variable-resolver.php
3 months ago
class-process-manager.php
3 months ago
class-rendering-context.php
3 months ago
content.css
11 months ago
index.php
11 months ago
class-rendering-context.php
127 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Integrations\Utils\Styles_Helper; |
| 6 | use WP_Theme_JSON; |
| 7 | class Rendering_Context { |
| 8 | private WP_Theme_JSON $theme_json; |
| 9 | private array $email_context; |
| 10 | private ?string $language; |
| 11 | public function __construct( WP_Theme_JSON $theme_json, array $email_context = array(), ?string $language = null ) { |
| 12 | $this->theme_json = $theme_json; |
| 13 | $this->email_context = $email_context; |
| 14 | $this->language = $language; |
| 15 | } |
| 16 | public function get_theme_json(): WP_Theme_JSON { |
| 17 | return $this->theme_json; |
| 18 | } |
| 19 | public function get_theme_styles(): array { |
| 20 | $theme = $this->get_theme_json(); |
| 21 | return $theme->get_data()['styles'] ?? array(); |
| 22 | } |
| 23 | public function get_theme_settings() { |
| 24 | return $this->get_theme_json()->get_settings(); |
| 25 | } |
| 26 | public function get_layout_width_without_padding(): string { |
| 27 | $styles = $this->get_theme_styles(); |
| 28 | $layout_settings = $this->get_theme_settings()['layout'] ?? array(); |
| 29 | $width = Styles_Helper::parse_value( $layout_settings['contentSize'] ?? '0px' ); |
| 30 | $padding = $styles['spacing']['padding'] ?? array(); |
| 31 | $width -= Styles_Helper::parse_value( $padding['left'] ?? '0px' ); |
| 32 | $width -= Styles_Helper::parse_value( $padding['right'] ?? '0px' ); |
| 33 | return "{$width}px"; |
| 34 | } |
| 35 | public function translate_slug_to_color( string $color_slug ): string { |
| 36 | $settings = $this->get_theme_settings(); |
| 37 | $color_definitions = array_merge( |
| 38 | $settings['color']['palette']['theme'] ?? array(), |
| 39 | $settings['color']['palette']['default'] ?? array() |
| 40 | ); |
| 41 | foreach ( $color_definitions as $color_definition ) { |
| 42 | if ( $color_definition['slug'] === $color_slug ) { |
| 43 | return strtolower( $color_definition['color'] ); |
| 44 | } |
| 45 | } |
| 46 | return $color_slug; |
| 47 | } |
| 48 | public function get_email_context(): array { |
| 49 | return $this->email_context; |
| 50 | } |
| 51 | public function get_language(): ?string { |
| 52 | return $this->language; |
| 53 | } |
| 54 | public function get_user_id(): ?int { |
| 55 | return isset( $this->email_context['user_id'] ) && is_numeric( $this->email_context['user_id'] ) ? (int) $this->email_context['user_id'] : null; |
| 56 | } |
| 57 | public function get_recipient_email(): ?string { |
| 58 | return isset( $this->email_context['recipient_email'] ) && is_string( $this->email_context['recipient_email'] ) ? $this->email_context['recipient_email'] : null; |
| 59 | } |
| 60 | public function get( string $key, $default_value = null ) { |
| 61 | return $this->email_context[ $key ] ?? $default_value; |
| 62 | } |
| 63 | public function is_rtl(): bool { |
| 64 | if ( isset( $this->email_context['is_rtl'] ) && is_bool( $this->email_context['is_rtl'] ) ) { |
| 65 | return $this->email_context['is_rtl']; |
| 66 | } |
| 67 | $primary_language = $this->get_primary_language_subtag( $this->language ); |
| 68 | if ( null === $primary_language ) { |
| 69 | return false; |
| 70 | } |
| 71 | return in_array( |
| 72 | $primary_language, |
| 73 | array( |
| 74 | 'ar', |
| 75 | 'arc', |
| 76 | 'azb', |
| 77 | 'ckb', |
| 78 | 'dv', |
| 79 | 'fa', |
| 80 | 'he', |
| 81 | 'ku', |
| 82 | 'nqo', |
| 83 | 'ps', |
| 84 | 'sd', |
| 85 | 'ug', |
| 86 | 'ur', |
| 87 | 'yi', |
| 88 | ), |
| 89 | true |
| 90 | ); |
| 91 | } |
| 92 | public function get_text_direction(): string { |
| 93 | return $this->is_rtl() ? 'rtl' : 'ltr'; |
| 94 | } |
| 95 | public function get_default_text_align(): string { |
| 96 | return $this->is_rtl() ? 'right' : 'left'; |
| 97 | } |
| 98 | public function get_start_side(): string { |
| 99 | return $this->is_rtl() ? 'right' : 'left'; |
| 100 | } |
| 101 | public function get_end_side(): string { |
| 102 | return $this->is_rtl() ? 'left' : 'right'; |
| 103 | } |
| 104 | public function sanitize_text_align( $alignment ): ?string { |
| 105 | if ( ! is_string( $alignment ) ) { |
| 106 | return null; |
| 107 | } |
| 108 | return in_array( $alignment, array( 'left', 'center', 'right' ), true ) ? $alignment : null; |
| 109 | } |
| 110 | public function resolve_text_align( $alignment ): string { |
| 111 | return $this->sanitize_text_align( $alignment ) ?? $this->get_default_text_align(); |
| 112 | } |
| 113 | private function get_primary_language_subtag( ?string $language ): ?string { |
| 114 | if ( null === $language || '' === trim( $language ) ) { |
| 115 | return null; |
| 116 | } |
| 117 | $language = strtolower( str_replace( '_', '-', trim( $language ) ) ); |
| 118 | $parts = explode( '-', $language ); |
| 119 | $primary = $parts[0] ?? ''; |
| 120 | $length = strlen( $primary ); |
| 121 | if ( $length < 2 || $length > 3 ) { |
| 122 | return null; |
| 123 | } |
| 124 | return strspn( $primary, 'abcdefghijklmnopqrstuvwxyz' ) === $length ? $primary : null; |
| 125 | } |
| 126 | } |
| 127 |