Allow comments to be queried by 'any' post_type
or post_status
.
Props kouratoras. Fixes #35512. git-svn-id: https://develop.svn.wordpress.org/trunk@36486 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
eb8e2fb6ed
commit
ddc9f3dccb
@ -220,10 +220,11 @@ class WP_Comment_Query {
|
||||
* @type array $post__not_in Array of post IDs to exclude affiliated comments for.
|
||||
* Default empty.
|
||||
* @type int $post_author Post author ID to limit results by. Default empty.
|
||||
* @type string $post_status Post status to retrieve affiliated comments for.
|
||||
* Default empty.
|
||||
* @type string $post_type Post type to retrieve affiliated comments for.
|
||||
* @type string|array $post_status Post status or array of post statuses to retrieve
|
||||
* affiliated comments for. Pass 'any' to match any value.
|
||||
* Default empty.
|
||||
* @type string $post_type Post type or array of post types to retrieve affiliated
|
||||
* comments for. Pass 'any' to match any value. Default empty.
|
||||
* @type string $post_name Post name to retrieve affiliated comments for.
|
||||
* Default empty.
|
||||
* @type int $post_parent Post parent ID to retrieve affiliated comments for.
|
||||
@ -760,7 +761,7 @@ class WP_Comment_Query {
|
||||
|
||||
// If any post-related query vars are passed, join the posts table.
|
||||
$join_posts_table = false;
|
||||
$plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
|
||||
$plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
|
||||
$post_fields = array_filter( $plucked );
|
||||
|
||||
if ( ! empty( $post_fields ) ) {
|
||||
@ -772,6 +773,27 @@ class WP_Comment_Query {
|
||||
}
|
||||
}
|
||||
|
||||
// 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'.
|
||||
foreach ( array( 'post_status', 'post_type' ) as $field_name ) {
|
||||
$q_values = array();
|
||||
if ( ! empty( $this->query_vars[ $field_name ] ) ) {
|
||||
$q_values = $this->query_vars[ $field_name ];
|
||||
if ( ! is_array( $q_values ) ) {
|
||||
$q_values = explode( ',', $q_values );
|
||||
}
|
||||
|
||||
// 'any' will cause the query var to be ignored.
|
||||
if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$join_posts_table = true;
|
||||
|
||||
$esses = array_fill( 0, count( $q_values ), '%s' );
|
||||
$this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
|
||||
}
|
||||
}
|
||||
|
||||
// Comment author IDs for an IN clause.
|
||||
if ( ! empty( $this->query_vars['author__in'] ) ) {
|
||||
$this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
|
||||
|
@ -1749,6 +1749,84 @@ class Tests_Comment_Query extends WP_UnitTestCase {
|
||||
$this->assertEqualSets( array_merge( $c1, $c3 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35512
|
||||
*/
|
||||
public function test_post_type_any_should_override_other_post_types() {
|
||||
register_post_type( 'post-type-1', array( 'exclude_from_search' => false ) );
|
||||
register_post_type( 'post-type-2', array( 'exclude_from_search' => false ) );
|
||||
|
||||
$p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
|
||||
$p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
|
||||
|
||||
$c1 = self::factory()->comment->create_post_comments( $p1, 1 );
|
||||
$c2 = self::factory()->comment->create_post_comments( $p2, 1 );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'fields' => 'ids',
|
||||
'post_type' => array( 'any', 'post-type-1' ),
|
||||
) );
|
||||
$this->assertEqualSets( array_merge( $c1, $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35512
|
||||
*/
|
||||
public function test_post_type_any_as_part_of_an_array_of_post_types() {
|
||||
register_post_type( 'post-type-1', array( 'exclude_from_search' => false ) );
|
||||
register_post_type( 'post-type-2', array( 'exclude_from_search' => false ) );
|
||||
|
||||
$p1 = self::factory()->post->create( array( 'post_type' => 'post-type-1' ) );
|
||||
$p2 = self::factory()->post->create( array( 'post_type' => 'post-type-2' ) );
|
||||
|
||||
$c1 = self::factory()->comment->create_post_comments( $p1, 1 );
|
||||
$c2 = self::factory()->comment->create_post_comments( $p2, 1 );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'fields' => 'ids',
|
||||
'post_type' => array( 'any' ),
|
||||
) );
|
||||
$this->assertEqualSets( array_merge( $c1, $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35512
|
||||
*/
|
||||
public function test_post_status_any_should_override_other_post_statuses() {
|
||||
$p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
|
||||
$p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
|
||||
|
||||
$c1 = self::factory()->comment->create_post_comments( $p1, 1 );
|
||||
$c2 = self::factory()->comment->create_post_comments( $p2, 1 );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'fields' => 'ids',
|
||||
'post_status' => array( 'any', 'draft' ),
|
||||
) );
|
||||
$this->assertEqualSets( array_merge( $c1, $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35512
|
||||
*/
|
||||
public function test_post_status_any_as_part_of_an_array_of_post_statuses() {
|
||||
$p1 = self::factory()->post->create( array( 'post_status' => 'publish' ) );
|
||||
$p2 = self::factory()->post->create( array( 'post_status' => 'draft' ) );
|
||||
|
||||
$c1 = self::factory()->comment->create_post_comments( $p1, 1 );
|
||||
$c2 = self::factory()->comment->create_post_comments( $p2, 1 );
|
||||
|
||||
$q = new WP_Comment_Query();
|
||||
$found = $q->query( array(
|
||||
'fields' => 'ids',
|
||||
'post_status' => array( 'any' ),
|
||||
) );
|
||||
$this->assertEqualSets( array_merge( $c1, $c2 ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 24826
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user