Use user theme in renderer instead of template themes
We no longer read styles associate with templates when we render an email but we read the user theme instead [MAILPOET-6335]
This commit is contained in:
committed by
Aschepikov
parent
15818f37ef
commit
1e1bec4ce0
@@ -46,20 +46,6 @@ class Content_Renderer {
|
|||||||
*/
|
*/
|
||||||
private Theme_Controller $theme_controller;
|
private Theme_Controller $theme_controller;
|
||||||
|
|
||||||
/**
|
|
||||||
* Post object
|
|
||||||
*
|
|
||||||
* @var WP_Post
|
|
||||||
*/
|
|
||||||
private $post = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Block template
|
|
||||||
*
|
|
||||||
* @var WP_Block_Template
|
|
||||||
*/
|
|
||||||
private $template = null;
|
|
||||||
|
|
||||||
const CONTENT_STYLES_FILE = 'content.css';
|
const CONTENT_STYLES_FILE = 'content.css';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,8 +89,6 @@ class Content_Renderer {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function render( WP_Post $post, WP_Block_Template $template ): string {
|
public function render( WP_Post $post, WP_Block_Template $template ): string {
|
||||||
$this->post = $post;
|
|
||||||
$this->template = $template;
|
|
||||||
$this->set_template_globals( $post, $template );
|
$this->set_template_globals( $post, $template );
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
$rendered_html = get_the_block_template_html();
|
$rendered_html = get_the_block_template_html();
|
||||||
@@ -129,7 +113,7 @@ class Content_Renderer {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function preprocess_parsed_blocks( array $parsed_blocks ): array {
|
public function preprocess_parsed_blocks( array $parsed_blocks ): array {
|
||||||
return $this->process_manager->preprocess( $parsed_blocks, $this->theme_controller->get_layout_settings(), $this->theme_controller->get_styles( $this->post, $this->template ) );
|
return $this->process_manager->preprocess( $parsed_blocks, $this->theme_controller->get_layout_settings(), $this->theme_controller->get_styles() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -41,13 +41,6 @@ class Renderer {
|
|||||||
*/
|
*/
|
||||||
private Templates $templates;
|
private Templates $templates;
|
||||||
|
|
||||||
/**
|
|
||||||
* Theme data for the template being rendered.
|
|
||||||
*
|
|
||||||
* @var WP_Theme_JSON|null
|
|
||||||
*/
|
|
||||||
private static $theme = null;
|
|
||||||
|
|
||||||
const TEMPLATE_FILE = 'template-canvas.php';
|
const TEMPLATE_FILE = 'template-canvas.php';
|
||||||
const TEMPLATE_STYLES_FILE = 'template-canvas.css';
|
const TEMPLATE_STYLES_FILE = 'template-canvas.css';
|
||||||
|
|
||||||
@@ -68,15 +61,6 @@ class Renderer {
|
|||||||
$this->theme_controller = $theme_controller;
|
$this->theme_controller = $theme_controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* During rendering, this stores the theme data for the template being rendered.
|
|
||||||
*
|
|
||||||
* @return ?\WP_Theme_JSON
|
|
||||||
*/
|
|
||||||
public static function get_theme() {
|
|
||||||
return self::$theme;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the email template
|
* Renders the email template
|
||||||
*
|
*
|
||||||
@@ -91,12 +75,8 @@ class Renderer {
|
|||||||
$template_id = 'mailpoet/mailpoet//' . ( get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general' );
|
$template_id = 'mailpoet/mailpoet//' . ( get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general' );
|
||||||
/** @var \WP_Block_Template $template */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
|
/** @var \WP_Block_Template $template */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
|
||||||
$template = $this->templates->get_block_template( $template_id );
|
$template = $this->templates->get_block_template( $template_id );
|
||||||
$theme = $this->templates->get_block_template_theme( $template_id, $template->wp_id );
|
|
||||||
|
|
||||||
// Set the theme for the template. This is merged with base theme.json and core json before rendering.
|
$email_styles = $this->theme_controller->get_styles();
|
||||||
self::$theme = new WP_Theme_JSON( $theme, 'default' );
|
|
||||||
|
|
||||||
$email_styles = $this->theme_controller->get_styles( $post, $template );
|
|
||||||
$template_html = $this->content_renderer->render( $post, $template );
|
$template_html = $this->content_renderer->render( $post, $template );
|
||||||
$layout = $this->theme_controller->get_layout_settings();
|
$layout = $this->theme_controller->get_layout_settings();
|
||||||
|
|
||||||
|
@@ -8,7 +8,6 @@
|
|||||||
declare(strict_types = 1);
|
declare(strict_types = 1);
|
||||||
namespace MailPoet\EmailEditor\Engine;
|
namespace MailPoet\EmailEditor\Engine;
|
||||||
|
|
||||||
use MailPoet\EmailEditor\Engine\Renderer\Renderer;
|
|
||||||
use WP_Block_Template;
|
use WP_Block_Template;
|
||||||
use WP_Post;
|
use WP_Post;
|
||||||
use WP_Theme_JSON;
|
use WP_Theme_JSON;
|
||||||
@@ -33,28 +32,44 @@ class Theme_Controller {
|
|||||||
*/
|
*/
|
||||||
private WP_Theme_JSON $base_theme;
|
private WP_Theme_JSON $base_theme;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User theme contains user custom styles and settings
|
||||||
|
*
|
||||||
|
* @var User_Theme
|
||||||
|
*/
|
||||||
|
private User_Theme $user_theme;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Theme_Controller constructor.
|
* Theme_Controller constructor.
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->core_theme = WP_Theme_JSON_Resolver::get_core_data();
|
$this->core_theme = WP_Theme_JSON_Resolver::get_core_data();
|
||||||
$this->base_theme = new WP_Theme_JSON( (array) json_decode( (string) file_get_contents( __DIR__ . '/theme.json' ), true ), 'default' );
|
$this->base_theme = new WP_Theme_JSON( (array) json_decode( (string) file_get_contents( __DIR__ . '/theme.json' ), true ), 'default' );
|
||||||
|
$this->user_theme = new User_Theme();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets combined theme data from the core and base theme, merged with the currently rendered template.
|
* Gets combined theme data from the core and base theme, merged with the user .
|
||||||
*
|
*
|
||||||
* @return WP_Theme_JSON
|
* @return WP_Theme_JSON
|
||||||
*/
|
*/
|
||||||
public function get_theme(): WP_Theme_JSON {
|
public function get_theme(): WP_Theme_JSON {
|
||||||
|
$theme = $this->get_base_theme();
|
||||||
|
$theme->merge( $this->user_theme->get_theme() );
|
||||||
|
|
||||||
|
return $theme;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets combined theme data from the core and base theme.
|
||||||
|
*
|
||||||
|
* @return WP_Theme_JSON
|
||||||
|
*/
|
||||||
|
public function get_base_theme(): WP_Theme_JSON {
|
||||||
$theme = new WP_Theme_JSON();
|
$theme = new WP_Theme_JSON();
|
||||||
$theme->merge( $this->core_theme );
|
$theme->merge( $this->core_theme );
|
||||||
$theme->merge( $this->base_theme );
|
$theme->merge( $this->base_theme );
|
||||||
|
|
||||||
if ( Renderer::get_theme() !== null ) {
|
|
||||||
$theme->merge( Renderer::get_theme() );
|
|
||||||
}
|
|
||||||
|
|
||||||
return apply_filters( 'mailpoet_email_editor_theme_json', $theme );
|
return apply_filters( 'mailpoet_email_editor_theme_json', $theme );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,8 +116,6 @@ class Theme_Controller {
|
|||||||
/**
|
/**
|
||||||
* Get styles for the e-mail.
|
* Get styles for the e-mail.
|
||||||
*
|
*
|
||||||
* @param \WP_Post|null $post Post object.
|
|
||||||
* @param \WP_Block_Template|null $template Template object.
|
|
||||||
* @return array{
|
* @return array{
|
||||||
* spacing: array{
|
* spacing: array{
|
||||||
* blockGap: string,
|
* blockGap: string,
|
||||||
@@ -116,16 +129,9 @@ class Theme_Controller {
|
|||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
public function get_styles( $post = null, $template = null ): array {
|
public function get_styles(): array {
|
||||||
$theme_styles = $this->get_theme()->get_data()['styles'];
|
$theme_styles = $this->get_theme()->get_data()['styles'];
|
||||||
|
|
||||||
// Replace template styles.
|
|
||||||
if ( $template && $template->wp_id ) {
|
|
||||||
$template_theme = (array) get_post_meta( $template->wp_id, 'mailpoet_email_theme', true );
|
|
||||||
$template_styles = (array) ( $template_theme['styles'] ?? array() );
|
|
||||||
$theme_styles = array_replace_recursive( $theme_styles, $template_styles );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract preset variables.
|
// Extract preset variables.
|
||||||
$theme_styles = $this->recursive_extract_preset_variables( $theme_styles );
|
$theme_styles = $this->recursive_extract_preset_variables( $theme_styles );
|
||||||
|
|
||||||
|
@@ -65,38 +65,10 @@ class Renderer_Test extends \MailPoetTest {
|
|||||||
$theme_controller_mock->method( 'get_styles' )->willReturn( $styles );
|
$theme_controller_mock->method( 'get_styles' )->willReturn( $styles );
|
||||||
$theme_controller_mock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) );
|
$theme_controller_mock->method( 'get_layout_settings' )->willReturn( array( 'contentSize' => '660px' ) );
|
||||||
|
|
||||||
// We need to mock only the get_block_template_theme method and templates need to be initialized.
|
|
||||||
$templates_mock = $this->getMockBuilder( Templates::class )
|
|
||||||
->setConstructorArgs( array( $this->di_container->get( Utils::class ) ) )
|
|
||||||
->onlyMethods( array( 'get_block_template_theme' ) )
|
|
||||||
->getMock();
|
|
||||||
$templates_mock->initialize();
|
|
||||||
|
|
||||||
$templates_mock->method( 'get_block_template_theme' )->willReturn(
|
|
||||||
array(
|
|
||||||
'version' => 3,
|
|
||||||
'styles' => array(
|
|
||||||
'elements' => array(
|
|
||||||
'h1' => array(
|
|
||||||
'typography' => array(
|
|
||||||
'fontFamily' => 'lato test',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
'heading' => array(
|
|
||||||
'color' => array(
|
|
||||||
'background' => 'pale-pink',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->renderer = $this->getServiceWithOverrides(
|
$this->renderer = $this->getServiceWithOverrides(
|
||||||
Renderer::class,
|
Renderer::class,
|
||||||
array(
|
array(
|
||||||
'theme_controller' => $theme_controller_mock,
|
'theme_controller' => $theme_controller_mock,
|
||||||
'templates' => $templates_mock,
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->email_post = $this->tester->create_post(
|
$this->email_post = $this->tester->create_post(
|
||||||
@@ -179,28 +151,6 @@ class Renderer_Test extends \MailPoetTest {
|
|||||||
verify( $style )->stringContainsString( 'max-width: 660px;' );
|
verify( $style )->stringContainsString( 'max-width: 660px;' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Test it inlines block styles from template.
|
|
||||||
*/
|
|
||||||
public function testItRendersTemplateStyles(): void {
|
|
||||||
$email = $this->tester->create_post(
|
|
||||||
array(
|
|
||||||
'post_content' => '<!-- wp:heading {"level":1} --><h1 class="wp-block-heading">Hello!</h1><!-- /wp:heading -->',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$rendered = $this->renderer->render(
|
|
||||||
$email,
|
|
||||||
'Subject 1',
|
|
||||||
'Preheader content 2',
|
|
||||||
'en',
|
|
||||||
'noindex,nofollow'
|
|
||||||
);
|
|
||||||
|
|
||||||
verify( $rendered['html'] )->stringContainsString( 'Subject' );
|
|
||||||
verify( $rendered['html'] )->stringContainsString( 'font-family: lato test;' );
|
|
||||||
verify( $rendered['html'] )->stringContainsString( ' background-color: pale-pink;' );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value of the style attribute for the first tag that matches the query.
|
* Returns the value of the style attribute for the first tag that matches the query.
|
||||||
*
|
*
|
||||||
|
@@ -24,6 +24,27 @@ class Theme_Controller_Test extends \MailPoetTest {
|
|||||||
*/
|
*/
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
|
|
||||||
|
// Crete a custom user theme post.
|
||||||
|
$styles_data = array(
|
||||||
|
'version' => 3,
|
||||||
|
'isGlobalStylesUserThemeJSON' => true,
|
||||||
|
'styles' => array(
|
||||||
|
'color' => array(
|
||||||
|
'background' => '#123456',
|
||||||
|
'text' => '#654321',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$post_data = array(
|
||||||
|
'post_title' => __( 'Custom Email Styles', 'mailpoet' ),
|
||||||
|
'post_name' => 'wp-global-styles-mailpoet-email',
|
||||||
|
'post_content' => (string) wp_json_encode( $styles_data, JSON_FORCE_OBJECT ),
|
||||||
|
'post_status' => 'publish',
|
||||||
|
'post_type' => 'wp_global_styles',
|
||||||
|
);
|
||||||
|
wp_insert_post( $post_data );
|
||||||
|
|
||||||
$this->theme_controller = $this->di_container->get( Theme_Controller::class );
|
$this->theme_controller = $this->di_container->get( Theme_Controller::class );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +143,34 @@ class Theme_Controller_Test extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the theme controller loads custom user theme
|
||||||
|
*/
|
||||||
|
public function testItLoadsCustomUserTheme() {
|
||||||
|
$theme = $this->theme_controller->get_theme();
|
||||||
|
verify( $theme->get_raw_data()['styles']['color']['background'] )->equals( '#123456' );
|
||||||
|
verify( $theme->get_raw_data()['styles']['color']['text'] )->equals( '#654321' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the theme controller loads custom user theme and applies it to the styles
|
||||||
|
*/
|
||||||
|
public function testItAddCustomUserThemeToStyles() {
|
||||||
|
$theme = $this->theme_controller->get_theme();
|
||||||
|
$styles = $theme->get_stylesheet();
|
||||||
|
verify( $styles )->stringContainsString( 'color: #654321' );
|
||||||
|
verify( $styles )->stringContainsString( 'background-color: #123456' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the theme controller returns correct layout settings
|
||||||
|
*/
|
||||||
|
public function testGetBaseThemeDoesNotIncludeUserThemeData() {
|
||||||
|
$theme = $this->theme_controller->get_base_theme();
|
||||||
|
verify( $theme->get_raw_data()['styles']['color']['background'] )->equals( '#f0f0f0' );
|
||||||
|
verify( $theme->get_raw_data()['styles']['color']['text'] )->equals( '#000000' );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test if the theme controller returns correct color palette
|
* Test if the theme controller returns correct color palette
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user