Switch template registration to use register_block_template
[MAILPOET-6356]
This commit is contained in:
committed by
Rostislav Wolný
parent
62b77b724c
commit
2fc0ad3c9e
@@ -251,7 +251,7 @@ export const getEmailTemplates = createRegistrySelector(
|
|||||||
?.filter(
|
?.filter(
|
||||||
( template ) =>
|
( template ) =>
|
||||||
// @ts-expect-error Missing property in type
|
// @ts-expect-error Missing property in type
|
||||||
template.theme === 'mailpoet/mailpoet'
|
template.plugin === 'mailpoet'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -72,7 +72,7 @@ class Renderer {
|
|||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function render( \WP_Post $post, string $subject, string $pre_header, string $language, $meta_robots = '' ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
public function render( \WP_Post $post, string $subject, string $pre_header, string $language, $meta_robots = '' ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
||||||
$template_id = 'mailpoet/mailpoet//' . ( get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general' );
|
$template_id = get_stylesheet() . '//' . ( 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 );
|
||||||
|
|
||||||
|
@@ -25,7 +25,7 @@ class Templates {
|
|||||||
*
|
*
|
||||||
* @var string $plugin_slug
|
* @var string $plugin_slug
|
||||||
*/
|
*/
|
||||||
private string $plugin_slug = 'mailpoet/mailpoet';
|
private string $template_prefix = 'mailpoet';
|
||||||
/**
|
/**
|
||||||
* The post type.
|
* The post type.
|
||||||
*
|
*
|
||||||
@@ -60,11 +60,6 @@ class Templates {
|
|||||||
* Initializes the class.
|
* Initializes the class.
|
||||||
*/
|
*/
|
||||||
public function initialize(): void {
|
public function initialize(): void {
|
||||||
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
|
|
||||||
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
|
|
||||||
add_filter( 'theme_templates', array( $this, 'add_theme_templates' ), 10, 4 ); // Needed when saving post – template association.
|
|
||||||
add_filter( 'get_block_template', array( $this, 'add_block_template_details' ), 10, 1 );
|
|
||||||
add_filter( 'rest_pre_insert_wp_template', array( $this, 'force_post_content' ), 9, 1 );
|
|
||||||
$this->initialize_templates();
|
$this->initialize_templates();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +70,7 @@ class Templates {
|
|||||||
* @return WP_Block_Template|null
|
* @return WP_Block_Template|null
|
||||||
*/
|
*/
|
||||||
public function get_block_template( $template_id ) {
|
public function get_block_template( $template_id ) {
|
||||||
$templates = $this->get_block_templates();
|
return get_block_template( $template_id );
|
||||||
return $templates[ $template_id ] ?? null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,7 +84,7 @@ class Templates {
|
|||||||
public function get_block_file_template( $result, $template_id, $template_type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
public function get_block_file_template( $result, $template_id, $template_type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
||||||
['prefix' => $template_prefix, 'slug' => $template_slug] = $this->utils->get_template_id_parts( $template_id );
|
['prefix' => $template_prefix, 'slug' => $template_slug] = $this->utils->get_template_id_parts( $template_id );
|
||||||
|
|
||||||
if ( $this->plugin_slug !== $template_prefix ) {
|
if ( $this->template_prefix !== $template_prefix ) {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,6 +195,9 @@ class Templates {
|
|||||||
* Initialize template details. This is done at runtime because of localisation.
|
* Initialize template details. This is done at runtime because of localisation.
|
||||||
*/
|
*/
|
||||||
private function initialize_templates(): void {
|
private function initialize_templates(): void {
|
||||||
|
if ( ! function_exists( 'register_block_template' ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
$this->templates['email-general'] = array(
|
$this->templates['email-general'] = array(
|
||||||
'title' => __( 'General Email', 'mailpoet' ),
|
'title' => __( 'General Email', 'mailpoet' ),
|
||||||
'description' => __( 'A general template for emails.', 'mailpoet' ),
|
'description' => __( 'A general template for emails.', 'mailpoet' ),
|
||||||
@@ -209,6 +206,19 @@ class Templates {
|
|||||||
'title' => __( 'Simple Light', 'mailpoet' ),
|
'title' => __( 'Simple Light', 'mailpoet' ),
|
||||||
'description' => __( 'A basic template with header and footer.', 'mailpoet' ),
|
'description' => __( 'A basic template with header and footer.', 'mailpoet' ),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
foreach ( $this->templates as $template_slug => $template ) {
|
||||||
|
$template = $this->get_block_template_from_file( $template_slug . '.html' );
|
||||||
|
register_block_template(
|
||||||
|
$this->template_prefix . '//' . $template_slug,
|
||||||
|
array(
|
||||||
|
'title' => $template->title,
|
||||||
|
'description' => $template->description,
|
||||||
|
'content' => $template->content,
|
||||||
|
'post_types' => array( $this->post_type ),
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -252,12 +262,12 @@ class Templates {
|
|||||||
$template_slug = $this->utils->get_block_template_slug_from_path( $template );
|
$template_slug = $this->utils->get_block_template_slug_from_path( $template );
|
||||||
$template_object = (object) array(
|
$template_object = (object) array(
|
||||||
'slug' => $template_slug,
|
'slug' => $template_slug,
|
||||||
'id' => $this->plugin_slug . '//' . $template_slug,
|
'id' => $this->template_prefix . '//' . $template_slug,
|
||||||
'title' => $this->templates[ $template_slug ]['title'] ?? '',
|
'title' => $this->templates[ $template_slug ]['title'] ?? '',
|
||||||
'description' => $this->templates[ $template_slug ]['description'] ?? '',
|
'description' => $this->templates[ $template_slug ]['description'] ?? '',
|
||||||
'path' => $this->template_directory . $template,
|
'path' => $this->template_directory . $template,
|
||||||
'type' => 'wp_template',
|
'type' => 'wp_template',
|
||||||
'theme' => $this->plugin_slug,
|
'theme' => $this->template_prefix,
|
||||||
'source' => 'plugin',
|
'source' => 'plugin',
|
||||||
'post_types' => array(
|
'post_types' => array(
|
||||||
$this->post_type,
|
$this->post_type,
|
||||||
@@ -282,7 +292,7 @@ class Templates {
|
|||||||
array(
|
array(
|
||||||
'taxonomy' => 'wp_theme',
|
'taxonomy' => 'wp_theme',
|
||||||
'field' => 'name',
|
'field' => 'name',
|
||||||
'terms' => array( $this->plugin_slug, get_stylesheet() ),
|
'terms' => array( $this->template_prefix, get_stylesheet() ),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@@ -73,6 +73,7 @@ class IntegrationTester extends \Codeception\Actor {
|
|||||||
*/
|
*/
|
||||||
public function cleanup(): void {
|
public function cleanup(): void {
|
||||||
$this->delete_posts();
|
$this->delete_posts();
|
||||||
|
$this->unregister_block_templates();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -94,4 +95,17 @@ class IntegrationTester extends \Codeception\Actor {
|
|||||||
}
|
}
|
||||||
$this->cleanup_user_theme_post();
|
$this->cleanup_user_theme_post();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregister block templates we may add during the tests.
|
||||||
|
*/
|
||||||
|
private function unregister_block_templates(): void {
|
||||||
|
$registry = WP_Block_Templates_Registry::get_instance();
|
||||||
|
$templates = $registry->get_all_registered();
|
||||||
|
foreach ( $templates as $name => $template ) {
|
||||||
|
if ( 'mailpoet' === $template->plugin && $registry->is_registered( $name ) ) {
|
||||||
|
$registry->unregister( $name );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -35,12 +35,12 @@ class Templates_Test extends \MailPoetTest {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testItCanFetchBlockTemplate(): void {
|
public function testItCanFetchBlockTemplate(): void {
|
||||||
$template_id = 'mailpoet/mailpoet//email-general';
|
$template_id = get_stylesheet() . '//email-general'; // Templates id is prefixed with the theme name.
|
||||||
$template = $this->templates->get_block_template( $template_id );
|
$template = $this->templates->get_block_template( $template_id );
|
||||||
|
|
||||||
self::assertInstanceOf( \WP_Block_Template::class, $template );
|
self::assertInstanceOf( \WP_Block_Template::class, $template );
|
||||||
verify( $template->slug )->equals( 'email-general' );
|
verify( $template->slug )->equals( 'email-general' );
|
||||||
verify( $template->id )->equals( 'mailpoet/mailpoet//email-general' );
|
verify( $template->id )->equals( $template_id );
|
||||||
verify( $template->title )->equals( 'General Email' );
|
verify( $template->title )->equals( 'General Email' );
|
||||||
verify( $template->description )->equals( 'A general template for emails.' );
|
verify( $template->description )->equals( 'A general template for emails.' );
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user