Make WordPress Core


Ignore:
Timestamp:
09/26/2015 04:01:05 PM (9 years ago)
Author:
boonebgorges
Message:

Improve post field lazyloading for comments.

[34583] modified comment queries so that all post fields are no longer loaded
by default. Instead, they are loaded only when requested on individual comment
objects. This changeset improves that flow:

  • WP_Comment magic methods __isset() and __get() should only load the post when a post field is being requested.
  • The new update_comment_post_cache argument for WP_Comment_Query allows developers to specify that, when comments are queried, all of the posts matching those comments should be loaded into cache with a single DB hit. This parameter defaults to false, since typical comment queries are linked to a single post.

Fixes #27571.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/query.php

    r34569 r34599  
    21092109        $this->assertEqualSets( array(), array_values( wp_list_pluck( $q->comments[ $c1 ]->get_child( $c2 )->get_children( $args ), 'comment_ID' ) ) );
    21102110    }
     2111
     2112    /**
     2113     * @ticket 27571
     2114     */
     2115    public function test_update_comment_post_cache_should_be_disabled_by_default() {
     2116        global $wpdb;
     2117
     2118        $p = $this->factory->post->create();
     2119        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     2120
     2121        $q = new WP_Comment_Query( array(
     2122            'post_ID' => $p,
     2123        ) );
     2124
     2125        $num_queries = $wpdb->num_queries;
     2126        $this->assertTrue( isset( $q->comments[0]->post_name ) );
     2127        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     2128    }
     2129
     2130    /**
     2131     * @ticket 27571
     2132     */
     2133    public function test_should_respect_update_comment_post_cache_true() {
     2134        global $wpdb;
     2135
     2136        $p = $this->factory->post->create();
     2137        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     2138
     2139        $q = new WP_Comment_Query( array(
     2140            'post_ID' => $p,
     2141            'update_comment_post_cache' => true,
     2142        ) );
     2143
     2144        $num_queries = $wpdb->num_queries;
     2145        $this->assertTrue( isset( $q->comments[0]->post_name ) );
     2146        $this->assertSame( $num_queries, $wpdb->num_queries );
     2147    }
    21112148}
Note: See TracChangeset for help on using the changeset viewer.