Logger
11 months ago
Patterns
11 months ago
PersonalizationTags
2 days ago
Renderer
2 days ago
Templates
4 months ago
class-assets-manager.php
5 months ago
class-dependency-check.php
11 months ago
class-email-api-controller.php
6 months ago
class-email-editor.php
2 days ago
class-email-styles-schema.php
11 months ago
class-personalizer.php
2 days ago
class-send-preview-email.php
5 months ago
class-settings-controller.php
3 months ago
class-site-style-sync-controller.php
4 months ago
class-theme-controller.php
3 months ago
class-user-theme.php
11 months ago
content-editor.css
2 weeks ago
content-shared.css
11 months ago
index.php
11 months ago
theme.json
3 months ago
class-settings-controller.php
126 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | class Settings_Controller { |
| 6 | const DEFAULT_SETTINGS = array( |
| 7 | 'enableCustomUnits' => array( 'px', '%' ), |
| 8 | ); |
| 9 | private Theme_Controller $theme_controller; |
| 10 | private array $allowed_iframe_style_handles = array(); |
| 11 | private array $iframe_assets = array(); |
| 12 | public function __construct( |
| 13 | Theme_Controller $theme_controller |
| 14 | ) { |
| 15 | $this->theme_controller = $theme_controller; |
| 16 | } |
| 17 | public function get_settings(): array { |
| 18 | $this->init_iframe_assets(); |
| 19 | $core_default_settings = \get_default_block_editor_settings(); |
| 20 | $theme_settings = $this->theme_controller->get_settings(); |
| 21 | $settings = array_merge( $core_default_settings, self::DEFAULT_SETTINGS ); |
| 22 | // Assets for iframe editor (component styles, scripts, etc.). |
| 23 | $settings['__unstableResolvedAssets'] = $this->iframe_assets; |
| 24 | $settings['allowedIframeStyleHandles'] = $this->allowed_iframe_style_handles; |
| 25 | $editor_content_styles = file_get_contents( __DIR__ . '/content-editor.css' ); |
| 26 | $shares_content_styles = file_get_contents( __DIR__ . '/content-shared.css' ); |
| 27 | $settings['styles'] = array( |
| 28 | array( 'css' => $editor_content_styles ), |
| 29 | array( 'css' => $shares_content_styles ), |
| 30 | ); |
| 31 | $settings['autosaveInterval'] = 60; |
| 32 | // Disable code editing in the email editor. We manipulate HTML in renderer so it doesn't make sense to have it enabled. |
| 33 | $settings['codeEditingEnabled'] = false; |
| 34 | $settings['__experimentalFeatures'] = $theme_settings; |
| 35 | // Controls which alignment options are available for blocks. |
| 36 | $settings['supportsLayout'] = true; // Allow using default layouts. |
| 37 | $settings['alignWide'] = true; // Enable wide and full alignment options. |
| 38 | $settings['__unstableIsBlockBasedTheme'] = false; // Setting to true disables wide and full alignments in flow layouts. |
| 39 | return $settings; |
| 40 | } |
| 41 | public function get_layout(): array { |
| 42 | $layout_settings = $this->theme_controller->get_layout_settings(); |
| 43 | $layout = array( |
| 44 | 'contentSize' => $layout_settings['contentSize'], |
| 45 | ); |
| 46 | if ( isset( $layout_settings['wideSize'] ) ) { |
| 47 | $layout['wideSize'] = $layout_settings['wideSize']; |
| 48 | } |
| 49 | return $layout; |
| 50 | } |
| 51 | public function get_email_styles(): array { |
| 52 | $theme = $this->get_theme(); |
| 53 | return $theme->get_data()['styles']; |
| 54 | } |
| 55 | public function get_layout_width_without_padding(): string { |
| 56 | $styles = $this->get_email_styles(); |
| 57 | $layout = $this->get_layout(); |
| 58 | $width = $this->parse_number_from_string_with_pixels( $layout['contentSize'] ); |
| 59 | $width -= $this->parse_number_from_string_with_pixels( $styles['spacing']['padding']['left'] ); |
| 60 | $width -= $this->parse_number_from_string_with_pixels( $styles['spacing']['padding']['right'] ); |
| 61 | return "{$width}px"; |
| 62 | } |
| 63 | public function parse_styles_to_array( string $styles ): array { |
| 64 | $styles = explode( ';', $styles ); |
| 65 | $parsed_styles = array(); |
| 66 | foreach ( $styles as $style ) { |
| 67 | $style = explode( ':', $style ); |
| 68 | if ( count( $style ) === 2 ) { |
| 69 | $parsed_styles[ trim( $style[0] ) ] = trim( $style[1] ); |
| 70 | } |
| 71 | } |
| 72 | return $parsed_styles; |
| 73 | } |
| 74 | public function parse_number_from_string_with_pixels( string $value ): float { |
| 75 | return (float) str_replace( 'px', '', $value ); |
| 76 | } |
| 77 | public function get_theme(): \WP_Theme_JSON { |
| 78 | return $this->theme_controller->get_theme(); |
| 79 | } |
| 80 | public function translate_slug_to_font_size( string $font_size ): string { |
| 81 | return $this->theme_controller->translate_slug_to_font_size( $font_size ); |
| 82 | } |
| 83 | public function translate_slug_to_color( string $color_slug ): string { |
| 84 | return $this->theme_controller->translate_slug_to_color( $color_slug ); |
| 85 | } |
| 86 | private function get_allowed_iframe_style_handles() { |
| 87 | // Core style handles. |
| 88 | $allowed_iframe_style_handles = array( |
| 89 | 'wp-components-css', |
| 90 | 'wp-reset-editor-styles-css', |
| 91 | 'wp-block-library-css', |
| 92 | 'wp-block-editor-content-css', |
| 93 | 'wp-edit-blocks-css', |
| 94 | ); |
| 95 | foreach ( \WP_Block_Type_Registry::get_instance()->get_all_registered() as $block ) { |
| 96 | if ( ! isset( $block->supports['email'] ) || ! $block->supports['email'] ) { |
| 97 | continue; |
| 98 | } |
| 99 | foreach ( $block->style_handles as $handle ) { |
| 100 | $allowed_iframe_style_handles[] = $handle . '-css'; |
| 101 | } |
| 102 | foreach ( $block->editor_style_handles as $handle ) { |
| 103 | $allowed_iframe_style_handles[] = $handle . '-css'; |
| 104 | } |
| 105 | } |
| 106 | return apply_filters( 'woocommerce_email_editor_allowed_iframe_style_handles', $allowed_iframe_style_handles ); |
| 107 | } |
| 108 | private function init_iframe_assets(): void { |
| 109 | if ( ! empty( $this->iframe_assets ) ) { |
| 110 | return; |
| 111 | } |
| 112 | $this->iframe_assets = _wp_get_iframed_editor_assets(); |
| 113 | $this->allowed_iframe_style_handles = $this->get_allowed_iframe_style_handles(); |
| 114 | $cleaned_styles = array(); |
| 115 | foreach ( explode( "\n", (string) $this->iframe_assets['styles'] ) as $asset ) { |
| 116 | foreach ( $this->allowed_iframe_style_handles as $handle ) { |
| 117 | if ( strpos( $asset, $handle ) !== false ) { |
| 118 | $cleaned_styles[] = $asset; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | $this->iframe_assets['styles'] = implode( "\n", $cleaned_styles ); |
| 124 | } |
| 125 | } |
| 126 |