~canonical-sysadmins/wordpress/4.7.1

« back to all changes in this revision

Viewing changes to wp-includes/comment.php

  • Committer: Paul Gear
  • Date: 2015-04-24 01:35:20 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: paul.gear@canonical.com-20150424013520-w4p9ksth76zh6opw
Merge new upstream release 4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
218
218
 *
219
219
 * @since 2.7.0
220
220
 *
221
 
 * @global wpdb $wpdb WordPress database abstraction object.
222
 
 *
223
 
 * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query::query()}
 
221
 * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query::parse_query()}
224
222
 *                           for information on accepted arguments. Default empty.
225
223
 * @return int|array List of comments or number of found comments if `$count` argument is true.
226
224
 */
232
230
/**
233
231
 * WordPress Comment Query class.
234
232
 *
235
 
 * See {@see WP_Comment_Query::query()} for accepted arguments.
 
233
 * See WP_Comment_Query::__construct() for accepted arguments.
236
234
 *
237
235
 * @since 3.1.0
238
236
 */
265
263
        public $date_query = false;
266
264
 
267
265
        /**
 
266
         * Query vars set by the user.
 
267
         *
 
268
         * @since 3.1.0
 
269
         * @access public
268
270
         * @var array
269
271
         */
270
272
        public $query_vars;
271
273
 
272
274
        /**
 
275
         * Default values for query vars.
 
276
         *
 
277
         * @since 4.2.0
 
278
         * @access public
 
279
         * @var array
 
280
         */
 
281
        public $query_var_defaults;
 
282
 
 
283
        /**
 
284
         * List of comments located by the query.
 
285
         *
 
286
         * @since 4.0.0
 
287
         * @access public
273
288
         * @var array
274
289
         */
275
290
        public $comments;
285
300
         * @return mixed|bool Return value of the callback, false otherwise.
286
301
         */
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 );
 
305
                }
 
306
                return false;
289
307
        }
290
308
 
291
309
        /**
292
 
         * Execute the query
293
 
         *
294
 
         * @since 3.1.0
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.
299
 
         *
300
 
         * @param string|array $query_vars {
301
 
         *     Optional. Array or query string of comment query parameters.
 
310
         * Constructor.
 
311
         *
 
312
         * Sets up the comment query, based on the query vars passed.
 
313
         *
 
314
         * @since  4.2.0
 
315
         * @access public
 
316
         *
 
317
         * @param string|array $query {
 
318
         *     Optional. Array or query string of comment query parameters. Default empty.
302
319
         *
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.
307
324
         *     @type array        $comment__not_in     Array of comment IDs to exclude. Default empty.
308
325
         *     @type bool         $count               Whether to return a comment count (true) or array of comment
309
326
         *                                             objects (false). Default false.
310
 
         *     @type array        $date_query          Date query clauses to limit comments by. See {@see WP_Date_Query}.
 
327
         *     @type array        $date_query          Date query clauses to limit comments by. See WP_Date_Query.
311
328
         *                                             Default null.
312
329
         *     @type string       $fields              Comment fields to return. Accepts 'ids' for comment IDs only or
313
330
         *                                             empty for all fields. 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.
325
342
         *                                             Default 0.
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.
356
377
         *     @type array        $type__not_in        Exclude comments from a given array of comment types. Default empty.
357
378
         *     @type int          $user_id             Include comments for a specific user ID. Default empty.
358
379
         * }
359
 
         * @return int|array Array of comments or number of found comments if `$count` is set to true.
 
380
         * @return WP_Comment_Query WP_Comment_Query instance.
360
381
         */
