Make WordPress Core

Ticket #45800: 45800.3.diff

File 45800.3.diff, 2.5 KB (added by spacedmonkey, 6 years ago)
  • src/wp-includes/class-wp-comment-query.php

     
    379379                        $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
    380380                }
    381381
     382                $comment_ids = null;
     383
     384                /**
     385                 * Filter the comments array before the query takes place.
     386                 *
     387                 * Return a non-null value to bypass WordPress's default comment queries.
     388                 *
     389                 *
     390                 * @since 5.3.0
     391                 *
     392                 * @param array|null    $comment_ids Return an array of comment data to short-circuit WP's comment query,
     393                 *                                or null to allow WP to run its normal queries.
     394                 * @param WP_Comment_Query $this The WP_Comment_Query instance, passed by reference.
     395                 */
     396                $comment_ids = apply_filters_ref_array( 'comments_pre_query', array( $comment_ids, &$this ) );
     397
     398                if ( null !== $comment_ids ) {
     399                        $this->comments = $comment_ids;
     400                        return $this->comments;
     401                }
     402
    382403                /*
    383404                 * Only use the args defined in the query_var_defaults to compute the key,
    384405                 * but ignore 'fields', which does not affect query results.
  • tests/phpunit/tests/comment/query.php

     
    48824882
    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(
     4898                        array(
     4899                                'fields' => 'ids',
     4900                        )
     4901                );
     4902
     4903                remove_filter( 'comments_pre_query', array( __CLASS__, 'filter_comments_pre_query' ), 10, 2 );
     4904
     4905                // Make sure no queries were executed.
     4906                $this->assertSame( $num_queries, $wpdb->num_queries );
     4907
     4908                // We manually inserted a non-existing site and overrode the results with it.
     4909                $this->assertSame( array( 555 ), $q->comments );
     4910
     4911                // Make sure manually setting total_users doesn't get overwritten.
     4912                $this->assertEquals( 1, $q->found_comments );
     4913        }
     4914
     4915        public static function filter_comments_pre_query( $comments, $query ) {
     4916                $query->found_comments = 1;
     4917
     4918                return array( 555 );
     4919        }
    48854920}