[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-comment-query.php (source)

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


Generated: Sat Sep 26 01:00:03 2020 Cross-referenced by PHPXref 0.7.1