/wp-includes/class-wp-comment-query.php

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