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 empty (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.  
  344.  
  345. $cache_key = "get_comments:$key:$last_changed"; 
  346. $cache_value = wp_cache_get( $cache_key, 'comment' ); 
  347. if ( false === $cache_value ) { 
  348. $comment_ids = $this->get_comment_ids(); 
  349. if ( $comment_ids ) { 
  350. $this->set_found_comments(); 
  351.  
  352. $cache_value = array( 
  353. 'comment_ids' => $comment_ids,  
  354. 'found_comments' => $this->found_comments,  
  355. ); 
  356. wp_cache_add( $cache_key, $cache_value, 'comment' ); 
  357. } else { 
  358. $comment_ids = $cache_value['comment_ids']; 
  359. $this->found_comments = $cache_value['found_comments']; 
  360.  
  361. if ( $this->found_comments && $this->query_vars['number'] ) { 
  362. $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] ); 
  363.  
  364. // If querying for a count only, there's nothing more to do. 
  365. if ( $this->query_vars['count'] ) { 
  366. // $comment_ids is actually a count in this case. 
  367. return intval( $comment_ids ); 
  368.  
  369. $comment_ids = array_map( 'intval', $comment_ids ); 
  370.  
  371. if ( 'ids' == $this->query_vars['fields'] ) { 
  372. $this->comments = $comment_ids; 
  373. return $this->comments; 
  374.  
  375. _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] ); 
  376.  
  377. // Fetch full comment objects from the primed cache. 
  378. $_comments = array(); 
  379. foreach ( $comment_ids as $comment_id ) { 
  380. if ( $_comment = get_comment( $comment_id ) ) { 
  381. $_comments[] = $_comment; 
  382.  
  383. // Prime comment post caches. 
  384. if ( $this->query_vars['update_comment_post_cache'] ) { 
  385. $comment_post_ids = array(); 
  386. foreach ( $_comments as $_comment ) { 
  387. $comment_post_ids[] = $_comment->comment_post_ID; 
  388.  
  389. _prime_post_caches( $comment_post_ids, false, false ); 
  390.  
  391. /** 
  392. * Filters the comment query results. 
  393. * @since 3.1.0 
  394. * @param array $results An array of comments. 
  395. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. 
  396. */ 
  397. $_comments = apply_filters_ref_array( 'the_comments', array( $_comments, &$this ) ); 
  398.  
  399. // Convert to WP_Comment instances 
  400. $comments = array_map( 'get_comment', $_comments ); 
  401.  
  402. if ( $this->query_vars['hierarchical'] ) { 
  403. $comments = $this->fill_descendants( $comments ); 
  404.  
  405. $this->comments = $comments; 
  406. return $this->comments; 
  407.  
  408. /** 
  409. * Used internally to get a list of comment IDs matching the query vars. 
  410. * @since 4.4.0 
  411. * @access protected 
  412. * @global wpdb $wpdb WordPress database abstraction object. 
  413. */ 
  414. protected function get_comment_ids() { 
  415. global $wpdb; 
  416.  
  417. // Assemble clauses related to 'comment_approved'. 
  418. $approved_clauses = array(); 
  419.  
  420. // 'status' accepts an array or a comma-separated string. 
  421. $status_clauses = array(); 
  422. $statuses = $this->query_vars['status']; 
  423. if ( ! is_array( $statuses ) ) { 
  424. $statuses = preg_split( '/[\s, ]+/', $statuses ); 
  425.  
  426. // 'any' overrides other statuses. 
  427. if ( ! in_array( 'any', $statuses ) ) { 
  428. foreach ( $statuses as $status ) { 
  429. switch ( $status ) { 
  430. case 'hold' : 
  431. $status_clauses[] = "comment_approved = '0'"; 
  432. break; 
  433.  
  434. case 'approve' : 
  435. $status_clauses[] = "comment_approved = '1'"; 
  436. break; 
  437.  
  438. case 'all' : 
  439. case '' : 
  440. $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; 
  441. break; 
  442.  
  443. default : 
  444. $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); 
  445. break; 
  446.  
  447. if ( ! empty( $status_clauses ) ) { 
  448. $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )'; 
  449.  
  450. // User IDs or emails whose unapproved comments are included, regardless of $status. 
  451. if ( ! empty( $this->query_vars['include_unapproved'] ) ) { 
  452. $include_unapproved = $this->query_vars['include_unapproved']; 
  453.  
  454. // Accepts arrays or comma-separated strings. 
  455. if ( ! is_array( $include_unapproved ) ) { 
  456. $include_unapproved = preg_split( '/[\s, ]+/', $include_unapproved ); 
  457.  
  458. $unapproved_ids = $unapproved_emails = array(); 
  459. foreach ( $include_unapproved as $unapproved_identifier ) { 
  460. // Numeric values are assumed to be user ids. 
  461. if ( is_numeric( $unapproved_identifier ) ) { 
  462. $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier ); 
  463.  
  464. // Otherwise we match against email addresses. 
  465. } else { 
  466. $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier ); 
  467.  
  468. // Collapse comment_approved clauses into a single OR-separated clause. 
  469. if ( ! empty( $approved_clauses ) ) { 
  470. if ( 1 === count( $approved_clauses ) ) { 
  471. $this->sql_clauses['where']['approved'] = $approved_clauses[0]; 
  472. } else { 
  473. $this->sql_clauses['where']['approved'] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 
  474.  
  475. $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; 
  476.  
  477. // Disable ORDER BY with 'none', an empty array, or boolean false. 
  478. if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) { 
  479. $orderby = ''; 
  480. } elseif ( ! empty( $this->query_vars['orderby'] ) ) { 
  481. $ordersby = is_array( $this->query_vars['orderby'] ) ? 
  482. $this->query_vars['orderby'] : 
  483. preg_split( '/[, \s]/', $this->query_vars['orderby'] ); 
  484.  
  485. $orderby_array = array(); 
  486. $found_orderby_comment_ID = false; 
  487. foreach ( $ordersby as $_key => $_value ) { 
  488. if ( ! $_value ) { 
  489. continue; 
  490.  
  491. if ( is_int( $_key ) ) { 
  492. $_orderby = $_value; 
  493. $_order = $order; 
  494. } else { 
  495. $_orderby = $_key; 
  496. $_order = $_value; 
  497.  
  498. if ( ! $found_orderby_comment_ID && in_array( $_orderby, array( 'comment_ID', 'comment__in' ) ) ) { 
  499. $found_orderby_comment_ID = true; 
  500.  
  501. $parsed = $this->parse_orderby( $_orderby ); 
  502.  
  503. if ( ! $parsed ) { 
  504. continue; 
  505.  
  506. if ( 'comment__in' === $_orderby ) { 
  507. $orderby_array[] = $parsed; 
  508. continue; 
  509.  
  510. $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); 
  511.  
  512. // If no valid clauses were found, order by comment_date_gmt. 
  513. if ( empty( $orderby_array ) ) { 
  514. $orderby_array[] = "$wpdb->comments.comment_date_gmt $order"; 
  515.  
  516. // To ensure determinate sorting, always include a comment_ID clause. 
  517. if ( ! $found_orderby_comment_ID ) { 
  518. $comment_ID_order = ''; 
  519.  
  520. // Inherit order from comment_date or comment_date_gmt, if available. 
  521. foreach ( $orderby_array as $orderby_clause ) { 
  522. if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) { 
  523. $comment_ID_order = $match[1]; 
  524. break; 
  525.  
  526. // If no date-related order is available, use the date from the first available clause. 
  527. if ( ! $comment_ID_order ) { 
  528. foreach ( $orderby_array as $orderby_clause ) { 
  529. if ( false !== strpos( 'ASC', $orderby_clause ) ) { 
  530. $comment_ID_order = 'ASC'; 
  531. } else { 
  532. $comment_ID_order = 'DESC'; 
  533.  
  534. break; 
  535.  
  536. // Default to DESC. 
  537. if ( ! $comment_ID_order ) { 
  538. $comment_ID_order = 'DESC'; 
  539.  
  540. $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order"; 
  541.  
  542. $orderby = implode( ', ', $orderby_array ); 
  543. } else { 
  544. $orderby = "$wpdb->comments.comment_date_gmt $order"; 
  545.  
  546. $number = absint( $this->query_vars['number'] ); 
  547. $offset = absint( $this->query_vars['offset'] ); 
  548.  
  549. if ( ! empty( $number ) ) { 
  550. if ( $offset ) { 
  551. $limits = 'LIMIT ' . $offset . ', ' . $number; 
  552. } else { 
  553. $limits = 'LIMIT ' . $number; 
  554.  
  555. if ( $this->query_vars['count'] ) { 
  556. $fields = 'COUNT(*)'; 
  557. } else { 
  558. $fields = "$wpdb->comments.comment_ID"; 
  559.  
  560. $post_id = absint( $this->query_vars['post_id'] ); 
  561. if ( ! empty( $post_id ) ) { 
  562. $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id ); 
  563.  
  564. // Parse comment IDs for an IN clause. 
  565. if ( ! empty( $this->query_vars['comment__in'] ) ) { 
  566. $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; 
  567.  
  568. // Parse comment IDs for a NOT IN clause. 
  569. if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 
  570. $this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ', ', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; 
  571.  
  572. // Parse comment parent IDs for an IN clause. 
  573. if ( ! empty( $this->query_vars['parent__in'] ) ) { 
  574. $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )'; 
  575.  
  576. // Parse comment parent IDs for a NOT IN clause. 
  577. if ( ! empty( $this->query_vars['parent__not_in'] ) ) { 
  578. $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )'; 
  579.  
  580. // Parse comment post IDs for an IN clause. 
  581. if ( ! empty( $this->query_vars['post__in'] ) ) { 
  582. $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; 
  583.  
  584. // Parse comment post IDs for a NOT IN clause. 
  585. if ( ! empty( $this->query_vars['post__not_in'] ) ) { 
  586. $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )'; 
  587.  
  588. if ( '' !== $this->query_vars['author_email'] ) { 
  589. $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); 
  590.  
  591. if ( '' !== $this->query_vars['author_url'] ) { 
  592. $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] ); 
  593.  
  594. if ( '' !== $this->query_vars['karma'] ) { 
  595. $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); 
  596.  
  597. // Filtering by comment_type: 'type', 'type__in', 'type__not_in'. 
  598. $raw_types = array( 
  599. 'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ),  
  600. 'NOT IN' => (array) $this->query_vars['type__not_in'],  
  601. ); 
  602.  
  603. $comment_types = array(); 
  604. foreach ( $raw_types as $operator => $_raw_types ) { 
  605. $_raw_types = array_unique( $_raw_types ); 
  606.  
  607. foreach ( $_raw_types as $type ) { 
  608. switch ( $type ) { 
  609. // An empty translates to 'all', for backward compatibility 
  610. case '': 
  611. case 'all' : 
  612. break; 
  613.  
  614. case 'comment': 
  615. case 'comments': 
  616. $comment_types[ $operator ][] = "''"; 
  617. break; 
  618.  
  619. case 'pings': 
  620. $comment_types[ $operator ][] = "'pingback'"; 
  621. $comment_types[ $operator ][] = "'trackback'"; 
  622. break; 
  623.  
  624. default: 
  625. $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type ); 
  626. break; 
  627.  
  628. if ( ! empty( $comment_types[ $operator ] ) ) { 
  629. $types_sql = implode( ', ', $comment_types[ $operator ] ); 
  630. $this->sql_clauses['where']['comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)"; 
  631.  
  632. $parent = $this->query_vars['parent']; 
  633. if ( $this->query_vars['hierarchical'] && ! $parent ) { 
  634. $parent = 0; 
  635.  
  636. if ( '' !== $parent ) { 
  637. $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent ); 
  638.  
  639. if ( is_array( $this->query_vars['user_id'] ) ) { 
  640. $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ', ', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; 
  641. } elseif ( '' !== $this->query_vars['user_id'] ) { 
  642. $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] ); 
  643.  
  644. // Falsy search strings are ignored. 
  645. if ( strlen( $this->query_vars['search'] ) ) { 
  646. $search_sql = $this->get_search_sql( 
  647. $this->query_vars['search'],  
  648. array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) 
  649. ); 
  650.  
  651. // Strip leading 'AND'. 
  652. $this->sql_clauses['where']['search'] = preg_replace( '/^\s*AND\s*/', '', $search_sql ); 
  653.  
  654. // If any post-related query vars are passed, join the posts table. 
  655. $join_posts_table = false; 
  656. $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) ); 
  657. $post_fields = array_filter( $plucked ); 
  658.  
  659. if ( ! empty( $post_fields ) ) { 
  660. $join_posts_table = true; 
  661. foreach ( $post_fields as $field_name => $field_value ) { 
  662. // $field_value may be an array. 
  663. $esses = array_fill( 0, count( (array) $field_value ), '%s' ); 
  664. $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ', ', $esses ) . ')', $field_value ); 
  665.  
  666. // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'. 
  667. foreach ( array( 'post_status', 'post_type' ) as $field_name ) { 
  668. $q_values = array(); 
  669. if ( ! empty( $this->query_vars[ $field_name ] ) ) { 
  670. $q_values = $this->query_vars[ $field_name ]; 
  671. if ( ! is_array( $q_values ) ) { 
  672. $q_values = explode( ', ', $q_values ); 
  673.  
  674. // 'any' will cause the query var to be ignored. 
  675. if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) { 
  676. continue; 
  677.  
  678. $join_posts_table = true; 
  679.  
  680. $esses = array_fill( 0, count( $q_values ), '%s' ); 
  681. $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ', ', $esses ) . ")", $q_values ); 
  682.  
  683. // Comment author IDs for an IN clause. 
  684. if ( ! empty( $this->query_vars['author__in'] ) ) { 
  685. $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )'; 
  686.  
  687. // Comment author IDs for a NOT IN clause. 
  688. if ( ! empty( $this->query_vars['author__not_in'] ) ) { 
  689. $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )'; 
  690.  
  691. // Post author IDs for an IN clause. 
  692. if ( ! empty( $this->query_vars['post_author__in'] ) ) { 
  693. $join_posts_table = true; 
  694. $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )'; 
  695.  
  696. // Post author IDs for a NOT IN clause. 
  697. if ( ! empty( $this->query_vars['post_author__not_in'] ) ) { 
  698. $join_posts_table = true; 
  699. $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ', ', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; 
  700.  
  701. $join = ''; 
  702.  
  703. if ( $join_posts_table ) { 
  704. $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; 
  705.  
  706. if ( ! empty( $this->meta_query_clauses ) ) { 
  707. $join .= $this->meta_query_clauses['join']; 
  708.  
  709. // Strip leading 'AND'. 
  710. $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); 
  711.  
  712. if ( ! $this->query_vars['count'] ) { 
  713. $groupby = "{$wpdb->comments}.comment_ID"; 
  714.  
  715. if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) { 
  716. $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' ); 
  717. $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() ); 
  718.  
  719. $where = implode( ' AND ', $this->sql_clauses['where'] ); 
  720.  
  721. $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' ); 
  722. /** 
  723. * Filters the comment query clauses. 
  724. * @since 3.1.0 
  725. * @param array $pieces A compacted array of comment query clauses. 
  726. * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. 
  727. */ 
  728. $clauses = apply_filters_ref_array( 'comments_clauses', array( compact( $pieces ), &$this ) ); 
  729.  
  730. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; 
  731. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; 
  732. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; 
  733. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; 
  734. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 
  735. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; 
  736.  
  737. $this->filtered_where_clause = $where; 
  738.  
  739. if ( $where ) { 
  740. $where = 'WHERE ' . $where; 
  741.  
  742. if ( $groupby ) { 
  743. $groupby = 'GROUP BY ' . $groupby; 
  744.  
  745. if ( $orderby ) { 
  746. $orderby = "ORDER BY $orderby"; 
  747.  
  748. $found_rows = ''; 
  749. if ( ! $this->query_vars['no_found_rows'] ) { 
  750. $found_rows = 'SQL_CALC_FOUND_ROWS'; 
  751.  
  752. $this->sql_clauses['select'] = "SELECT $found_rows $fields"; 
  753. $this->sql_clauses['from'] = "FROM $wpdb->comments $join"; 
  754. $this->sql_clauses['groupby'] = $groupby; 
  755. $this->sql_clauses['orderby'] = $orderby; 
  756. $this->sql_clauses['limits'] = $limits; 
  757.  
  758. $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}"; 
  759.  
  760. if ( $this->query_vars['count'] ) { 
  761. return intval( $wpdb->get_var( $this->request ) ); 
  762. } else { 
  763. $comment_ids = $wpdb->get_col( $this->request ); 
  764. return array_map( 'intval', $comment_ids ); 
  765.  
  766. /** 
  767. * Populates found_comments and max_num_pages properties for the current 
  768. * query if the limit clause was used. 
  769. * @since 4.6.0 
  770. * @access private 
  771. * @global wpdb $wpdb WordPress database abstraction object. 
  772. */ 
  773. private function set_found_comments() { 
  774. global $wpdb; 
  775.  
  776. if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { 
  777. /** 
  778. * Filters the query used to retrieve found comment count. 
  779. * @since 4.4.0 
  780. * @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'. 
  781. * @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance. 
  782. */ 
  783. $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this ); 
  784.  
  785. $this->found_comments = (int) $wpdb->get_var( $found_comments_query ); 
  786.  
  787. /** 
  788. * Fetch descendants for located comments. 
  789. * Instead of calling `get_children()` separately on each child comment, we do a single set of queries to fetch 
  790. * the descendant trees for all matched top-level comments. 
  791. * @since 4.4.0 
  792. * @param array $comments Array of top-level comments whose descendants should be filled in. 
  793. * @return array 
  794. */ 
  795. protected function fill_descendants( $comments ) { 
  796. global $wpdb; 
  797.  
  798. $levels = array( 
  799. 0 => wp_list_pluck( $comments, 'comment_ID' ),  
  800. ); 
  801.  
  802. /** 
  803. * The WHERE clause for the descendant query is the same as for the top-level 
  804. * query, minus the `parent`, `parent__in`, and `parent__not_in` sub-clauses. 
  805. */ 
  806. $_where = $this->filtered_where_clause; 
  807. $exclude_keys = array( 'parent', 'parent__in', 'parent__not_in' ); 
  808. foreach ( $exclude_keys as $exclude_key ) { 
  809. if ( isset( $this->sql_clauses['where'][ $exclude_key ] ) ) { 
  810. $clause = $this->sql_clauses['where'][ $exclude_key ]; 
  811.  
  812. // Strip the clause as well as any adjacent ANDs. 
  813. $pattern = '|(?:AND)?\s*' . $clause . '\s*(?:AND)?|'; 
  814. $_where_parts = preg_split( $pattern, $_where ); 
  815.  
  816. // Remove empties. 
  817. $_where_parts = array_filter( array_map( 'trim', $_where_parts ) ); 
  818.  
  819. // Reassemble with an AND. 
  820. $_where = implode( ' AND ', $_where_parts ); 
  821.  
  822. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 
  823. $last_changed = wp_cache_get_last_changed( 'comment' ); 
  824.  
  825. // Fetch an entire level of the descendant tree at a time. 
  826. $level = 0; 
  827. do { 
  828. // Parent-child relationships may be cached. Only query for those that are not. 
  829. $child_ids = $uncached_parent_ids = array(); 
  830. $_parent_ids = $levels[ $level ]; 
  831. foreach ( $_parent_ids as $parent_id ) { 
  832. $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; 
  833. $parent_child_ids = wp_cache_get( $cache_key, 'comment' ); 
  834. if ( false !== $parent_child_ids ) { 
  835. $child_ids = array_merge( $child_ids, $parent_child_ids ); 
  836. } else { 
  837. $uncached_parent_ids[] = $parent_id; 
  838.  
  839. if ( $uncached_parent_ids ) { 
  840. // Fetch this level of comments. 
  841. $parent_query_args = $this->query_vars; 
  842. foreach ( $exclude_keys as $exclude_key ) { 
  843. $parent_query_args[ $exclude_key ] = ''; 
  844. $parent_query_args['parent__in'] = $uncached_parent_ids; 
  845. $parent_query_args['no_found_rows'] = true; 
  846. $parent_query_args['hierarchical'] = false; 
  847. $parent_query_args['offset'] = 0; 
  848. $parent_query_args['number'] = 0; 
  849.  
  850. $level_comments = get_comments( $parent_query_args ); 
  851.  
  852. // Cache parent-child relationships. 
  853. $parent_map = array_fill_keys( $uncached_parent_ids, array() ); 
  854. foreach ( $level_comments as $level_comment ) { 
  855. $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID; 
  856. $child_ids[] = $level_comment->comment_ID; 
  857.  
  858. foreach ( $parent_map as $parent_id => $children ) { 
  859. $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; 
  860. wp_cache_set( $cache_key, $children, 'comment' ); 
  861.  
  862. $level++; 
  863. $levels[ $level ] = $child_ids; 
  864. } while ( $child_ids ); 
  865.  
  866. // Prime comment caches for non-top-level comments. 
  867. $descendant_ids = array(); 
  868. for ( $i = 1, $c = count( $levels ); $i < $c; $i++ ) { 
  869. $descendant_ids = array_merge( $descendant_ids, $levels[ $i ] ); 
  870.  
  871. _prime_comment_caches( $descendant_ids, $this->query_vars['update_comment_meta_cache'] ); 
  872.  
  873. // Assemble a flat array of all comments + descendants. 
  874. $all_comments = $comments; 
  875. foreach ( $descendant_ids as $descendant_id ) { 
  876. $all_comments[] = get_comment( $descendant_id ); 
  877.  
  878. // If a threaded representation was requested, build the tree. 
  879. if ( 'threaded' === $this->query_vars['hierarchical'] ) { 
  880. $threaded_comments = $ref = array(); 
  881. foreach ( $all_comments as $k => $c ) { 
  882. $_c = get_comment( $c->comment_ID ); 
  883.  
  884. // If the comment isn't in the reference array, it goes in the top level of the thread. 
  885. if ( ! isset( $ref[ $c->comment_parent ] ) ) { 
  886. $threaded_comments[ $_c->comment_ID ] = $_c; 
  887. $ref[ $_c->comment_ID ] = $threaded_comments[ $_c->comment_ID ]; 
  888.  
  889. // Otherwise, set it as a child of its parent. 
  890. } else { 
  891.  
  892. $ref[ $_c->comment_parent ]->add_child( $_c ); 
  893. $ref[ $_c->comment_ID ] = $ref[ $_c->comment_parent ]->get_child( $_c->comment_ID ); 
  894.  
  895. // Set the 'populated_children' flag, to ensure additional database queries aren't run. 
  896. foreach ( $ref as $_ref ) { 
  897. $_ref->populated_children( true ); 
  898.  
  899. $comments = $threaded_comments; 
  900. } else { 
  901. $comments = $all_comments; 
  902.  
  903. return $comments; 
  904.  
  905. /** 
  906. * Used internally to generate an SQL string for searching across multiple columns 
  907. * @since 3.1.0 
  908. * @access protected 
  909. * @global wpdb $wpdb WordPress database abstraction object. 
  910. * @param string $string 
  911. * @param array $cols 
  912. * @return string 
  913. */ 
  914. protected function get_search_sql( $string, $cols ) { 
  915. global $wpdb; 
  916.  
  917. $like = '%' . $wpdb->esc_like( $string ) . '%'; 
  918.  
  919. $searches = array(); 
  920. foreach ( $cols as $col ) { 
  921. $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); 
  922.  
  923. return ' AND (' . implode(' OR ', $searches) . ')'; 
  924.  
  925. /** 
  926. * Parse and sanitize 'orderby' keys passed to the comment query. 
  927. * @since 4.2.0 
  928. * @access protected 
  929. * @global wpdb $wpdb WordPress database abstraction object. 
  930. * @param string $orderby Alias for the field to order by. 
  931. * @return string|false Value to used in the ORDER clause. False otherwise. 
  932. */ 
  933. protected function parse_orderby( $orderby ) { 
  934. global $wpdb; 
  935.  
  936. $allowed_keys = array( 
  937. 'comment_agent',  
  938. 'comment_approved',  
  939. 'comment_author',  
  940. 'comment_author_email',  
  941. 'comment_author_IP',  
  942. 'comment_author_url',  
  943. 'comment_content',  
  944. 'comment_date',  
  945. 'comment_date_gmt',  
  946. 'comment_ID',  
  947. 'comment_karma',  
  948. 'comment_parent',  
  949. 'comment_post_ID',  
  950. 'comment_type',  
  951. 'user_id',  
  952. ); 
  953.  
  954. if ( ! empty( $this->query_vars['meta_key'] ) ) { 
  955. $allowed_keys[] = $this->query_vars['meta_key']; 
  956. $allowed_keys[] = 'meta_value'; 
  957. $allowed_keys[] = 'meta_value_num'; 
  958.  
  959. $meta_query_clauses = $this->meta_query->get_clauses(); 
  960. if ( $meta_query_clauses ) { 
  961. $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) ); 
  962.  
  963. $parsed = false; 
  964. if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) { 
  965. $parsed = "$wpdb->commentmeta.meta_value"; 
  966. } elseif ( $orderby == 'meta_value_num' ) { 
  967. $parsed = "$wpdb->commentmeta.meta_value+0"; 
  968. } elseif ( $orderby == 'comment__in' ) { 
  969. $comment__in = implode( ', ', array_map( 'absint', $this->query_vars['comment__in'] ) ); 
  970. $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )"; 
  971. } elseif ( in_array( $orderby, $allowed_keys ) ) { 
  972.  
  973. if ( isset( $meta_query_clauses[ $orderby ] ) ) { 
  974. $meta_clause = $meta_query_clauses[ $orderby ]; 
  975. $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); 
  976. } else { 
  977. $parsed = "$wpdb->comments.$orderby"; 
  978.  
  979. return $parsed; 
  980.  
  981. /** 
  982. * Parse an 'order' query variable and cast it to ASC or DESC as necessary. 
  983. * @since 4.2.0 
  984. * @access protected 
  985. * @param string $order The 'order' query variable. 
  986. * @return string The sanitized 'order' query variable. 
  987. */ 
  988. protected function parse_order( $order ) { 
  989. if ( ! is_string( $order ) || empty( $order ) ) { 
  990. return 'DESC'; 
  991.  
  992. if ( 'ASC' === strtoupper( $order ) ) { 
  993. return 'ASC'; 
  994. } else { 
  995. return 'DESC';