WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/02/2015 01:04:22 AM (6 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.

File:
1 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            }
Note: See TracChangeset for help on using the changeset viewer.