285
300
* @return mixed|bool Return value of the callback, false otherwise.
287
302
public function __call( $name, $arguments ) {
288
return call_user_func_array( array( $this, $name ), $arguments );
303
if ( 'get_search_sql' === $name ) {
304
return call_user_func_array( array( $this, $name ), $arguments );
295
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
296
* 'post_author__not_in', 'author__in', 'author__not_in', 'post__in',
297
* 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'
298
* arguments to $query_vars.
300
* @param string|array $query_vars {
301
* Optional. Array or query string of comment query parameters.
312
* Sets up the comment query, based on the query vars passed.
317
* @param string|array $query {
318
* Optional. Array or query string of comment query parameters. Default empty.
303
320
* @type string $author_email Comment author email address. Default empty.
304
321
* @type array $author__in Array of author IDs to include comments for. Default empty.
319
336
* @type string $meta_value Include comments with a matching comment meta value. Requires
320
337
* `$meta_key` to be set. Default empty.
321
338
* @type array $meta_query Meta query clauses to limit retrieved comments by.
322
* See {@see WP_Meta_Query}. Default empty.
339
* See WP_Meta_Query. Default empty.
323
340
* @type int $number Maximum number of comments to retrieve. Default null (no limit).
324
341
* @type int $offset Number of comments to offset the query. Used to build LIMIT clause.
326
* @type string|array $orderby Comment status or array of statuses. Accepts 'comment_agent',
327
* 'comment_approved', 'comment_author', 'comment_author_email',
328
* 'comment_author_IP', 'comment_author_url', 'comment_content',
329
* 'comment_date', 'comment_date_gmt', 'comment_ID', 'comment_karma',
343
* @type string|array $orderby Comment status or array of statuses. To use 'meta_value' or
344
* 'meta_value_num', `$meta_key` must also be defined. To sort by
345
* a specific `$meta_query` clause, use that clause's array key.
346
* Accepts 'comment_agent', 'comment_approved', 'comment_author',
347
* 'comment_author_email', 'comment_author_IP',
348
* 'comment_author_url', 'comment_content', 'comment_date',
349
* 'comment_date_gmt', 'comment_ID', 'comment_karma',
330
350
* 'comment_parent', 'comment_post_ID', 'comment_type', 'user_id',
331
* 'meta_value', 'meta_value_num', or value of $meta_key.
332
* Also accepts false, empty array, or 'none' to disable `ORDER BY`
333
* clause. Default: 'comment_date_gmt'.
351
* 'meta_value', 'meta_value_num', the value of $meta_key, and the
352
* array keys of `$meta_query`. Also accepts false, an empty array,
353
* or 'none' to disable `ORDER BY` clause.
354
* Default: 'comment_date_gmt'.
334
355
* @type string $order How to order retrieved comments. Accepts 'ASC', 'DESC'.
335
356
* Default: 'DESC'.
336
357
* @type int $parent Parent ID of comment to retrieve children of. Default empty.
400
419
'date_query' => null, // See WP_Date_Query
422
if ( ! empty( $query ) ) {
423
$this->query( $query );
428
* Parse arguments passed to the comment query with default query parameters.
430
* @since 4.2.0 Extracted from WP_Comment_Query::query().
434
* @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct()
436
public function parse_query( $query = '' ) {
437
if ( empty( $query ) ) {
438
$query = $this->query_vars;
441
$this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
442
do_action_ref_array( 'parse_comment_query', array( &$this ) );
446
* Sets up the WordPress query for retrieving comments.
449
* @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in',
450
* 'post_author__not_in', 'author__in', 'author__not_in', 'post__in',
451
* 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in'
452
* arguments to $query_vars.
453
* @since 4.2.0 Moved parsing to WP_Comment_Query::parse_query().
456
* @param string|array $query Array or URL query string of parameters.
457
* @return array List of comments.
459
public function query( $query ) {
460
$this->query_vars = wp_parse_args( $query );
461
return $this->get_comments();
465
* Get a list of comments matching the query vars.
470
* @global wpdb $wpdb WordPress database abstraction object.
472
* @return array The list of comments.
474
public function get_comments() {
405
$this->query_vars = wp_parse_args( $query_vars, $defaults );
479
$this->parse_query();
407
481
// Parse meta query
408
482
$this->meta_query = new WP_Meta_Query();
409
483
$this->meta_query->parse_query_vars( $this->query_vars );
485
if ( ! empty( $this->meta_query->queries ) ) {
486
$meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
412
490
* Fires before comments are retrieved.
418
496
do_action_ref_array( 'pre_get_comments', array( &$this ) );
420
// $args can be whatever, only use the args defined in defaults to compute the key
421
$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) ) );
498
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
499
$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
422
500
$last_changed = wp_cache_get( 'last_changed', 'comment' );
423
501
if ( ! $last_changed ) {
424
502
$last_changed = microtime();
506
585
// Disable ORDER BY with 'none', an empty array, or boolean false.
507
586
if ( in_array( $this->query_vars['orderby'], array( 'none', array(), false ), true ) ) {
509
} else if ( ! empty( $this->query_vars['orderby'] ) ) {
588
} elseif ( ! empty( $this->query_vars['orderby'] ) ) {
510
589
$ordersby = is_array( $this->query_vars['orderby'] ) ?
511
590
$this->query_vars['orderby'] :
512
591
preg_split( '/[,\s]/', $this->query_vars['orderby'] );
514
$allowed_keys = array(
518
'comment_author_email',
520
'comment_author_url',
531
if ( ! empty( $this->query_vars['meta_key'] ) ) {
532
$allowed_keys[] = $this->query_vars['meta_key'];
533
$allowed_keys[] = 'meta_value';
534
$allowed_keys[] = 'meta_value_num';
536
$ordersby = array_intersect( $ordersby, $allowed_keys );
537
foreach ( $ordersby as $key => $value ) {
538
if ( $value == $this->query_vars['meta_key'] || $value == 'meta_value' ) {
539
$ordersby[ $key ] = "$wpdb->commentmeta.meta_value";
540
} elseif ( $value == 'meta_value_num' ) {
541
$ordersby[ $key ] = "$wpdb->commentmeta.meta_value+0";
544
$orderby = empty( $ordersby ) ? 'comment_date_gmt' : implode(', ', $ordersby);
593
$orderby_array = array();
594
$found_orderby_comment_ID = false;
595
foreach ( $ordersby as $_key => $_value ) {
600
if ( is_int( $_key ) ) {
608
if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
609
$found_orderby_comment_ID = true;
612
$parsed = $this->parse_orderby( $_orderby );
618
$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
621
// If no valid clauses were found, order by comment_date_gmt.
622
if ( empty( $orderby_array ) ) {
623
$orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
626
// To ensure determinate sorting, always include a comment_ID clause.
627
if ( ! $found_orderby_comment_ID ) {
628
$comment_ID_order = '';
630
// Inherit order from comment_date or comment_date_gmt, if available.
631
foreach ( $orderby_array as $orderby_clause ) {
632
if ( preg_match( '/comment_date(?:_gmt)*\ (ASC|DESC)/', $orderby_clause, $match ) ) {
633
$comment_ID_order = $match[1];
638
// If no date-related order is available, use the date from the first available clause.
639
if ( ! $comment_ID_order ) {
640
foreach ( $orderby_array as $orderby_clause ) {
641
if ( false !== strpos( 'ASC', $orderby_clause ) ) {
642
$comment_ID_order = 'ASC';
644
$comment_ID_order = 'DESC';
652
if ( ! $comment_ID_order ) {
653
$comment_ID_order = 'DESC';
656
$orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
659
$orderby = implode( ', ', $orderby_array );
546
$orderby = 'comment_date_gmt';
661
$orderby = "$wpdb->comments.comment_date_gmt $order";
549
664
$number = absint( $this->query_vars['number'] );
674
789
if ( ! empty( $post_fields ) ) {
675
790
$join_posts_table = true;
676
791
foreach ( $post_fields as $field_name => $field_value ) {
677
$where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} = %s", $field_value );
792
// $field_value may be an array.
793
$esses = array_fill( 0, count( (array) $field_value ), '%s' );
794
$where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
704
821
$join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
707
if ( ! empty( $this->meta_query->queries ) ) {
708
$clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
709
$join .= $clauses['join'];
824
if ( ! empty( $meta_query_clauses ) ) {
825
$join .= $meta_query_clauses['join'];
711
827
// Strip leading 'AND'.
712
$where[] = preg_replace( '/^\s*AND\s*/', '', $clauses['where'] );
828
$where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] );
714
830
if ( ! $this->query_vars['count'] ) {
715
831
$groupby = "{$wpdb->comments}.comment_ID";
725
841
$where = implode( ' AND ', $where );
727
$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
843
$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
729
845
* Filter the comment query clauses.
805
921
return ' AND (' . implode(' OR ', $searches) . ')';
925
* Parse and sanitize 'orderby' keys passed to the comment query.
930
* @global wpdb $wpdb WordPress database abstraction object.
932
* @param string $orderby Alias for the field to order by.
933
* @return string|bool Value to used in the ORDER clause. False otherwise.
935
protected function parse_orderby( $orderby ) {
938
$allowed_keys = array(
942
'comment_author_email',
944
'comment_author_url',
956
if ( ! empty( $this->query_vars['meta_key'] ) ) {
957
$allowed_keys[] = $this->query_vars['meta_key'];
958
$allowed_keys[] = 'meta_value';
959
$allowed_keys[] = 'meta_value_num';
962
$meta_query_clauses = $this->meta_query->get_clauses();
963
if ( $meta_query_clauses ) {
964
$allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );
968
if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
969
$parsed = "$wpdb->commentmeta.meta_value";
970
} else if ( $orderby == 'meta_value_num' ) {
971
$parsed = "$wpdb->commentmeta.meta_value+0";
972
} else if ( in_array( $orderby, $allowed_keys ) ) {
974
if ( isset( $meta_query_clauses[ $orderby ] ) ) {
975
$meta_clause = $meta_query_clauses[ $orderby ];
976
$parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
978
$parsed = "$wpdb->comments.$orderby";
986
* Parse an 'order' query variable and cast it to ASC or DESC as necessary.
991
* @param string $order The 'order' query variable.
992
* @return string The sanitized 'order' query variable.
994
protected function parse_order( $order ) {
995
if ( ! is_string( $order ) || empty( $order ) ) {
999
if ( 'ASC' === strtoupper( $order ) ) {
1921
2119
$compacted = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id' );
1922
2120
if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
2121
$fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
2123
foreach( $fields as $field ) {
2124
if ( isset( $compacted[ $field ] ) ) {
2125
$compacted[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $compacted[ $field ] );
2129
if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
1926
2134
$id = (int) $wpdb->insert_id;
2073
2281
$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
2074
2282
$commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '';
2076
$commentdata['comment_date'] = current_time('mysql');
2077
$commentdata['comment_date_gmt'] = current_time('mysql', 1);
2284
if ( empty( $commentdata['comment_date'] ) ) {
2285
$commentdata['comment_date'] = current_time('mysql');
2288
if ( empty( $commentdata['comment_date_gmt'] ) ) {
2289
$commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
2079
2292
$commentdata = wp_filter_comment($commentdata);
2227
2446
if ( ! isset( $data['comment_approved'] ) ) {
2228
2447
$data['comment_approved'] = 1;
2229
} else if ( 'hold' == $data['comment_approved'] ) {
2448
} elseif ( 'hold' == $data['comment_approved'] ) {
2230
2449
$data['comment_approved'] = 0;
2231
} else if ( 'approve' == $data['comment_approved'] ) {
2450
} elseif ( 'approve' == $data['comment_approved'] ) {
2232
2451
$data['comment_approved'] = 1;
2235
2454
$comment_ID = $data['comment_ID'];
2236
2455
$comment_post_ID = $data['comment_post_ID'];
2237
$keys = array( 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_parent' );
2456
$keys = array( 'comment_post_ID', 'comment_content', 'comment_author', 'comment_author_email', 'comment_approved', 'comment_karma', 'comment_author_url', 'comment_date', 'comment_date_gmt', 'comment_type', 'comment_parent', 'user_id' );
2238
2457
$data = wp_array_slice_assoc( $data, $keys );
2239
2458
$rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) );