Improve category check in `redirect_canonical()` when permastruct contains category slug.

[37262] changed a check in `redirect_canonical()` so that it checked
categories in the object cache rather than querying the database. However,
the check was based on the identity of `WP_Term` objects, which in
certain cases can be augmented by the main WP query routine, causing
failures of the `in_array()` check. This caused unnecessary redirects
for URLs where `is_single()` is true, but the URL is different from the
post permalink, such as the `embed` endpoint.

`has_term()` also checks the cache, but does not sufer from this bug.

Merges [38216] to the 4.6 branch.

Props cmillerdev.
Fixes #36602.

Built from https://develop.svn.wordpress.org/branches/4.6@38220


git-svn-id: http://core.svn.wordpress.org/branches/4.6@38161 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2016-08-09 00:13:31 +00:00
parent dcf9711226
commit 14fa6a89ca
2 changed files with 2 additions and 3 deletions

View File

@ -256,8 +256,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
}
} elseif ( is_single() && strpos($wp_rewrite->permalink_structure, '%category%') !== false && $cat = get_query_var( 'category_name' ) ) {
$category = get_category_by_path( $cat );
$post_terms = get_the_terms( $wp_query->get_queried_object_id(), 'category' );
if ( ( ! $category || is_wp_error( $category ) ) || ( ! is_wp_error( $post_terms ) && ! empty( $post_terms ) && ! in_array( $category, $post_terms ) ) ) {
if ( ( ! $category || is_wp_error( $category ) ) || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() ) ) {
$redirect_url = get_permalink($wp_query->get_queried_object_id());
}
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-RC1-38219';
$wp_version = '4.6-RC1-38220';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.