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.

git-svn-id: https://develop.svn.wordpress.org/trunk@34599 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-09-26 16:01:05 +00:00
parent 5da80b3d64
commit eb8b241559
4 changed files with 90 additions and 15 deletions

View File

@@ -158,6 +158,15 @@ final class WP_Comment {
*/
protected $children;
/**
* Post fields.
*
* @since 4.4.0
* @access protected
* @var array
*/
protected $post_fields = array( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_content_filtered', 'post_parent', 'guid', 'menu_order', 'post_type', 'post_mime_type', 'comment_count' );
/**
* Retrieves a WP_Comment instance.
*
@@ -322,30 +331,27 @@ final class WP_Comment {
}
/**
* Whether a comment has post from which to retrieve magic properties
* Check whether a non-public property is set.
*
* If `$name` matches a post field, the comment post will be loaded and the post's value checked.
*
* @since 4.4.0
* @access public
*
* @param string $name
* @param string $name Property name.
* @return bool
*/
public function __isset( $name ) {
if (
0 === (int) $this->comment_post_ID
|| property_exists( $this, $name )
) {
return;
}
$post = get_post( $this->comment_post_ID );
if ( $post ) {
if ( in_array( $name, $this->post_fields ) && 0 !== (int) $this->comment_post_ID ) {
$post = get_post( $this->comment_post_ID );
return property_exists( $post, $name );
}
}
/**
* Magic getter for $post properties
* Magic getter.
*
* If `$name` matches a post field, the comment post will be loaded and the post's value returned.
*
* @since 4.4.0
* @access public
@@ -354,8 +360,8 @@ final class WP_Comment {
* @return mixed
*/
public function __get( $name ) {
$post = get_post( $this->comment_post_ID );
if ( $post ) {
if ( in_array( $name, $this->post_fields ) ) {
$post = get_post( $this->comment_post_ID );
return $post->$name;
}
}