Refactor template registration to use Template_Registry

[MAILPOET-6453]
This commit is contained in:
Jan Lysý
2025-02-05 20:05:32 +01:00
committed by Jan Lysý
parent 9abf223005
commit 50e986715e
3 changed files with 43 additions and 29 deletions

View File

@@ -33,6 +33,21 @@ class Templates {
* @var string $template_directory
*/
private string $template_directory = __DIR__ . DIRECTORY_SEPARATOR;
/**
* The templates registry.
*
* @var Templates_Registry $templates_registry
*/
private Templates_Registry $templates_registry;
/**
* Constructor of the class.
*
* @param Templates_Registry $templates_registry The templates registry.
*/
public function __construct( Templates_Registry $templates_registry ) {
$this->templates_registry = $templates_registry;
}
/**
* Initializes the class.
@@ -42,7 +57,8 @@ class Templates {
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();
add_filter( 'mailpoet_email_editor_register_templates', array( $this, 'register_templates' ) );
$this->templates_registry->initialize();
$this->register_post_types_to_api();
}
@@ -60,36 +76,26 @@ class Templates {
/**
* Register the templates via register_block_template
*
* @param Templates_Registry $templates_registry The templates registry.
*/
private function register_templates(): void {
// The function was added in WordPress 6.7. We can remove this check after we drop support for WordPress 6.6.
if ( ! function_exists( 'register_block_template' ) ) {
return;
}
public function register_templates( Templates_Registry $templates_registry ): Templates_Registry {
// Register basic blank template.
$general_email = array(
'title' => __( 'General Email', 'mailpoet' ),
'description' => __( 'A general template for emails.', 'mailpoet' ),
'slug' => 'email-general',
$general_email_slug = 'email-general';
$template_filename = $general_email_slug . '.html';
$general_email = new Template(
$this->template_prefix,
$general_email_slug,
__( 'General Email', 'mailpoet' ),
__( 'A general template for emails.', 'mailpoet' ),
(string) file_get_contents( $this->template_directory . $template_filename ),
$this->post_types
);
$template_filename = $general_email['slug'] . '.html';
$template_name = $this->template_prefix . '//' . $general_email['slug'];
$templates_registry->register( $general_email );
if ( ! \WP_Block_Templates_Registry::get_instance()->is_registered( $template_name ) ) {
// skip registration if the template was already registered.
register_block_template(
$template_name,
array(
'title' => $general_email['title'],
'description' => $general_email['description'],
'content' => (string) file_get_contents( $this->template_directory . $template_filename ),
'post_types' => $this->post_types,
)
);
}
do_action( 'mailpoet_email_editor_register_templates' );
return $templates_registry;
}
/**

View File

@@ -53,8 +53,9 @@ class Templates_Test extends \MailPoetTest {
$trigger_check = false;
add_action(
'mailpoet_email_editor_register_templates',
function () use ( &$trigger_check ) {
function ( $registry ) use ( &$trigger_check ) {
$trigger_check = true;
return $registry;
}
);
$this->templates->initialize( array( 'mailpoet_email' ) );

View File

@@ -28,6 +28,7 @@ use MailPoet\EmailEditor\Engine\Renderer\Renderer;
use MailPoet\EmailEditor\Engine\Send_Preview_Email;
use MailPoet\EmailEditor\Engine\Settings_Controller;
use MailPoet\EmailEditor\Engine\Templates\Templates;
use MailPoet\EmailEditor\Engine\Templates\Templates_Registry;
use MailPoet\EmailEditor\Engine\Theme_Controller;
use MailPoet\EmailEditor\Engine\User_Theme;
use MailPoet\EmailEditor\Integrations\Core\Initializer;
@@ -188,9 +189,15 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
}
);
$container->set(
Templates::class,
Templates_Registry::class,
function () {
return new Templates();
return new Templates_Registry();
}
);
$container->set(
Templates::class,
function ( $container ) {
return new Templates( $container->get( Templates_Registry::class ) );
}
);
$container->set(