From 146320744f7c9ee169b67127613ef0cea882fa5c Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 6 Feb 2016 04:51:25 +0000 Subject: [PATCH] Allow comments to be queried by 'any' `post_type` or `post_status`. Props kouratoras. Fixes #35512. Built from https://develop.svn.wordpress.org/trunk@36486 git-svn-id: http://core.svn.wordpress.org/trunk@36453 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-comment-query.php | 30 ++++++++++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index 4145d8fd76..824fc58fdf 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -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'] ) ) . ' )'; diff --git a/wp-includes/version.php b/wp-includes/version.php index 36862408b3..c7fe2c32e3 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36485'; +$wp_version = '4.5-alpha-36486'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.