Move the template_id construction logic to Templates class

[MAILPOET-6356]
This commit is contained in:
Rostislav Wolny
2024-12-13 17:35:14 +01:00
committed by Rostislav Wolný
parent a5db2564c8
commit 9b9187bb3e
3 changed files with 8 additions and 7 deletions

View File

@ -72,9 +72,9 @@ 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 = get_stylesheet() . '//' . ( get_page_template_slug( $post ) ? get_page_template_slug( $post ) : 'email-general' ); $template_slug = 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_slug );
$email_styles = $this->theme_controller->get_styles(); $email_styles = $this->theme_controller->get_styles();
$template_html = $this->content_renderer->render( $post, $template ); $template_html = $this->content_renderer->render( $post, $template );

View File

@ -51,10 +51,12 @@ class Templates {
/** /**
* Get a block template by ID. * Get a block template by ID.
* *
* @param string $template_id The template ID. * @param string $template_slug The template slug.
* @return WP_Block_Template|null * @return WP_Block_Template|null
*/ */
public function get_block_template( $template_id ) { public function get_block_template( $template_slug ) {
// Template id is always prefixed by active theme and get_stylesheet returns the active theme slug.
$template_id = get_stylesheet() . '//' . $template_slug;
return get_block_template( $template_id ); return get_block_template( $template_id );
} }

View File

@ -35,12 +35,11 @@ class Templates_Test extends \MailPoetTest {
* @return void * @return void
*/ */
public function testItCanFetchBlockTemplate(): void { public function testItCanFetchBlockTemplate(): void {
$template_id = get_stylesheet() . '//email-general'; // Templates id is prefixed with the theme name. $template = $this->templates->get_block_template( 'email-general' );
$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( $template_id ); verify( $template->id )->stringContainsString( 'email-general' );
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.' );
} }