Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Sensei Email template not displayed in site editor #7463

Merged
merged 4 commits into from Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-no-email-template-in-site-editor
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed Sensei Email template not available in site editor
32 changes: 16 additions & 16 deletions includes/internal/emails/class-email-page-template.php
Expand Up @@ -11,9 +11,6 @@
exit;
}




/**
* The class responsible for handling the email page template.
*
Expand Down Expand Up @@ -58,7 +55,6 @@
public function init(): void {
add_filter( 'pre_get_block_file_template', [ $this, 'get_from_file' ], 10, 3 );
add_filter( 'get_block_templates', [ $this, 'add_email_template' ], 10, 3 );

add_filter( 'get_block_template', [ $this, 'get_template' ], 10, 3 );
}

Expand Down Expand Up @@ -107,35 +103,39 @@
}

/**
* Return sensei email's template from file when there is no template on the database.
* Add Sensei email template.
*
* @internal
*
* @param WP_Block_Template $original_templates Original template.
* @param string $query Original query to load the template.
* @param string $template_type The template type "wp_template" or "wp_template_part".
* @param WP_Block_Template $query_result Array of found block templates.
* @param array $query Arguments to retrieve templates.
* @param string $template_type wp_template or wp_template_part.
*
* @return WP_Block_Template The original or the email template.
*/
public function add_email_template( $original_templates, $query, $template_type ) {
public function add_email_template( $query_result, $query, $template_type ) {
if ( ! \Sensei_Course_Theme_Editor::is_site_editor_request() ) {
return $query_result;
}

if ( 'wp_template' !== $template_type || ! empty( $query['theme'] ) ) {
return $original_templates;
return $query_result;

Check warning on line 122 in includes/internal/emails/class-email-page-template.php

View check run for this annotation

Codecov / codecov/patch

includes/internal/emails/class-email-page-template.php#L122

Added line #L122 was not covered by tests
}

$post_type = $query['post_type'] ?? get_post_type();

if ( empty( $post_type ) || Email_Post_Type::POST_TYPE !== $post_type ) {
return $original_templates;
if ( ! empty( $post_type ) && Email_Post_Type::POST_TYPE !== $post_type ) {
return $query_result;

Check warning on line 128 in includes/internal/emails/class-email-page-template.php

View check run for this annotation

Codecov / codecov/patch

includes/internal/emails/class-email-page-template.php#L128

Added line #L128 was not covered by tests
}

$from_db = $this->repository->get( self::ID );

if ( ! empty( $from_db ) ) {
$original_templates[] = $from_db;
} else {
$original_templates[] = $this->repository->get_from_file( self::TEMPLATE_PATH, self::ID );
$query_result[] = $from_db;
} else { // Use the PHP email template.
$query_result[] = $this->repository->get_from_file( self::TEMPLATE_PATH, self::ID );
}

return $original_templates;
return $query_result;
}
}
56 changes: 40 additions & 16 deletions tests/unit-tests/internal/emails/test-class-email-page-template.php
Expand Up @@ -13,7 +13,19 @@
* @covers \Sensei\Internal\Emails\Email_Page_Template
*/
class Email_Page_Template_Test extends \WP_UnitTestCase {

/**
* The URI which was given in order to access this page.
*
* @var string
*/
private static $initial_request_uri;

public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();

// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
self::$initial_request_uri = wp_unslash( $_SERVER['REQUEST_URI'] );
}

public function setUp(): void {
parent::setUp();
Expand All @@ -22,6 +34,11 @@ public function setUp(): void {
add_filter( 'get_block_templates', [ $this, 'get_block_templates' ], 10, 3 );
}

public function tearDown(): void {
parent::tearDown();

$_SERVER['REQUEST_URI'] = self::$initial_request_uri;
}

public function testHasInit_Always_RegistersFilters() {

Expand Down Expand Up @@ -94,10 +111,11 @@ public function testGetFromFile_WhenThePostTypeIsIncorrect_ReturnsDefaultTemplat
public function testAddEmailTemplate_TemplateGiven_ReturnsUpdatedList() {

/* Arrange. */
$repository = $this->createMock( Email_Page_Template_Repository::class );
$page_template = new Email_Page_Template( $repository );
$default_list = [ $this->createMock( \WP_Block_Template::class ) ];
$template_to_be_added = $this->createMock( \WP_Block_Template::class );
$_SERVER['REQUEST_URI'] = '/wp-admin/site-editor.php';
$repository = $this->createMock( Email_Page_Template_Repository::class );
$page_template = new Email_Page_Template( $repository );
$default_list = [ $this->createMock( \WP_Block_Template::class ) ];
$template_to_be_added = $this->createMock( \WP_Block_Template::class );

$repository
->method( 'get' )
Expand Down Expand Up @@ -132,7 +150,7 @@ public function testAddEmailTemplate_WhenThePostTypeIsIncorrect_ReturnsDefaultTe
self::assertSame( $default_list, $result );
}

public function testAddEmailTemplate_GivenQuerySpecifPostType_ReturnsDefaultTemplate() {
public function testAddEmailTemplate_GivenQuerySpecificPostType_ReturnsDefaultTemplate() {

/* Arrange. */
$repository = $this->createMock( Email_Page_Template_Repository::class );
Expand All @@ -146,18 +164,23 @@ public function testAddEmailTemplate_GivenQuerySpecifPostType_ReturnsDefaultTemp
self::assertSame( $default_list, $result );
}

public function testAddEmailTemplate_GivenQueryWithoutPostType_ReturnsDefaultTemplate() {

public function testAddEmailTemplate_GivenQueryWithoutPostType_ReturnsUpdatedList() {
/* Arrange. */
$repository = $this->createMock( Email_Page_Template_Repository::class );
$page_template = new Email_Page_Template( $repository );
$default_list = [ $this->createMock( \WP_Block_Template::class ) ];
$_SERVER['REQUEST_URI'] = '/wp-admin/site-editor.php';
$repository = $this->createMock( Email_Page_Template_Repository::class );
$page_template = new Email_Page_Template( $repository );
$default_list = [ $this->createMock( \WP_Block_Template::class ) ];
$template_to_be_added = $this->createMock( \WP_Block_Template::class );

$repository
->method( 'get' )
->willReturn( $template_to_be_added );

/* Act. */
$result = $page_template->add_email_template( $default_list, [], 'wp_template' );

/* Assert. */
self::assertSame( $default_list, $result );
self::assertSame( $template_to_be_added, $result[1] );
}

public function testAddEmailTemplate_WhenPostTypeIsIncorrect_ReturnsDefaultList() {
Expand Down Expand Up @@ -201,10 +224,11 @@ public function testAddEmailTemplate_WhenThemeIsSet_ReturnsDefaultList() {
public function testAddEmailTemplate_WhenThereIsNoTemplateStoredOnDB_ReturnsFromFile() {

/* Arrange. */
$repository = $this->createMock( Email_Page_Template_Repository::class );
$page_template = new Email_Page_Template( $repository );
$default_list = [ $this->createMock( \WP_Block_Template::class ) ];
$template_to_be_added = $this->createMock( \WP_Block_Template::class );
$_SERVER['REQUEST_URI'] = '/wp-admin/site-editor.php';
$repository = $this->createMock( Email_Page_Template_Repository::class );
$page_template = new Email_Page_Template( $repository );
$default_list = [ $this->createMock( \WP_Block_Template::class ) ];
$template_to_be_added = $this->createMock( \WP_Block_Template::class );

$repository
->method( 'get' )
Expand Down