359
364
return $comment_count;
368
// Comment meta functions
372
* Add meta data field to a comment.
376
* @link http://codex.wordpress.org/Function_Reference/add_comment_meta
378
* @param int $comment_id Comment ID.
379
* @param string $key Metadata name.
380
* @param mixed $value Metadata value.
381
* @param bool $unique Optional, default is false. Whether the same key should not be added.
382
* @return bool False for failure. True for success.
384
function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) {
385
return add_metadata('comment', $comment_id, $meta_key, $meta_value, $unique);
389
* Remove metadata matching criteria from a comment.
391
* You can match based on the key, or key and value. Removing based on key and
392
* value, will keep from removing duplicate metadata with the same key. It also
393
* allows removing all metadata matching key, if needed.
396
* @uses delete_metadata
397
* @link http://codex.wordpress.org/Function_Reference/delete_comment_meta
399
* @param int $comment_id comment ID
400
* @param string $meta_key Metadata name.
401
* @param mixed $meta_value Optional. Metadata value.
402
* @return bool False for failure. True for success.
404
function delete_comment_meta($comment_id, $meta_key, $meta_value = '') {
405
return delete_metadata('comment', $comment_id, $meta_key, $meta_value);
409
* Retrieve comment meta field for a comment.
413
* @link http://codex.wordpress.org/Function_Reference/get_comment_meta
415
* @param int $comment_id Comment ID.
416
* @param string $key The meta key to retrieve.
417
* @param bool $single Whether to return a single value.
418
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single
421
function get_comment_meta($comment_id, $key, $single = false) {
422
return get_metadata('comment', $comment_id, $key, $single);
426
* Update comment meta field based on comment ID.
428
* Use the $prev_value parameter to differentiate between meta fields with the
429
* same key and comment ID.
431
* If the meta field for the comment does not exist, it will be added.
434
* @uses update_metadata
435
* @link http://codex.wordpress.org/Function_Reference/update_comment_meta
437
* @param int $comment_id Comment ID.
438
* @param string $key Metadata key.
439
* @param mixed $value Metadata value.
440
* @param mixed $prev_value Optional. Previous value to check before removing.
441
* @return bool False on failure, true if success.
443
function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') {
444
return update_metadata('comment', $comment_id, $meta_key, $meta_value, $prev_value);
363
448
* Sanitizes the cookies sent to the user already.
464
549
if ( current_user_can( 'manage_options' ) )
465
550
return; // don't throttle admins
466
if ( $lasttime = $wpdb->get_var( $wpdb->prepare("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_author_IP = %s OR comment_author_email = %s ORDER BY comment_date DESC LIMIT 1", $ip, $email) ) ) {
551
$hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 );
552
if ( $lasttime = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_date_gmt` FROM `$wpdb->comments` WHERE `comment_date_gmt` >= %s AND ( `comment_author_IP` = %s OR `comment_author_email` = %s ) ORDER BY `comment_date_gmt` DESC LIMIT 1", $hour_ago, $ip, $email ) ) ) {
467
553
$time_lastcomment = mysql2date('U', $lasttime, false);
468
554
$time_newcomment = mysql2date('U', $date, false);
469
555
$flood_die = apply_filters('comment_flood_filter', false, $time_lastcomment, $time_newcomment);
623
709
function wp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
624
710
do_action('wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
626
if ( preg_match_all('/&#(\d+);/', $comment . $author . $url, $chars) ) {
627
foreach ( (array) $chars[1] as $char ) {
628
// If it's an encoded char in the normal ASCII set, reject
630
continue; // Unless it's &
636
712
$mod_keys = trim( get_option('blacklist_keys') );
637
713
if ( '' == $mod_keys )
638
714
return false; // If moderation keys are empty
697
773
$where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id );
699
775
$count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A );
702
$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
778
$approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam', 'trash' => 'trash', 'post-trashed' => 'post-trashed');
703
779
$known_types = array_keys( $approved );
704
780
foreach( (array) $count as $row_num => $row ) {
705
$total += $row['num_comments'];
781
// Don't count post-trashed toward totals
782
if ( 'post-trashed' != $row['comment_approved'] && 'trash' != $row['comment_approved'] )
783
$total += $row['num_comments'];
706
784
if ( in_array( $row['comment_approved'], $known_types ) )
707
785
$stats[$approved[$row['comment_approved']]] = $row['num_comments'];
737
816
function wp_delete_comment($comment_id) {
818
if (!$comment = get_comment($comment_id))
821
if (wp_get_comment_status($comment_id) != 'trash' && wp_get_comment_status($comment_id) != 'spam' && EMPTY_TRASH_DAYS > 0)
822
return wp_trash_comment($comment_id);
739
824
do_action('delete_comment', $comment_id);
741
$comment = get_comment($comment_id);
743
if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
746
826
// Move children up a level.
747
827
$children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_id) );
748
828
if ( !empty($children) ) {
750
830
clean_comment_cache($children);
834
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d ", $comment_id ) );
835
if ( !empty($meta_ids) ) {
836
do_action( 'delete_commentmeta', $meta_ids );
837
$in_meta_ids = "'" . implode("', '", $meta_ids) . "'";
838
$wpdb->query( "DELETE FROM $wpdb->commentmeta WHERE meta_id IN ($in_meta_ids)" );
839
do_action( 'deleted_commentmeta', $meta_ids );
842
if ( ! $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->comments WHERE comment_ID = %d LIMIT 1", $comment_id) ) )
844
do_action('deleted_comment', $comment_id);
753
846
$post_id = $comment->comment_post_ID;
754
847
if ( $post_id && $comment->comment_approved == 1 )
755
848
wp_update_comment_count($post_id);
858
* Moves a comment to the Trash
861
* @uses do_action() on 'trash_comment' before trashing
862
* @uses do_action() on 'trashed_comment' after trashing
864
* @param int $comment_id Comment ID.
865
* @return mixed False on failure
867
function wp_trash_comment($comment_id) {
868
if ( EMPTY_TRASH_DAYS == 0 )
869
return wp_delete_comment($comment_id);
871
if ( !$comment = get_comment($comment_id) )
874
do_action('trash_comment', $comment_id);
876
if ( wp_set_comment_status($comment_id, 'trash') ) {
877
add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
878
add_comment_meta($comment_id, '_wp_trash_meta_time', time() );
879
do_action('trashed_comment', $comment_id);
887
* Removes a comment from the Trash
890
* @uses do_action() on 'untrash_comment' before untrashing
891
* @uses do_action() on 'untrashed_comment' after untrashing
893
* @param int $comment_id Comment ID.
894
* @return mixed False on failure
896
function wp_untrash_comment($comment_id) {
897
if ( ! (int)$comment_id )
900
do_action('untrash_comment', $comment_id);
902
$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
903
if ( empty($status) )
906
if ( wp_set_comment_status($comment_id, $status) ) {
907
delete_comment_meta($comment_id, '_wp_trash_meta_time');
908
delete_comment_meta($comment_id, '_wp_trash_meta_status');
909
do_action('untrashed_comment', $comment_id);
917
* Marks a comment as Spam
920
* @uses do_action() on 'spam_comment' before spamming
921
* @uses do_action() on 'spammed_comment' after spamming
923
* @param int $comment_id Comment ID.
924
* @return mixed False on failure
926
function wp_spam_comment($comment_id) {
927
if ( !$comment = get_comment($comment_id) )
930
do_action('spam_comment', $comment_id);
932
if ( wp_set_comment_status($comment_id, 'spam') ) {
933
add_comment_meta($comment_id, '_wp_trash_meta_status', $comment->comment_approved);
934
do_action('spammed_comment', $comment_id);
942
* Removes a comment from the Spam
945
* @uses do_action() on 'unspam_comment' before unspamming
946
* @uses do_action() on 'unspammed_comment' after unspamming
948
* @param int $comment_id Comment ID.
949
* @return mixed False on failure
951
function wp_unspam_comment($comment_id) {
952
if ( ! (int)$comment_id )
955
do_action('unspam_comment', $comment_id);
957
$status = (string) get_comment_meta($comment_id, '_wp_trash_meta_status', true);
958
if ( empty($status) )
961
if ( wp_set_comment_status($comment_id, $status) ) {
962
delete_comment_meta($comment_id, '_wp_trash_meta_status');
963
do_action('unspammed_comment', $comment_id);
765
971
* The status of a comment by ID.
769
975
* @param int $comment_id Comment ID
770
* @return string|bool Status might be 'deleted', 'approved', 'unapproved', 'spam'. False on failure.
976
* @return string|bool Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure.
772
978
function wp_get_comment_status($comment_id) {
773
979
$comment = get_comment($comment_id);
925
1133
* @return array Parsed comment information.
927
1135
function wp_filter_comment($commentdata) {
928
$commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
1136
if ( isset($commentdata['user_ID']) )
1137
$commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_ID']);
1138
elseif ( isset($commentdata['user_id']) )
1139
$commentdata['user_id'] = apply_filters('pre_user_id', $commentdata['user_id']);
929
1140
$commentdata['comment_agent'] = apply_filters('pre_comment_user_agent', $commentdata['comment_agent']);
930
1141
$commentdata['comment_author'] = apply_filters('pre_comment_author_name', $commentdata['comment_author']);
931
1142
$commentdata['comment_content'] = apply_filters('pre_comment_content', $commentdata['comment_content']);
976
1187
$commentdata = apply_filters('preprocess_comment', $commentdata);
978
1189
$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
979
$commentdata['user_ID'] = (int) $commentdata['user_ID'];
1190
if ( isset($commentdata['user_ID']) )
1191
$commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID'];
1192
elseif ( isset($commentdata['user_id']) )
1193
$commentdata['user_id'] = (int) $commentdata['user_id'];
981
$commentdata['comment_parent'] = absint($commentdata['comment_parent']);
1195
$commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0;
982
1196
$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
983
1197
$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;
985
1199
$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
986
$commentdata['comment_agent'] = $_SERVER['HTTP_USER_AGENT'];
1200
$commentdata['comment_agent'] = substr($_SERVER['HTTP_USER_AGENT'], 0, 254);
988
1202
$commentdata['comment_date'] = current_time('mysql');
989
1203
$commentdata['comment_date_gmt'] = current_time('mysql', 1);
1310
1528
// Do pingbacks
1311
1529
while ($ping = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) {
1312
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme';");
1530
$mid = $wpdb->get_var( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = {$ping->ID} AND meta_key = '_pingme' LIMIT 1");
1531
do_action( 'delete_postmeta', $mid );
1532
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mid ) );
1533
do_action( 'deleted_postmeta', $mid );
1313
1534
pingback($ping->post_content, $ping->ID);
1316
1537
// Do Enclosures
1317
1538
while ($enclosure = $wpdb->get_row("SELECT * FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) {
1318
$wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme';", $enclosure->ID) );
1539
$mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = '_encloseme'", $enclosure->ID) );
1540
do_action( 'delete_postmeta', $mid );
1541
$wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_id = %d", $mid) );
1542
do_action( 'deleted_postmeta', $mid );
1319
1543
do_enclose($enclosure->post_content, $enclosure->ID);