361
 
        public function query( $query_vars ) {
362
 
                global $wpdb;
363
 
 
364
 
                $defaults = array(
 
382
        public function __construct( $query = '' ) {
 
383
                $this->query_var_defaults = array(
365
384
                        'author_email' => '',
366
385
                        'author__in' => '',
367
386
                        'author__not_in' => '',
400
419
                        'date_query' => null, // See WP_Date_Query
401
420
                );
402
421
 
 
422
                if ( ! empty( $query ) ) {
 
423
                        $this->query( $query );
 
424
                }
 
425
        }
 
426
 
 
427
        /**
 
428
         * Parse arguments passed to the comment query with default query parameters.
 
429
         *
 
430
         * @since  4.2.0 Extracted from WP_Comment_Query::query().
 
431
         *
 
432
         * @access public
 
433
         *
 
434
         * @param string|array $query WP_Comment_Query arguments. See WP_Comment_Query::__construct()
 
435
         */
 
436
        public function parse_query( $query = '' ) {
 
437
                if ( empty( $query ) ) {
 
438
                        $query = $this->query_vars;
 
439
                }
 
440
 
 
441
                $this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
 
442
                do_action_ref_array( 'parse_comment_query', array( &$this ) );
 
443
        }
 
444
 
 
445
        /**
 
446
         * Sets up the WordPress query for retrieving comments.
 
447
         *
 
448
         * @since 3.1.0
 
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().
 
454
         * @access public
 
455
         *
 
456
         * @param string|array $query Array or URL query string of parameters.
 
457
         * @return array List of comments.
 
458
         */
 
459
        public function query( $query ) {
 
460
                $this->query_vars = wp_parse_args( $query );
 
461
                return $this->get_comments();
 
462
        }
 
463
 
 
464
        /**
 
465
         * Get a list of comments matching the query vars.
 
466
         *
 
467
         * @since 4.2.0
 
468
         * @access public
 
469
         *
 
470
         * @global wpdb $wpdb WordPress database abstraction object.
 
471
         *
 
472
         * @return array The list of comments.
 
473
         */
 
474
        public function get_comments() {
 
475
                global $wpdb;
 
476
 
403
477
                $groupby = '';
404
478
 
405
 
                $this->query_vars = wp_parse_args( $query_vars, $defaults );
 
479
                $this->parse_query();
406
480
 
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 );
410
484
 
 
485
                if ( ! empty( $this->meta_query->queries ) ) {
 
486
                        $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
 
487
                }
 
488
 
411
489
                /**
412
490
                 * Fires before comments are retrieved.
413
491
                 *
417
495
                 */
418
496
                do_action_ref_array( 'pre_get_comments', array( &$this ) );
419
497
 
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();
427
505
                $cache_key = "get_comments:$key:$last_changed";
428
506
 
429
507
                if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) {
430
 
                        return $cache;
 
508
                        $this->comments = $cache;
 
509
                        return $this->comments;
431
510
                }
432
511
 
433
512
                $where = array();
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 ) ) {
508
587
                        $orderby = '';
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'] );
513
592
 
514
 
                        $allowed_keys = array(
515
 
                                'comment_agent',
516
 
                                'comment_approved',
517
 
                                'comment_author',
518
 
                                'comment_author_email',
519
 
                                'comment_author_IP',
520
 
                                'comment_author_url',
521
 
                                'comment_content',
522
 
                                'comment_date',
523
 
                                'comment_date_gmt',
524
 
                                'comment_ID',
525
 
                                'comment_karma',
526
 
                                'comment_parent',
527
 
                                'comment_post_ID',
528
 
                                'comment_type',
529
 
                                'user_id',
530
 
                        );
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';
535
 
                        }
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";
542
 
                                }
543
 
                        }
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 ) {
 
596
                                if ( ! $_value ) {
 
597
                                        continue;
 
598
                                }
 
599
 
 
600
                                if ( is_int( $_key ) ) {
 
601
                                        $_orderby = $_value;
 
602
                                        $_order = $order;
 
603
                                } else {
 
604
                                        $_orderby = $_key;
 
605
                                        $_order = $_value;
 
606
                                }
 
607
 
 
608
                                if ( ! $found_orderby_comment_ID && 'comment_ID' === $_orderby ) {
 
609
                                        $found_orderby_comment_ID = true;
 
610
                                }
 
611
 
 
612
                                $parsed = $this->parse_orderby( $_orderby );
 
613
 
 
614
                                if ( ! $parsed ) {
 
615
                                        continue;
 
616
                                }
 
617
 
 
618
                                $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
 
619
                        }
 
