Make WordPress Core

Changeset 53169


Ignore:
Timestamp:
04/13/2022 02:55:34 AM (2 years ago)
Author:
peterwilsoncc
Message:

Cache API: Improve cache key generation in WP_Comment_Query.

Discard the parameters update_comment_meta_cache and update_comment_post_cache when generating the cache key as neither have an effect on the database query generated.

Props uday17035, spacedmonkey.
Fixes #55460.

Location:
trunk
Files:
2 edited

Legend:

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

    r52977 r53169  
    445445        /*
    446446         * Only use the args defined in the query_var_defaults to compute the key,
    447          * but ignore 'fields', which does not affect query results.
     447         * but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect query results.
    448448         */
    449449        $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
    450         unset( $_args['fields'] );
     450        unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
    451451
    452452        $key          = md5( serialize( $_args ) );
  • trunk/tests/phpunit/tests/comment/query.php

    r52010 r53169  
    49424942        return array( get_comment( $c ) );
    49434943    }
     4944
     4945    /**
     4946     * @ticket 55460
     4947     */
     4948    public function test_comment_cache_key_should_ignore_unset_params() {
     4949        $p = self::factory()->post->create();
     4950        $c = self::factory()->comment->create( array( 'comment_post_ID' => $p ) );
     4951
     4952        $_args = array(
     4953            'post_id'                   => $p,
     4954            'fields'                    => 'ids',
     4955            'update_comment_meta_cache' => true,
     4956            'update_comment_post_cache' => false,
     4957        );
     4958
     4959        $q1 = new WP_Comment_Query();
     4960        $q1->query( $_args );
     4961
     4962        $num_queries_all_args = get_num_queries();
     4963
     4964        // Ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache'.
     4965        unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
     4966        $q2 = new WP_Comment_Query();
     4967        $q2->query( $_args );
     4968
     4969        $this->assertSame( $num_queries_all_args, get_num_queries() );
     4970    }
    49444971}
Note: See TracChangeset for help on using the changeset viewer.