WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 8 weeks ago

#45800 assigned enhancement

Add short circuits for WP_Comment_Query

Reported by: spacedmonkey Owned by: adamsilverstein
Milestone: 5.3 Priority: normal
Severity: normal Version: 3.1
Component: Comments Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Now that WP_Query and WP_User_Query have filters to short circuit results in from a different source. WP_Comment_Query should have some ability to do the same.

Attachments (1)

45800.diff (6.8 KB) - added by felipeelia 8 weeks ago.

Download all attachments as: .zip

Change History (9)

#2 @SergeyBiryukov
4 months ago

  • Component changed from General to Comments

#3 @desrosj
4 months ago

  • Milestone changed from Awaiting Review to Future Release

#4 @adamsilverstein
2 months ago

  • Milestone changed from Future Release to 5.2
  • Owner set to adamsilverstein
  • Status changed from new to assigned

#5 @desrosj
2 months ago

  • Milestone changed from 5.2 to 5.3

This still needs a patch. 5.2 beta is in less than 2 days. Going to punt, but feel free to move it back @adamsilverstein if a patch is created and you have time to shepherd.

#6 @felipeelia
8 weeks ago

Probably not soon enough to 5.2, but I'll try to submit a patch for this during the next few days.

@felipeelia
8 weeks ago

#7 @adamsilverstein
8 weeks ago

Thanks for the patch @felipeelia! I'll take a look at this soon.

#8 @felipeelia
8 weeks ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

Just uploaded a patch with the necessary changes and a new unit test. FWIW, here is a callback that could be hooked to the new comments_pre_query filter and serve as an example:

<?php
function wp45800_comments_pre_query( $comments, $query ) {
        // Necessary to avoid an infinite loop for hierarchical queries.
        if ( ! empty( $query->query_vars['parent'] ) ) {
                return [];
        }
        $comments = [
                (object) [
                        'comment_ID'      => 100,
                        'comment_content' => 'Lorem Ipsum',
                ],
                (object) [
                        'comment_ID'      => 200,
                        'comment_content' => 'Ipsum lorem',
                ],
        ];
        if ( $query->query_vars['count'] ) {
                return count( $comments );
        }
        if ( 'ids' == $query->query_vars['fields'] ) {
                return wp_list_pluck( $comments, 'comment_ID' );
        }
        return $comments;
}
add_filter( 'comments_pre_query', 'wp45800_comments_pre_query', 10, 2 );

Thank you, @adamsilverstein. Let me know if any change is needed :)

Note: See TracTickets for help on using tickets.