From 9f36a943b5eae30e97d92df5b8e2dd783da4af18 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 26 Sep 2015 16:02:25 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@34599 git-svn-id: http://core.svn.wordpress.org/trunk@34563 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-comment-query.php | 15 +++++++++++- wp-includes/class-wp-comment.php | 34 +++++++++++++++----------- wp-includes/version.php | 2 +- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index 4050e48182..ab49452ed8 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -138,7 +138,7 @@ class WP_Comment_Query { * @since 4.2.0 * @since 4.4.0 `$parent__in` and `$parent__not_in` were added. * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`, - * and `$hierarchical` were added. + * `$hierarchical`, and `$update_comment_post_cache` were added. * @access public * * @param string|array $query { @@ -238,6 +238,8 @@ class WP_Comment_Query { * 'flat', or false. Default: false. * @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments. * Default true. + * @type bool $update_comment_post_cache Whether to prime the cache for comment posts. + * Default false. * } */ public function __construct( $query = '' ) { @@ -281,6 +283,7 @@ class WP_Comment_Query { 'date_query' => null, // See WP_Date_Query 'hierarchical' => false, 'update_comment_meta_cache' => true, + 'update_comment_post_cache' => false, ); if ( ! empty( $query ) ) { @@ -414,6 +417,16 @@ class WP_Comment_Query { } } + // Prime comment post caches. + if ( $this->query_vars['update_comment_post_cache'] ) { + $comment_post_ids = array(); + foreach ( $_comments as $_comment ) { + $comment_post_ids[] = $_comment->comment_post_ID; + } + + _prime_post_caches( $comment_post_ids, false, false ); + } + /** * Filter the comment query results. * diff --git a/wp-includes/class-wp-comment.php b/wp-includes/class-wp-comment.php index c141c6ecd9..8dc9e9413b 100644 --- a/wp-includes/class-wp-comment.php +++ b/wp-includes/class-wp-comment.php @@ -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; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 3a05523705..624d4092ec 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34598'; +$wp_version = '4.4-alpha-34599'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.