Make WordPress Core

Changeset 54785


Ignore:
Timestamp:
11/10/2022 02:59:56 AM (16 months ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r53043 r54785  
    332332
    333333            foreach ( $wp_query->tax_query->queried_terms as $tax_query ) {
    334                 $term_count += count( $tax_query['terms'] );
     334                if ( isset( $tax_query['terms'] ) && is_countable( $tax_query['terms'] ) ) {
     335                    $term_count += count( $tax_query['terms'] );
     336                }
    335337            }
    336338
  • trunk/tests/phpunit/tests/canonical.php

    r53043 r54785  
    376376        delete_option( 'page_on_front' );
    377377    }
     378
     379    /**
     380     * Ensure NOT EXISTS queries do not trigger not-countable or undefined array key errors.
     381     *
     382     * @ticket 55955
     383     */
     384    public function test_feed_canonical_with_not_exists_query() {
     385        // Set a NOT EXISTS tax_query on the global query.
     386        $global_query        = $GLOBALS['wp_query'];
     387        $GLOBALS['wp_query'] = new WP_Query(
     388            array(
     389                'post_type' => 'post',
     390                'tax_query' => array(
     391                    array(
     392                        'taxonomy' => 'post_format',
     393                        'operator' => 'NOT EXISTS',
     394                    ),
     395                ),
     396            )
     397        );
     398
     399        $url = redirect_canonical( get_term_feed_link( self::$terms['/category/parent/'] ), false );
     400        // Restore original global.
     401        $GLOBALS['wp_query'] = $global_query;
     402
     403        $this->assertNull( $url );
     404    }
    378405}
Note: See TracChangeset for help on using the changeset viewer.