Themes: Add the non-encoded form of the queried item slug to the template hierarchy when the slug contains non-ASCII characters.

This affects category, tag, and custom taxonomy archives, and single posts, pages, and custom post types.

Fixes #37655


git-svn-id: https://develop.svn.wordpress.org/trunk@38583 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2016-09-09 00:47:17 +00:00
parent 40ebbc7713
commit e7b058117a
2 changed files with 59 additions and 20 deletions

View File

@ -189,6 +189,12 @@ function get_category_template() {
$templates = array();
if ( ! empty( $category->slug ) ) {
$slug_decoded = urldecode( $category->slug );
if ( $slug_decoded !== $category->slug ) {
$templates[] = "category-{$slug_decoded}.php";
}
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
}
@ -219,6 +225,12 @@ function get_tag_template() {
$templates = array();
if ( ! empty( $tag->slug ) ) {
$slug_decoded = urldecode( $tag->slug );
if ( $slug_decoded !== $tag->slug ) {
$templates[] = "tag-{$slug_decoded}.php";
}
$templates[] = "tag-{$tag->slug}.php";
$templates[] = "tag-{$tag->term_id}.php";
}
@ -255,6 +267,12 @@ function get_taxonomy_template() {
if ( ! empty( $term->slug ) ) {
$taxonomy = $term->taxonomy;
$slug_decoded = urldecode( $term->slug );
if ( $slug_decoded !== $term->slug ) {
$templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
}
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
}
@ -349,8 +367,13 @@ function get_page_template() {
$templates = array();
if ( $template && 0 === validate_file( $template ) )
$templates[] = $template;
if ( $pagename )
if ( $pagename ) {
$pagename_decoded = urldecode( $pagename );
if ( $pagename_decoded !== $pagename ) {
$templates[] = "page-{$pagename_decoded}.php";
}
$templates[] = "page-$pagename.php";
}
if ( $id )
$templates[] = "page-$id.php";
$templates[] = 'page.php';
@ -409,6 +432,12 @@ function get_single_template() {
$templates = array();
if ( ! empty( $object->post_type ) ) {
$name_decoded = urldecode( $object->post_name );
if ( $name_decoded !== $object->post_name ) {
$templates[] = "single-{$object->post_type}-{$name_decoded}.php";
}
$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
$templates[] = "single-{$object->post_type}.php";
}

View File

@ -16,23 +16,23 @@ class Tests_Template extends WP_UnitTestCase {
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$page_on_front = $factory->post->create_and_get( array(
'post_type' => 'page',
'post_name' => 'page-on-front',
'post_name' => 'page-on-front-😀',
) );
self::$page_for_posts = $factory->post->create_and_get( array(
'post_type' => 'page',
'post_name' => 'page-for-posts',
'post_name' => 'page-for-posts-😀',
) );
self::$page = $factory->post->create_and_get( array(
'post_type' => 'page',
'post_name' => 'page-name',
'post_name' => 'page-name-😀',
) );
add_post_meta( self::$page->ID, '_wp_page_template', 'templates/page.php' );
self::$post = $factory->post->create_and_get( array(
'post_type' => 'post',
'post_name' => 'post-name',
'post_name' => 'post-name-😀',
'post_date' => '1984-02-25 12:34:56',
) );
set_post_format( self::$post, 'quote' );
@ -84,11 +84,12 @@ class Tests_Template extends WP_UnitTestCase {
public function test_category_template_hierarchy() {
$term = self::factory()->term->create_and_get( array(
'taxonomy' => 'category',
'slug' => 'foo',
'slug' => 'foo-😀',
) );
$this->assertTemplateHierarchy( get_term_link( $term ), array(
'category-foo.php',
'category-foo-😀.php',
'category-foo-%f0%9f%98%80.php',
"category-{$term->term_id}.php",
'category.php',
'archive.php',
@ -98,11 +99,12 @@ class Tests_Template extends WP_UnitTestCase {
public function test_tag_template_hierarchy() {
$term = self::factory()->term->create_and_get( array(
'taxonomy' => 'post_tag',
'slug' => 'foo',
'slug' => 'foo-😀',
) );
$this->assertTemplateHierarchy( get_term_link( $term ), array(
'tag-foo.php',
'tag-foo-😀.php',
'tag-foo-%f0%9f%98%80.php',
"tag-{$term->term_id}.php",
'tag.php',
'archive.php',
@ -112,11 +114,12 @@ class Tests_Template extends WP_UnitTestCase {
public function test_taxonomy_template_hierarchy() {
$term = self::factory()->term->create_and_get( array(
'taxonomy' => 'taxo',
'slug' => 'foo',
'slug' => 'foo-😀',
) );
$this->assertTemplateHierarchy( get_term_link( $term ), array(
'taxonomy-taxo-foo.php',
'taxonomy-taxo-foo-😀.php',
'taxonomy-taxo-foo-%f0%9f%98%80.php',
'taxonomy-taxo.php',
'taxonomy.php',
'archive.php',
@ -170,7 +173,8 @@ class Tests_Template extends WP_UnitTestCase {
$this->assertTemplateHierarchy( home_url(), array(
'front-page.php',
'page-page-on-front.php',
'page-page-on-front-😀.php',
'page-page-on-front-%f0%9f%98%80.php',
'page-' . self::$page_on_front->ID . '.php',
'page.php',
'singular.php',
@ -191,7 +195,8 @@ class Tests_Template extends WP_UnitTestCase {
public function test_page_template_hierarchy() {
$this->assertTemplateHierarchy( get_permalink( self::$page ), array(
'templates/page.php',
'page-page-name.php',
'page-page-name-😀.php',
'page-page-name-%f0%9f%98%80.php',
'page-' . self::$page->ID . '.php',
'page.php',
'singular.php',
@ -200,7 +205,8 @@ class Tests_Template extends WP_UnitTestCase {
public function test_single_template_hierarchy_for_post() {
$this->assertTemplateHierarchy( get_permalink( self::$post ), array(
'single-post-post-name.php',
'single-post-post-name-😀.php',
'single-post-post-name-%f0%9f%98%80.php',
'single-post.php',
'single.php',
'singular.php',
@ -210,11 +216,12 @@ class Tests_Template extends WP_UnitTestCase {
public function test_single_template_hierarchy_for_custom_post_type() {
$cpt = self::factory()->post->create_and_get( array(
'post_type' => 'cpt',
'post_name' => 'cpt-name',
'post_name' => 'cpt-name-😀',
) );
$this->assertTemplateHierarchy( get_permalink( $cpt ), array(
'single-cpt-cpt-name.php',
'single-cpt-cpt-name-😀.php',
'single-cpt-cpt-name-%f0%9f%98%80.php',
'single-cpt.php',
'single.php',
'singular.php',
@ -223,7 +230,7 @@ class Tests_Template extends WP_UnitTestCase {
public function test_attachment_template_hierarchy() {
$attachment = self::factory()->attachment->create_and_get( array(
'post_name' => 'attachment-name',
'post_name' => 'attachment-name-😀',
'file' => 'image.jpg',
'post_mime_type' => 'image/jpeg',
) );
@ -232,7 +239,8 @@ class Tests_Template extends WP_UnitTestCase {
'jpeg.php',
'image.php',
'attachment.php',
'single-attachment-attachment-name.php',
'single-attachment-attachment-name-😀.php',
'single-attachment-attachment-name-%f0%9f%98%80.php',
'single-attachment.php',
'single.php',
'singular.php',
@ -244,7 +252,8 @@ class Tests_Template extends WP_UnitTestCase {
'embed-post-quote.php',
'embed-post.php',
'embed.php',
'single-post-post-name.php',
'single-post-post-name-😀.php',
'single-post-post-name-%f0%9f%98%80.php',
'single-post.php',
'single.php',
'singular.php',
@ -256,7 +265,8 @@ class Tests_Template extends WP_UnitTestCase {
'embed-page.php',
'embed.php',
'templates/page.php',
'page-page-name.php',
'page-page-name-😀.php',
'page-page-name-%f0%9f%98%80.php',
'page-' . self::$page->ID . '.php',
'page.php',
'singular.php',