Make WordPress Core


Ignore:
Timestamp:
09/26/2015 04:01:05 PM (11 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/src/wp-includes/class-wp-comment.php

    r34595 r34599  
    158158         */
    159159        protected $children;
     160
     161        /**
     162         * Post fields.
     163         *
     164         * @since 4.4.0
     165         * @access protected
     166         * @var array
     167         */
     168        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' );
    160169
    161170        /**
    … …  
    323332
    324333        /**
    325          * Whether a comment has post from which to retrieve magic properties
    326          *
    327          * @since 4.4.0
    328          * @access public
    329          *
    330          * @param string $name
     334         * Check whether a non-public property is set.
     335         *
     336         * If `$name` matches a post field, the comment post will be loaded and the post's value checked.
     337         *
     338         * @since 4.4.0
     339         * @access public
     340         *
     341         * @param string $name Property name.
    331342         * @return bool
    332343         */
    333344        public function __isset( $name ) {
    334                 if (
    335                         0 === (int) $this->comment_post_ID
    336                         || property_exists( $this, $name )
    337                 ) {
    338                         return;
    339                 }
    340 
    341                 $post = get_post( $this->comment_post_ID );
    342                 if ( $post ) {
     345                if ( in_array( $name, $this->post_fields ) && 0 !== (int) $this->comment_post_ID ) {
     346                        $post = get_post( $this->comment_post_ID );
    343347                        return property_exists( $post, $name );
    344348                }
    … …  
    346350
    347351        /**
    348          * Magic getter for $post properties
     352         * Magic getter.
     353         *
     354         * If `$name` matches a post field, the comment post will be loaded and the post's value returned.
    349355         *
    350356         * @since 4.4.0
    … …  
    355361         */
    356362        public function __get( $name ) {
    357                 $post = get_post( $this->comment_post_ID );
    358                 if ( $post ) {
     363                if ( in_array( $name, $this->post_fields ) ) {
     364                        $post = get_post( $this->comment_post_ID );
    359365                        return $post->$name;
    360366                }
Note: See TracChangeset for help on using the changeset viewer.