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.

git-svn-id: https://develop.svn.wordpress.org/trunk@31793 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2015-03-16 14:23:33 +00:00
parent ec49827b0b
commit 12784e13d4
2 changed files with 167 additions and 54 deletions

View File

@@ -1587,4 +1587,49 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEqualSets( array_merge( $c1, $c3 ), $found );
}
/**
* @ticket 24826
*/
public function test_comment_query_object() {
$comment_id = $this->factory->comment->create();
$query1 = new WP_Comment_Query();
$this->assertNull( $query1->query_vars );
$this->assertEmpty( $query1->comments );
$comments = $query1->query( array( 'status' => 'all' ) );
$this->assertInternalType( 'array', $query1->query_vars );
$this->assertNotEmpty( $query1->comments );
$this->assertInternalType( 'array', $query1->comments );
$query2 = new WP_Comment_Query( array( 'status' => 'all' ) );
$this->assertNotEmpty( $query2->query_vars );
$this->assertNotEmpty( $query2->comments );
$this->assertEquals( $query2->comments, $query1->get_comments() );
}
/**
* @ticket 22400
*/
public function test_comment_cache_key_should_ignore_custom_params() {
global $wpdb;
$p = $this->factory->post->create();
$c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) );
$q1 = new WP_Comment_Query();
$q1->query( array(
'post_id' => $p,
) );
$num_queries = $wpdb->num_queries;
$q2 = new WP_Comment_Query();
$q2->query( array(
'post_id' => $p,
'foo' => 'bar',
) );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
}