Make WordPress Core

Changeset 38446


Ignore:
Timestamp:
08/30/2016 02:48:00 PM (10 years ago)
Author:
boonebgorges
Message:

Comments: Don't do direct SQL query when fetching decendants.

The SQL query was built using the clauses compiled when querying for
top-level comments. But in cases where the top-level comment query
results are already in the cache, the SQL clauses are not built, and
so are unavailable for fill_descendants(). Instead, we call
get_comments(), using modified versions of the parameters passed
to the main WP_Comment_Query class.

Props Akeif, Rarst for testing.
Fixes #37696.

Location:
trunk
Files:
2 edited

Legend:

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

    r38336 r38446  
    991991
    992992                        if ( $uncached_parent_ids ) {
    993                                 $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
    994                                 $level_comments = $this->db->get_results( "SELECT {$this->db->comments}.comment_ID, {$this->db->comments}.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
     993                                // Fetch this level of comments.
     994                                $parent_query_args = $this->query_vars;
     995                                foreach ( $exclude_keys as $exclude_key ) {
     996                                        $parent_query_args[ $exclude_key ] = '';
     997                                }
     998                                $parent_query_args['parent__in']    = $uncached_parent_ids;
     999                                $parent_query_args['no_found_rows'] = true;
     1000                                $parent_query_args['hierarchical']  = false;
     1001
     1002                                $level_comments = get_comments( $parent_query_args );
    9951003
    9961004                                // Cache parent-child relationships.
  • trunk/tests/phpunit/tests/comment/query.php

    r38398 r38446  
    24962496
    24972497        /**
     2498         * @ticket 37696
     2499         */
     2500        public function test_hierarchy_should_be_filled_when_cache_is_incomplete() {
     2501                global $wpdb;
     2502
     2503                $p = self::factory()->post->create();
     2504                $comment_1 = self::factory()->comment->create( array(
     2505                        'comment_post_ID' => $p,
     2506                        'comment_approved' => '1',
     2507                ) );
     2508                $comment_2 = self::factory()->comment->create( array(
     2509                        'comment_post_ID' => $p,
     2510                        'comment_approved' => '1',
     2511                        'comment_parent' => $comment_1,
     2512                ) );
     2513                $comment_3 = self::factory()->comment->create( array(
     2514                        'comment_post_ID' => $p,
     2515                        'comment_approved' => '1',
     2516                        'comment_parent' => $comment_1,
     2517                ) );
     2518                $comment_4 = self::factory()->comment->create( array(
     2519                        'comment_post_ID' => $p,
     2520                        'comment_approved' => '1',
     2521                        'comment_parent' => $comment_2,
     2522                ) );
     2523
     2524                // Prime cache.
     2525                $q1 = new WP_Comment_Query( array(
     2526                        'post_id' => $p,
     2527                        'hierarchical' => true,
     2528                ) );
     2529                $q1_ids = wp_list_pluck( $q1->comments, 'comment_ID' );
     2530                $this->assertEqualSets( array( $comment_1, $comment_2, $comment_3, $comment_4 ), $q1_ids );
     2531
     2532                // Delete one of the parent caches.
     2533                $last_changed = wp_cache_get( 'last_changed', 'comment' );
     2534                $key = md5( serialize( wp_array_slice_assoc( $q1->query_vars, array_keys( $q1->query_var_defaults ) ) ) );
     2535                $cache_key = "get_comment_child_ids:$comment_2:$key:$last_changed";
     2536                wp_cache_delete( $cache_key, 'comment' );
     2537
     2538                $q2 = new WP_Comment_Query( array(
     2539                        'post_id' => $p,
     2540                        'hierarchical' => true,
     2541                ) );
     2542                $q2_ids = wp_list_pluck( $q2->comments, 'comment_ID' );
     2543                $this->assertEqualSets( $q1_ids, $q2_ids );
     2544        }
     2545
     2546        /**
    24982547         * @ticket 27571
    24992548         */
Note: See TracChangeset for help on using the changeset viewer.