Make WordPress Core

Changeset 41190


Ignore:
Timestamp:
07/30/2017 02:55:17 PM (9 years ago)
Author:
boonebgorges
Message:

Comments: Ignore the 'fields' parameter in the comment query cache.

WP_Comment_Query always queries runs an ID query, and so is unaffected
by the 'fields' parameter. As such, 'fields' can be ignored when building
a cache key for the results of the ID query.

Props spacedmonkey.
Fixes #41348.

Location:
trunk
Files:
2 edited

Legend:

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

    r41162 r41190  
    376376                }
    377377
    378                 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    379                 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
     378                /*
     379                 * Only use the args defined in the query_var_defaults to compute the key,
     380                 * but ignore 'fields', which does not affect query results.
     381                 */
     382                $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
     383                unset( $_args['fields'] );
     384
     385                $key = md5( serialize( $_args ) );
    380386                $last_changed = wp_cache_get_last_changed( 'comment' );
    381 
    382387
    383388                $cache_key   = "get_comments:$key:$last_changed";
  • trunk/tests/phpunit/tests/comment/query.php

    r41188 r41190  
    28782878                $this->assertEqualSets( array( $c ), $q->comments );
    28792879        }
     2880
     2881        /**
     2882         * @ticket 41348
     2883         */
     2884        public function test_count_query_should_miss_noncount_cache() {
     2885                global $wpdb;
     2886
     2887                $q = new WP_Comment_Query();
     2888
     2889                $query_1 = $q->query( array(
     2890                        'fields' => 'ids',
     2891                        'number' => 3,
     2892                        'order' => 'ASC',
     2893                ) );
     2894
     2895                $number_of_queries = $wpdb->num_queries;
     2896
     2897                $query_2 = $q->query( array(
     2898                        'fields' => 'ids',
     2899                        'number' => 3,
     2900                        'order' => 'ASC',
     2901                        'count' => true,
     2902                ) );
     2903                $this->assertEquals( $number_of_queries + 1, $wpdb->num_queries );
     2904        }
     2905
     2906        /**
     2907         * @ticket 41348
     2908         */
     2909        public function test_count_query_should_hit_count_cache() {
     2910                global $wpdb;
     2911
     2912                $q = new WP_Comment_Query();
     2913
     2914                $query_1 = $q->query( array(
     2915                        'fields' => 'ids',
     2916                        'number' => 3,
     2917                        'order' => 'ASC',
     2918                        'count' => true,
     2919                ) );
     2920                $number_of_queries = $wpdb->num_queries;
     2921
     2922                $query_2 = $q->query( array(
     2923                        'fields' => 'ids',
     2924                        'number' => 3,
     2925                        'order' => 'ASC',
     2926                        'count' => true,
     2927                ) );
     2928                $this->assertEquals( $number_of_queries, $wpdb->num_queries );
     2929        }
     2930
     2931        /**
     2932         * @ticket 41348
     2933         */
     2934        public function test_different_values_of_fields_should_share_cached_values() {
     2935                global $wpdb;
     2936
     2937                $q = new WP_Comment_Query();
     2938
     2939                $query_1 = $q->query( array(
     2940                        'fields' => 'all',
     2941                        'number' => 3,
     2942                        'order' => 'ASC',
     2943                ) );
     2944                $number_of_queries = $wpdb->num_queries;
     2945
     2946                $query_2 = $q->query( array(
     2947                        'fields' => 'ids',
     2948                        'number' => 3,
     2949                        'order' => 'ASC',
     2950                ) );
     2951
     2952                $this->assertEquals( $number_of_queries, $wpdb->num_queries );
     2953        }
    28802954}
Note: See TracChangeset for help on using the changeset viewer.