Logger
10 months ago
Patterns
10 months ago
PersonalizationTags
7 months ago
Renderer
4 weeks ago
Templates
2 months ago
class-assets-manager.php
4 months ago
class-dependency-check.php
1 year ago
class-email-api-controller.php
4 months ago
class-email-editor.php
4 months ago
class-email-styles-schema.php
1 year ago
class-personalizer.php
4 months ago
class-send-preview-email.php
3 months ago
class-settings-controller.php
2 months ago
class-site-style-sync-controller.php
2 months ago
class-theme-controller.php
2 months ago
class-user-theme.php
1 year ago
content-editor.css
9 months ago
content-shared.css
10 months ago
theme.json
2 months ago
class-settings-controller.php
253 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; |
| 10 | |
| 11 | /** |
| 12 | * Class managing the settings for the email editor. |
| 13 | */ |
| 14 | class Settings_Controller { |
| 15 | |
| 16 | const DEFAULT_SETTINGS = array( |
| 17 | 'enableCustomUnits' => array( 'px', '%' ), |
| 18 | ); |
| 19 | |
| 20 | /** |
| 21 | * Theme controller. |
| 22 | * |
| 23 | * @var Theme_Controller |
| 24 | */ |
| 25 | private Theme_Controller $theme_controller; |
| 26 | |
| 27 | /** |
| 28 | * Allowed iframe style handles. |
| 29 | * |
| 30 | * @var string[] |
| 31 | */ |
| 32 | private array $allowed_iframe_style_handles = array(); |
| 33 | |
| 34 | /** |
| 35 | * Assets for iframe editor (component styles, scripts, etc.) |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | private array $iframe_assets = array(); |
| 40 | |
| 41 | /** |
| 42 | * Class constructor. |
| 43 | * |
| 44 | * @param Theme_Controller $theme_controller Theme controller. |
| 45 | */ |
| 46 | public function __construct( |
| 47 | Theme_Controller $theme_controller |
| 48 | ) { |
| 49 | $this->theme_controller = $theme_controller; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the settings for the email editor. |
| 54 | * |
| 55 | * @return array |
| 56 | */ |
| 57 | public function get_settings(): array { |
| 58 | $this->init_iframe_assets(); |
| 59 | |
| 60 | $core_default_settings = \get_default_block_editor_settings(); |
| 61 | $theme_settings = $this->theme_controller->get_settings(); |
| 62 | |
| 63 | $settings = array_merge( $core_default_settings, self::DEFAULT_SETTINGS ); |
| 64 | // Assets for iframe editor (component styles, scripts, etc.). |
| 65 | $settings['__unstableResolvedAssets'] = $this->iframe_assets; |
| 66 | $settings['allowedIframeStyleHandles'] = $this->allowed_iframe_style_handles; |
| 67 | $editor_content_styles = file_get_contents( __DIR__ . '/content-editor.css' ); |
| 68 | $shares_content_styles = file_get_contents( __DIR__ . '/content-shared.css' ); |
| 69 | $settings['styles'] = array( |
| 70 | array( 'css' => $editor_content_styles ), |
| 71 | array( 'css' => $shares_content_styles ), |
| 72 | ); |
| 73 | |
| 74 | $settings['autosaveInterval'] = 60; |
| 75 | // Disable code editing in the email editor. We manipulate HTML in renderer so it doesn't make sense to have it enabled. |
| 76 | $settings['codeEditingEnabled'] = false; |
| 77 | |
| 78 | $settings['__experimentalFeatures'] = $theme_settings; |
| 79 | // Controls which alignment options are available for blocks. |
| 80 | $settings['supportsLayout'] = true; // Allow using default layouts. |
| 81 | $settings['alignWide'] = true; // Enable wide and full alignment options. |
| 82 | $settings['__unstableIsBlockBasedTheme'] = false; // Setting to true disables wide and full alignments in flow layouts. |
| 83 | return $settings; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns the layout settings for the email editor. |
| 88 | * |
| 89 | * @return array{contentSize: string, wideSize?: string} |
| 90 | */ |
| 91 | public function get_layout(): array { |
| 92 | $layout_settings = $this->theme_controller->get_layout_settings(); |
| 93 | $layout = array( |
| 94 | 'contentSize' => $layout_settings['contentSize'], |
| 95 | ); |
| 96 | if ( isset( $layout_settings['wideSize'] ) ) { |
| 97 | $layout['wideSize'] = $layout_settings['wideSize']; |
| 98 | } |
| 99 | return $layout; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the email styles. |
| 104 | * |
| 105 | * @return array{ |
| 106 | * spacing: array{ |
| 107 | * blockGap: string, |
| 108 | * padding: array{bottom: string, left: string, right: string, top: string} |
| 109 | * }, |
| 110 | * color: array{ |
| 111 | * background: string, |
| 112 | * text: string |
| 113 | * }, |
| 114 | * typography: array{ |
| 115 | * fontFamily: string |
| 116 | * } |
| 117 | * } |
| 118 | */ |
| 119 | public function get_email_styles(): array { |
| 120 | $theme = $this->get_theme(); |
| 121 | return $theme->get_data()['styles']; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the width of the layout without padding. |
| 126 | * |
| 127 | * @return string |
| 128 | */ |
| 129 | public function get_layout_width_without_padding(): string { |
| 130 | $styles = $this->get_email_styles(); |
| 131 | $layout = $this->get_layout(); |
| 132 | $width = $this->parse_number_from_string_with_pixels( $layout['contentSize'] ); |
| 133 | $width -= $this->parse_number_from_string_with_pixels( $styles['spacing']['padding']['left'] ); |
| 134 | $width -= $this->parse_number_from_string_with_pixels( $styles['spacing']['padding']['right'] ); |
| 135 | return "{$width}px"; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Parse styles string to array. |
| 140 | * |
| 141 | * @param string $styles Styles string. |
| 142 | * @return array |
| 143 | */ |
| 144 | public function parse_styles_to_array( string $styles ): array { |
| 145 | $styles = explode( ';', $styles ); |
| 146 | $parsed_styles = array(); |
| 147 | foreach ( $styles as $style ) { |
| 148 | $style = explode( ':', $style ); |
| 149 | if ( count( $style ) === 2 ) { |
| 150 | $parsed_styles[ trim( $style[0] ) ] = trim( $style[1] ); |
| 151 | } |
| 152 | } |
| 153 | return $parsed_styles; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns float number parsed from string with pixels. |
| 158 | * |
| 159 | * @param string $value Value with pixels. |
| 160 | * @return float |
| 161 | */ |
| 162 | public function parse_number_from_string_with_pixels( string $value ): float { |
| 163 | return (float) str_replace( 'px', '', $value ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns the theme. |
| 168 | * |
| 169 | * @return \WP_Theme_JSON |
| 170 | */ |
| 171 | public function get_theme(): \WP_Theme_JSON { |
| 172 | return $this->theme_controller->get_theme(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Translate slug to font size. |
| 177 | * |
| 178 | * @param string $font_size Font size slug. |
| 179 | * @return string |
| 180 | */ |
| 181 | public function translate_slug_to_font_size( string $font_size ): string { |
| 182 | return $this->theme_controller->translate_slug_to_font_size( $font_size ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Translate slug to color. |
| 187 | * |
| 188 | * @param string $color_slug Color slug. |
| 189 | * @return string |
| 190 | */ |
| 191 | public function translate_slug_to_color( string $color_slug ): string { |
| 192 | return $this->theme_controller->translate_slug_to_color( $color_slug ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Get the allowed iframe style handles. |
| 197 | * |
| 198 | * @return array |
| 199 | */ |
| 200 | private function get_allowed_iframe_style_handles() { |
| 201 | // Core style handles. |
| 202 | $allowed_iframe_style_handles = array( |
| 203 | 'wp-components-css', |
| 204 | 'wp-reset-editor-styles-css', |
| 205 | 'wp-block-library-css', |
| 206 | 'wp-block-editor-content-css', |
| 207 | 'wp-edit-blocks-css', |
| 208 | ); |
| 209 | |
| 210 | foreach ( \WP_Block_Type_Registry::get_instance()->get_all_registered() as $block ) { |
| 211 | if ( ! isset( $block->supports['email'] ) || ! $block->supports['email'] ) { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | foreach ( $block->style_handles as $handle ) { |
| 216 | $allowed_iframe_style_handles[] = $handle . '-css'; |
| 217 | } |
| 218 | |
| 219 | foreach ( $block->editor_style_handles as $handle ) { |
| 220 | $allowed_iframe_style_handles[] = $handle . '-css'; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return apply_filters( 'woocommerce_email_editor_allowed_iframe_style_handles', $allowed_iframe_style_handles ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Method to initialize iframe assets. |
| 229 | * |
| 230 | * @return void |
| 231 | */ |
| 232 | private function init_iframe_assets(): void { |
| 233 | if ( ! empty( $this->iframe_assets ) ) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | $this->iframe_assets = _wp_get_iframed_editor_assets(); |
| 238 | $this->allowed_iframe_style_handles = $this->get_allowed_iframe_style_handles(); |
| 239 | |
| 240 | $cleaned_styles = array(); |
| 241 | foreach ( explode( "\n", (string) $this->iframe_assets['styles'] ) as $asset ) { |
| 242 | foreach ( $this->allowed_iframe_style_handles as $handle ) { |
| 243 | if ( strpos( $asset, $handle ) !== false ) { |
| 244 | $cleaned_styles[] = $asset; |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | $this->iframe_assets['styles'] = implode( "\n", $cleaned_styles ); |
| 251 | } |
| 252 | } |
| 253 |