From 30f11dc9ccd78bb42bd73ba69d3ae5be963b333e Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Mon, 16 Dec 2024 15:52:27 +0100 Subject: [PATCH] Use registered email post types for templates instead of hardcoded mailpoet_email The general email template is registered and works with all post types that are marked as email post types during editor initialization. [MAILPOET-6356] --- .../src/Engine/Templates/class-templates.php | 17 ++++++++++------- .../src/Engine/class-email-editor.php | 5 +++-- .../Engine/Templates/Templates_Test.php | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/php/email-editor/src/Engine/Templates/class-templates.php b/packages/php/email-editor/src/Engine/Templates/class-templates.php index eb5af54d3a..f70af63db3 100644 --- a/packages/php/email-editor/src/Engine/Templates/class-templates.php +++ b/packages/php/email-editor/src/Engine/Templates/class-templates.php @@ -24,9 +24,9 @@ class Templates { /** * The post type. * - * @var string $post_type + * @var string[] $post_type */ - private string $post_type = 'mailpoet_email'; + private array $post_types = array(); /** * The template directory. * @@ -36,8 +36,11 @@ class Templates { /** * Initializes the class. + * + * @param string[] $post_types The list of post types registered for usage with email editor. */ - public function initialize(): void { + public function initialize( array $post_types ): void { + $this->post_types = $post_types; add_filter( 'theme_templates', array( $this, 'add_theme_templates' ), 10, 4 ); // Workaround needed when saving post – template association. $this->register_templates(); $this->register_post_types_to_api(); @@ -77,7 +80,7 @@ class Templates { 'title' => $general_email['title'], 'description' => $general_email['description'], 'content' => (string) file_get_contents( $this->template_directory . $template_filename ), - 'post_types' => array( $this->post_type ), + 'post_types' => $this->post_types, ) ); @@ -118,7 +121,7 @@ class Templates { if ( isset( $response_object['plugin'] ) && $response_object['plugin'] !== $this->template_prefix ) { return array(); } - return array( $this->post_type ); + return $this->post_types; } /** @@ -136,12 +139,12 @@ class Templates { * @return array */ public function add_theme_templates( $templates, $theme, $post, $post_type ) { - if ( $post_type && $post_type !== $this->post_type ) { + if ( $post_type && ! in_array( $post_type, $this->post_types, true ) ) { return $templates; } $block_templates = get_block_templates(); foreach ( $block_templates as $block_template ) { - if ( ! is_array( $block_template->post_types ) || ! in_array( $this->post_type, $block_template->post_types, true ) ) { + if ( ! is_array( $block_template->post_types ) || ! array_intersect( $this->post_types, $block_template->post_types ) ) { continue; } $templates[ $block_template->slug ] = $block_template; diff --git a/packages/php/email-editor/src/Engine/class-email-editor.php b/packages/php/email-editor/src/Engine/class-email-editor.php index febfecdbbd..df0a8560eb 100644 --- a/packages/php/email-editor/src/Engine/class-email-editor.php +++ b/packages/php/email-editor/src/Engine/class-email-editor.php @@ -96,9 +96,9 @@ class Email_Editor { public function initialize(): void { do_action( 'mailpoet_email_editor_initialized' ); add_filter( 'mailpoet_email_editor_rendering_theme_styles', array( $this, 'extend_email_theme_styles' ), 10, 2 ); - $this->register_block_templates(); $this->register_block_patterns(); $this->register_email_post_types(); + $this->register_block_templates(); $this->register_email_post_send_status(); $this->register_personalization_tags(); $is_editor_page = apply_filters( 'mailpoet_is_email_editor_page', false ); @@ -118,7 +118,8 @@ class Email_Editor { private function register_block_templates(): void { // Since we cannot currently disable blocks in the editor for specific templates, disable templates when viewing site editor. @see https://github.com/WordPress/gutenberg/issues/41062. if ( strstr( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ), 'site-editor.php' ) === false ) { - $this->templates->initialize(); + $post_types = array_column( $this->get_post_types(), 'name' ); + $this->templates->initialize( $post_types ); } } diff --git a/packages/php/email-editor/tests/integration/Engine/Templates/Templates_Test.php b/packages/php/email-editor/tests/integration/Engine/Templates/Templates_Test.php index f24878a4d7..1b9c55aca9 100644 --- a/packages/php/email-editor/tests/integration/Engine/Templates/Templates_Test.php +++ b/packages/php/email-editor/tests/integration/Engine/Templates/Templates_Test.php @@ -26,7 +26,6 @@ class Templates_Test extends \MailPoetTest { public function _before() { parent::_before(); $this->templates = $this->di_container->get( Templates::class ); - $this->templates->initialize(); } /** @@ -35,6 +34,7 @@ class Templates_Test extends \MailPoetTest { * @return void */ public function testItCanFetchBlockTemplate(): void { + $this->templates->initialize( array( 'mailpoet_email' ) ); $template = $this->templates->get_block_template( 'email-general' ); self::assertInstanceOf( \WP_Block_Template::class, $template ); @@ -43,4 +43,21 @@ class Templates_Test extends \MailPoetTest { verify( $template->title )->equals( 'General Email' ); verify( $template->description )->equals( 'A general template for emails.' ); } + + /** + * Test that action for registering templates is triggered + * + * @return void + */ + public function testItTriggersActionForRegisteringTemplates(): void { + $trigger_check = false; + add_action( + 'mailpoet_email_editor_register_templates', + function () use ( &$trigger_check ) { + $trigger_check = true; + } + ); + $this->templates->initialize( array( 'mailpoet_email' ) ); + verify( $trigger_check )->true(); + } }