620
 
 
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";
 
624
                        }
 
625
 
 
626
                        // To ensure determinate sorting, always include a comment_ID clause.
 
627
                        if ( ! $found_orderby_comment_ID ) {
 
628
                                $comment_ID_order = '';
 
629
 
 
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];
 
634
                                                break;
 
635
                                        }
 
636
                                }
 
637
 
 
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';
 
643
                                                } else {
 
644
                                                        $comment_ID_order = 'DESC';
 
645
                                                }
 
646
 
 
647
                                                break;
 
648
                                        }
 
649
                                }
 
650
 
 
651
                                // Default to DESC.
 
652
                                if ( ! $comment_ID_order ) {
 
653
                                        $comment_ID_order = 'DESC';
 
654
                                }
 
655
 
 
656
                                $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
 
657
                        }
 
658
 
 
659
                        $orderby = implode( ', ', $orderby_array );
545
660
                } else {
546
 
                        $orderby = 'comment_date_gmt';
 
661
                        $orderby = "$wpdb->comments.comment_date_gmt $order";
547
662
                }
548
663
 
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 );
678
795
                        }
679
796
                }
680
797
 
704
821
                        $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
705
822
                }
706
823
 
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'];
710
826
 
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'] );
713
829
 
714
830
                        if ( ! $this->query_vars['count'] ) {
715
831
                                $groupby = "{$wpdb->comments}.comment_ID";
724
840
 
725
841
                $where = implode( ' AND ', $where );
726
842
 
727
 
                $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' );
 
843
                $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
728
844
                /**
729
845
                 * Filter the comment query clauses.
730
846
                 *
739
855
                $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
740
856
                $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
741
857
                $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
742
 
                $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
743
858
                $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
744
859
                $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
745
860
 
752
867
                }
753
868
 
754
869
                if ( $orderby ) {
755
 
                        $orderby = "ORDER BY $orderby $order";
 
870
                        $orderby = "ORDER BY $orderby";
756
871
                }
757
872
 
758
873
                $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
779
894
 
780
895
                wp_cache_add( $cache_key, $comments, 'comment' );
781
896
 
782
 
                return $comments;
 
897
                $this->comments = $comments;
 
898
                return $this->comments;
783
899
        }
784
900
 
785
901
        /**
804
920
 
805
921
                return ' AND (' . implode(' OR ', $searches) . ')';
806
922
        }
 
923
 
 
924
        /**
 
925
         * Parse and sanitize 'orderby' keys passed to the comment query.
 
926
         *
 
927
         * @since 4.2.0
 
928
         * @access protected
 
929
         *
 
930
         * @global wpdb $wpdb WordPress database abstraction object.
 
931
         *
 
932
         * @param string $orderby Alias for the field to order by.
 
933
         * @return string|bool Value to used in the ORDER clause. False otherwise.
 
934
         */
 
935
        protected function parse_orderby( $orderby ) {
 
936
                global $wpdb;
 
937
 
 
938
                $allowed_keys = array(
 
939
                        'comment_agent',
 
940
                        'comment_approved',
 
941
                        'comment_author',
 
942
                        'comment_author_email',
 
943
                        'comment_author_IP',
 
944
                        'comment_author_url',
 
945
                        'comment_content',
 
946
                        'comment_date',
 
947
                        'comment_date_gmt',
 
948
                        'comment_ID',
 
949
                        'comment_karma',
 
950
                        'comment_parent',
 
951
                        'comment_post_ID',
 
952
                        'comment_type',
 
953
                        'user_id',
 
954
                );
 
955
 
 
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';
 
960
                }
 
961
 
 
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 ) );
 
965
                }
 
