Moving fetching iframed assets

Because we called the function for getting editor iframed assets in init hook, it could cause that some properties were not set yet.
This commit is contained in:
Jan Lysý
2025-01-30 14:38:23 +01:00
committed by Rostislav Wolný
parent d26fb1b026
commit ad52971692
3 changed files with 29 additions and 40 deletions

View File

@@ -41,13 +41,6 @@ class Email_Editor {
* @var Patterns Patterns. * @var Patterns Patterns.
*/ */
private Patterns $patterns; private Patterns $patterns;
/**
* Property for the settings controller.
*
* @var Settings_Controller Settings controller.
*/
private Settings_Controller $settings_controller;
/** /**
* Property for the send preview email controller. * Property for the send preview email controller.
* *
@@ -68,7 +61,6 @@ class Email_Editor {
* @param Email_Api_Controller $email_api_controller Email API controller. * @param Email_Api_Controller $email_api_controller Email API controller.
* @param Templates $templates Templates. * @param Templates $templates Templates.
* @param Patterns $patterns Patterns. * @param Patterns $patterns Patterns.
* @param Settings_Controller $settings_controller Settings controller.
* @param Send_Preview_Email $send_preview_email Preview email controller. * @param Send_Preview_Email $send_preview_email Preview email controller.
* @param Personalization_Tags_Registry $personalization_tags_controller Personalization tags registry that allows initializing personalization tags. * @param Personalization_Tags_Registry $personalization_tags_controller Personalization tags registry that allows initializing personalization tags.
*/ */
@@ -76,14 +68,12 @@ class Email_Editor {
Email_Api_Controller $email_api_controller, Email_Api_Controller $email_api_controller,
Templates $templates, Templates $templates,
Patterns $patterns, Patterns $patterns,
Settings_Controller $settings_controller,
Send_Preview_Email $send_preview_email, Send_Preview_Email $send_preview_email,
Personalization_Tags_Registry $personalization_tags_controller Personalization_Tags_Registry $personalization_tags_controller
) { ) {
$this->email_api_controller = $email_api_controller; $this->email_api_controller = $email_api_controller;
$this->templates = $templates; $this->templates = $templates;
$this->patterns = $patterns; $this->patterns = $patterns;
$this->settings_controller = $settings_controller;
$this->send_preview_email = $send_preview_email; $this->send_preview_email = $send_preview_email;
$this->personalization_tags_registry = $personalization_tags_controller; $this->personalization_tags_registry = $personalization_tags_controller;
} }
@@ -104,7 +94,6 @@ class Email_Editor {
$is_editor_page = apply_filters( 'mailpoet_is_email_editor_page', false ); $is_editor_page = apply_filters( 'mailpoet_is_email_editor_page', false );
if ( $is_editor_page ) { if ( $is_editor_page ) {
$this->extend_email_post_api(); $this->extend_email_post_api();
$this->settings_controller->init();
} }
add_action( 'rest_api_init', array( $this, 'register_email_editor_api_routes' ) ); add_action( 'rest_api_init', array( $this, 'register_email_editor_api_routes' ) );
add_filter( 'mailpoet_email_editor_send_preview_email', array( $this->send_preview_email, 'send_preview_email' ), 11, 1 ); // allow for other filter methods to take precedent. add_filter( 'mailpoet_email_editor_send_preview_email', array( $this->send_preview_email, 'send_preview_email' ), 11, 1 ); // allow for other filter methods to take precedent.

View File

@@ -56,40 +56,14 @@ class Settings_Controller {
$this->theme_controller = $theme_controller; $this->theme_controller = $theme_controller;
} }
/**
* Method to initialize the settings controller.
*
* @return void
*/
public function init(): void {
/*
* We need to initialize these assets early because they are read from global variables $wp_styles and $wp_scripts
* and in later WordPress page load pages they contain stuff we don't want (e.g. html for admin login popup)
* in the post editor this is called directly in post.php.
*/
$this->iframe_assets = _wp_get_iframed_editor_assets();
// Remove layout styles and block library for classic themes. They are added only when a classic theme is active
// and they add unwanted margins and paddings in the editor content.
$cleaned_styles = array();
foreach ( explode( "\n", (string) $this->iframe_assets['styles'] ) as $asset ) {
if ( strpos( $asset, 'wp-editor-classic-layout-styles-css' ) !== false ) {
continue;
}
if ( strpos( $asset, 'wp-block-library-theme-css' ) !== false ) {
continue;
}
$cleaned_styles[] = $asset;
}
$this->iframe_assets['styles'] = implode( "\n", $cleaned_styles );
}
/** /**
* Get the settings for the email editor. * Get the settings for the email editor.
* *
* @return array * @return array
*/ */
public function get_settings(): array { public function get_settings(): array {
$this->init_iframe_assets();
$core_default_settings = \get_default_block_editor_settings(); $core_default_settings = \get_default_block_editor_settings();
$theme_settings = $this->theme_controller->get_settings(); $theme_settings = $this->theme_controller->get_settings();
@@ -215,4 +189,31 @@ class Settings_Controller {
public function translate_slug_to_color( string $color_slug ): string { public function translate_slug_to_color( string $color_slug ): string {
return $this->theme_controller->translate_slug_to_color( $color_slug ); return $this->theme_controller->translate_slug_to_color( $color_slug );
} }
/**
* Method to initialize iframe assets.
*
* @return void
*/
private function init_iframe_assets(): void {
if ( ! empty( $this->iframe_assets ) ) {
return;
}
$this->iframe_assets = _wp_get_iframed_editor_assets();
// Remove layout styles and block library for classic themes. They are added only when a classic theme is active
// and they add unwanted margins and paddings in the editor content.
$cleaned_styles = array();
foreach ( explode( "\n", (string) $this->iframe_assets['styles'] ) as $asset ) {
if ( strpos( $asset, 'wp-editor-classic-layout-styles-css' ) !== false ) {
continue;
}
if ( strpos( $asset, 'wp-block-library-theme-css' ) !== false ) {
continue;
}
$cleaned_styles[] = $asset;
}
$this->iframe_assets['styles'] = implode( "\n", $cleaned_styles );
}
} }

View File

@@ -321,7 +321,6 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
$container->get( Email_Api_Controller::class ), $container->get( Email_Api_Controller::class ),
$container->get( Templates::class ), $container->get( Templates::class ),
$container->get( Patterns::class ), $container->get( Patterns::class ),
$container->get( Settings_Controller::class ),
$container->get( Send_Preview_Email::class ), $container->get( Send_Preview_Email::class ),
$container->get( Personalization_Tags_Registry::class ), $container->get( Personalization_Tags_Registry::class ),
); );