mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 21:00:21 +00:00
Canonical: Protect against error for term not exists queries.
Prevent term `NOT EXISTS` queries causing `redirect_canonical()` to throw a fatal error in PHP 8 and above, or a warning in earlier versions. This ensures the `tax_query`'s `terms` property both exists and is countable before attempting to count it. Props codesdnc, SergeyBiryukov, kadamwhite, costdev, miguelaxcar. Fixes #55955. git-svn-id: https://develop.svn.wordpress.org/trunk@54785 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -331,7 +331,9 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
|
||||
$term_count = 0;
|
||||
|
||||
foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
|
||||
$term_count += count( $tax_query['terms'] );
|
||||
if ( isset( $tax_query['terms'] ) && is_countable( $tax_query['terms'] ) ) {
|
||||
$term_count += count( $tax_query['terms'] );
|
||||
}
|
||||
}
|
||||
|
||||
$obj = $wp_query->get_queried_object();
|
||||
|
||||
@@ -375,4 +375,31 @@ class Tests_Canonical extends WP_Canonical_UnitTestCase {
|
||||
|
||||
delete_option( 'page_on_front' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure NOT EXISTS queries do not trigger not-countable or undefined array key errors.
|
||||
*
|
||||
* @ticket 55955
|
||||
*/
|
||||
public function test_feed_canonical_with_not_exists_query() {
|
||||
// Set a NOT EXISTS tax_query on the global query.
|
||||
$global_query = $GLOBALS['wp_query'];
|
||||
$GLOBALS['wp_query'] = new WP_Query(
|
||||
array(
|
||||
'post_type' => 'post',
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'post_format',
|
||||
'operator' => 'NOT EXISTS',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$url = redirect_canonical( get_term_feed_link( self::$terms['/category/parent/'] ), false );
|
||||
// Restore original global.
|
||||
$GLOBALS['wp_query'] = $global_query;
|
||||
|
||||
$this->assertNull( $url );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user