Make WordPress Core


Ignore:
Timestamp:
09/10/2019 06:41:03 PM (6 years ago)
Author:
adamsilverstein
Message:

Comments: add a new comments_pre_query filter to short circuit WP_Comment_Query 'get_comments' queries.

Return a non-null value to bypass WordPress's default comment queries.

Props felipeelia, spacedmonkey.
Fixes #45800.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/comment/query.php

    r45424 r46086  
    48834883        $this->assertEqualSets( $c1, $found );
    48844884    }
     4885
     4886    /**
     4887     * @ticket 45800
     4888     */
     4889    public function test_comments_pre_query_filter_should_bypass_database_query() {
     4890        global $wpdb;
     4891
     4892        add_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
     4893
     4894        $num_queries = $wpdb->num_queries;
     4895
     4896        $q       = new WP_Comment_Query();
     4897        $results = $q->query( array() );
     4898
     4899        remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
     4900
     4901        // Make sure no queries were executed.
     4902        $this->assertSame( $num_queries, $wpdb->num_queries );
     4903
     4904        // We manually inserted a non-existing site and overrode the results with it.
     4905        $this->assertSame( array( 555 ), $results );
     4906
     4907        // Make sure manually setting total_users doesn't get overwritten.
     4908        $this->assertEquals( 1, $q->found_comments );
     4909    }
     4910
     4911    public static function filter_comments_pre_query( $comments, $query ) {
     4912        $query->found_comments = 1;
     4913
     4914        return array( 555 );
     4915    }
    48854916}
Note: See TracChangeset for help on using the changeset viewer.