Make WordPress Core

Changeset 54634


Ignore:
Timestamp:
10/18/2022 03:06:48 PM (3 years ago)
Author:
spacedmonkey
Message:

Query: Remove placeholder from query cache key.

Remove escape placeholder from query cache key, as placeholders are a based on a unique id on every request. This means that it is impossible for a cache to be reused, making queries that use escape placeholders such as LIKE searches, unable to be cached.

Props dhl, spacedmonkey, peterwilsoncc, desrosj, chaion07, davidbaumwald, mukesh27.
Fixes #56802.

Location:
trunk
Files:
2 edited

Legend:

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

    r54524 r54634  
    31163116                $cache_args['update_post_term_cache'],
    31173117                $cache_args['lazy_load_term_meta'],
    3118                 $cache_args['update_menu_item_cache']
     3118                $cache_args['update_menu_item_cache'],
     3119                $cache_args['search_orderby_title']
    31193120            );
    31203121
    31213122            $new_request = str_replace( $fields, "{$wpdb->posts}.*", $this->request );
     3123            $new_request = $wpdb->remove_placeholder_escape( $new_request );
    31223124            $key         = md5( serialize( $cache_args ) . $new_request );
    31233125
     
    31273129            }
    31283130
    3129             $cache_key   = "wp_query:$key:$last_changed";
     3131            $cache_key = "wp_query:$key:$last_changed";
     3132
     3133            /**
     3134             * Filters query cache key.
     3135             *
     3136             * @since 6.1.0
     3137             *
     3138             * @param string   $cache_key   Cache key.
     3139             * @param array    $cache_args  Query args used to generate the cache key.
     3140             * @param string   $new_request SQL Query.
     3141             * @param WP_Query $query       The WP_Query instance.
     3142             */
     3143            $cache_key = apply_filters( 'wp_query_cache_key', $cache_key, $cache_args, $new_request, $this );
     3144
    31303145            $cache_found = false;
    31313146            if ( null === $this->posts ) {
  • trunk/tests/phpunit/tests/query/cacheResults.php

    r54352 r54634  
    3333     */
    3434    public static $author_id;
     35
     36    /**
     37     * @var array
     38     */
     39    protected $cache_args;
     40
     41    /**
     42     * @var string
     43     */
     44    protected $new_request;
    3545
    3646    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    5868    }
    5969
     70    function set_up() {
     71        parent::set_up();
     72        $this->cache_args  = null;
     73        $this->new_request = null;
     74        add_filter( 'wp_query_cache_key', array( $this, 'filter_wp_query_cache_key' ), 15, 3 );
     75    }
     76
    6077    /**
    6178     * @dataProvider data_query_cache
     
    6380     */
    6481    public function test_query_cache( $args ) {
    65         $query1 = new WP_Query();
    66         $posts1 = $query1->query( $args );
     82        global $wpdb;
     83
     84        $query1 = new WP_Query();
     85        $posts1 = $query1->query( $args );
     86
     87        $placeholder = $wpdb->placeholder_escape();
     88        $this->assertNotEmpty( $this->new_request, 'Check new request is not empty' );
     89        $this->assertStringNotContainsString( $placeholder, $this->new_request, 'Check if request does not contain placeholder' );
     90        $this->assertStringNotContainsString( $placeholder, wp_json_encode( $this->cache_args ), 'Check if cache arrays does not contain placeholder' );
    6791
    6892        $queries_before = get_num_queries();
     
    192216                ),
    193217            ),
     218            'cache meta query search'                     => array(
     219                'args' => array(
     220                    'cache_results' => true,
     221                    'meta_query'    => array(
     222                        array(
     223                            'key'     => 'color',
     224                            'value'   => '00',
     225                            'compare' => 'LIKE',
     226                        ),
     227                    ),
     228                ),
     229            ),
     230            'cache meta query not search'                 => array(
     231                'args' => array(
     232                    'cache_results' => true,
     233                    'meta_query'    => array(
     234                        array(
     235                            'key'     => 'color',
     236                            'value'   => 'ff',
     237                            'compare' => 'NOT LIKE',
     238                        ),
     239                    ),
     240                ),
     241            ),
    194242            'cache comment_count'                         => array(
    195243                'args' => array(
     
    208256                        ),
    209257                    ),
     258                ),
     259            ),
     260            'cache search query'                          => array(
     261                'args' => array(
     262                    'cache_results' => true,
     263                    's'             => 'title',
     264                ),
     265            ),
     266            'cache search query multiple terms'           => array(
     267                'args' => array(
     268                    'cache_results' => true,
     269                    's'             => 'Post title',
    210270                ),
    211271            ),
     
    828888    }
    829889
     890    public function filter_wp_query_cache_key( $cache_key, $cache_args, $new_request ) {
     891        $this->cache_args  = $cache_args;
     892        $this->new_request = $new_request;
     893
     894        return $cache_key;
     895    }
     896
    830897    /**
    831898     * @ticket 22176
Note: See TracChangeset for help on using the changeset viewer.