WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/16/2015 02:23:33 PM (6 years ago)
Author:
boonebgorges
Message:

Improve method consistency in WP_Comment_Query.

  • Introduce a __construct() method, which can accept an array of query vars.
  • Move query logic out of query() method and into a new get_comments() method.
  • Ensure that $this->comments is set whenever get_comments() returns a value.
  • Introduce a parse_query() method, where query vars are parsed with default values and the 'parse_comment_query' action is fired.

These changes bring WP_Comment_Query syntax closer to that of WP_Query.

Props westonruter, morganestes, boonebgorges.
Fixes #24826.

File:
1 edited

Legend:

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

    r31666 r31793  
    15881588        $this->assertEqualSets( array_merge( $c1, $c3 ), $found );
    15891589    }
     1590
     1591    /**
     1592     * @ticket 24826
     1593     */
     1594    public function test_comment_query_object() {
     1595        $comment_id = $this->factory->comment->create();
     1596
     1597        $query1 = new WP_Comment_Query();
     1598        $this->assertNull( $query1->query_vars );
     1599        $this->assertEmpty( $query1->comments );
     1600        $comments = $query1->query( array( 'status' => 'all' ) );
     1601        $this->assertInternalType( 'array', $query1->query_vars );
     1602        $this->assertNotEmpty( $query1->comments );
     1603        $this->assertInternalType( 'array', $query1->comments );
     1604
     1605        $query2 = new WP_Comment_Query( array( 'status' => 'all' ) );
     1606        $this->assertNotEmpty( $query2->query_vars );
     1607        $this->assertNotEmpty( $query2->comments );
     1608        $this->assertEquals( $query2->comments, $query1->get_comments() );
     1609    }
     1610
     1611    /**
     1612     * @ticket 22400
     1613     */
     1614    public function test_comment_cache_key_should_ignore_custom_params() {
     1615        global $wpdb;
     1616
     1617        $p = $this->factory->post->create();
     1618        $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
     1619
     1620        $q1 = new WP_Comment_Query();
     1621        $q1->query( array(
     1622            'post_id' => $p,
     1623        ) );
     1624
     1625        $num_queries = $wpdb->num_queries;
     1626
     1627        $q2 = new WP_Comment_Query();
     1628        $q2->query( array(
     1629            'post_id' => $p,
     1630            'foo' => 'bar',
     1631        ) );
     1632
     1633        $this->assertSame( $num_queries, $wpdb->num_queries );
     1634    }
    15901635}
Note: See TracChangeset for help on using the changeset viewer.