WP_Comment_Query

Core class used for querying comments.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-comment-query.php  
  1. class WP_Comment_Query { 
  2.  
  3. /** 
  4. * SQL for database query. 
  5. * @since 4.0.1 
  6. * @access public 
  7. * @var string 
  8. */ 
  9. public $request; 
  10.  
  11. /** 
  12. * Metadata query container 
  13. * @since 3.5.0 
  14. * @access public 
  15. * @var object WP_Meta_Query 
  16. */ 
  17. public $meta_query = false; 
  18.  
  19. /** 
  20. * Metadata query clauses. 
  21. * @since 4.4.0 
  22. * @access protected 
  23. * @var array 
  24. */ 
  25. protected $meta_query_clauses; 
  26.  
  27. /** 
  28. * SQL query clauses. 
  29. * @since 4.4.0 
  30. * @access protected 
  31. * @var array 
  32. */ 
  33. protected $sql_clauses = array( 
  34. 'select' => '',  
  35. 'from' => '',  
  36. 'where' => array(),  
  37. 'groupby' => '',  
  38. 'orderby' => '',  
  39. 'limits' => '',  
  40. ); 
  41.  
  42. /** 
  43. * SQL WHERE clause. 
  44. * Stored after the {@see 'comments_clauses'} filter is run on the compiled WHERE sub-clauses. 
  45. * @since 4.4.2 
  46. * @access protected 
  47. * @var string 
  48. */ 
  49. protected $filtered_where_clause; 
  50.  
  51. /** 
  52. * Date query container 
  53. * @since 3.7.0 
  54. * @access public 
  55. * @var object WP_Date_Query 
  56. */ 
  57. public $date_query = false; 
  58.  
  59. /** 
  60. * Query vars set by the user. 
  61. * @since 3.1.0 
  62. * @access public 
  63. * @var array 
  64. */ 
  65. public $query_vars; 
  66.  
  67. /** 
  68. * Default values for query vars. 
  69. * @since 4.2.0 
  70. * @access public 
  71. * @var array 
  72. */ 
  73. public $query_var_defaults; 
  74.  
  75. /** 
  76. * List of comments located by the query. 
  77. * @since 4.0.0 
  78. * @access public 
  79. * @var array 
  80. */ 
  81. public $comments; 
  82.  
  83. /** 
  84. * The amount of found comments for the current query. 
  85. * @since 4.4.0 
  86. * @access public 
  87. * @var int 
  88. */ 
  89. public $found_comments = 0; 
  90.  
  91. /** 
  92. * The number of pages. 
  93. * @since 4.4.0 
  94. * @access public 
  95. * @var int 
  96. */ 
  97. public $max_num_pages = 0; 
  98.  
  99. /** 
  100. * Make private/protected methods readable for backward compatibility. 
  101. * @since 4.0.0 
  102. * @access public 
  103. * @param callable $name Method to call. 
  104. * @param array $arguments Arguments to pass when calling. 
  105. * @return mixed|false Return value of the callback, false otherwise. 
  106. */ 
  107. public function __call( $name, $arguments ) { 
  108. if ( 'get_search_sql' === $name ) { 
  109. return call_user_func_array( array( $this, $name ), $arguments ); 
  110. return false; 
  111.  
  112. /** 
  113. * Constructor. 
  114. * Sets up the comment query, based on the query vars passed. 
  115. * @since 4.2.0 
  116. * @since 4.4.0 `$parent__in` and `$parent__not_in` were added. 
  117. * @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,  
  118. * `$hierarchical`, and `$update_comment_post_cache` were added. 
  119. * @since 4.5.0 Introduced the `$author_url` argument. 
  120. * @since 4.6.0 Introduced the `$cache_domain` argument. 
  121. * @access public 
  122. * @param string|array $query { 
  123. * Optional. Array or query string of comment query parameters. Default empty. 
  124. * @type string $author_email Comment author email address. Default empty. 
  125. * @type string $author_url Comment author URL. Default empty. 
  126. * @type array $author__in Array of author IDs to include comments for. Default empty. 
  127. * @type array $author__not_in Array of author IDs to exclude comments for. Default empty. 
  128. * @type array $comment__in Array of comment IDs to include. Default empty. 
  129. * @type array $comment__not_in Array of comment IDs to exclude. Default empty. 
  130. * @type bool $count Whether to return a comment count (true) or array of 
  131. * comment objects (false). Default false. 
  132. * @type array $date_query Date query clauses to limit comments by. See WP_Date_Query. 
  133. * Default null. 
  134. * @type string $fields Comment fields to return. Accepts 'ids' for comment IDs 
  135. * only or empty for all fields. Default empty. 
  136. * @type int $ID Currently unused. 
  137. * @type array $include_unapproved Array of IDs or email addresses of users whose unapproved 
  138. * comments will be returned by the query regardless of 
  139. * `$status`. Default empty. 
  140. * @type int $karma Karma score to retrieve matching comments for. 
  141. * Default empty. 
  142. * @type string $meta_key Include comments with a matching comment meta key. 
  143. * Default empty. 
  144. * @type string $meta_value Include comments with a matching comment meta value. 
  145. * Requires `$meta_key` to be set. Default empty. 
  146. * @type array $meta_query Meta query clauses to limit retrieved comments by. 
  147. * See WP_Meta_Query. Default empty. 
  148. * @type int $number Maximum number of comments to retrieve. 
  149. * Default null (no limit). 
  150. * @type int $offset Number of comments to offset the query. Used to build 
  151. * LIMIT clause. Default 0. 
  152. * @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query. 
  153. * Default: true. 
  154. * @type string|array $orderby Comment status or array of statuses. To use 'meta_value' 
  155. * or 'meta_value_num', `$meta_key` must also be defined. 
  156. * To sort by a specific `$meta_query` clause, use that 
  157. * clause's array key. Accepts 'comment_agent',  
  158. * 'comment_approved', 'comment_author',  
  159. * 'comment_author_email', 'comment_author_IP',  
  160. * 'comment_author_url', 'comment_content', 'comment_date',  
  161. * 'comment_date_gmt', 'comment_ID', 'comment_karma',  
  162. * 'comment_parent', 'comment_post_ID', 'comment_type',  
  163. * 'user_id', 'comment__in', 'meta_value', 'meta_value_num',  
  164. * the value of $meta_key, and the array keys of 
  165. * `$meta_query`. Also accepts false, an empty array, or 
  166. * 'none' to disable `ORDER BY` clause. 
  167. * Default: 'comment_date_gmt'. 
  168. * @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'. 
  169. * Default: 'DESC'. 
  170. * @type int $parent Parent ID of comment to retrieve children of. 
  171. * Default empty. 
  172. * @type array $parent__in Array of parent IDs of comments to retrieve children for. 
  173. * Default empty. 
  174. * @type array $parent__not_in Array of parent IDs of comments *not* to retrieve 
  175. * children for. Default empty. 
  176. * @type array $post_author__in Array of author IDs to retrieve comments for. 
  177. * Default empty. 
  178. * @type array $post_author__not_in Array of author IDs *not* to retrieve comments for. 
  179. * Default empty. 
  180. * @type int $post_ID Currently unused. 
  181. * @type int $post_id Limit results to those affiliated with a given post ID. 
  182. * Default 0. 
  183. * @type array $post__in Array of post IDs to include affiliated comments for. 
  184. * Default empty. 
  185. * @type array $post__not_in Array of post IDs to exclude affiliated comments for. 
  186. * Default empty. 
  187. * @type int $post_author Post author ID to limit results by. Default empty. 
  188. * @type string|array $post_status Post status or array of post statuses to retrieve 
  189. * affiliated comments for. Pass 'any' to match any value. 
  190. * Default empty. 
  191. * @type string $post_type Post type or array of post types to retrieve affiliated 
  192. * comments for. Pass 'any' to match any value. Default empty. 
  193. * @type string $post_name Post name to retrieve affiliated comments for. 
  194. * Default empty. 
  195. * @type int $post_parent Post parent ID to retrieve affiliated comments for. 
  196. * Default empty. 
  197. * @type string $search Search term(s) to retrieve matching comments for. 
  198. * Default empty. 
  199. * @type string $status Comment status to limit results by. Accepts 'hold' 
  200. * (`comment_status=0`), 'approve' (`comment_status=1`),  
  201. * 'all', or a custom comment status. Default 'all'. 
  202. * @type string|array $type Include comments of a given type, or array of types. 
  203. * Accepts 'comment', 'pings' (includes 'pingback' and 
  204. * 'trackback'), or anycustom type string. Default empty. 
  205. * @type array $type__in Include comments from a given array of comment types. 
  206. * Default empty. 
  207. * @type array $type__not_in Exclude comments from a given array of comment types. 
  208. * Default empty. 
  209. * @type int $user_id Include comments for a specific user ID. Default empty. 
  210. * @type bool|string $hierarchical Whether to include comment descendants in the results. 
  211. * 'threaded' returns a tree, with each comment's children 
  212. * stored in a `children` property on the `WP_Comment` 
  213. * object. 'flat' returns a flat array of found comments plus 
  214. * their children. Pass `false` to leave out descendants. 
  215. * The parameter is ignored (forced to `false`) when 
  216. * `$fields` is 'ids' or 'counts'. Accepts 'threaded',  
  217. * 'flat', or false. Default: false. 
  218. * @type string $cache_domain Unique cache key to be produced when this query is stored in 
  219. * an object cache. Default is 'core'. 
  220. * @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments. 
  221. * Default true. 
  222. * @type bool $update_comment_post_cache Whether to prime the cache for comment posts. 
  223. * Default false. 
  224. * } 
  225. */ 
  226. public function __construct( $query = '' ) { 
  227. $this->query_var_defaults = array( 
  228. 'author_email' => '',  
  229. 'author_url' => '',  
  230. 'author__in' => '',  
  231. 'author__not_in' => '',  
  232. 'include_unapproved' => '',  
  233. 'fields' => '',  
  234. 'ID' => '',  
  235. 'comment__in' => '',  
  236. 'comment__not_in' => '',  
  237. 'karma' => '',  
  238. 'number' => '',  
  239. 'offset' => '',  
  240. 'no_found_rows' => true,  
  241. 'orderby' => '',  
  242. 'order' => 'DESC',  
  243. 'parent' => '',  
  244. 'parent__in' => '',  
  245. 'parent__not_in' => '',  
  246. 'post_author__in' => '',  
  247. 'post_author__not_in' => '',  
  248. 'post_ID' => '',  
  249. 'post_id' => 0,  
  250. 'post__in' => '',  
  251. 'post__not_in' => '',  
  252. 'post_author' => '',  
  253. 'post_name' => '',  
  254. 'post_parent' => '',  
  255. 'post_status' => '',  
  256. 'post_type' => '',  
  257. 'status' => 'all',  
  258. 'type' => '',  
  259. 'type__in' => '',  
  260. 'type__not_in' => '',  
  261. 'user_id' => '',  
  262. 'search' => '',  
  263. 'count' => false,  
  264. 'meta_key' => '',  
  265. 'meta_value' => '',  
  266. 'meta_query' => '',  
  267. 'date_query' => null, // See WP_Date_Query 
  268. 'hierarchical' => false,  
  269. 'cache_domain' => 'core',  
  270. 'update_comment_meta_cache' => true,  
  271. 'update_comment_post_cache' => false,  
  272. ); 
  273.  
  274. if ( ! empty( $query ) ) { 
  275. $this->query( $query ); 
  276.  
  277. /** 
  278. * Parse arguments passed to the comment query with default query parameters. 
  279. * @since 4.2.0 Extracted from WP_Comment_Query::query(). 
  280. * @access public 
  281. * @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct() 
  282. */ 
  283. public function parse_query( $query = '' ) { 
  284. if ( empty( $query ) ) { 
  285. $query = $this->query_vars; 
  286.  
  287. $this->query_vars = wp_parse_args( $query, $this->query_var_defaults ); 
  288.  
  289. /** 
  290. * Fires after the comment query vars have been parsed. 
  291. * @since 4.2.0 
  292. * @param WP_Comment_Query &$this The WP_Comment_Query instance (passed by reference). 
  293. */ 
  294. do_action_ref_array( 'parse_comment_query', array( &$this ) ); 
  295.  
  296. /** 
  297. * Sets up the WordPress query for retrieving comments. 
  298. * @since 3.1.0 
  299. * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',  
  300. * 'post_author__not_in', 'author__in', 'author__not_in', 'post__in',  
  301. * 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in' 
  302. * arguments to $query_vars. 
  303. * @since 4.2.0 Moved parsing to WP_Comment_Query::parse_query(). 
  304. * @access public 
  305. * @param string|array $query Array or URL query string of parameters. 
  306. * @return array|int List of comments, or number of comments when 'count' is passed as a query var. 
  307. */ 
  308. public function query( $query ) { 
  309. $this->query_vars = wp_parse_args( $query ); 
  310. return $this->get_comments(); 
  311.  
  312. /** 
  313. * Get a list of comments matching the query vars. 
  314. * @since 4.2.0 
  315. * @access public 
  316. * @global wpdb $wpdb WordPress database abstraction object. 
  317. * @return int|array List of comments or number of found comments if `$count` argument is true. 
  318. */ 
  319. public function get_comments() { 
  320. global $wpdb; 
  321.  
  322. $this->parse_query(); 
  323.  
  324. // Parse meta query 
  325. $this->meta_query = new WP_Meta_Query(); 
  326. $this->meta_query->parse_query_vars( $this->query_vars ); 
  327.  
  328. /** 
  329. * Fires before comments are retrieved. 
  330. * @since 3.1.0 
  331. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. 
  332. */ 
  333. do_action_ref_array( 'pre_get_comments', array( &$this ) ); 
  334.  
  335. // Reparse query vars, in case they were modified in a 'pre_get_comments' callback. 
  336. $this->meta_query->parse_query_vars( $this->query_vars ); 
  337. if ( ! empty( $this->meta_query->queries ) ) { 
  338. $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 
  339.  
  340. // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. 
  341. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 
  342. $last_changed = wp_cache_get( 'last_changed', 'comment' ); 
  343. if ( ! $last_changed ) { 
  344. $last_changed = microtime(); 
  345. wp_cache_set( 'last_changed', $last_changed, 'comment' ); 
  346.  
  347. $cache_key = "get_comments:$key:$last_changed"; 
  348. $cache_value = wp_cache_get( $cache_key, 'comment' ); 
  349. if ( false === $cache_value ) { 
  350. $comment_ids = $this->get_comment_ids(); 
  351. if ( $comment_ids ) { 
  352. $this->set_found_comments(); 
  353.  
  354. $cache_value = array( 
  355. 'comment_ids' => $comment_ids,  
  356. 'found_comments' => $this->found_comments,  
  357. ); 
  358. wp_cache_add( $cache_key, $cache_value, 'comment' ); 
  359. } else { 
  360. $comment_ids = $cache_value['comment_ids']; 
  361. $this->found_comments = $cache_value['found_comments']; 
  362.  
  363. if ( $this->found_comments && $this->query_vars['number'] ) { 
  364. $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] ); 
  365.  
  366. // If querying for a count only, there's nothing more to do. 
  367. if ( $this->query_vars['count'] ) { 
  368. // $comment_ids is actually a count in this case. 
  369. return intval( $comment_ids ); 
  370.  
  371. $comment_ids = array_map( 'intval', $comment_ids ); 
  372.  
  373. if ( 'ids' == $this->query_vars['fields'] ) { 
  374. $this->comments = $comment_ids; 
  375. return $this->comments; 
  376.  
  377. _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] ); 
  378.  
  379. // Fetch full comment objects from the primed cache. 
  380. $_comments = array(); 
  381. foreach ( $comment_ids as $comment_id ) { 
  382. if ( $_comment = get_comment( $comment_id ) ) { 
  383. $_comments[] = $_comment; 
  384.  
  385. // Prime comment post caches. 
  386. if ( $this->query_vars['update_comment_post_cache'] ) { 
  387. $comment_post_ids = array(); 
  388. foreach ( $_comments as $_comment ) { 
  389. $comment_post_ids[] = $_comment->comment_post_ID; 
  390.  
  391. _prime_post_caches( $comment_post_ids, false, false ); 
  392.  
  393. /** 
  394. * Filters the comment query results. 
  395. * @since 3.1.0 
  396. * @param array $results An array of comments. 
  397. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. 
  398. */ 
  399. $_comments = apply_filters_ref_array( 'the_comments', array( $_comments, &$this ) ); 
  400.  
  401. // Convert to WP_Comment instances 
  402. $comments = array_map( 'get_comment', $_comments ); 
  403.  
  404. if ( $this->query_vars['hierarchical'] ) { 
  405. $comments = $this->fill_descendants( $comments ); 
  406.  
  407. $this->comments = $comments; 
  408. return $this->comments; 
  409.  
  410. /** 
  411. * Used internally to get a list of comment IDs matching the query vars. 
  412. * @since 4.4.0 
  413. * @access protected 
  414. * @global wpdb $wpdb WordPress database abstraction object. 
  415. */ 
  416. protected function get_comment_ids() { 
  417. global $wpdb; 
  418.  
  419. // Assemble clauses related to 'comment_approved'. 
  420. $approved_clauses = array(); 
  421.  
  422. // 'status' accepts an array or a comma-separated string. 
  423. $status_clauses = array(); 
  424. $statuses = $this->query_vars['status']; 
  425. if ( ! is_array( $statuses ) ) { 
  426. $statuses = preg_split( '/[\s, ]+/', $statuses ); 
  427.  
  428. // 'any' overrides other statuses. 
  429. if ( ! in_array( 'any', $statuses ) ) { 
  430. foreach ( $statuses as $status ) { 
  431. switch ( $status ) { 
  432. case 'hold' : 
  433. $status_clauses[] = "comment_approved = '0'"; 
  434. break; 
  435.  
  436. case 'approve' : 
  437. $status_clauses[] = "comment_approved = '1'"; 
  438. break; 
  439.  
  440. case 'all' : 
  441. case '' : 
  442. $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; 
  443. break; 
  444.  
  445. default : 
  446. $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); 
  447. break; 
  448.  
  449. if ( ! empty( $status_clauses ) ) { 
  450. $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )'; 
  451.  
  452. // User IDs or emails whose unapproved comments are included, regardless of $status. 
  453. if ( ! empty( $this->query_vars['include_unapproved'] ) ) { 
  454. $include_unapproved = $this->query_vars['include_unapproved']; 
  455.  
  456. // Accepts arrays or comma-separated strings. 
  457. if ( ! is_array( $include_unapproved ) ) { 
  458. $include_unapproved = preg_split( '/[\s, ]+/', $include_unapproved ); 
  459.  
  460. $unapproved_ids = $unapproved_emails = array(); 
  461. foreach ( $include_unapproved as $unapproved_identifier ) { 
  462. // Numeric values are assumed to be user ids. 
  463. if ( is_numeric( $unapproved_identifier ) ) { 
  464. $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier ); 
  465.  
  466. // Otherwise we match against email addresses. 
  467. } else { 
  468. $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier ); 
  469.  
  470. // Collapse comment_approved clauses into a single OR-separated clause. 
  471. if ( ! empty( $approved_clauses ) ) { 
  472. if ( 1 === count( $approved_clauses ) ) { 
  473. $this->sql_clauses['where']['approved'] = $approved_clauses[0]; 
  474. } else { 
  475. $this->sql_clauses['where']['approved'] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 
  476.  
  477. $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; 
  478.  
  479. // Disable ORDER BY with 'none', an empty array, or boolean false. 
  480. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { 
  481. $orderby = ''; 
  482. } elseif ( ! empty( $this->query_vars['orderby'] ) ) { 
  483. $ordersby = is_array( $this->query_vars['orderby'] ) ? 
  484. $this->query_vars['orderby'] : 
  485. preg_split( '/[, \s]/', $this->query_vars['orderby'] ); 
  486.  
  487. $orderby_array = array(); 
  488. $found_orderby_comment_ID = false; 
  489. foreach ( $ordersby as $_key => $_value ) { 
  490. if ( ! $_value ) { 
  491. continue; 
  492.  
  493. if ( is_int( $_key ) ) { 
  494. $_orderby = $_value; 
  495. $_order = $order; 
  496. } else { 
  497. $_orderby = $_key; 
  498. $_order = $_value; 
  499.  
  500. if ( ! $found_orderby_comment_ID && in_array( $_orderby, array( 'comment_ID', 'comment__in' ) ) ) { 
  501. $found_orderby_comment_ID = true; 
  502.  
  503. $parsed = $this->parse_orderby( $_orderby ); 
  504.  
  505. if ( ! $parsed ) { 
  506. continue; 
  507.  
  508. if ( 'comment__in' === $_orderby ) { 
  509. $orderby_array[] = $parsed; 
  510. continue; 
  511.  
  512. $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 
  513.  
  514. // If no valid clauses were found, order by comment_date_gmt. 
  515. if ( empty( $orderby_array ) ) { 
  516. $orderby_array[] = "$wpdb->comments.comment_date_gmt $order"; 
  517.  
  518. // To ensure determinate sorting, always include a comment_ID clause. 
  519. if ( ! $found_orderby_comment_ID ) { 
  520. $comment_ID_order = ''; 
  521.  
  522. // Inherit order from comment_date or comment_date_gmt, if available. 
  523. foreach ( $orderby_array as $orderby_clause ) { 
  524. if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) { 
  525. $comment_ID_order = $match[1]; 
  526. break; 
  527.  
  528. // If no date-related order is available, use the date from the first available clause. 
  529. if ( ! $comment_ID_order ) { 
  530. foreach ( $orderby_array as $orderby_clause ) { 
  531. if ( false !== strpos( 'ASC', $orderby_clause ) ) { 
  532. $comment_ID_order = 'ASC'; 
  533. } else { 
  534. $comment_ID_order = 'DESC'; 
  535.  
  536. break; 
  537.  
  538. // Default to DESC. 
  539. if ( ! $comment_ID_order ) { 
  540. $comment_ID_order = 'DESC'; 
  541.  
  542. $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order"; 
  543.  
  544. $orderby = implode( ', ', $orderby_array ); 
  545. } else { 
  546. $orderby = "$wpdb->comments.comment_date_gmt $order"; 
  547.  
  548. $number = absint( $this->query_vars['number'] ); 
  549. $offset = absint( $this->query_vars['offset'] ); 
  550.  
  551. if ( ! empty( $number ) ) { 
  552. if ( $offset ) { 
  553. $limits = 'LIMIT ' . $offset . ', ' . $number; 
  554. } else { 
  555. $limits = 'LIMIT ' . $number; 
  556.  
  557. if ( $this->query_vars['count'] ) { 
  558. $fields = 'COUNT(*)'; 
  559. } else { 
  560. $fields = "$wpdb->comments.comment_ID"; 
  561.  
  562. $post_id = absint( $this->query_vars['post_id'] ); 
  563. if ( ! empty( $post_id ) ) { 
  564. $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id ); 
  565.  
  566. // Parse comment IDs for an IN clause. 
  567. if ( ! empty( $this->query_vars['comment__in'] ) ) { 
  568. $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; 
  569.  
  570. // Parse comment IDs for a NOT IN clause. 
  571. if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 
  572. $this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; 
  573.  
  574. // Parse comment parent IDs for an IN clause. 
  575. if ( ! empty( $this->query_vars['parent__in'] ) ) { 
  576. $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )'; 
  577.  
  578. // Parse comment parent IDs for a NOT IN clause. 
  579. if ( ! empty( $this->query_vars['parent__not_in'] ) ) { 
  580. $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )'; 
  581.  
  582. // Parse comment post IDs for an IN clause. 
  583. if ( ! empty( $this->query_vars['post__in'] ) ) { 
  584. $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; 
  585.  
  586. // Parse comment post IDs for a NOT IN clause. 
  587. if ( ! empty( $this->query_vars['post__not_in'] ) ) { 
  588. $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )'; 
  589.  
  590. if ( '' !== $this->query_vars['author_email'] ) { 
  591. $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); 
  592.  
  593. if ( '' !== $this->query_vars['author_url'] ) { 
  594. $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] ); 
  595.  
  596. if ( '' !== $this->query_vars['karma'] ) { 
  597. $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); 
  598.  
  599. // Filtering by comment_type: 'type', 'type__in', 'type__not_in'. 
  600. $raw_types = array( 
  601. 'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),  
  602. 'NOT IN' => (array) $this->query_vars['type__not_in'],  
  603. ); 
  604.  
  605. $comment_types = array(); 
  606. foreach ( $raw_types as $operator => $_raw_types ) { 
  607. $_raw_types = array_unique( $_raw_types ); 
  608.  
  609. foreach ( $_raw_types as $type ) { 
  610. switch ( $type ) { 
  611. // An empty translates to 'all', for backward compatibility 
  612. case '': 
  613. case 'all' : 
  614. break; 
  615.  
  616. case 'comment': 
  617. case 'comments': 
  618. $comment_types[ $operator ][] = "''"; 
  619. break; 
  620.  
  621. case 'pings': 
  622. $comment_types[ $operator ][] = "'pingback'"; 
  623. $comment_types[ $operator ][] = "'trackback'"; 
  624. break; 
  625.  
  626. default: 
  627. $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type ); 
  628. break; 
  629.  
  630. if ( ! empty( $comment_types[ $operator ] ) ) { 
  631. $types_sql = implode( ', ', $comment_types[ $operator ] ); 
  632. $this->sql_clauses['where']['comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)"; 
  633.  
  634. $parent = $this->query_vars['parent']; 
  635. if ( $this->query_vars['hierarchical'] && ! $parent ) { 
  636. $parent = 0; 
  637.  
  638. if ( '' !== $parent ) { 
  639. $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent ); 
  640.  
  641. if ( is_array( $this->query_vars['user_id'] ) ) { 
  642. $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ', ', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; 
  643. } elseif ( '' !== $this->query_vars['user_id'] ) { 
  644. $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] ); 
  645.  
  646. // Falsy search strings are ignored. 
  647. if ( strlen( $this->query_vars['search'] ) ) { 
  648. $search_sql = $this->get_search_sql( 
  649. $this->query_vars['search'],  
  650. array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) 
  651. ); 
  652.  
  653. // Strip leading 'AND'. 
  654. $this->sql_clauses['where']['search'] = preg_replace( '/^\s*AND\s*/', '', $search_sql ); 
  655.  
  656. // If any post-related query vars are passed, join the posts table. 
  657. $join_posts_table = false; 
  658. $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) ); 
  659. $post_fields = array_filter( $plucked ); 
  660.  
  661. if ( ! empty( $post_fields ) ) { 
  662. $join_posts_table = true; 
  663. foreach ( $post_fields as $field_name => $field_value ) { 
  664. // $field_value may be an array. 
  665. $esses = array_fill( 0, count( (array) $field_value ), '%s' ); 
  666. $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ', ', $esses ) . ')', $field_value ); 
  667.  
  668. // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'. 
  669. foreach ( array( 'post_status', 'post_type' ) as $field_name ) { 
  670. $q_values = array(); 
  671. if ( ! empty( $this->query_vars[ $field_name ] ) ) { 
  672. $q_values = $this->query_vars[ $field_name ]; 
  673. if ( ! is_array( $q_values ) ) { 
  674. $q_values = explode( ', ', $q_values ); 
  675.  
  676. // 'any' will cause the query var to be ignored. 
  677. if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) { 
  678. continue; 
  679.  
  680. $join_posts_table = true; 
  681.  
  682. $esses = array_fill( 0, count( $q_values ), '%s' ); 
  683. $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ', ', $esses ) . ")", $q_values ); 
  684.  
  685. // Comment author IDs for an IN clause. 
  686. if ( ! empty( $this->query_vars['author__in'] ) ) { 
  687. $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )'; 
  688.  
  689. // Comment author IDs for a NOT IN clause. 
  690. if ( ! empty( $this->query_vars['author__not_in'] ) ) { 
  691. $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )'; 
  692.  
  693. // Post author IDs for an IN clause. 
  694. if ( ! empty( $this->query_vars['post_author__in'] ) ) { 
  695. $join_posts_table = true; 
  696. $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )'; 
  697.  
  698. // Post author IDs for a NOT IN clause. 
  699. if ( ! empty( $this->query_vars['post_author__not_in'] ) ) { 
  700. $join_posts_table = true; 
  701. $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; 
  702.  
  703. $join = ''; 
  704.  
  705. if ( $join_posts_table ) { 
  706. $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; 
  707.  
  708. if ( ! empty( $this->meta_query_clauses ) ) { 
  709. $join .= $this->meta_query_clauses['join']; 
  710.  
  711. // Strip leading 'AND'. 
  712. $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); 
  713.  
  714. if ( ! $this->query_vars['count'] ) { 
  715. $groupby = "{$wpdb->comments}.comment_ID"; 
  716.  
  717. if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) { 
  718. $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' ); 
  719. $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); 
  720.  
  721. $where = implode( ' AND ', $this->sql_clauses['where'] ); 
  722.  
  723. $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); 
  724. /** 
  725. * Filters the comment query clauses. 
  726. * @since 3.1.0 
  727. * @param array $pieces A compacted array of comment query clauses. 
  728. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. 
  729. */ 
  730. $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 
  731.  
  732. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; 
  733. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; 
  734. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 
  735. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 
  736. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 
  737. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; 
  738.  
  739. $this->filtered_where_clause = $where; 
  740.  
  741. if ( $where ) { 
  742. $where = 'WHERE ' . $where; 
  743.  
  744. if ( $groupby ) { 
  745. $groupby = 'GROUP BY ' . $groupby; 
  746.  
  747. if ( $orderby ) { 
  748. $orderby = "ORDER BY $orderby"; 
  749.  
  750. $found_rows = ''; 
  751. if ( ! $this->query_vars['no_found_rows'] ) { 
  752. $found_rows = 'SQL_CALC_FOUND_ROWS'; 
  753.  
  754. $this->sql_clauses['select'] = "SELECT $found_rows $fields"; 
  755. $this->sql_clauses['from'] = "FROM $wpdb->comments $join"; 
  756. $this->sql_clauses['groupby'] = $groupby; 
  757. $this->sql_clauses['orderby'] = $orderby; 
  758. $this->sql_clauses['limits'] = $limits; 
  759.  
  760. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; 
  761.  
  762. if ( $this->query_vars['count'] ) { 
  763. return intval( $wpdb->get_var( $this->request ) ); 
  764. } else { 
  765. $comment_ids = $wpdb->get_col( $this->request ); 
  766. return array_map( 'intval', $comment_ids ); 
  767.  
  768. /** 
  769. * Populates found_comments and max_num_pages properties for the current 
  770. * query if the limit clause was used. 
  771. * @since 4.6.0 
  772. * @access private 
  773. * @global wpdb $wpdb WordPress database abstraction object. 
  774. */ 
  775. private function set_found_comments() { 
  776. global $wpdb; 
  777.  
  778. if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { 
  779. /** 
  780. * Filters the query used to retrieve found comment count. 
  781. * @since 4.4.0 
  782. * @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'. 
  783. * @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance. 
  784. */ 
  785. $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this ); 
  786.  
  787. $this->found_comments = (int) $wpdb->get_var( $found_comments_query ); 
  788.  
  789. /** 
  790. * Fetch descendants for located comments. 
  791. * Instead of calling `get_children()` separately on each child comment, we do a single set of queries to fetch 
  792. * the descendant trees for all matched top-level comments. 
  793. * @since 4.4.0 
  794. * @param array $comments Array of top-level comments whose descendants should be filled in. 
  795. * @return array 
  796. */ 
  797. protected function fill_descendants( $comments ) { 
  798. global $wpdb; 
  799.  
  800. $levels = array( 
  801. 0 => wp_list_pluck( $comments, 'comment_ID' ),  
  802. ); 
  803.  
  804. /** 
  805. * The WHERE clause for the descendant query is the same as for the top-level 
  806. * query, minus the `parent`, `parent__in`, and `parent__not_in` sub-clauses. 
  807. */ 
  808. $_where = $this->filtered_where_clause; 
  809. $exclude_keys = array( 'parent', 'parent__in', 'parent__not_in' ); 
  810. foreach ( $exclude_keys as $exclude_key ) { 
  811. if ( isset( $this->sql_clauses['where'][ $exclude_key ] ) ) { 
  812. $clause = $this->sql_clauses['where'][ $exclude_key ]; 
  813.  
  814. // Strip the clause as well as any adjacent ANDs. 
  815. $pattern = '|(?:AND)?\s*' . $clause . '\s*(?:AND)?|'; 
  816. $_where_parts = preg_split( $pattern, $_where ); 
  817.  
  818. // Remove empties. 
  819. $_where_parts = array_filter( array_map( 'trim', $_where_parts ) ); 
  820.  
  821. // Reassemble with an AND. 
  822. $_where = implode( ' AND ', $_where_parts ); 
  823.  
  824. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 
  825. $last_changed = wp_cache_get( 'last_changed', 'comment' ); 
  826. if ( ! $last_changed ) { 
  827. $last_changed = microtime(); 
  828. wp_cache_set( 'last_changed', $last_changed, 'comment' ); 
  829.  
  830. // Fetch an entire level of the descendant tree at a time. 
  831. $level = 0; 
  832. do { 
  833. // Parent-child relationships may be cached. Only query for those that are not. 
  834. $child_ids = $uncached_parent_ids = array(); 
  835. $_parent_ids = $levels[ $level ]; 
  836. foreach ( $_parent_ids as $parent_id ) { 
  837. $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; 
  838. $parent_child_ids = wp_cache_get( $cache_key, 'comment' ); 
  839. if ( false !== $parent_child_ids ) { 
  840. $child_ids = array_merge( $child_ids, $parent_child_ids ); 
  841. } else { 
  842. $uncached_parent_ids[] = $parent_id; 
  843.  
  844. if ( $uncached_parent_ids ) { 
  845. $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ', ', array_map( 'intval', $uncached_parent_ids ) ) . ')'; 
  846. $level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" ); 
  847.  
  848. // Cache parent-child relationships. 
  849. $parent_map = array_fill_keys( $uncached_parent_ids, array() ); 
  850. foreach ( $level_comments as $level_comment ) { 
  851. $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID; 
  852. $child_ids[] = $level_comment->comment_ID; 
  853.  
  854. foreach ( $parent_map as $parent_id => $children ) { 
  855. $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; 
  856. wp_cache_set( $cache_key, $children, 'comment' ); 
  857.  
  858. $level++; 
  859. $levels[ $level ] = $child_ids; 
  860. } while ( $child_ids ); 
  861.  
  862. // Prime comment caches for non-top-level comments. 
  863. $descendant_ids = array(); 
  864. for ( $i = 1; $i < count( $levels ); $i++ ) { 
  865. $descendant_ids = array_merge( $descendant_ids, $levels[ $i ] ); 
  866.  
  867. _prime_comment_caches( $descendant_ids, $this->query_vars['update_comment_meta_cache'] ); 
  868.  
  869. // Assemble a flat array of all comments + descendants. 
  870. $all_comments = $comments; 
  871. foreach ( $descendant_ids as $descendant_id ) { 
  872. $all_comments[] = get_comment( $descendant_id ); 
  873.  
  874. // If a threaded representation was requested, build the tree. 
  875. if ( 'threaded' === $this->query_vars['hierarchical'] ) { 
  876. $threaded_comments = $ref = array(); 
  877. foreach ( $all_comments as $k => $c ) { 
  878. $_c = get_comment( $c->comment_ID ); 
  879.  
  880. // If the comment isn't in the reference array, it goes in the top level of the thread. 
  881. if ( ! isset( $ref[ $c->comment_parent ] ) ) { 
  882. $threaded_comments[ $_c->comment_ID ] = $_c; 
  883. $ref[ $_c->comment_ID ] = $threaded_comments[ $_c->comment_ID ]; 
  884.  
  885. // Otherwise, set it as a child of its parent. 
  886. } else { 
  887.  
  888. $ref[ $_c->comment_parent ]->add_child( $_c ); 
  889. $ref[ $_c->comment_ID ] = $ref[ $_c->comment_parent ]->get_child( $_c->comment_ID ); 
  890.  
  891. // Set the 'populated_children' flag, to ensure additional database queries aren't run. 
  892. foreach ( $ref as $_ref ) { 
  893. $_ref->populated_children( true ); 
  894.  
  895. $comments = $threaded_comments; 
  896. } else { 
  897. $comments = $all_comments; 
  898.  
  899. return $comments; 
  900.  
  901. /** 
  902. * Used internally to generate an SQL string for searching across multiple columns 
  903. * @since 3.1.0 
  904. * @access protected 
  905. * @global wpdb $wpdb WordPress database abstraction object. 
  906. * @param string $string 
  907. * @param array $cols 
  908. * @return string 
  909. */ 
  910. protected function get_search_sql( $string, $cols ) { 
  911. global $wpdb; 
  912.  
  913. $like = '%' . $wpdb->esc_like( $string ) . '%'; 
  914.  
  915. $searches = array(); 
  916. foreach ( $cols as $col ) { 
  917. $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); 
  918.  
  919. return ' AND (' . implode(' OR ', $searches) . ')'; 
  920.  
  921. /** 
  922. * Parse and sanitize 'orderby' keys passed to the comment query. 
  923. * @since 4.2.0 
  924. * @access protected 
  925. * @global wpdb $wpdb WordPress database abstraction object. 
  926. * @param string $orderby Alias for the field to order by. 
  927. * @return string|false Value to used in the ORDER clause. False otherwise. 
  928. */ 
  929. protected function parse_orderby( $orderby ) { 
  930. global $wpdb; 
  931.  
  932. $allowed_keys = array( 
  933. 'comment_agent',  
  934. 'comment_approved',  
  935. 'comment_author',  
  936. 'comment_author_email',  
  937. 'comment_author_IP',  
  938. 'comment_author_url',  
  939. 'comment_content',  
  940. 'comment_date',  
  941. 'comment_date_gmt',  
  942. 'comment_ID',  
  943. 'comment_karma',  
  944. 'comment_parent',  
  945. 'comment_post_ID',  
  946. 'comment_type',  
  947. 'user_id',  
  948. ); 
  949.  
  950. if ( ! empty( $this->query_vars['meta_key'] ) ) { 
  951. $allowed_keys[] = $this->query_vars['meta_key']; 
  952. $allowed_keys[] = 'meta_value'; 
  953. $allowed_keys[] = 'meta_value_num'; 
  954.  
  955. $meta_query_clauses = $this->meta_query->get_clauses(); 
  956. if ( $meta_query_clauses ) { 
  957. $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) ); 
  958.  
  959. $parsed = false; 
  960. if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) { 
  961. $parsed = "$wpdb->commentmeta.meta_value"; 
  962. } elseif ( $orderby == 'meta_value_num' ) { 
  963. $parsed = "$wpdb->commentmeta.meta_value+0"; 
  964. } elseif ( $orderby == 'comment__in' ) { 
  965. $comment__in = implode( ', ', array_map( 'absint', $this->query_vars['comment__in'] ) ); 
  966. $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )"; 
  967. } elseif ( in_array( $orderby, $allowed_keys ) ) { 
  968.  
  969. if ( isset( $meta_query_clauses[ $orderby ] ) ) { 
  970. $meta_clause = $meta_query_clauses[ $orderby ]; 
  971. $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); 
  972. } else { 
  973. $parsed = "$wpdb->comments.$orderby"; 
  974.  
  975. return $parsed; 
  976.  
  977. /** 
  978. * Parse an 'order' query variable and cast it to ASC or DESC as necessary. 
  979. * @since 4.2.0 
  980. * @access protected 
  981. * @param string $order The 'order' query variable. 
  982. * @return string The sanitized 'order' query variable. 
  983. */ 
  984. protected function parse_order( $order ) { 
  985. if ( ! is_string( $order ) || empty( $order ) ) { 
  986. return 'DESC'; 
  987.  
  988. if ( 'ASC' === strtoupper( $order ) ) { 
  989. return 'ASC'; 
  990. } else { 
  991. return 'DESC';