Avoid duplicate classes for different terms with UTF-8 slugs in `post_class()` and `body_class()`.

Fall back to term ID if the sanitized slug is numeric or only contains hyphens.

props SergeyBiryukov, A5hleyRich, sgrant, davideugenepratt.
fixes #30883.

git-svn-id: https://develop.svn.wordpress.org/trunk@31979 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2015-04-02 01:04:22 +00:00
parent b9537f9488
commit 4738590d5e
3 changed files with 158 additions and 8 deletions

View File

@ -471,11 +471,16 @@ function get_post_class( $class = '', $post_id = null ) {
continue;
}
$term_class = sanitize_html_class( $term->slug, $term->term_id );
if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
$term_class = $term->term_id;
}
// 'post_tag' uses the 'tag' prefix for backward compatibility.
if ( 'post_tag' == $taxonomy ) {
$classes[] = 'tag-' . sanitize_html_class( $term->slug, $term->term_id );
$classes[] = 'tag-' . $term_class;
} else {
$classes[] = sanitize_html_class( $taxonomy . '-' . $term->slug, $taxonomy . '-' . $term->term_id );
$classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
}
}
}
@ -588,21 +593,36 @@ function get_body_class( $class = '' ) {
$cat = $wp_query->get_queried_object();
$classes[] = 'category';
if ( isset( $cat->term_id ) ) {
$classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
$cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
$cat_class = $cat->term_id;
}
$classes[] = 'category-' . $cat_class;
$classes[] = 'category-' . $cat->term_id;
}
} elseif ( is_tag() ) {
$tags = $wp_query->get_queried_object();
$tag = $wp_query->get_queried_object();
$classes[] = 'tag';
if ( isset( $tags->term_id ) ) {
$classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
$classes[] = 'tag-' . $tags->term_id;
if ( isset( $tag->term_id ) ) {
$tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
$tag_class = $tag->term_id;
}
$classes[] = 'tag-' . $tag_class;
$classes[] = 'tag-' . $tag->term_id;
}
} elseif ( is_tax() ) {
$term = $wp_query->get_queried_object();
if ( isset( $term->term_id ) ) {
$term_class = sanitize_html_class( $term->slug, $term->term_id );
if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
$term_class = $term->term_id;
}
$classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
$classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
$classes[] = 'term-' . $term_class;
$classes[] = 'term-' . $term->term_id;
}
}

View File

