mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
comment.php
4017 lines (3516 loc) · 126 KB
/
comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Core Comment API
*
* @package WordPress
* @subpackage Comment
*/
/**
* Checks whether a comment passes internal checks to be allowed to add.
*
* If manual comment moderation is set in the administration, then all checks,
* regardless of their type and substance, will fail and the function will
* return false.
*
* If the number of links exceeds the amount in the administration, then the
* check fails. If any of the parameter contents contain any disallowed words,
* then the check fails.
*
* If the comment author was approved before, then the comment is automatically
* approved.
*
* If all checks pass, the function will return true.
*
* @since 1.2.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $author Comment author name.
* @param string $email Comment author email.
* @param string $url Comment author URL.
* @param string $comment Content of the comment.
* @param string $user_ip Comment author IP address.
* @param string $user_agent Comment author User-Agent.
* @param string $comment_type Comment type, either user-submitted comment,
* trackback, or pingback.
* @return bool If all checks pass, true, otherwise false.
*/
function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) {
global $wpdb;
// If manual moderation is enabled, skip all checks and return false.
if ( 1 == get_option( 'comment_moderation' ) ) {
return false;
}
/** This filter is documented in wp-includes/comment-template.php */
$comment = apply_filters( 'comment_text', $comment, null, array() );
// Check for the number of external links if a max allowed number is set.
$max_links = get_option( 'comment_max_links' );
if ( $max_links ) {
$num_links = preg_match_all( '/<a [^>]*href/i', $comment, $out );
/**
* Filters the number of links found in a comment.
*
* @since 3.0.0
* @since 4.7.0 Added the `$comment` parameter.
*
* @param int $num_links The number of links found.
* @param string $url Comment author's URL. Included in allowed links total.
* @param string $comment Content of the comment.
*/
$num_links = apply_filters( 'comment_max_links_url', $num_links, $url, $comment );
/*
* If the number of links in the comment exceeds the allowed amount,
* fail the check by returning false.
*/
if ( $num_links >= $max_links ) {
return false;
}
}
$mod_keys = trim( get_option( 'moderation_keys' ) );
// If moderation 'keys' (keywords) are set, process them.
if ( ! empty( $mod_keys ) ) {
$words = explode( "\n", $mod_keys );
foreach ( (array) $words as $word ) {
$word = trim( $word );
// Skip empty lines.
if ( empty( $word ) ) {
continue;
}
/*
* Do some escaping magic so that '#' (number of) characters in the spam
* words don't break things:
*/
$word = preg_quote( $word, '#' );
/*
* Check the comment fields for moderation keywords. If any are found,
* fail the check for the given field by returning false.
*/
$pattern = "#$word#iu";
if ( preg_match( $pattern, $author ) ) {
return false;
}
if ( preg_match( $pattern, $email ) ) {
return false;
}
if ( preg_match( $pattern, $url ) ) {
return false;
}
if ( preg_match( $pattern, $comment ) ) {
return false;
}
if ( preg_match( $pattern, $user_ip ) ) {
return false;
}
if ( preg_match( $pattern, $user_agent ) ) {
return false;
}
}
}
/*
* Check if the option to approve comments by previously-approved authors is enabled.
*
* If it is enabled, check whether the comment author has a previously-approved comment,
* as well as whether there are any moderation keywords (if set) present in the author
* email address. If both checks pass, return true. Otherwise, return false.
*/
if ( 1 == get_option( 'comment_previously_approved' ) ) {
if ( 'trackback' !== $comment_type && 'pingback' !== $comment_type && '' !== $author && '' !== $email ) {
$comment_user = get_user_by( 'email', wp_unslash( $email ) );
if ( ! empty( $comment_user->ID ) ) {
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE user_id = %d AND comment_approved = '1' LIMIT 1", $comment_user->ID ) );
} else {
// expected_slashed ($author, $email)
$ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_approved FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s and comment_approved = '1' LIMIT 1", $author, $email ) );
}
if ( ( 1 == $ok_to_comment ) &&
( empty( $mod_keys ) || ! str_contains( $email, $mod_keys ) ) ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
return true;
}
/**
* Retrieves the approved comments for a post.
*
* @since 2.0.0
* @since 4.1.0 Refactored to leverage WP_Comment_Query over a direct query.
*
* @param int $post_id The ID of the post.
* @param array $args {
* Optional. See WP_Comment_Query::__construct() for information on accepted arguments.
*
* @type int $status Comment status to limit results by. Defaults to approved comments.
* @type int $post_id Limit results to those affiliated with a given post ID.
* @type string $order How to order retrieved comments. Default 'ASC'.
* }
* @return WP_Comment[]|int[]|int The approved comments, or number of comments if `$count`
* argument is true.
*/
function get_approved_comments( $post_id, $args = array() ) {
if ( ! $post_id ) {
return array();
}
$defaults = array(
'status' => 1,
'post_id' => $post_id,
'order' => 'ASC',
);
$parsed_args = wp_parse_args( $args, $defaults );
$query = new WP_Comment_Query();
return $query->query( $parsed_args );
}
/**
* Retrieves comment data given a comment ID or comment object.
*
* If an object is passed then the comment data will be cached and then returned
* after being passed through a filter. If the comment is empty, then the global
* comment variable will be used, if it is set.
*
* @since 2.0.0
*
* @global WP_Comment $comment Global comment object.
*
* @param WP_Comment|string|int $comment Comment to retrieve.
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
* correspond to a WP_Comment object, an associative array, or a numeric array,
* respectively. Default OBJECT.
* @return WP_Comment|array|null Depends on $output value.
*/
function get_comment( $comment = null, $output = OBJECT ) {
if ( empty( $comment ) && isset( $GLOBALS['comment'] ) ) {
$comment = $GLOBALS['comment'];
}
if ( $comment instanceof WP_Comment ) {
$_comment = $comment;
} elseif ( is_object( $comment ) ) {
$_comment = new WP_Comment( $comment );
} else {
$_comment = WP_Comment::get_instance( $comment );
}
if ( ! $_comment ) {
return null;
}
/**
* Fires after a comment is retrieved.
*
* @since 2.3.0
*
* @param WP_Comment $_comment Comment data.
*/
$_comment = apply_filters( 'get_comment', $_comment );
if ( OBJECT === $output ) {
return $_comment;
} elseif ( ARRAY_A === $output ) {
return $_comment->to_array();
} elseif ( ARRAY_N === $output ) {
return array_values( $_comment->to_array() );
}
return $_comment;
}
/**
* Retrieves a list of comments.
*
* The comment list can be for the blog as a whole or for an individual post.
*
* @since 2.7.0
*
* @param string|array $args Optional. Array or string of arguments. See WP_Comment_Query::__construct()
* for information on accepted arguments. Default empty string.
* @return WP_Comment[]|int[]|int List of comments or number of found comments if `$count` argument is true.
*/
function get_comments( $args = '' ) {
$query = new WP_Comment_Query();
return $query->query( $args );
}
/**
* Retrieves all of the WordPress supported comment statuses.
*
* Comments have a limited set of valid status values, this provides the comment
* status values and descriptions.
*
* @since 2.7.0
*
* @return string[] List of comment status labels keyed by status.
*/
function get_comment_statuses() {
$status = array(
'hold' => __( 'Unapproved' ),
'approve' => _x( 'Approved', 'comment status' ),
'spam' => _x( 'Spam', 'comment status' ),
'trash' => _x( 'Trash', 'comment status' ),
);
return $status;
}
/**
* Gets the default comment status for a post type.
*
* @since 4.3.0
*
* @param string $post_type Optional. Post type. Default 'post'.
* @param string $comment_type Optional. Comment type. Default 'comment'.
* @return string Expected return value is 'open' or 'closed'.
*/
function get_default_comment_status( $post_type = 'post', $comment_type = 'comment' ) {
switch ( $comment_type ) {
case 'pingback':
case 'trackback':
$supports = 'trackbacks';
$option = 'ping';
break;
default:
$supports = 'comments';
$option = 'comment';
break;
}
// Set the status.
if ( 'page' === $post_type ) {
$status = 'closed';
} elseif ( post_type_supports( $post_type, $supports ) ) {
$status = get_option( "default_{$option}_status" );
} else {
$status = 'closed';
}
/**
* Filters the default comment status for the given post type.
*
* @since 4.3.0
*
* @param string $status Default status for the given post type,
* either 'open' or 'closed'.
* @param string $post_type Post type. Default is `post`.
* @param string $comment_type Type of comment. Default is `comment`.
*/
return apply_filters( 'get_default_comment_status', $status, $post_type, $comment_type );
}
/**
* Retrieves the date the last comment was modified.
*
* @since 1.5.0
* @since 4.7.0 Replaced caching the modified date in a local static variable
* with the Object Cache API.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $timezone Which timezone to use in reference to 'gmt', 'blog', or 'server' locations.
* @return string|false Last comment modified date on success, false on failure.
*/
function get_lastcommentmodified( $timezone = 'server' ) {
global $wpdb;
$timezone = strtolower( $timezone );
$key = "lastcommentmodified:$timezone";
$comment_modified_date = wp_cache_get( $key, 'timeinfo' );
if ( false !== $comment_modified_date ) {
return $comment_modified_date;
}
switch ( $timezone ) {
case 'gmt':
$comment_modified_date = $wpdb->get_var( "SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
break;
case 'blog':
$comment_modified_date = $wpdb->get_var( "SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1" );
break;
case 'server':
$add_seconds_server = gmdate( 'Z' );
$comment_modified_date = $wpdb->get_var( $wpdb->prepare( "SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server ) );
break;
}
if ( $comment_modified_date ) {
wp_cache_set( $key, $comment_modified_date, 'timeinfo' );
return $comment_modified_date;
}
return false;
}
/**
* Retrieves the total comment counts for the whole site or a single post.
*
* @since 2.0.0
*
* @param int $post_id Optional. Restrict the comment counts to the given post. Default 0, which indicates that
* comment counts for the whole site will be retrieved.
* @return int[] {
* The number of comments keyed by their status.
*
* @type int $approved The number of approved comments.
* @type int $awaiting_moderation The number of comments awaiting moderation (a.k.a. pending).
* @type int $spam The number of spam comments.
* @type int $trash The number of trashed comments.
* @type int $post-trashed The number of comments for posts that are in the trash.
* @type int $total_comments The total number of non-trashed comments, including spam.
* @type int $all The total number of pending or approved comments.
* }
*/
function get_comment_count( $post_id = 0 ) {
$post_id = (int) $post_id;
$comment_count = array(
'approved' => 0,
'awaiting_moderation' => 0,
'spam' => 0,
'trash' => 0,
'post-trashed' => 0,
'total_comments' => 0,
'all' => 0,
);
$args = array(
'count' => true,
'update_comment_meta_cache' => false,
);
if ( $post_id > 0 ) {
$args['post_id'] = $post_id;
}
$mapping = array(
'approved' => 'approve',
'awaiting_moderation' => 'hold',
'spam' => 'spam',
'trash' => 'trash',
'post-trashed' => 'post-trashed',
);
$comment_count = array();
foreach ( $mapping as $key => $value ) {
$comment_count[ $key ] = get_comments( array_merge( $args, array( 'status' => $value ) ) );
}
$comment_count['all'] = $comment_count['approved'] + $comment_count['awaiting_moderation'];
$comment_count['total_comments'] = $comment_count['all'] + $comment_count['spam'];
return array_map( 'intval', $comment_count );
}
//
// Comment meta functions.
//
/**
* Adds meta data field to a comment.
*
* @since 2.9.0
*
* @link https://developer.wordpress.org/reference/functions/add_comment_meta/
*
* @param int $comment_id Comment ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Optional. Whether the same key should not be added.
* Default false.
* @return int|false Meta ID on success, false on failure.
*/
function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) {
return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique );
}
/**
* Removes metadata matching criteria from a comment.
*
* You can match based on the key, or key and value. Removing based on key and
* value, will keep from removing duplicate metadata with the same key. It also
* allows removing all metadata matching key, if needed.
*
* @since 2.9.0
*
* @link https://developer.wordpress.org/reference/functions/delete_comment_meta/
*
* @param int $comment_id Comment ID.
* @param string $meta_key Metadata name.
* @param mixed $meta_value Optional. Metadata value. If provided,
* rows will only be removed that match the value.
* Must be serializable if non-scalar. Default empty string.
* @return bool True on success, false on failure.
*/
function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) {
return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value );
}
/**
* Retrieves comment meta field for a comment.
*
* @since 2.9.0
*
* @link https://developer.wordpress.org/reference/functions/get_comment_meta/
*
* @param int $comment_id Comment ID.
* @param string $key Optional. The meta key to retrieve. By default,
* returns data for all keys. Default empty string.
* @param bool $single Optional. Whether to return a single value.
* This parameter has no effect if `$key` is not specified.
* Default false.
* @return mixed An array of values if `$single` is false.
* The value of meta data field if `$single` is true.
* False for an invalid `$comment_id` (non-numeric, zero, or negative value).
* An empty string if a valid but non-existing comment ID is passed.
*/
function get_comment_meta( $comment_id, $key = '', $single = false ) {
return get_metadata( 'comment', $comment_id, $key, $single );
}
/**
* Queue comment meta for lazy-loading.
*
* @since 6.3.0
*
* @param array $comment_ids List of comment IDs.
*/
function wp_lazyload_comment_meta( array $comment_ids ) {
if ( empty( $comment_ids ) ) {
return;
}
$lazyloader = wp_metadata_lazyloader();
$lazyloader->queue_objects( 'comment', $comment_ids );
}
/**
* Updates comment meta field based on comment ID.
*
* Use the $prev_value parameter to differentiate between meta fields with the
* same key and comment ID.
*
* If the meta field for the comment does not exist, it will be added.
*
* @since 2.9.0
*
* @link https://developer.wordpress.org/reference/functions/update_comment_meta/
*
* @param int $comment_id Comment ID.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param mixed $prev_value Optional. Previous value to check before updating.
* If specified, only update existing metadata entries with
* this value. Otherwise, update all entries. Default empty string.
* @return int|bool Meta ID if the key didn't exist, true on successful update,
* false on failure or if the value passed to the function
* is the same as the one that is already in the database.
*/
function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) {
return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value );
}
/**
* Sets the cookies used to store an unauthenticated commentator's identity. Typically used
* to recall previous comments by this commentator that are still held in moderation.
*
* @since 3.4.0
* @since 4.9.6 The `$cookies_consent` parameter was added.
*
* @param WP_Comment $comment Comment object.
* @param WP_User $user Comment author's user object. The user may not exist.
* @param bool $cookies_consent Optional. Comment author's consent to store cookies. Default true.
*/
function wp_set_comment_cookies( $comment, $user, $cookies_consent = true ) {
// If the user already exists, or the user opted out of cookies, don't set cookies.
if ( $user->exists() ) {
return;
}
if ( false === $cookies_consent ) {
// Remove any existing cookies.
$past = time() - YEAR_IN_SECONDS;
setcookie( 'comment_author_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
setcookie( 'comment_author_email_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
setcookie( 'comment_author_url_' . COOKIEHASH, ' ', $past, COOKIEPATH, COOKIE_DOMAIN );
return;
}
/**
* Filters the lifetime of the comment cookie in seconds.
*
* @since 2.8.0
*
* @param int $seconds Comment cookie lifetime. Default 30000000.
*/
$comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 );
$secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) );
setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure );
}
/**
* Sanitizes the cookies sent to the user already.
*
* Will only do anything if the cookies have already been created for the user.
* Mostly used after cookies had been sent to use elsewhere.
*
* @since 2.0.4
*/
function sanitize_comment_cookies() {
if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) {
/**
* Filters the comment author's name cookie before it is set.
*
* When this filter hook is evaluated in wp_filter_comment(),
* the comment author's name string is passed.
*
* @since 1.5.0
*
* @param string $author_cookie The comment author name cookie.
*/
$comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE[ 'comment_author_' . COOKIEHASH ] );
$comment_author = wp_unslash( $comment_author );
$comment_author = esc_attr( $comment_author );
$_COOKIE[ 'comment_author_' . COOKIEHASH ] = $comment_author;
}
if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
/**
* Filters the comment author's email cookie before it is set.
*
* When this filter hook is evaluated in wp_filter_comment(),
* the comment author's email string is passed.
*
* @since 1.5.0
*
* @param string $author_email_cookie The comment author email cookie.
*/
$comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] );
$comment_author_email = wp_unslash( $comment_author_email );
$comment_author_email = esc_attr( $comment_author_email );
$_COOKIE[ 'comment_author_email_' . COOKIEHASH ] = $comment_author_email;
}
if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) {
/**
* Filters the comment author's URL cookie before it is set.
*
* When this filter hook is evaluated in wp_filter_comment(),
* the comment author's URL string is passed.
*
* @since 1.5.0
*
* @param string $author_url_cookie The comment author URL cookie.
*/
$comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] );
$comment_author_url = wp_unslash( $comment_author_url );
$_COOKIE[ 'comment_author_url_' . COOKIEHASH ] = $comment_author_url;
}
}
/**
* Validates whether this comment is allowed to be made.
*
* @since 2.0.0
* @since 4.7.0 The `$avoid_die` parameter was added, allowing the function
* to return a WP_Error object instead of dying.
* @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $commentdata Contains information on the comment.
* @param bool $wp_error When true, a disallowed comment will result in the function
* returning a WP_Error object, rather than executing wp_die().
* Default false.
* @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam'|'trash').
* If `$wp_error` is true, disallowed comments return a WP_Error.
*/
function wp_allow_comment( $commentdata, $wp_error = false ) {
global $wpdb;
/*
* Simple duplicate check.
* expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
*/
$dupe = $wpdb->prepare(
"SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
wp_unslash( $commentdata['comment_post_ID'] ),
wp_unslash( $commentdata['comment_parent'] ),
wp_unslash( $commentdata['comment_author'] )
);
if ( $commentdata['comment_author_email'] ) {
$dupe .= $wpdb->prepare(
'AND comment_author_email = %s ',
wp_unslash( $commentdata['comment_author_email'] )
);
}
$dupe .= $wpdb->prepare(
') AND comment_content = %s LIMIT 1',
wp_unslash( $commentdata['comment_content'] )
);
$dupe_id = $wpdb->get_var( $dupe );
/**
* Filters the ID, if any, of the duplicate comment found when creating a new comment.
*
* Return an empty value from this filter to allow what WP considers a duplicate comment.
*
* @since 4.4.0
*
* @param int $dupe_id ID of the comment identified as a duplicate.
* @param array $commentdata Data for the comment being created.
*/
$dupe_id = apply_filters( 'duplicate_comment_id', $dupe_id, $commentdata );
if ( $dupe_id ) {
/**
* Fires immediately after a duplicate comment is detected.
*
* @since 3.0.0
*
* @param array $commentdata Comment data.
*/
do_action( 'comment_duplicate_trigger', $commentdata );
/**
* Filters duplicate comment error message.
*
* @since 5.2.0
*
* @param string $comment_duplicate_message Duplicate comment error message.
*/
$comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ) );
if ( $wp_error ) {
return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 );
} else {
if ( wp_doing_ajax() ) {
die( $comment_duplicate_message );
}
wp_die( $comment_duplicate_message, 409 );
}
}
/**
* Fires immediately before a comment is marked approved.
*
* Allows checking for comment flooding.
*
* @since 2.3.0
* @since 4.7.0 The `$avoid_die` parameter was added.
* @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`.
*
* @param string $comment_author_ip Comment author's IP address.
* @param string $comment_author_email Comment author's email.
* @param string $comment_date_gmt GMT date the comment was posted.
* @param bool $wp_error Whether to return a WP_Error object instead of executing
* wp_die() or die() if a comment flood is occurring.
*/
do_action(
'check_comment_flood',
$commentdata['comment_author_IP'],
$commentdata['comment_author_email'],
$commentdata['comment_date_gmt'],
$wp_error
);
/**
* Filters whether a comment is part of a comment flood.
*
* The default check is wp_check_comment_flood(). See check_comment_flood_db().
*
* @since 4.7.0
* @since 5.5.0 The `$avoid_die` parameter was renamed to `$wp_error`.
*
* @param bool $is_flood Is a comment flooding occurring? Default false.
* @param string $comment_author_ip Comment author's IP address.
* @param string $comment_author_email Comment author's email.
* @param string $comment_date_gmt GMT date the comment was posted.
* @param bool $wp_error Whether to return a WP_Error object instead of executing
* wp_die() or die() if a comment flood is occurring.
*/
$is_flood = apply_filters(
'wp_is_comment_flood',
false,
$commentdata['comment_author_IP'],
$commentdata['comment_author_email'],
$commentdata['comment_date_gmt'],
$wp_error
);
if ( $is_flood ) {
/** This filter is documented in wp-includes/comment-template.php */
$comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) );
return new WP_Error( 'comment_flood', $comment_flood_message, 429 );
}
if ( ! empty( $commentdata['user_id'] ) ) {
$user = get_userdata( $commentdata['user_id'] );
$post_author = $wpdb->get_var(
$wpdb->prepare(
"SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1",
$commentdata['comment_post_ID']
)
);
}
if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) {
// The author and the admins get respect.
$approved = 1;
} else {
// Everyone else's comments will be checked.
if ( check_comment(
$commentdata['comment_author'],
$commentdata['comment_author_email'],
$commentdata['comment_author_url'],
$commentdata['comment_content'],
$commentdata['comment_author_IP'],
$commentdata['comment_agent'],
$commentdata['comment_type']
) ) {
$approved = 1;
} else {
$approved = 0;
}
if ( wp_check_comment_disallowed_list(
$commentdata['comment_author'],
$commentdata['comment_author_email'],
$commentdata['comment_author_url'],
$commentdata['comment_content'],
$commentdata['comment_author_IP'],
$commentdata['comment_agent']
) ) {
$approved = EMPTY_TRASH_DAYS ? 'trash' : 'spam';
}
}
/**
* Filters a comment's approval status before it is set.
*
* @since 2.1.0
* @since 4.9.0 Returning a WP_Error value from the filter will short-circuit comment insertion
* and allow skipping further processing.
*
* @param int|string|WP_Error $approved The approval status. Accepts 1, 0, 'spam', 'trash',
* or WP_Error.
* @param array $commentdata Comment data.
*/
return apply_filters( 'pre_comment_approved', $approved, $commentdata );
}
/**
* Hooks WP's native database-based comment-flood check.
*
* This wrapper maintains backward compatibility with plugins that expect to
* be able to unhook the legacy check_comment_flood_db() function from
* 'check_comment_flood' using remove_action().
*
* @since 2.3.0
* @since 4.7.0 Converted to be an add_filter() wrapper.
*/
function check_comment_flood_db() {
add_filter( 'wp_is_comment_flood', 'wp_check_comment_flood', 10, 5 );
}
/**
* Checks whether comment flooding is occurring.
*
* Won't run, if current user can manage options, so to not block
* administrators.
*
* @since 4.7.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param bool $is_flood Is a comment flooding occurring?
* @param string $ip Comment author's IP address.
* @param string $email Comment author's email address.
* @param string $date MySQL time string.
* @param bool $avoid_die When true, a disallowed comment will result in the function
* returning without executing wp_die() or die(). Default false.
* @return bool Whether comment flooding is occurring.
*/
function wp_check_comment_flood( $is_flood, $ip, $email, $date, $avoid_die = false ) {
global $wpdb;
// Another callback has declared a flood. Trust it.
if ( true === $is_flood ) {
return $is_flood;
}
// Don't throttle admins or moderators.
if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) {
return false;
}
$hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS );
if ( is_user_logged_in() ) {
$user = get_current_user_id();
$check_column = '`user_id`';
} else {
$user = $ip;
$check_column = '`comment_author_IP`';
}
$sql = $wpdb->prepare(
"SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( $check_column = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1",
$hour_ago,
$user,
$email
);
$lasttime = $wpdb->get_var( $sql );
if ( $lasttime ) {
$time_lastcomment = mysql2date( 'U', $lasttime, false );
$time_newcomment = mysql2date( 'U', $date, false );
/**
* Filters the comment flood status.
*
* @since 2.1.0
*
* @param bool $bool Whether a comment flood is occurring. Default false.
* @param int $time_lastcomment Timestamp of when the last comment was posted.
* @param int $time_newcomment Timestamp of when the new comment was posted.
*/
$flood_die = apply_filters( 'comment_flood_filter', false, $time_lastcomment, $time_newcomment );
if ( $flood_die ) {
/**
* Fires before the comment flood message is triggered.
*
* @since 1.5.0
*
* @param int $time_lastcomment Timestamp of when the last comment was posted.
* @param int $time_newcomment Timestamp of when the new comment was posted.
*/
do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
if ( $avoid_die ) {
return true;
} else {
/**
* Filters the comment flood error message.
*
* @since 5.2.0
*
* @param string $comment_flood_message Comment flood error message.
*/
$comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) );
if ( wp_doing_ajax() ) {
die( $comment_flood_message );
}
wp_die( $comment_flood_message, 429 );
}
}
}
return false;
}
/**
* Separates an array of comments into an array keyed by comment_type.
*
* @since 2.7.0
*
* @param WP_Comment[] $comments Array of comments
* @return WP_Comment[] Array of comments keyed by comment_type.
*/
function separate_comments( &$comments ) {
$comments_by_type = array(
'comment' => array(),
'trackback' => array(),
'pingback' => array(),
'pings' => array(),
);
$count = count( $comments );
for ( $i = 0; $i < $count; $i++ ) {
$type = $comments[ $i ]->comment_type;
if ( empty( $type ) ) {
$type = 'comment';
}
$comments_by_type[ $type ][] = &$comments[ $i ];
if ( 'trackback' === $type || 'pingback' === $type ) {
$comments_by_type['pings'][] = &$comments[ $i ];
}
}
return $comments_by_type;
}
/**
* Calculates the total number of comment pages.
*
* @since 2.7.0
*
* @uses Walker_Comment
*
* @global WP_Query $wp_query WordPress Query object.
*
* @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to `$wp_query->comments`.
* @param int $per_page Optional. Comments per page. Defaults to the value of `comments_per_page`
* query var, option of the same name, or 1 (in that order).
* @param bool $threaded Optional. Control over flat or threaded comments. Defaults to the value
* of `thread_comments` option.
* @return int Number of comment pages.
*/
function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
global $wp_query;
if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) {
return $wp_query->max_num_comment_pages;
}
if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) {