Skip to content

Commit

Permalink
Merge pull request #7517 from Automattic/fix/email-post-type-being-se…
Browse files Browse the repository at this point in the history
…t-for-all-posts

Fix Sensei Email template being set as default template in editor when Gutenberg is installed
  • Loading branch information
Imran92 committed Feb 29, 2024
2 parents 28aef97 + 0c601eb commit 66c1ab6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog/fix-email-post-type-being-set-for-all-posts
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Email Template showing up as default template in editor when GB is installed
9 changes: 8 additions & 1 deletion includes/internal/emails/class-email-page-template.php
Expand Up @@ -44,7 +44,6 @@ class Email_Page_Template {
*/
public function __construct( Email_Page_Template_Repository $repository ) {
$this->repository = $repository;

}

/**
Expand Down Expand Up @@ -118,6 +117,14 @@ public function add_email_template( $query_result, $query, $template_type ) {
return $query_result;
}

$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';

// Returning early if it's Gutenberg's template lookup ajax request,
// otherwise it shows the template in editor as default template.
if ( $uri && strpos( $uri, '/wp-json/wp/v2/templates/lookup?slug' ) !== false ) {
return $query_result;
}

if ( 'wp_template' !== $template_type || ! empty( $query['theme'] ) ) {
return $query_result;
}
Expand Down
Expand Up @@ -245,5 +245,22 @@ public function testAddEmailTemplate_WhenThereIsNoTemplateStoredOnDB_ReturnsFrom
self::assertSame( $template_to_be_added, $result[1] );
}

public function testAddEmailTemplate_WhenAjaxLookupRequest_ReturnsListWithoutUpdating() {
/* Arrange. */
$_SERVER['REQUEST_URI'] = '/wp-json/wp/v2/templates/lookup?slug=single-post-hello-world&_locale=user';
$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 );
}
}

0 comments on commit 66c1ab6

Please sign in to comment.