From 5f4e2cdba38489fb40bd833f09fd339524da2b93 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 2 Apr 2015 01:05:27 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@31979 git-svn-id: http://core.svn.wordpress.org/trunk@31958 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post-template.php | 36 +++++++++++++++++++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index 22acff563f..64e6e049ea 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -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; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 772aaef60f..b21f9f5d01 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-beta3-31978'; +$wp_version = '4.2-beta3-31979'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.