@ -0,0 +1,81 @@
<?php
/**
* @group post
* @covers ::get_body_class
*/
class Tests_Post_GetBodyClass extends WP_UnitTestCase {
protected $post_id;
public function setUp() {
parent::setUp();
$this->post_id = $this->factory->post->create();
}
/**
* @ticket 30883
*/
public function test_with_utf8_category_slugs() {
$cat_id1 = $this->factory->category->create( array( 'name' => 'Первая рубрика' ) );
$cat_id2 = $this->factory->category->create( array( 'name' => 'Вторая рубрика' ) );
$cat_id3 = $this->factory->category->create( array( 'name' => '25кадр' ) );
wp_set_post_terms( $this->post_id, array( $cat_id1, $cat_id2, $cat_id3 ), 'category' );
$this->go_to( home_url( "?cat=$cat_id1" ) );
$this->assertContains( "category-$cat_id1", get_body_class() );
$this->go_to( home_url( "?cat=$cat_id2" ) );
$this->assertContains( "category-$cat_id2", get_body_class() );
$this->go_to( home_url( "?cat=$cat_id3" ) );
$this->assertContains( "category-$cat_id3", get_body_class() );
}
/**
* @ticket 30883
*/
public function test_with_utf8_tag_slugs() {
$tag_id1 = $this->factory->tag->create( array( 'name' => 'Первая метка' ) );
$tag_id2 = $this->factory->tag->create( array( 'name' => 'Вторая метка' ) );
$tag_id3 = $this->factory->tag->create( array( 'name' => '25кадр' ) );
wp_set_post_terms( $this->post_id, array( $tag_id1, $tag_id2, $tag_id3 ), 'post_tag' );
$tag1 = get_term( $tag_id1, 'post_tag' );
$tag2 = get_term( $tag_id2, 'post_tag' );
$tag3 = get_term( $tag_id3, 'post_tag' );
$this->go_to( home_url( "?tag={$tag1->slug}" ) );
$this->assertContains( "tag-$tag_id1", get_body_class() );
$this->go_to( home_url( "?tag={$tag2->slug}" ) );
$this->assertContains( "tag-$tag_id2", get_body_class() );
$this->go_to( home_url( "?tag={$tag3->slug}" ) );
$this->assertContains( "tag-$tag_id3", get_body_class() );
}
/**
* @ticket 30883
*/
public function test_with_utf8_term_slugs() {
register_taxonomy( 'wptests_tax', 'post' );
$term_id1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'Первая метка' ) );
$term_id2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'Вторая метка' ) );
$term_id3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => '25кадр' ) );
wp_set_post_terms( $this->post_id, array( $term_id1, $term_id2, $term_id3 ), 'wptests_tax' );
$term1 = get_term( $term_id1, 'wptests_tax' );
$term2 = get_term( $term_id2, 'wptests_tax' );
$term3 = get_term( $term_id3, 'wptests_tax' );
$this->go_to( home_url( "?wptests_tax={$term1->slug}" ) );
$this->assertContains( "term-$term_id1", get_body_class() );
$this->go_to( home_url( "?wptests_tax={$term2->slug}" ) );
$this->assertContains( "term-$term_id2", get_body_class() );
$this->go_to( home_url( "?wptests_tax={$term3->slug}" ) );
$this->assertContains( "term-$term_id3", get_body_class() );
}
}

View File

@ -53,6 +53,55 @@ class Tests_Post_GetPostClass extends WP_UnitTestCase {
$this->assertEquals( array( 'foo', 'bar' ), get_post_class( array( 'foo', 'bar' ), null ) );
}
/**
* @ticket 30883
*/
public function test_with_utf8_category_slugs() {
$cat_id1 = $this->factory->category->create( array( 'name' => 'Первая рубрика' ) );
$cat_id2 = $this->factory->category->create( array( 'name' => 'Вторая рубрика' ) );
$cat_id3 = $this->factory->category->create( array( 'name' => '25кадр' ) );
wp_set_post_terms( $this->post_id, array( $cat_id1, $cat_id2, $cat_id3 ), 'category' );
$found = get_post_class( '', $this->post_id );
$this->assertContains( "category-$cat_id1", $found );
$this->assertContains( "category-$cat_id2", $found );
$this->assertContains( "category-$cat_id3", $found );
}
/**
* @ticket 30883
*/
public function test_with_utf8_tag_slugs() {
$tag_id1 = $this->factory->tag->create( array( 'name' => 'Первая метка' ) );
$tag_id2 = $this->factory->tag->create( array( 'name' => 'Вторая метка' ) );
$tag_id3 = $this->factory->tag->create( array( 'name' => '25кадр' ) );
wp_set_post_terms( $this->post_id, array( $tag_id1, $tag_id2, $tag_id3 ), 'post_tag' );
$found = get_post_class( '', $this->post_id );
$this->assertContains( "tag-$tag_id1", $found );
$this->assertContains( "tag-$tag_id2", $found );
$this->assertContains( "tag-$tag_id3", $found );
}
/**
* @ticket 30883
*/
public function test_with_utf8_term_slugs() {
register_taxonomy( 'wptests_tax', 'post' );
$term_id1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'Первая метка' ) );
$term_id2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'Вторая метка' ) );
$term_id3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => '25кадр' ) );
wp_set_post_terms( $this->post_id, array( $term_id1, $term_id2, $term_id3 ), 'wptests_tax' );
$found = get_post_class( '', $this->post_id );
$this->assertContains( "wptests_tax-$term_id1", $found );
$this->assertContains( "wptests_tax-$term_id2", $found );
$this->assertContains( "wptests_tax-$term_id3", $found );
}
/**
* @group cache
*/