WordPress.org

Make WordPress Core

Changeset 31979


Ignore:
Timestamp:
04/02/2015 01:04:22 AM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-template.php

    r31905 r31979  
    472472                }
    473473
     474                $term_class = sanitize_html_class( $term->slug, $term->term_id );
     475                if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
     476                    $term_class = $term->term_id;
     477                }
     478
    474479                // 'post_tag' uses the 'tag' prefix for backward compatibility.
    475480                if ( 'post_tag' == $taxonomy ) {
    476                     $classes[] = 'tag-' . sanitize_html_class( $term->slug, $term->term_id );
     481                    $classes[] = 'tag-' . $term_class;
    477482                } else {
    478                     $classes[] = sanitize_html_class( $taxonomy . '-' . $term->slug, $taxonomy . '-' . $term->term_id );
     483                    $classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
    479484                }
    480485            }
     
    589594            $classes[] = 'category';
    590595            if ( isset( $cat->term_id ) ) {
    591                 $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id );
     596                $cat_class = sanitize_html_class( $cat->slug, $cat->term_id );
     597                if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) {
     598                    $cat_class = $cat->term_id;
     599                }
     600
     601                $classes[] = 'category-' . $cat_class;
    592602                $classes[] = 'category-' . $cat->term_id;
    593603            }
    594604        } elseif ( is_tag() ) {
    595             $tags = $wp_query->get_queried_object();
     605            $tag = $wp_query->get_queried_object();
    596606            $classes[] = 'tag';
    597             if ( isset( $tags->term_id ) ) {
    598                 $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id );
    599                 $classes[] = 'tag-' . $tags->term_id;
     607            if ( isset( $tag->term_id ) ) {
     608                $tag_class = sanitize_html_class( $tag->slug, $tag->term_id );
     609                if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) {
     610                    $tag_class = $tag->term_id;
     611                }
     612
     613                $classes[] = 'tag-' . $tag_class;
     614                $classes[] = 'tag-' . $tag->term_id;
    600615            }
    601616        } elseif ( is_tax() ) {
    602617            $term = $wp_query->get_queried_object();
    603618            if ( isset( $term->term_id ) ) {
     619                $term_class = sanitize_html_class( $term->slug, $term->term_id );
     620                if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
     621                    $term_class = $term->term_id;
     622                }
     623
    604624                $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy );
    605                 $classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id );
     625                $classes[] = 'term-' . $term_class;
    606626                $classes[] = 'term-' . $term->term_id;
    607627            }
  • trunk/tests/phpunit/tests/post/getPostClass.php

    r31408 r31979  
    5555
    5656    /**
     57     * @ticket 30883
     58     */
     59    public function test_with_utf8_category_slugs() {
     60        $cat_id1 = $this->factory->category->create( array( 'name' => 'Первая рубрика' ) );
     61        $cat_id2 = $this->factory->category->create( array( 'name' => 'Вторая рубрика' ) );
     62        $cat_id3 = $this->factory->category->create( array( 'name' => '25кадр' ) );
     63        wp_set_post_terms( $this->post_id, array( $cat_id1, $cat_id2, $cat_id3 ), 'category' );
     64
     65        $found = get_post_class( '', $this->post_id );
     66
     67        $this->assertContains( "category-$cat_id1", $found );
     68        $this->assertContains( "category-$cat_id2", $found );
     69        $this->assertContains( "category-$cat_id3", $found );
     70    }
     71
     72    /**
     73     * @ticket 30883
     74     */
     75    public function test_with_utf8_tag_slugs() {
     76        $tag_id1 = $this->factory->tag->create( array( 'name' => 'Первая метка' ) );
     77        $tag_id2 = $this->factory->tag->create( array( 'name' => 'Вторая метка' ) );
     78        $tag_id3 = $this->factory->tag->create( array( 'name' => '25кадр' ) );
     79        wp_set_post_terms( $this->post_id, array( $tag_id1, $tag_id2, $tag_id3 ), 'post_tag' );
     80
     81        $found = get_post_class( '', $this->post_id );
     82
     83        $this->assertContains( "tag-$tag_id1", $found );
     84        $this->assertContains( "tag-$tag_id2", $found );
     85        $this->assertContains( "tag-$tag_id3", $found );
     86    }
     87
     88    /**
     89     * @ticket 30883
     90     */
     91    public function test_with_utf8_term_slugs() {
     92        register_taxonomy( 'wptests_tax', 'post' );
     93        $term_id1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'Первая метка' ) );
     94        $term_id2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => 'Вторая метка' ) );
     95        $term_id3 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax', 'name' => '25кадр' ) );
     96        wp_set_post_terms( $this->post_id, array( $term_id1, $term_id2, $term_id3 ), 'wptests_tax' );
     97
     98        $found = get_post_class( '', $this->post_id );
     99
     100        $this->assertContains( "wptests_tax-$term_id1", $found );
     101        $this->assertContains( "wptests_tax-$term_id2", $found );
     102        $this->assertContains( "wptests_tax-$term_id3", $found );
     103    }
     104
     105    /**
    57106     * @group cache
    58107     */
Note: See TracChangeset for help on using the changeset viewer.