Make WordPress Core


Ignore:
Timestamp:
01/18/2023 09:56:55 AM (3 years ago)
Author:
spacedmonkey
Message:

Taxonomy: Remove placeholder from WP_Term_Query cache key.

Remove escape placeholder from query cache key, as placeholders are on a based on a unique id on every request. This meant that it is impossible for a cache to be reused, making queries that use escape placeholders such as searches, meta queries or using the description__like / name__like parameters were unable to be cached.

Follow on from [54634].

Props spacedmonkey, peterwilsoncc.
Fixes #57298.

File:
1 edited

Legend:

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

    r54867 r55083  
    775775        }
    776776
    777         // $args can be anything. Only use the args defined in defaults to compute the key.
    778         $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
    779 
    780         unset( $cache_args['update_term_meta_cache'] );
    781 
    782         if ( 'count' !== $_fields && 'all_with_object_id' !== $_fields ) {
    783             $cache_args['fields'] = 'all';
    784         }
    785 
    786         $key          = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $this->request );
    787         $last_changed = wp_cache_get_last_changed( 'terms' );
    788         $cache_key    = "get_terms:$key:$last_changed";
    789         $cache        = wp_cache_get( $cache_key, 'terms' );
     777        $cache_key = $this->generate_cache_key( $args, $this->request );
     778        $cache     = wp_cache_get( $cache_key, 'terms' );
    790779
    791780        if ( false !== $cache ) {
     
    11431132        return $term_objects;
    11441133    }
     1134
     1135    /**
     1136     * Generate cache key.
     1137     *
     1138     * @since 6.2.0
     1139     *
     1140     * @global wpdb $wpdb WordPress database abstraction object.
     1141     *
     1142     * @param array  $args WP_Term_Query arguments.
     1143     * @param string $sql  SQL statement.
     1144     *
     1145     * @return string Cache key.
     1146     */
     1147    protected function generate_cache_key( array $args, $sql ) {
     1148        global $wpdb;
     1149        // $args can be anything. Only use the args defined in defaults to compute the key.
     1150        $cache_args = wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) );
     1151
     1152        unset( $cache_args['update_term_meta_cache'] );
     1153
     1154        if ( 'count' !== $args['fields'] && 'all_with_object_id' !== $args['fields'] ) {
     1155            $cache_args['fields'] = 'all';
     1156        }
     1157        $taxonomies = (array) $args['taxonomy'];
     1158
     1159        // Replace wpdb placeholder in the SQL statement used by the cache key.
     1160        $sql = $wpdb->remove_placeholder_escape( $sql );
     1161
     1162        $key          = md5( serialize( $cache_args ) . serialize( $taxonomies ) . $sql );
     1163        $last_changed = wp_cache_get_last_changed( 'terms' );
     1164        return "get_terms:$key:$last_changed";
     1165    }
    11451166}
Note: See TracChangeset for help on using the changeset viewer.