Make WordPress Core

Changeset 64300


Ignore:
Timestamp:
10/11/2026 09:19:47 PM (4 weeks ago)
Author:
peterwilsoncc
Message:

Canonical: Generate stable cache for redirect_guess_404_permalink().

Run $wpdb->remove_placeholder_escape() on the prepared database query when generating the cache key.

The WPDB placeholder varies each time WordPress is executed so without replacement the cache key is unstable resulting in an unreachable cache entry.

Follow up to r64043.
Developed in ​https://github.com/WordPress/wordpress-develop/pull/14146

Props maheshbohara, therssoftware.
Fixes #66282.
See #64250.

Location:
trunk
Files:
2 edited

Legend:

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

    r64236 r64300  
    10061006                $query = "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')";
    10071007
    1008                 $key          = md5( $query );
     1008                // Remove the per-request placeholder escape so the cache key is stable across requests.
     1009                $key          = md5( $wpdb->remove_placeholder_escape( $query ) );
    10091010                $last_changed = wp_cache_get_last_changed( 'posts' );
    10101011                $cache_key    = "redirect_guess_404_permalink:$key";
  • trunk/tests/phpunit/tests/canonical.php

    r64043 r64300  
    441441
    442442        /**
     443         * Ensures the cache key does not contain the per-request `wpdb` placeholder escape.
     444         *
     445         * The placeholder escape string changes on every request, so a cache key derived from
     446         * the escaped query could never be hit in a subsequent request.
     447         *
     448         * @ticket 66282
     449         * @ticket 64250
     450         *
     451         * @covers ::redirect_guess_404_permalink
     452         */
     453        public function test_redirect_guess_404_permalink_cache_key_excludes_placeholder_escape() {
     454                $post = self::factory()->post->create(
     455                        array(
     456                                'post_title' => 'redirect-guess-404-permalink-cache-key',
     457                        )
     458                );
     459
     460                $this->go_to( 'redirect-guess-404-permalink-cache-ke' );
     461
     462                // The `query` filter receives the SQL after the placeholder escape has been removed.
     463                $queries = array();
     464                $filter  = static function ( $query ) use ( &$queries ) {
     465                        if ( str_contains( $query, 'post_name LIKE' ) ) {
     466                                $queries[] = $query;
     467                        }
     468                        return $query;
     469                };
     470
     471                add_filter( 'query', $filter );
     472                $guess = redirect_guess_404_permalink();
     473                remove_filter( 'query', $filter );
     474
     475                $this->assertSame( get_permalink( $post ), $guess, 'Did not guess the correct permalink.' );
     476                $this->assertCount( 1, $queries, 'Expected exactly one loose-match query to be run.' );
     477
     478                $cache = wp_cache_get_salted(
     479                        'redirect_guess_404_permalink:' . md5( $queries[0] ),
     480                        'post-queries',
     481                        wp_cache_get_last_changed( 'posts' )
     482                );
     483
     484                $this->assertSame( $post, $cache, 'The cache key should be generated from the query without the placeholder escape.' );
     485        }
     486
     487        /**
    443488         * @ticket 64250
    444489         *
Note: See TracChangeset for help on using the changeset viewer.