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

Honor postTypes property in theme's theme.json when getting templates from post #3131

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ function _build_block_template_result_from_post( $post ) {
}

$theme = $terms[0]->name;
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme &&
null !== _get_block_template_file( $post->post_type, $post->post_name );
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
$has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file;

$origin = get_post_meta( $post->ID, 'origin', true );

Expand All @@ -573,6 +573,10 @@ function _build_block_template_result_from_post( $post ) {
$template->is_custom = true;
$template->author = $post->post_author;

if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
}

if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
$template->is_custom = false;
}
Expand Down Expand Up @@ -675,6 +679,13 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
continue;
}

if ( $post_type &&
isset( $template->post_types ) &&
! in_array( $post_type, $template->post_types, true )
) {
continue;
}

$query_result[] = $template;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Custom Single Post template</p>
<!-- /wp:paragraph -->
5 changes: 5 additions & 0 deletions tests/phpunit/data/themedir1/block-theme/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
{
"name": "page-home",
"title": "Homepage template"
},
{
"name": "custom-single-post-template",
"title": "Custom Single Post template",
"postTypes": ["post"]
}
],
"templateParts": [
Expand Down
74 changes: 74 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
class Tests_Block_Template_Utils extends WP_UnitTestCase {
private static $post;
private static $custom_single_post_template_post;
private static $template_part_post;
private static $test_theme = 'block-theme';

Expand Down Expand Up @@ -50,6 +51,22 @@ public static function wpSetUpBeforeClass() {
self::$post = self::factory()->post->create_and_get( $args );
wp_set_post_terms( self::$post->ID, self::$test_theme, 'wp_theme' );

// Set up template post.
$args = array(
'post_type' => 'wp_template',
'post_name' => 'custom-single-post-template',
'post_title' => 'Custom Single Post template (modified)',
'post_content' => 'Content',
'post_excerpt' => 'Description of custom single post template',
'tax_input' => array(
'wp_theme' => array(
self::$test_theme,
),
),
);
self::$custom_single_post_template_post = self::factory()->post->create_and_get( $args );
wp_set_post_terms( self::$custom_single_post_template_post->ID, self::$test_theme, 'wp_theme' );

// Set up template part post.
$template_part_args = array(
'post_type' => 'wp_template_part',
Expand Down Expand Up @@ -78,6 +95,7 @@ public function set_up() {

public static function wpTearDownAfterClass() {
wp_delete_post( self::$post->ID );
wp_delete_post( self::$custom_single_post_template_post->ID );
}

public function test_build_block_template_result_from_post() {
Expand Down Expand Up @@ -321,6 +339,62 @@ static function( $template ) {
*/
}

/**
* @dataProvider data_get_block_template_should_respect_posttypes_property
* @ticket 55881
* @covers ::get_block_templates
*
* @param string $post_type Post type for query.
* @param array $expected Expected template IDs.
*/
public function test_get_block_template_should_respect_posttypes_property( $post_type, $expected ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To isolate the tests for the fix, I've:

  • created each dataset for switching the post type
  • encapsulated the tests for validating that it respects the post type property

By isolating the tests, it makes it easier to identify the purpose of the test. It will also will make it easier when the test class is split into separate files one per function (this is out-of-scope for this PR).

The test class itself needs to be split into separate files, one for each function. But that's out-of-scope for this PR.

$templates = get_block_templates( array( 'post_type' => $post_type ) );

$this->assertSame(
$expected,
$this->get_template_ids( $templates )
);
}

/**
* Data provider.
*
* @return array
*/
public function data_get_block_template_should_respect_posttypes_property() {
return array(
'post' => array(
'post_type' => 'post',
'expected' => array(
'block-theme//my_template',
'block-theme//custom-single-post-template',
),
),
'page' => array(
'post_type' => 'page',
'expected' => array(
'block-theme//my_template',
'block-theme//page-home',
),
),
);
}

/**
* Gets the template IDs from the given array.
*
* @param object[] $templates Array of template objects to parse.
* @return string[] The template IDs.
*/
private function get_template_ids( $templates ) {
return array_map(
static function( $template ) {
return $template->id;
},
$templates
);
}

/**
* Should flatten nested blocks
*/
Expand Down
21 changes: 13 additions & 8 deletions tests/phpunit/tests/theme/wpThemeJsonResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ public function test_translations_are_applied() {
),
$theme_data->get_settings()
);
$this->assertSameSets(

$custom_templates = $theme_data->get_custom_templates();
$this->assertArrayHasKey( 'page-home', $custom_templates );
$this->assertSame(
$custom_templates['page-home'],
array(
'page-home' => array(
'title' => 'Szablon strony głównej',
'postTypes' => array( 'page' ),
),
),
$theme_data->get_custom_templates()
'title' => 'Szablon strony głównej',
'postTypes' => array( 'page' ),
)
);
$this->assertSameSets(
array(
Expand Down Expand Up @@ -340,10 +341,14 @@ function test_merges_child_theme_json_into_parent_theme_json() {
$this->assertSame(
WP_Theme_JSON_Resolver::get_theme_data()->get_custom_templates(),
array(
'page-home' => array(
'page-home' => array(
'title' => 'Homepage',
'postTypes' => array( 'page' ),
),
'custom-single-post-template' => array(
'title' => 'Custom Single Post template',
'postTypes' => array( 'post' ),
),
)
);
}
Expand Down