class-wp-comment-query.php 43 KB

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