暫無描述

class-wp-comment-query.php 44KB

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