Make WordPress Core

Changeset 48990


Ignore:
Timestamp:
09/17/2020 07:53:47 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Comments: Assign the array of comment data returned from the comments_pre_query filter to the comments property of the current WP_Comment_Query instance.

This avoids the performance overhead of calling WP_Comment_Query::get_comments() twice: first when creating the object instance, then to retrieve the filtered results.

This also makes the filter a bit more consistent with other similar filters, e.g. posts_pre_query, terms_pre_query, or users_pre_query.

Follow-up to [46086].

Props dinhtungdu, imath, spacedmonkey, adamsilverstein, SergeyBiryukov.
Fixes #50521.

Location:
trunk
Files:
2 edited

Legend:

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

    r48986 r48990  
    395395         * - Otherwise the filter should return an array of WP_Comment objects.
    396396         *
     397         * Note that if the filter returns an array of comment data, it will be assigned
     398         * to the `comments` property of the current WP_Comment_Query instance.
     399         *
     400         * Filtering functions that require pagination information are encouraged to set
     401         * the `found_comments` and `max_num_pages` properties of the WP_Comment_Query object,
     402         * passed to the filter by reference. If WP_Comment_Query does not perform a database
     403         * query, it will not have enough information to generate these values itself.
     404         *
    397405         * @since 5.3.0
     406         * @since 5.6.0 The returned array of comment data is assigned to the `comments` property
     407         *              of the current WP_Comment_Query instance.
    398408         *
    399409         * @param array|int|null   $comment_data Return an array of comment data to short-circuit WP's comment query,
     
    405415
    406416        if ( null !== $comment_data ) {
     417            if ( is_array( $comment_data ) && ! $this->query_vars['count'] ) {
     418                $this->comments = $comment_data;
     419            }
     420
    407421            return $comment_data;
    408422        }
  • trunk/tests/phpunit/tests/comment/query.php

    r48987 r48990  
    49174917        return array( 555 );
    49184918    }
     4919
     4920    /**
     4921     * @ticket 50521
     4922     */
     4923    public function test_comments_pre_query_filter_should_set_comments_property() {
     4924        add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10, 2 );
     4925
     4926        $q       = new WP_Comment_Query();
     4927        $results = $q->query( array() );
     4928
     4929        remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query_and_set_comments' ), 10, 2 );
     4930
     4931        // Make sure the comments property is the same as the results.
     4932        $this->assertSame( $results, $q->comments );
     4933
     4934        // Make sure the comment type is `foobar`.
     4935        $this->assertSame( 'foobar', $q->comments[0]->comment_type );
     4936    }
     4937
     4938    public static function filter_comments_pre_query_and_set_comments( $comments, $query ) {
     4939        $c = self::factory()->comment->create(
     4940            array(
     4941                'comment_type'     => 'foobar',
     4942                'comment_approved' => '1',
     4943            )
     4944        );
     4945
     4946        return array( get_comment( $c ) );
     4947    }
    49194948}
Note: See TracChangeset for help on using the changeset viewer.