966
 
 
967
                $parsed = false;
 
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 ) ) {
 
973
 
 
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'] ) );
 
977
                        } else {
 
978
                                $parsed = "$wpdb->comments.$orderby";
 
979
                        }
 
980
                }
 
981
 
 
982
                return $parsed;
 
983
        }
 
984
 
 
985
        /**
 
986
         * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
 
987
         *
 
988
         * @since 4.2.0
 
989
         * @access protected
 
990
         *
 
991
         * @param string $order The 'order' query variable.
 
992
         * @return string The sanitized 'order' query variable.
 
993
         */
 
994
        protected function parse_order( $order ) {
 
995
                if ( ! is_string( $order ) || empty( $order ) ) {
 
996
                        return 'DESC';
 
997
                }
 
998
 
 
999
                if ( 'ASC' === strtoupper( $order ) ) {
 
1000
                        return 'ASC';
 
1001
                } else {
 
1002
                        return 'DESC';
 
1003
                }
 
1004
        }
807
1005
}
808
1006
 
809
1007
/**
933
1131
 * Add meta data field to a comment.
934
1132
 *
935
1133
 * @since 2.9.0
936
 
 * @link http://codex.wordpress.org/Function_Reference/add_comment_meta
 
1134
 * @link https://codex.wordpress.org/Function_Reference/add_comment_meta
937
1135
 *
938
1136
 * @param int $comment_id Comment ID.
939
1137
 * @param string $meta_key Metadata name.
953
1151
 * allows removing all metadata matching key, if needed.
954
1152
 *
955
1153
 * @since 2.9.0
956
 
 * @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
 
1154
 * @link https://codex.wordpress.org/Function_Reference/delete_comment_meta
957
1155
 *
958
1156
 * @param int $comment_id comment ID
959
1157
 * @param string $meta_key Metadata name.
968
1166
 * Retrieve comment meta field for a comment.
969
1167
 *
970
1168
 * @since 2.9.0
971
 
 * @link http://codex.wordpress.org/Function_Reference/get_comment_meta
 
1169
 * @link https://codex.wordpress.org/Function_Reference/get_comment_meta
972
1170
 *
973
1171
 * @param int $comment_id Comment ID.
974
1172
 * @param string $key Optional. The meta key to retrieve. By default, returns data for all keys.
989
1187
 * If the meta field for the comment does not exist, it will be added.
990
1188
 *
991
1189
 * @since 2.9.0
992
 
 * @link http://codex.wordpress.org/Function_Reference/update_comment_meta
 
1190
 * @link https://codex.wordpress.org/Function_Reference/update_comment_meta
993
1191
 *
994
1192
 * @param int $comment_id Comment ID.
995
1193
 * @param string $meta_key Metadata key.
1920
2118
 
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 ) ) {
1923
 
                return false;
 
2121
                $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
 
2122
 
 
2123
                foreach( $fields as $field ) {
 
2124
                        if ( isset( $compacted[ $field ] ) ) {
 
2125
                                $compacted[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $compacted[ $field ] );
 
2126
                        }
 
2127
                }
 
2128
 
 
2129
                if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
 
2130
                        return false;
 
2131
                }
1924
2132
        }
1925
2133
 
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 ) : '';
2075
2283
 
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');
 
2286
        }
 
2287
 
 
2288
        if ( empty( $commentdata['comment_date_gmt'] ) ) {
 
2289
                $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 );
 
2290
        }
2078
2291
 
2079
2292
        $commentdata = wp_filter_comment($commentdata);
2080
2293
 
2200
2413
        if ( empty( $comment ) ) {
2201
2414
                return 0;
2202
2415
        }
 
2416
 
 
2417
        // Make sure that the comment post ID is valid (if specified).
 
2418
        if ( isset( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) {
 
2419
                return 0;
 
2420
        }
 
2421
 
2203
2422
        // Escape data pulled from DB.
2204
2423
        $comment = wp_slash($comment);
2205
2424
 
2226
2445
 
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;
2233
2452
        }
2234
2453
 
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' ) );
2240
2459