wp/wp-includes/class-wp-comment-query.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    91 
    91 
    92 	/**
    92 	/**
    93 	 * List of comments located by the query.
    93 	 * List of comments located by the query.
    94 	 *
    94 	 *
    95 	 * @since 4.0.0
    95 	 * @since 4.0.0
    96 	 * @var array
    96 	 * @var int[]|WP_Comment[]
    97 	 */
    97 	 */
    98 	public $comments;
    98 	public $comments;
    99 
    99 
   100 	/**
   100 	/**
   101 	 * The amount of found comments for the current query.
   101 	 * The amount of found comments for the current query.
   139 	 * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
   139 	 * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
   140 	 *              `$hierarchical`, and `$update_comment_post_cache` were added.
   140 	 *              `$hierarchical`, and `$update_comment_post_cache` were added.
   141 	 * @since 4.5.0 Introduced the `$author_url` argument.
   141 	 * @since 4.5.0 Introduced the `$author_url` argument.
   142 	 * @since 4.6.0 Introduced the `$cache_domain` argument.
   142 	 * @since 4.6.0 Introduced the `$cache_domain` argument.
   143 	 * @since 4.9.0 Introduced the `$paged` argument.
   143 	 * @since 4.9.0 Introduced the `$paged` argument.
       
   144 	 * @since 5.1.0 Introduced the `$meta_compare_key` argument.
       
   145 	 * @since 5.3.0 Introduced the `$meta_type_key` argument.
   144 	 *
   146 	 *
   145 	 * @param string|array $query {
   147 	 * @param string|array $query {
   146 	 *     Optional. Array or query string of comment query parameters. Default empty.
   148 	 *     Optional. Array or query string of comment query parameters. Default empty.
   147 	 *
   149 	 *
   148 	 *     @type string       $author_email              Comment author email address. Default empty.
   150 	 *     @type string          $author_email              Comment author email address. Default empty.
   149 	 *     @type string       $author_url                Comment author URL. Default empty.
   151 	 *     @type string          $author_url                Comment author URL. Default empty.
   150 	 *     @type int[]        $author__in                Array of author IDs to include comments for. Default empty.
   152 	 *     @type int[]           $author__in                Array of author IDs to include comments for. Default empty.
   151 	 *     @type int[]        $author__not_in            Array of author IDs to exclude comments for. Default empty.
   153 	 *     @type int[]           $author__not_in            Array of author IDs to exclude comments for. Default empty.
   152 	 *     @type int[]        $comment__in               Array of comment IDs to include. Default empty.
   154 	 *     @type int[]           $comment__in               Array of comment IDs to include. Default empty.
   153 	 *     @type int[]        $comment__not_in           Array of comment IDs to exclude. Default empty.
   155 	 *     @type int[]           $comment__not_in           Array of comment IDs to exclude. Default empty.
   154 	 *     @type bool         $count                     Whether to return a comment count (true) or array of
   156 	 *     @type bool            $count                     Whether to return a comment count (true) or array of
   155 	 *                                                   comment objects (false). Default false.
   157 	 *                                                      comment objects (false). Default false.
   156 	 *     @type array        $date_query                Date query clauses to limit comments by. See WP_Date_Query.
   158 	 *     @type array           $date_query                Date query clauses to limit comments by. See WP_Date_Query.
   157 	 *                                                   Default null.
   159 	 *                                                      Default null.
   158 	 *     @type string       $fields                    Comment fields to return. Accepts 'ids' for comment IDs
   160 	 *     @type string          $fields                    Comment fields to return. Accepts 'ids' for comment IDs
   159 	 *                                                   only or empty for all fields. Default empty.
   161 	 *                                                      only or empty for all fields. Default empty.
   160 	 *     @type int          $ID                        Currently unused.
   162 	 *     @type int             $ID                        Currently unused.
   161 	 *     @type array        $include_unapproved        Array of IDs or email addresses of users whose unapproved
   163 	 *     @type array           $include_unapproved        Array of IDs or email addresses of users whose unapproved
   162 	 *                                                   comments will be returned by the query regardless of
   164 	 *                                                      comments will be returned by the query regardless of
   163 	 *                                                   `$status`. Default empty.
   165 	 *                                                      `$status`. Default empty.
   164 	 *     @type int          $karma                     Karma score to retrieve matching comments for.
   166 	 *     @type int             $karma                     Karma score to retrieve matching comments for.
   165 	 *                                                   Default empty.
   167 	 *                                                      Default empty.
   166 	 *     @type string       $meta_key                  Include comments with a matching comment meta key.
   168 	 *     @type string|string[] $meta_key                  Meta key or keys to filter by.
   167 	 *                                                   Default empty.
   169 	 *     @type string|string[] $meta_value                Meta value or values to filter by.
   168 	 *     @type string       $meta_value                Include comments with a matching comment meta value.
   170 	 *     @type string          $meta_compare              MySQL operator used for comparing the meta value.
   169 	 *                                                   Requires `$meta_key` to be set. Default empty.
   171 	 *                                                      See WP_Meta_Query::__construct for accepted values and default value.
   170 	 *     @type array        $meta_query                Meta query clauses to limit retrieved comments by.
   172 	 *     @type string          $meta_compare_key          MySQL operator used for comparing the meta key.
   171 	 *                                                   See WP_Meta_Query. Default empty.
   173 	 *                                                      See WP_Meta_Query::__construct for accepted values and default value.
   172 	 *     @type int          $number                    Maximum number of comments to retrieve.
   174 	 *     @type string          $meta_type                 MySQL data type that the meta_value column will be CAST to for comparisons.
   173 	 *                                                   Default empty (no limit).
   175 	 *                                                      See WP_Meta_Query::__construct for accepted values and default value.
   174 	 *     @type int          $paged                     When used with $number, defines the page of results to return.
   176 	 *     @type string          $meta_type_key             MySQL data type that the meta_key column will be CAST to for comparisons.
   175 	 *                                                   When used with $offset, $offset takes precedence. Default 1.
   177 	 *                                                      See WP_Meta_Query::__construct for accepted values and default value.
   176 	 *     @type int          $offset                    Number of comments to offset the query. Used to build
   178 	 *     @type array           $meta_query                An associative array of WP_Meta_Query arguments.
   177 	 *                                                   LIMIT clause. Default 0.
   179 	 *                                                      See WP_Meta_Query::__construct for accepted values.
   178 	 *     @type bool         $no_found_rows             Whether to disable the `SQL_CALC_FOUND_ROWS` query.
   180 	 *     @type int             $number                    Maximum number of comments to retrieve.
   179 	 *                                                   Default: true.
   181 	 *                                                      Default empty (no limit).
   180 	 *     @type string|array $orderby                   Comment status or array of statuses. To use 'meta_value'
   182 	 *     @type int             $paged                     When used with `$number`, defines the page of results to return.
   181 	 *                                                   or 'meta_value_num', `$meta_key` must also be defined.
   183 	 *                                                      When used with `$offset`, `$offset` takes precedence. Default 1.
   182 	 *                                                   To sort by a specific `$meta_query` clause, use that
   184 	 *     @type int             $offset                    Number of comments to offset the query. Used to build
   183 	 *                                                   clause's array key. Accepts 'comment_agent',
   185 	 *                                                      LIMIT clause. Default 0.
   184 	 *                                                   'comment_approved', 'comment_author',
   186 	 *     @type bool            $no_found_rows             Whether to disable the `SQL_CALC_FOUND_ROWS` query.
   185 	 *                                                   'comment_author_email', 'comment_author_IP',
   187 	 *                                                      Default: true.
   186 	 *                                                   'comment_author_url', 'comment_content', 'comment_date',
   188 	 *     @type string|array    $orderby                   Comment status or array of statuses. To use 'meta_value'
   187 	 *                                                   'comment_date_gmt', 'comment_ID', 'comment_karma',
   189 	 *                                                      or 'meta_value_num', `$meta_key` must also be defined.
   188 	 *                                                   'comment_parent', 'comment_post_ID', 'comment_type',
   190 	 *                                                      To sort by a specific `$meta_query` clause, use that
   189 	 *                                                   'user_id', 'comment__in', 'meta_value', 'meta_value_num',
   191 	 *                                                      clause's array key. Accepts:
   190 	 *                                                   the value of $meta_key, and the array keys of
   192 	 *                                                      - 'comment_agent'
   191 	 *                                                   `$meta_query`. Also accepts false, an empty array, or
   193 	 *                                                      - 'comment_approved'
   192 	 *                                                   'none' to disable `ORDER BY` clause.
   194 	 *                                                      - 'comment_author'
   193 	 *                                                   Default: 'comment_date_gmt'.
   195 	 *                                                      - 'comment_author_email'
   194 	 *     @type string       $order                     How to order retrieved comments. Accepts 'ASC', 'DESC'.
   196 	 *                                                      - 'comment_author_IP'
   195 	 *                                                   Default: 'DESC'.
   197 	 *                                                      - 'comment_author_url'
   196 	 *     @type int          $parent                    Parent ID of comment to retrieve children of.
   198 	 *                                                      - 'comment_content'
   197 	 *                                                   Default empty.
   199 	 *                                                      - 'comment_date'
   198 	 *     @type int[]        $parent__in                Array of parent IDs of comments to retrieve children for.
   200 	 *                                                      - 'comment_date_gmt'
   199 	 *                                                   Default empty.
   201 	 *                                                      - 'comment_ID'
   200 	 *     @type int[]        $parent__not_in            Array of parent IDs of comments *not* to retrieve
   202 	 *                                                      - 'comment_karma'
   201 	 *                                                   children for. Default empty.
   203 	 *                                                      - 'comment_parent'
   202 	 *     @type int[]        $post_author__in           Array of author IDs to retrieve comments for.
   204 	 *                                                      - 'comment_post_ID'
   203 	 *                                                   Default empty.
   205 	 *                                                      - 'comment_type'
   204 	 *     @type int[]        $post_author__not_in       Array of author IDs *not* to retrieve comments for.
   206 	 *                                                      - 'user_id'
   205 	 *                                                   Default empty.
   207 	 *                                                      - 'comment__in'
   206 	 *     @type int          $post_ID                   Currently unused.
   208 	 *                                                      - 'meta_value'
   207 	 *     @type int          $post_id                   Limit results to those affiliated with a given post ID.
   209 	 *                                                      - 'meta_value_num'
   208 	 *                                                   Default 0.
   210 	 *                                                      - The value of `$meta_key`
   209 	 *     @type int[]        $post__in                  Array of post IDs to include affiliated comments for.
   211 	 *                                                      - The array keys of `$meta_query`
   210 	 *                                                   Default empty.
   212 	 *                                                      - false, an empty array, or 'none' to disable `ORDER BY` clause.
   211 	 *     @type int[]        $post__not_in              Array of post IDs to exclude affiliated comments for.
   213 	 *                                                      Default: 'comment_date_gmt'.
   212 	 *                                                   Default empty.
   214 	 *     @type string          $order                     How to order retrieved comments. Accepts 'ASC', 'DESC'.
   213 	 *     @type int          $post_author               Post author ID to limit results by. Default empty.
   215 	 *                                                      Default: 'DESC'.
   214 	 *     @type string|array $post_status               Post status or array of post statuses to retrieve
   216 	 *     @type int             $parent                    Parent ID of comment to retrieve children of.
   215 	 *                                                   affiliated comments for. Pass 'any' to match any value.
   217 	 *                                                      Default empty.
   216 	 *                                                   Default empty.
   218 	 *     @type int[]           $parent__in                Array of parent IDs of comments to retrieve children for.
   217 	 *     @type string       $post_type                 Post type or array of post types to retrieve affiliated
   219 	 *                                                      Default empty.
   218 	 *                                                   comments for. Pass 'any' to match any value. Default empty.
   220 	 *     @type int[]           $parent__not_in            Array of parent IDs of comments *not* to retrieve
   219 	 *     @type string       $post_name                 Post name to retrieve affiliated comments for.
   221 	 *                                                      children for. Default empty.
   220 	 *                                                   Default empty.
   222 	 *     @type int[]           $post_author__in           Array of author IDs to retrieve comments for.
   221 	 *     @type int          $post_parent               Post parent ID to retrieve affiliated comments for.
   223 	 *                                                      Default empty.
   222 	 *                                                   Default empty.
   224 	 *     @type int[]           $post_author__not_in       Array of author IDs *not* to retrieve comments for.
   223 	 *     @type string       $search                    Search term(s) to retrieve matching comments for.
   225 	 *                                                      Default empty.
   224 	 *                                                   Default empty.
   226 	 *     @type int             $post_ID                   Currently unused.
   225 	 *     @type string|array $status                    Comment statuses to limit results by. Accepts an array
   227 	 *     @type int             $post_id                   Limit results to those affiliated with a given post ID.
   226 	 *                                                   or space/comma-separated list of 'hold' (`comment_status=0`),
   228 	 *                                                      Default 0.
   227 	 *                                                   'approve' (`comment_status=1`), 'all', or a custom
   229 	 *     @type int[]           $post__in                  Array of post IDs to include affiliated comments for.
   228 	 *                                                   comment status. Default 'all'.
   230 	 *                                                      Default empty.
   229 	 *     @type string|array $type                      Include comments of a given type, or array of types.
   231 	 *     @type int[]           $post__not_in              Array of post IDs to exclude affiliated comments for.
   230 	 *                                                   Accepts 'comment', 'pings' (includes 'pingback' and
   232 	 *                                                      Default empty.
   231 	 *                                                   'trackback'), or any custom type string. Default empty.
   233 	 *     @type int             $post_author               Post author ID to limit results by. Default empty.
   232 	 *     @type string[]     $type__in                  Include comments from a given array of comment types.
   234 	 *     @type string|string[] $post_status               Post status or array of post statuses to retrieve
   233 	 *                                                   Default empty.
   235 	 *                                                      affiliated comments for. Pass 'any' to match any value.
   234 	 *     @type string[]     $type__not_in              Exclude comments from a given array of comment types.
   236 	 *                                                      Default empty.
   235 	 *                                                   Default empty.
   237 	 *     @type string|string[] $post_type                 Post type or array of post types to retrieve affiliated
   236 	 *     @type int          $user_id                   Include comments for a specific user ID. Default empty.
   238 	 *                                                      comments for. Pass 'any' to match any value. Default empty.
   237 	 *     @type bool|string  $hierarchical              Whether to include comment descendants in the results.
   239 	 *     @type string          $post_name                 Post name to retrieve affiliated comments for.
   238 	 *                                                   - 'threaded' returns a tree, with each comment's children
   240 	 *                                                      Default empty.
   239 	 *                                                   stored in a `children` property on the `WP_Comment` object.
   241 	 *     @type int             $post_parent               Post parent ID to retrieve affiliated comments for.
   240 	 *                                                   - 'flat' returns a flat array of found comments plus
   242 	 *                                                      Default empty.
   241 	 *                                                   their children.
   243 	 *     @type string          $search                    Search term(s) to retrieve matching comments for.
   242 	 *                                                   - Boolean `false` leaves out descendants.
   244 	 *                                                      Default empty.
   243 	 *                                                   The parameter is ignored (forced to `false`) when
   245 	 *     @type string|array    $status                    Comment statuses to limit results by. Accepts an array
   244 	 *                                                   `$fields` is 'ids' or 'counts'. Accepts 'threaded',
   246 	 *                                                      or space/comma-separated list of 'hold' (`comment_status=0`),
   245 	 *                                                   'flat', or false. Default: false.
   247 	 *                                                      'approve' (`comment_status=1`), 'all', or a custom
   246 	 *     @type string       $cache_domain              Unique cache key to be produced when this query is stored in
   248 	 *                                                      comment status. Default 'all'.
   247 	 *                                                   an object cache. Default is 'core'.
   249 	 *     @type string|string[] $type                      Include comments of a given type, or array of types.
   248 	 *     @type bool         $update_comment_meta_cache Whether to prime the metadata cache for found comments.
   250 	 *                                                      Accepts 'comment', 'pings' (includes 'pingback' and
   249 	 *                                                   Default true.
   251 	 *                                                      'trackback'), or any custom type string. Default empty.
   250 	 *     @type bool         $update_comment_post_cache Whether to prime the cache for comment posts.
   252 	 *     @type string[]        $type__in                  Include comments from a given array of comment types.
   251 	 *                                                   Default false.
   253 	 *                                                      Default empty.
       
   254 	 *     @type string[]        $type__not_in              Exclude comments from a given array of comment types.
       
   255 	 *                                                      Default empty.
       
   256 	 *     @type int             $user_id                   Include comments for a specific user ID. Default empty.
       
   257 	 *     @type bool|string     $hierarchical              Whether to include comment descendants in the results.
       
   258 	 *                                                      - 'threaded' returns a tree, with each comment's children
       
   259 	 *                                                        stored in a `children` property on the `WP_Comment` object.
       
   260 	 *                                                      - 'flat' returns a flat array of found comments plus
       
   261 	 *                                                        their children.
       
   262 	 *                                                      - Boolean `false` leaves out descendants.
       
   263 	 *                                                      The parameter is ignored (forced to `false`) when
       
   264 	 *                                                      `$fields` is 'ids' or 'counts'. Accepts 'threaded',
       
   265 	 *                                                      'flat', or false. Default: false.
       
   266 	 *     @type string          $cache_domain              Unique cache key to be produced when this query is stored in
       
   267 	 *                                                      an object cache. Default is 'core'.
       
   268 	 *     @type bool            $update_comment_meta_cache Whether to prime the metadata cache for found comments.
       
   269 	 *                                                      Default true.
       
   270 	 *     @type bool            $update_comment_post_cache Whether to prime the cache for comment posts.
       
   271 	 *                                                      Default false.
   252 	 * }
   272 	 * }
   253 	 */
   273 	 */
   254 	public function __construct( $query = '' ) {
   274 	public function __construct( $query = '' ) {
   255 		$this->query_var_defaults = array(
   275 		$this->query_var_defaults = array(
   256 			'author_email'              => '',
   276 			'author_email'              => '',
   322 		/**
   342 		/**
   323 		 * Fires after the comment query vars have been parsed.
   343 		 * Fires after the comment query vars have been parsed.
   324 		 *
   344 		 *
   325 		 * @since 4.2.0
   345 		 * @since 4.2.0
   326 		 *
   346 		 *
   327 		 * @param WP_Comment_Query $this The WP_Comment_Query instance (passed by reference).
   347 		 * @param WP_Comment_Query $query The WP_Comment_Query instance (passed by reference).
   328 		 */
   348 		 */
   329 		do_action_ref_array( 'parse_comment_query', array( &$this ) );
   349 		do_action_ref_array( 'parse_comment_query', array( &$this ) );
   330 	}
   350 	}
   331 
   351 
   332 	/**
   352 	/**
   352 	 *
   372 	 *
   353 	 * @since 4.2.0
   373 	 * @since 4.2.0
   354 	 *
   374 	 *
   355 	 * @global wpdb $wpdb WordPress database abstraction object.
   375 	 * @global wpdb $wpdb WordPress database abstraction object.
   356 	 *
   376 	 *
   357 	 * @return int|array List of comments or number of found comments if `$count` argument is true.
   377 	 * @return int|int[]|WP_Comment[] List of comments or number of found comments if `$count` argument is true.
   358 	 */
   378 	 */
   359 	public function get_comments() {
   379 	public function get_comments() {
   360 		global $wpdb;
   380 		global $wpdb;
   361 
   381 
   362 		$this->parse_query();
   382 		$this->parse_query();
   368 		/**
   388 		/**
   369 		 * Fires before comments are retrieved.
   389 		 * Fires before comments are retrieved.
   370 		 *
   390 		 *
   371 		 * @since 3.1.0
   391 		 * @since 3.1.0
   372 		 *
   392 		 *
   373 		 * @param WP_Comment_Query $this Current instance of WP_Comment_Query (passed by reference).
   393 		 * @param WP_Comment_Query $query Current instance of WP_Comment_Query (passed by reference).
   374 		 */
   394 		 */
   375 		do_action_ref_array( 'pre_get_comments', array( &$this ) );
   395 		do_action_ref_array( 'pre_get_comments', array( &$this ) );
   376 
   396 
   377 		// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
   397 		// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
   378 		$this->meta_query->parse_query_vars( $this->query_vars );
   398 		$this->meta_query->parse_query_vars( $this->query_vars );
   422 			return $comment_data;
   442 			return $comment_data;
   423 		}
   443 		}
   424 
   444 
   425 		/*
   445 		/*
   426 		 * Only use the args defined in the query_var_defaults to compute the key,
   446 		 * Only use the args defined in the query_var_defaults to compute the key,
   427 		 * but ignore 'fields', which does not affect query results.
   447 		 * but ignore 'fields', 'update_comment_meta_cache', 'update_comment_post_cache' which does not affect query results.
   428 		 */
   448 		 */
   429 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   449 		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
   430 		unset( $_args['fields'] );
   450 		unset( $_args['fields'], $_args['update_comment_meta_cache'], $_args['update_comment_post_cache'] );
   431 
   451 
   432 		$key          = md5( serialize( $_args ) );
   452 		$key          = md5( serialize( $_args ) );
   433 		$last_changed = wp_cache_get_last_changed( 'comment' );
   453 		$last_changed = wp_cache_get_last_changed( 'comment' );
   434 
   454 
   435 		$cache_key   = "get_comments:$key:$last_changed";
   455 		$cache_key   = "get_comments:$key:$last_changed";
   573 					$approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
   593 					$approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
   574 				} else {
   594 				} else {
   575 					// Otherwise we match against email addresses.
   595 					// Otherwise we match against email addresses.
   576 					if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
   596 					if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) {
   577 						// Only include requested comment.
   597 						// Only include requested comment.
   578 						$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' AND comment_ID = %d )", $unapproved_identifier, (int) $_GET['unapproved'] );
   598 						$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' AND {$wpdb->comments}.comment_ID = %d )", $unapproved_identifier, (int) $_GET['unapproved'] );
   579 					} else {
   599 					} else {
   580 						// Include all of the author's unapproved comments.
   600 						// Include all of the author's unapproved comments.
   581 						$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
   601 						$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
   582 					}
   602 					}
   583 				}
   603 				}
   799 		} elseif ( '' !== $this->query_vars['user_id'] ) {
   819 		} elseif ( '' !== $this->query_vars['user_id'] ) {
   800 			$this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
   820 			$this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
   801 		}
   821 		}
   802 
   822 
   803 		// Falsey search strings are ignored.
   823 		// Falsey search strings are ignored.
   804 		if ( strlen( $this->query_vars['search'] ) ) {
   824 		if ( isset( $this->query_vars['search'] ) && strlen( $this->query_vars['search'] ) ) {
   805 			$search_sql = $this->get_search_sql(
   825 			$search_sql = $this->get_search_sql(
   806 				$this->query_vars['search'],
   826 				$this->query_vars['search'],
   807 				array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
   827 				array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
   808 			);
   828 			);
   809 
   829 
   895 			$this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
   915 			$this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
   896 		}
   916 		}
   897 
   917 
   898 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   918 		$where = implode( ' AND ', $this->sql_clauses['where'] );
   899 
   919 
   900 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
   920 		$clauses = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
       
   921 
   901 		/**
   922 		/**
   902 		 * Filters the comment query clauses.
   923 		 * Filters the comment query clauses.
   903 		 *
   924 		 *
   904 		 * @since 3.1.0
   925 		 * @since 3.1.0
   905 		 *
   926 		 *
   906 		 * @param string[]         $pieces An associative array of comment query clauses.
   927 		 * @param string[]         $clauses An associative array of comment query clauses.
   907 		 * @param WP_Comment_Query $query  Current instance of WP_Comment_Query (passed by reference).
   928 		 * @param WP_Comment_Query $query   Current instance of WP_Comment_Query (passed by reference).
   908 		 */
   929 		 */
   909 		$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) );
   930 		$clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $clauses ), &$this ) );
   910 
   931 
   911 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   932 		$fields  = isset( $clauses['fields'] ) ? $clauses['fields'] : '';
   912 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   933 		$join    = isset( $clauses['join'] ) ? $clauses['join'] : '';
   913 		$where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
   934 		$where   = isset( $clauses['where'] ) ? $clauses['where'] : '';
   914 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   935 		$orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : '';
   938 		$this->sql_clauses['from']    = "FROM $wpdb->comments $join";
   959 		$this->sql_clauses['from']    = "FROM $wpdb->comments $join";
   939 		$this->sql_clauses['groupby'] = $groupby;
   960 		$this->sql_clauses['groupby'] = $groupby;
   940 		$this->sql_clauses['orderby'] = $orderby;
   961 		$this->sql_clauses['orderby'] = $orderby;
   941 		$this->sql_clauses['limits']  = $limits;
   962 		$this->sql_clauses['limits']  = $limits;
   942 
   963 
   943 		$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
   964 		$this->request = "
       
   965 			{$this->sql_clauses['select']}
       
   966 			{$this->sql_clauses['from']}
       
   967 			{$where}
       
   968 			{$this->sql_clauses['groupby']}
       
   969 			{$this->sql_clauses['orderby']}
       
   970 			{$this->sql_clauses['limits']}
       
   971 		";
   944 
   972 
   945 		if ( $this->query_vars['count'] ) {
   973 		if ( $this->query_vars['count'] ) {
   946 			return (int) $wpdb->get_var( $this->request );
   974 			return (int) $wpdb->get_var( $this->request );
   947 		} else {
   975 		} else {
   948 			$comment_ids = $wpdb->get_col( $this->request );
   976 			$comment_ids = $wpdb->get_col( $this->request );
  1036 				foreach ( $level_comments as $level_comment ) {
  1064 				foreach ( $level_comments as $level_comment ) {
  1037 					$parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
  1065 					$parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
  1038 					$child_ids[]                                    = $level_comment->comment_ID;
  1066 					$child_ids[]                                    = $level_comment->comment_ID;
  1039 				}
  1067 				}
  1040 
  1068 
       
  1069 				$data = array();
  1041 				foreach ( $parent_map as $parent_id => $children ) {
  1070 				foreach ( $parent_map as $parent_id => $children ) {
  1042 					$cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
  1071 					$cache_key          = "get_comment_child_ids:$parent_id:$key:$last_changed";
  1043 					wp_cache_set( $cache_key, $children, 'comment' );
  1072 					$data[ $cache_key ] = $children;
  1044 				}
  1073 				}
       
  1074 				wp_cache_set_multiple( $data, 'comment' );
  1045 			}
  1075 			}
  1046 
  1076 
  1047 			$level++;
  1077 			$level++;
  1048 			$levels[ $level ] = $child_ids;
  1078 			$levels[ $level ] = $child_ids;
  1049 		} while ( $child_ids );
  1079 		} while ( $child_ids );
  1094 
  1124 
  1095 		return $comments;
  1125 		return $comments;
  1096 	}
  1126 	}
  1097 
  1127 
  1098 	/**
  1128 	/**
  1099 	 * Used internally to generate an SQL string for searching across multiple columns
  1129 	 * Used internally to generate an SQL string for searching across multiple columns.
  1100 	 *
  1130 	 *
  1101 	 * @since 3.1.0
  1131 	 * @since 3.1.0
  1102 	 *
  1132 	 *
  1103 	 * @global wpdb $wpdb WordPress database abstraction object.
  1133 	 * @global wpdb $wpdb WordPress database abstraction object.
  1104 	 *
  1134 	 *
  1105 	 * @param string $string
  1135 	 * @param string   $search  Search string.
  1106 	 * @param array  $cols
  1136 	 * @param string[] $columns Array of columns to search.
  1107 	 * @return string
  1137 	 * @return string Search SQL.
  1108 	 */
  1138 	 */
  1109 	protected function get_search_sql( $string, $cols ) {
  1139 	protected function get_search_sql( $search, $columns ) {
  1110 		global $wpdb;
  1140 		global $wpdb;
  1111 
  1141 
  1112 		$like = '%' . $wpdb->esc_like( $string ) . '%';
  1142 		$like = '%' . $wpdb->esc_like( $search ) . '%';
  1113 
  1143 
  1114 		$searches = array();
  1144 		$searches = array();
  1115 		foreach ( $cols as $col ) {
  1145 		foreach ( $columns as $column ) {
  1116 			$searches[] = $wpdb->prepare( "$col LIKE %s", $like );
  1146 			$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
  1117 		}
  1147 		}
  1118 
  1148 
  1119 		return ' AND (' . implode( ' OR ', $searches ) . ')';
  1149 		return ' AND (' . implode( ' OR ', $searches ) . ')';
  1120 	}
  1150 	}
  1121 
  1151