From 0bbd0f3a5f756076787a00a4aa00123fcaaffc4f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 8 Aug 2016 18:48:53 +0000 Subject: [PATCH] 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. Props cmillerdev. Fixes #36602. git-svn-id: https://develop.svn.wordpress.org/trunk@38216 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/canonical.php | 3 +-- tests/phpunit/tests/canonical/category.php | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 387b9f2383..11c513d31a 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -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()); } } diff --git a/tests/phpunit/tests/canonical/category.php b/tests/phpunit/tests/canonical/category.php index 888002f6f2..36a5adfb58 100644 --- a/tests/phpunit/tests/canonical/category.php +++ b/tests/phpunit/tests/canonical/category.php @@ -62,6 +62,9 @@ class Tests_Canonical_Category extends WP_Canonical_UnitTestCase { // Nonexistent category will redirect to correct one. array( '/foo/post0/', array( 'url' => '/cat0/post0/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'page' => '' ) ) ), + + // Embed URLs should not redirect to post permalinks. + array( '/cat0/post0/embed/', array( 'url' => '/cat0/post0/embed/', 'qv' => array( 'category_name' => 'cat0', 'name' => 'post0', 'embed' => 'true' ) ) ), ); } }