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]
This commit is contained in:
committed by
Rostislav Wolný
parent
2c3932bdd3
commit
30f11dc9cc
@@ -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;
|
||||
|
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user