diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 2d23f8545e87f..d17aa174398e3 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -729,7 +729,8 @@ function _wp_build_title_and_description_for_single_post_type_block_template( $p ); $args = array( - 'title' => $post_title, + 'title' => $post_title, + 'posts_per_page' => 2, ); $args = wp_parse_args( $args, $default_args ); diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index 5dbf91d911f07..05bd88bcd24dd 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -176,6 +176,191 @@ public function test_should_not_inject_ignored_hooked_block_into_template() { $this->assertStringNotContainsString( '', $template->content ); } + /** + * @ticket 65966 + */ + public function test_should_append_post_slug_to_title_when_posts_share_the_same_title() { + self::factory()->post->create( + array( + 'post_title' => 'Same Title', + 'post_name' => 'first-post', + ) + ); + self::factory()->post->create( + array( + 'post_title' => 'Same Title', + 'post_name' => 'second-post', + ) + ); + + $first_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'single-post-first-post', + 'post_title' => 'single-post-first-post', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $first_template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $second_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'single-post-second-post', + 'post_title' => 'single-post-second-post', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $second_template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $first_template = _build_block_template_result_from_post( $first_template_post ); + $second_template = _build_block_template_result_from_post( $second_template_post ); + + $this->assertNotWPError( $first_template, 'Building the template for the first post should not return an error.' ); + $this->assertSame( 'Post: Same Title (first-post)', $first_template->title, 'The title of the first post template should be suffixed with the post slug.' ); + $this->assertNotWPError( $second_template, 'Building the template for the second post should not return an error.' ); + $this->assertSame( 'Post: Same Title (second-post)', $second_template->title, 'The title of the second post template should be suffixed with the post slug.' ); + } + + /** + * @ticket 65966 + */ + public function test_should_not_append_post_slug_to_title_when_post_title_is_unique() { + self::factory()->post->create( + array( + 'post_title' => 'Unique Title', + 'post_name' => 'unique-post', + ) + ); + + $template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'single-post-unique-post', + 'post_title' => 'single-post-unique-post', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $template = _build_block_template_result_from_post( $template_post ); + + $this->assertNotWPError( $template, 'Building the template should not return an error.' ); + $this->assertSame( 'Post: Unique Title', $template->title, 'The template title should not be suffixed with the post slug when the post title is unique.' ); + } + + /** + * @ticket 65966 + */ + public function test_should_append_term_slug_to_title_when_terms_share_the_same_name() { + self::factory()->term->create( + array( + 'taxonomy' => 'post_tag', + 'name' => 'Same Name', + 'slug' => 'first-tag', + ) + ); + self::factory()->term->create( + array( + 'taxonomy' => 'post_tag', + 'name' => 'Same Name', + 'slug' => 'second-tag', + ) + ); + + $first_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'tag-first-tag', + 'post_title' => 'tag-first-tag', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $first_template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $second_template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'tag-second-tag', + 'post_title' => 'tag-second-tag', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $second_template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $first_template = _build_block_template_result_from_post( $first_template_post ); + $second_template = _build_block_template_result_from_post( $second_template_post ); + + $this->assertNotWPError( $first_template, 'Building the template for the first term should not return an error.' ); + $this->assertSame( 'Tag: Same Name (first-tag)', $first_template->title, 'The title of the first term template should be suffixed with the term slug.' ); + $this->assertNotWPError( $second_template, 'Building the template for the second term should not return an error.' ); + $this->assertSame( 'Tag: Same Name (second-tag)', $second_template->title, 'The title of the second term template should be suffixed with the term slug.' ); + } + + /** + * @ticket 65966 + */ + public function test_should_not_append_term_slug_to_title_when_term_name_is_unique() { + self::factory()->term->create( + array( + 'taxonomy' => 'category', + 'name' => 'Unique Category', + 'slug' => 'unique-category', + ) + ); + + $template_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'wp_template', + 'post_name' => 'category-unique-category', + 'post_title' => 'category-unique-category', + 'post_content' => 'Content', + 'post_excerpt' => '', + 'tax_input' => array( + 'wp_theme' => array( + self::TEST_THEME, + ), + ), + ) + ); + wp_set_post_terms( $template_post->ID, self::TEST_THEME, 'wp_theme' ); + + $template = _build_block_template_result_from_post( $template_post ); + + $this->assertNotWPError( $template, 'Building the template should not return an error.' ); + $this->assertSame( 'Category: Unique Category', $template->title, 'The template title should not be suffixed with the term slug when the term name is unique.' ); + } + /** * @ticket 59646 * @ticket 60506