31 * @param string $url Comment author URL. |
31 * @param string $url Comment author URL. |
32 * @param string $comment Content of the comment. |
32 * @param string $comment Content of the comment. |
33 * @param string $user_ip Comment author IP address. |
33 * @param string $user_ip Comment author IP address. |
34 * @param string $user_agent Comment author User-Agent. |
34 * @param string $user_agent Comment author User-Agent. |
35 * @param string $comment_type Comment type, either user-submitted comment, |
35 * @param string $comment_type Comment type, either user-submitted comment, |
36 * trackback, or pingback. |
36 * trackback, or pingback. |
37 * @return bool If all checks pass, true, otherwise false. |
37 * @return bool If all checks pass, true, otherwise false. |
38 */ |
38 */ |
39 function check_comment($author, $email, $url, $comment, $user_ip, $user_agent, $comment_type) { |
39 function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent, $comment_type ) { |
40 global $wpdb; |
40 global $wpdb; |
41 |
41 |
42 // If manual moderation is enabled, skip all checks and return false. |
42 // If manual moderation is enabled, skip all checks and return false. |
43 if ( 1 == get_option('comment_moderation') ) |
43 if ( 1 == get_option( 'comment_moderation' ) ) { |
44 return false; |
44 return false; |
|
45 } |
45 |
46 |
46 /** This filter is documented in wp-includes/comment-template.php */ |
47 /** This filter is documented in wp-includes/comment-template.php */ |
47 $comment = apply_filters( 'comment_text', $comment, null, array() ); |
48 $comment = apply_filters( 'comment_text', $comment, null, array() ); |
48 |
49 |
49 // Check for the number of external links if a max allowed number is set. |
50 // Check for the number of external links if a max allowed number is set. |
64 |
65 |
65 /* |
66 /* |
66 * If the number of links in the comment exceeds the allowed amount, |
67 * If the number of links in the comment exceeds the allowed amount, |
67 * fail the check by returning false. |
68 * fail the check by returning false. |
68 */ |
69 */ |
69 if ( $num_links >= $max_links ) |
70 if ( $num_links >= $max_links ) { |
70 return false; |
71 return false; |
71 } |
72 } |
72 |
73 } |
73 $mod_keys = trim(get_option('moderation_keys')); |
74 |
|
75 $mod_keys = trim( get_option( 'moderation_keys' ) ); |
74 |
76 |
75 // If moderation 'keys' (keywords) are set, process them. |
77 // If moderation 'keys' (keywords) are set, process them. |
76 if ( !empty($mod_keys) ) { |
78 if ( ! empty( $mod_keys ) ) { |
77 $words = explode("\n", $mod_keys ); |
79 $words = explode( "\n", $mod_keys ); |
78 |
80 |
79 foreach ( (array) $words as $word) { |
81 foreach ( (array) $words as $word ) { |
80 $word = trim($word); |
82 $word = trim( $word ); |
81 |
83 |
82 // Skip empty lines. |
84 // Skip empty lines. |
83 if ( empty($word) ) |
85 if ( empty( $word ) ) { |
84 continue; |
86 continue; |
|
87 } |
85 |
88 |
86 /* |
89 /* |
87 * Do some escaping magic so that '#' (number of) characters in the spam |
90 * Do some escaping magic so that '#' (number of) characters in the spam |
88 * words don't break things: |
91 * words don't break things: |
89 */ |
92 */ |
90 $word = preg_quote($word, '#'); |
93 $word = preg_quote( $word, '#' ); |
91 |
94 |
92 /* |
95 /* |
93 * Check the comment fields for moderation keywords. If any are found, |
96 * Check the comment fields for moderation keywords. If any are found, |
94 * fail the check for the given field by returning false. |
97 * fail the check for the given field by returning false. |
95 */ |
98 */ |
96 $pattern = "#$word#i"; |
99 $pattern = "#$word#i"; |
97 if ( preg_match($pattern, $author) ) return false; |
100 if ( preg_match( $pattern, $author ) ) { |
98 if ( preg_match($pattern, $email) ) return false; |
101 return false; |
99 if ( preg_match($pattern, $url) ) return false; |
102 } |
100 if ( preg_match($pattern, $comment) ) return false; |
103 if ( preg_match( $pattern, $email ) ) { |
101 if ( preg_match($pattern, $user_ip) ) return false; |
104 return false; |
102 if ( preg_match($pattern, $user_agent) ) return false; |
105 } |
|
106 if ( preg_match( $pattern, $url ) ) { |
|
107 return false; |
|
108 } |
|
109 if ( preg_match( $pattern, $comment ) ) { |
|
110 return false; |
|
111 } |
|
112 if ( preg_match( $pattern, $user_ip ) ) { |
|
113 return false; |
|
114 } |
|
115 if ( preg_match( $pattern, $user_agent ) ) { |
|
116 return false; |
|
117 } |
103 } |
118 } |
104 } |
119 } |
105 |
120 |
106 /* |
121 /* |
107 * Check if the option to approve comments by previously-approved authors is enabled. |
122 * Check if the option to approve comments by previously-approved authors is enabled. |
108 * |
123 * |
109 * If it is enabled, check whether the comment author has a previously-approved comment, |
124 * If it is enabled, check whether the comment author has a previously-approved comment, |
110 * as well as whether there are any moderation keywords (if set) present in the author |
125 * as well as whether there are any moderation keywords (if set) present in the author |
111 * email address. If both checks pass, return true. Otherwise, return false. |
126 * email address. If both checks pass, return true. Otherwise, return false. |
112 */ |
127 */ |
113 if ( 1 == get_option('comment_whitelist')) { |
128 if ( 1 == get_option( 'comment_whitelist' ) ) { |
114 if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) { |
129 if ( 'trackback' != $comment_type && 'pingback' != $comment_type && $author != '' && $email != '' ) { |
115 $comment_user = get_user_by( 'email', wp_unslash( $email ) ); |
130 $comment_user = get_user_by( 'email', wp_unslash( $email ) ); |
116 if ( ! empty( $comment_user->ID ) ) { |
131 if ( ! empty( $comment_user->ID ) ) { |
117 $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 ) ); |
132 $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 ) ); |
118 } else { |
133 } else { |
119 // expected_slashed ($author, $email) |
134 // expected_slashed ($author, $email) |
120 $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 ) ); |
135 $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 ) ); |
121 } |
136 } |
122 if ( ( 1 == $ok_to_comment ) && |
137 if ( ( 1 == $ok_to_comment ) && |
123 ( empty($mod_keys) || false === strpos( $email, $mod_keys) ) ) |
138 ( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) { |
124 return true; |
139 return true; |
125 else |
140 } else { |
126 return false; |
141 return false; |
|
142 } |
127 } else { |
143 } else { |
128 return false; |
144 return false; |
129 } |
145 } |
130 } |
146 } |
131 return true; |
147 return true; |
420 * @param string $meta_key Metadata name. |
439 * @param string $meta_key Metadata name. |
421 * @param mixed $meta_value Metadata value. |
440 * @param mixed $meta_value Metadata value. |
422 * @param bool $unique Optional, default is false. Whether the same key should not be added. |
441 * @param bool $unique Optional, default is false. Whether the same key should not be added. |
423 * @return int|bool Meta ID on success, false on failure. |
442 * @return int|bool Meta ID on success, false on failure. |
424 */ |
443 */ |
425 function add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) { |
444 function add_comment_meta( $comment_id, $meta_key, $meta_value, $unique = false ) { |
426 $added = add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); |
445 return add_metadata( 'comment', $comment_id, $meta_key, $meta_value, $unique ); |
427 if ( $added ) { |
|
428 wp_cache_set( 'last_changed', microtime(), 'comment' ); |
|
429 } |
|
430 return $added; |
|
431 } |
446 } |
432 |
447 |
433 /** |
448 /** |
434 * Remove metadata matching criteria from a comment. |
449 * Remove metadata matching criteria from a comment. |
435 * |
450 * |
443 * @param int $comment_id comment ID |
458 * @param int $comment_id comment ID |
444 * @param string $meta_key Metadata name. |
459 * @param string $meta_key Metadata name. |
445 * @param mixed $meta_value Optional. Metadata value. |
460 * @param mixed $meta_value Optional. Metadata value. |
446 * @return bool True on success, false on failure. |
461 * @return bool True on success, false on failure. |
447 */ |
462 */ |
448 function delete_comment_meta($comment_id, $meta_key, $meta_value = '') { |
463 function delete_comment_meta( $comment_id, $meta_key, $meta_value = '' ) { |
449 $deleted = delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); |
464 return delete_metadata( 'comment', $comment_id, $meta_key, $meta_value ); |
450 if ( $deleted ) { |
|
451 wp_cache_set( 'last_changed', microtime(), 'comment' ); |
|
452 } |
|
453 return $deleted; |
|
454 } |
465 } |
455 |
466 |
456 /** |
467 /** |
457 * Retrieve comment meta field for a comment. |
468 * Retrieve comment meta field for a comment. |
458 * |
469 * |
484 * @param string $meta_key Metadata key. |
495 * @param string $meta_key Metadata key. |
485 * @param mixed $meta_value Metadata value. |
496 * @param mixed $meta_value Metadata value. |
486 * @param mixed $prev_value Optional. Previous value to check before removing. |
497 * @param mixed $prev_value Optional. Previous value to check before removing. |
487 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
498 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
488 */ |
499 */ |
489 function update_comment_meta($comment_id, $meta_key, $meta_value, $prev_value = '') { |
500 function update_comment_meta( $comment_id, $meta_key, $meta_value, $prev_value = '' ) { |
490 $updated = update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); |
501 return update_metadata( 'comment', $comment_id, $meta_key, $meta_value, $prev_value ); |
491 if ( $updated ) { |
|
492 wp_cache_set( 'last_changed', microtime(), 'comment' ); |
|
493 } |
|
494 return $updated; |
|
495 } |
502 } |
496 |
503 |
497 /** |
504 /** |
498 * Queues comments for metadata lazy-loading. |
505 * Queues comments for metadata lazy-loading. |
499 * |
506 * |
500 * @since 4.5.0 |
507 * @since 4.5.0 |
501 * |
508 * |
502 * @param array $comments Array of comment objects. |
509 * @param WP_Comment[] $comments Array of comment objects. |
503 */ |
510 */ |
504 function wp_queue_comments_for_comment_meta_lazyload( $comments ) { |
511 function wp_queue_comments_for_comment_meta_lazyload( $comments ) { |
505 // Don't use `wp_list_pluck()` to avoid by-reference manipulation. |
512 // Don't use `wp_list_pluck()` to avoid by-reference manipulation. |
506 $comment_ids = array(); |
513 $comment_ids = array(); |
507 if ( is_array( $comments ) ) { |
514 if ( is_array( $comments ) ) { |
551 * @since 2.8.0 |
558 * @since 2.8.0 |
552 * |
559 * |
553 * @param int $seconds Comment cookie lifetime. Default 30000000. |
560 * @param int $seconds Comment cookie lifetime. Default 30000000. |
554 */ |
561 */ |
555 $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 ); |
562 $comment_cookie_lifetime = time() + apply_filters( 'comment_cookie_lifetime', 30000000 ); |
556 $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); |
563 $secure = ( 'https' === parse_url( home_url(), PHP_URL_SCHEME ) ); |
557 setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
564 setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
558 setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
565 setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
559 setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
566 setcookie( 'comment_author_url_' . COOKIEHASH, esc_url( $comment->comment_author_url ), $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN, $secure ); |
560 } |
567 } |
561 |
568 |
566 * Mostly used after cookies had been sent to use elsewhere. |
573 * Mostly used after cookies had been sent to use elsewhere. |
567 * |
574 * |
568 * @since 2.0.4 |
575 * @since 2.0.4 |
569 */ |
576 */ |
570 function sanitize_comment_cookies() { |
577 function sanitize_comment_cookies() { |
571 if ( isset( $_COOKIE['comment_author_' . COOKIEHASH] ) ) { |
578 if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) { |
572 /** |
579 /** |
573 * Filters the comment author's name cookie before it is set. |
580 * Filters the comment author's name cookie before it is set. |
574 * |
581 * |
575 * When this filter hook is evaluated in wp_filter_comment(), |
582 * When this filter hook is evaluated in wp_filter_comment(), |
576 * the comment author's name string is passed. |
583 * the comment author's name string is passed. |
577 * |
584 * |
578 * @since 1.5.0 |
585 * @since 1.5.0 |
579 * |
586 * |
580 * @param string $author_cookie The comment author name cookie. |
587 * @param string $author_cookie The comment author name cookie. |
581 */ |
588 */ |
582 $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE['comment_author_' . COOKIEHASH] ); |
589 $comment_author = apply_filters( 'pre_comment_author_name', $_COOKIE[ 'comment_author_' . COOKIEHASH ] ); |
583 $comment_author = wp_unslash($comment_author); |
590 $comment_author = wp_unslash( $comment_author ); |
584 $comment_author = esc_attr($comment_author); |
591 $comment_author = esc_attr( $comment_author ); |
585 $_COOKIE['comment_author_' . COOKIEHASH] = $comment_author; |
592 $_COOKIE[ 'comment_author_' . COOKIEHASH ] = $comment_author; |
586 } |
593 } |
587 |
594 |
588 if ( isset( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) { |
595 if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { |
589 /** |
596 /** |
590 * Filters the comment author's email cookie before it is set. |
597 * Filters the comment author's email cookie before it is set. |
591 * |
598 * |
592 * When this filter hook is evaluated in wp_filter_comment(), |
599 * When this filter hook is evaluated in wp_filter_comment(), |
593 * the comment author's email string is passed. |
600 * the comment author's email string is passed. |
594 * |
601 * |
595 * @since 1.5.0 |
602 * @since 1.5.0 |
596 * |
603 * |
597 * @param string $author_email_cookie The comment author email cookie. |
604 * @param string $author_email_cookie The comment author email cookie. |
598 */ |
605 */ |
599 $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE['comment_author_email_' . COOKIEHASH] ); |
606 $comment_author_email = apply_filters( 'pre_comment_author_email', $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ); |
600 $comment_author_email = wp_unslash($comment_author_email); |
607 $comment_author_email = wp_unslash( $comment_author_email ); |
601 $comment_author_email = esc_attr($comment_author_email); |
608 $comment_author_email = esc_attr( $comment_author_email ); |
602 $_COOKIE['comment_author_email_'.COOKIEHASH] = $comment_author_email; |
609 $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] = $comment_author_email; |
603 } |
610 } |
604 |
611 |
605 if ( isset( $_COOKIE['comment_author_url_' . COOKIEHASH] ) ) { |
612 if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) { |
606 /** |
613 /** |
607 * Filters the comment author's URL cookie before it is set. |
614 * Filters the comment author's URL cookie before it is set. |
608 * |
615 * |
609 * When this filter hook is evaluated in wp_filter_comment(), |
616 * When this filter hook is evaluated in wp_filter_comment(), |
610 * the comment author's URL string is passed. |
617 * the comment author's URL string is passed. |
611 * |
618 * |
612 * @since 1.5.0 |
619 * @since 1.5.0 |
613 * |
620 * |
614 * @param string $author_url_cookie The comment author URL cookie. |
621 * @param string $author_url_cookie The comment author URL cookie. |
615 */ |
622 */ |
616 $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE['comment_author_url_' . COOKIEHASH] ); |
623 $comment_author_url = apply_filters( 'pre_comment_author_url', $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ); |
617 $comment_author_url = wp_unslash($comment_author_url); |
624 $comment_author_url = wp_unslash( $comment_author_url ); |
618 $_COOKIE['comment_author_url_'.COOKIEHASH] = $comment_author_url; |
625 $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] = $comment_author_url; |
619 } |
626 } |
620 } |
627 } |
621 |
628 |
622 /** |
629 /** |
623 * Validates whether this comment is allowed to be made. |
630 * Validates whether this comment is allowed to be made. |
646 wp_unslash( $commentdata['comment_parent'] ), |
653 wp_unslash( $commentdata['comment_parent'] ), |
647 wp_unslash( $commentdata['comment_author'] ) |
654 wp_unslash( $commentdata['comment_author'] ) |
648 ); |
655 ); |
649 if ( $commentdata['comment_author_email'] ) { |
656 if ( $commentdata['comment_author_email'] ) { |
650 $dupe .= $wpdb->prepare( |
657 $dupe .= $wpdb->prepare( |
651 "AND comment_author_email = %s ", |
658 'AND comment_author_email = %s ', |
652 wp_unslash( $commentdata['comment_author_email'] ) |
659 wp_unslash( $commentdata['comment_author_email'] ) |
653 ); |
660 ); |
654 } |
661 } |
655 $dupe .= $wpdb->prepare( |
662 $dupe .= $wpdb->prepare( |
656 ") AND comment_content = %s LIMIT 1", |
663 ') AND comment_content = %s LIMIT 1', |
657 wp_unslash( $commentdata['comment_content'] ) |
664 wp_unslash( $commentdata['comment_content'] ) |
658 ); |
665 ); |
659 |
666 |
660 $dupe_id = $wpdb->get_var( $dupe ); |
667 $dupe_id = $wpdb->get_var( $dupe ); |
661 |
668 |
678 * @since 3.0.0 |
685 * @since 3.0.0 |
679 * |
686 * |
680 * @param array $commentdata Comment data. |
687 * @param array $commentdata Comment data. |
681 */ |
688 */ |
682 do_action( 'comment_duplicate_trigger', $commentdata ); |
689 do_action( 'comment_duplicate_trigger', $commentdata ); |
|
690 |
|
691 /** |
|
692 * Filters duplicate comment error message. |
|
693 * |
|
694 * @since 5.2.0 |
|
695 * |
|
696 * @param string $comment_duplicate_message Duplicate comment error message. |
|
697 */ |
|
698 $comment_duplicate_message = apply_filters( 'comment_duplicate_message', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ) ); |
|
699 |
683 if ( true === $avoid_die ) { |
700 if ( true === $avoid_die ) { |
684 return new WP_Error( 'comment_duplicate', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); |
701 return new WP_Error( 'comment_duplicate', $comment_duplicate_message, 409 ); |
685 } else { |
702 } else { |
686 if ( wp_doing_ajax() ) { |
703 if ( wp_doing_ajax() ) { |
687 die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); |
704 die( $comment_duplicate_message ); |
688 } |
705 } |
689 |
706 |
690 wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); |
707 wp_die( $comment_duplicate_message, 409 ); |
691 } |
708 } |
692 } |
709 } |
693 |
710 |
694 /** |
711 /** |
695 * Fires immediately before a comment is marked approved. |
712 * Fires immediately before a comment is marked approved. |
735 $commentdata['comment_date_gmt'], |
752 $commentdata['comment_date_gmt'], |
736 $avoid_die |
753 $avoid_die |
737 ); |
754 ); |
738 |
755 |
739 if ( $is_flood ) { |
756 if ( $is_flood ) { |
740 return new WP_Error( 'comment_flood', __( 'You are posting comments too quickly. Slow down.' ), 429 ); |
757 /** This filter is documented in wp-includes/comment-template.php */ |
|
758 $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); |
|
759 |
|
760 return new WP_Error( 'comment_flood', $comment_flood_message, 429 ); |
741 } |
761 } |
742 |
762 |
743 if ( ! empty( $commentdata['user_id'] ) ) { |
763 if ( ! empty( $commentdata['user_id'] ) ) { |
744 $user = get_userdata( $commentdata['user_id'] ); |
764 $user = get_userdata( $commentdata['user_id'] ); |
745 $post_author = $wpdb->get_var( $wpdb->prepare( |
765 $post_author = $wpdb->get_var( |
746 "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", |
766 $wpdb->prepare( |
747 $commentdata['comment_post_ID'] |
767 "SELECT post_author FROM $wpdb->posts WHERE ID = %d LIMIT 1", |
748 ) ); |
768 $commentdata['comment_post_ID'] |
|
769 ) |
|
770 ); |
749 } |
771 } |
750 |
772 |
751 if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { |
773 if ( isset( $user ) && ( $commentdata['user_id'] == $post_author || $user->has_cap( 'moderate_comments' ) ) ) { |
752 // The author and the admins get respect. |
774 // The author and the admins get respect. |
753 $approved = 1; |
775 $approved = 1; |
840 return false; |
862 return false; |
841 } |
863 } |
842 $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); |
864 $hour_ago = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS ); |
843 |
865 |
844 if ( is_user_logged_in() ) { |
866 if ( is_user_logged_in() ) { |
845 $user = get_current_user_id(); |
867 $user = get_current_user_id(); |
846 $check_column = '`user_id`'; |
868 $check_column = '`user_id`'; |
847 } else { |
869 } else { |
848 $user = $ip; |
870 $user = $ip; |
849 $check_column = '`comment_author_IP`'; |
871 $check_column = '`comment_author_IP`'; |
850 } |
872 } |
851 |
873 |
852 $sql = $wpdb->prepare( |
874 $sql = $wpdb->prepare( |
853 "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", |
875 "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", |
854 $hour_ago, |
876 $hour_ago, |
855 $user, |
877 $user, |
856 $email |
878 $email |
857 ); |
879 ); |
858 $lasttime = $wpdb->get_var( $sql ); |
880 $lasttime = $wpdb->get_var( $sql ); |
859 if ( $lasttime ) { |
881 if ( $lasttime ) { |
860 $time_lastcomment = mysql2date('U', $lasttime, false); |
882 $time_lastcomment = mysql2date( 'U', $lasttime, false ); |
861 $time_newcomment = mysql2date('U', $date, false); |
883 $time_newcomment = mysql2date( 'U', $date, false ); |
862 /** |
884 /** |
863 * Filters the comment flood status. |
885 * Filters the comment flood status. |
864 * |
886 * |
865 * @since 2.1.0 |
887 * @since 2.1.0 |
866 * |
888 * |
877 * |
899 * |
878 * @param int $time_lastcomment Timestamp of when the last comment was posted. |
900 * @param int $time_lastcomment Timestamp of when the last comment was posted. |
879 * @param int $time_newcomment Timestamp of when the new comment was posted. |
901 * @param int $time_newcomment Timestamp of when the new comment was posted. |
880 */ |
902 */ |
881 do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); |
903 do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); |
|
904 |
882 if ( true === $avoid_die ) { |
905 if ( true === $avoid_die ) { |
883 return true; |
906 return true; |
884 } else { |
907 } else { |
|
908 /** |
|
909 * Filters the comment flood error message. |
|
910 * |
|
911 * @since 5.2.0 |
|
912 * |
|
913 * @param string $comment_flood_message Comment flood error message. |
|
914 */ |
|
915 $comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) ); |
|
916 |
885 if ( wp_doing_ajax() ) { |
917 if ( wp_doing_ajax() ) { |
886 die( __('You are posting comments too quickly. Slow down.') ); |
918 die( $comment_flood_message ); |
887 } |
919 } |
888 |
920 |
889 wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 ); |
921 wp_die( $comment_flood_message, 429 ); |
890 } |
922 } |
891 } |
923 } |
892 } |
924 } |
893 |
925 |
894 return false; |
926 return false; |
897 /** |
929 /** |
898 * Separates an array of comments into an array keyed by comment_type. |
930 * Separates an array of comments into an array keyed by comment_type. |
899 * |
931 * |
900 * @since 2.7.0 |
932 * @since 2.7.0 |
901 * |
933 * |
902 * @param array $comments Array of comments |
934 * @param WP_Comment[] $comments Array of comments |
903 * @return array Array of comments keyed by comment_type. |
935 * @return WP_Comment[] Array of comments keyed by comment_type. |
904 */ |
936 */ |
905 function separate_comments(&$comments) { |
937 function separate_comments( &$comments ) { |
906 $comments_by_type = array('comment' => array(), 'trackback' => array(), 'pingback' => array(), 'pings' => array()); |
938 $comments_by_type = array( |
907 $count = count($comments); |
939 'comment' => array(), |
|
940 'trackback' => array(), |
|
941 'pingback' => array(), |
|
942 'pings' => array(), |
|
943 ); |
|
944 $count = count( $comments ); |
908 for ( $i = 0; $i < $count; $i++ ) { |
945 for ( $i = 0; $i < $count; $i++ ) { |
909 $type = $comments[$i]->comment_type; |
946 $type = $comments[ $i ]->comment_type; |
910 if ( empty($type) ) |
947 if ( empty( $type ) ) { |
911 $type = 'comment'; |
948 $type = 'comment'; |
912 $comments_by_type[$type][] = &$comments[$i]; |
949 } |
913 if ( 'trackback' == $type || 'pingback' == $type ) |
950 $comments_by_type[ $type ][] = &$comments[ $i ]; |
914 $comments_by_type['pings'][] = &$comments[$i]; |
951 if ( 'trackback' == $type || 'pingback' == $type ) { |
|
952 $comments_by_type['pings'][] = &$comments[ $i ]; |
|
953 } |
915 } |
954 } |
916 |
955 |
917 return $comments_by_type; |
956 return $comments_by_type; |
918 } |
957 } |
919 |
958 |
924 * |
963 * |
925 * @uses Walker_Comment |
964 * @uses Walker_Comment |
926 * |
965 * |
927 * @global WP_Query $wp_query |
966 * @global WP_Query $wp_query |
928 * |
967 * |
929 * @param array $comments Optional array of WP_Comment objects. Defaults to $wp_query->comments |
968 * @param WP_Comment[] $comments Optional. Array of WP_Comment objects. Defaults to $wp_query->comments. |
930 * @param int $per_page Optional comments per page. |
969 * @param int $per_page Optional. Comments per page. |
931 * @param bool $threaded Optional control over flat or threaded comments. |
970 * @param bool $threaded Optional. Control over flat or threaded comments. |
932 * @return int Number of comment pages. |
971 * @return int Number of comment pages. |
933 */ |
972 */ |
934 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { |
973 function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) { |
935 global $wp_query; |
974 global $wp_query; |
936 |
975 |
937 if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) ) |
976 if ( null === $comments && null === $per_page && null === $threaded && ! empty( $wp_query->max_num_comment_pages ) ) { |
938 return $wp_query->max_num_comment_pages; |
977 return $wp_query->max_num_comment_pages; |
939 |
978 } |
940 if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) |
979 |
|
980 if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments ) ) { |
941 $comments = $wp_query->comments; |
981 $comments = $wp_query->comments; |
942 |
982 } |
943 if ( empty($comments) ) |
983 |
|
984 if ( empty( $comments ) ) { |
944 return 0; |
985 return 0; |
|
986 } |
945 |
987 |
946 if ( ! get_option( 'page_comments' ) ) { |
988 if ( ! get_option( 'page_comments' ) ) { |
947 return 1; |
989 return 1; |
948 } |
990 } |
949 |
991 |
950 if ( !isset($per_page) ) |
992 if ( ! isset( $per_page ) ) { |
951 $per_page = (int) get_query_var('comments_per_page'); |
993 $per_page = (int) get_query_var( 'comments_per_page' ); |
952 if ( 0 === $per_page ) |
994 } |
953 $per_page = (int) get_option('comments_per_page'); |
995 if ( 0 === $per_page ) { |
954 if ( 0 === $per_page ) |
996 $per_page = (int) get_option( 'comments_per_page' ); |
|
997 } |
|
998 if ( 0 === $per_page ) { |
955 return 1; |
999 return 1; |
956 |
1000 } |
957 if ( !isset($threaded) ) |
1001 |
958 $threaded = get_option('thread_comments'); |
1002 if ( ! isset( $threaded ) ) { |
|
1003 $threaded = get_option( 'thread_comments' ); |
|
1004 } |
959 |
1005 |
960 if ( $threaded ) { |
1006 if ( $threaded ) { |
961 $walker = new Walker_Comment; |
1007 $walker = new Walker_Comment; |
962 $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); |
1008 $count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page ); |
963 } else { |
1009 } else { |
964 $count = ceil( count( $comments ) / $per_page ); |
1010 $count = ceil( count( $comments ) / $per_page ); |
965 } |
1011 } |
966 |
1012 |
967 return $count; |
1013 return $count; |
990 function get_page_of_comment( $comment_ID, $args = array() ) { |
1036 function get_page_of_comment( $comment_ID, $args = array() ) { |
991 global $wpdb; |
1037 global $wpdb; |
992 |
1038 |
993 $page = null; |
1039 $page = null; |
994 |
1040 |
995 if ( !$comment = get_comment( $comment_ID ) ) |
1041 if ( ! $comment = get_comment( $comment_ID ) ) { |
996 return; |
1042 return; |
997 |
1043 } |
998 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' ); |
1044 |
999 $args = wp_parse_args( $args, $defaults ); |
1045 $defaults = array( |
|
1046 'type' => 'all', |
|
1047 'page' => '', |
|
1048 'per_page' => '', |
|
1049 'max_depth' => '', |
|
1050 ); |
|
1051 $args = wp_parse_args( $args, $defaults ); |
1000 $original_args = $args; |
1052 $original_args = $args; |
1001 |
1053 |
1002 // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option. |
1054 // Order of precedence: 1. `$args['per_page']`, 2. 'comments_per_page' query_var, 3. 'comments_per_page' option. |
1003 if ( get_option( 'page_comments' ) ) { |
1055 if ( get_option( 'page_comments' ) ) { |
1004 if ( '' === $args['per_page'] ) { |
1056 if ( '' === $args['per_page'] ) { |
1008 if ( '' === $args['per_page'] ) { |
1060 if ( '' === $args['per_page'] ) { |
1009 $args['per_page'] = get_option( 'comments_per_page' ); |
1061 $args['per_page'] = get_option( 'comments_per_page' ); |
1010 } |
1062 } |
1011 } |
1063 } |
1012 |
1064 |
1013 if ( empty($args['per_page']) ) { |
1065 if ( empty( $args['per_page'] ) ) { |
1014 $args['per_page'] = 0; |
1066 $args['per_page'] = 0; |
1015 $args['page'] = 0; |
1067 $args['page'] = 0; |
1016 } |
1068 } |
1017 |
1069 |
1018 if ( $args['per_page'] < 1 ) { |
1070 if ( $args['per_page'] < 1 ) { |
1019 $page = 1; |
1071 $page = 1; |
1020 } |
1072 } |
1021 |
1073 |
1022 if ( null === $page ) { |
1074 if ( null === $page ) { |
1023 if ( '' === $args['max_depth'] ) { |
1075 if ( '' === $args['max_depth'] ) { |
1024 if ( get_option('thread_comments') ) |
1076 if ( get_option( 'thread_comments' ) ) { |
1025 $args['max_depth'] = get_option('thread_comments_depth'); |
1077 $args['max_depth'] = get_option( 'thread_comments_depth' ); |
1026 else |
1078 } else { |
1027 $args['max_depth'] = -1; |
1079 $args['max_depth'] = -1; |
|
1080 } |
1028 } |
1081 } |
1029 |
1082 |
1030 // Find this comment's top level parent if threading is enabled |
1083 // Find this comment's top level parent if threading is enabled |
1031 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) |
1084 if ( $args['max_depth'] > 1 && 0 != $comment->comment_parent ) { |
1032 return get_page_of_comment( $comment->comment_parent, $args ); |
1085 return get_page_of_comment( $comment->comment_parent, $args ); |
|
1086 } |
1033 |
1087 |
1034 $comment_args = array( |
1088 $comment_args = array( |
1035 'type' => $args['type'], |
1089 'type' => $args['type'], |
1036 'post_id' => $comment->comment_post_ID, |
1090 'post_id' => $comment->comment_post_ID, |
1037 'fields' => 'ids', |
1091 'fields' => 'ids', |
1201 * @param string $user_ip Comment author's IP address. |
1255 * @param string $user_ip Comment author's IP address. |
1202 * @param string $user_agent Comment author's browser user agent. |
1256 * @param string $user_agent Comment author's browser user agent. |
1203 */ |
1257 */ |
1204 do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent ); |
1258 do_action( 'wp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent ); |
1205 |
1259 |
1206 $mod_keys = trim( get_option('blacklist_keys') ); |
1260 $mod_keys = trim( get_option( 'blacklist_keys' ) ); |
1207 if ( '' == $mod_keys ) |
1261 if ( '' == $mod_keys ) { |
1208 return false; // If moderation keys are empty |
1262 return false; // If moderation keys are empty |
|
1263 } |
1209 |
1264 |
1210 // Ensure HTML tags are not being used to bypass the blacklist. |
1265 // Ensure HTML tags are not being used to bypass the blacklist. |
1211 $comment_without_html = wp_strip_all_tags( $comment ); |
1266 $comment_without_html = wp_strip_all_tags( $comment ); |
1212 |
1267 |
1213 $words = explode("\n", $mod_keys ); |
1268 $words = explode( "\n", $mod_keys ); |
1214 |
1269 |
1215 foreach ( (array) $words as $word ) { |
1270 foreach ( (array) $words as $word ) { |
1216 $word = trim($word); |
1271 $word = trim( $word ); |
1217 |
1272 |
1218 // Skip empty lines |
1273 // Skip empty lines |
1219 if ( empty($word) ) { continue; } |
1274 if ( empty( $word ) ) { |
|
1275 continue; } |
1220 |
1276 |
1221 // Do some escaping magic so that '#' chars in the |
1277 // Do some escaping magic so that '#' chars in the |
1222 // spam words don't break things: |
1278 // spam words don't break things: |
1223 $word = preg_quote($word, '#'); |
1279 $word = preg_quote( $word, '#' ); |
1224 |
1280 |
1225 $pattern = "#$word#i"; |
1281 $pattern = "#$word#i"; |
1226 if ( |
1282 if ( preg_match( $pattern, $author ) |
1227 preg_match($pattern, $author) |
1283 || preg_match( $pattern, $email ) |
1228 || preg_match($pattern, $email) |
1284 || preg_match( $pattern, $url ) |
1229 || preg_match($pattern, $url) |
1285 || preg_match( $pattern, $comment ) |
1230 || preg_match($pattern, $comment) |
1286 || preg_match( $pattern, $comment_without_html ) |
1231 || preg_match($pattern, $comment_without_html) |
1287 || preg_match( $pattern, $user_ip ) |
1232 || preg_match($pattern, $user_ip) |
1288 || preg_match( $pattern, $user_agent ) |
1233 || preg_match($pattern, $user_agent) |
1289 ) { |
1234 ) |
|
1235 return true; |
1290 return true; |
|
1291 } |
1236 } |
1292 } |
1237 return false; |
1293 return false; |
1238 } |
1294 } |
1239 |
1295 |
1240 /** |
1296 /** |
1299 * |
1355 * |
1300 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. |
1356 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object. |
1301 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false. |
1357 * @param bool $force_delete Whether to bypass trash and force deletion. Default is false. |
1302 * @return bool True on success, false on failure. |
1358 * @return bool True on success, false on failure. |
1303 */ |
1359 */ |
1304 function wp_delete_comment($comment_id, $force_delete = false) { |
1360 function wp_delete_comment( $comment_id, $force_delete = false ) { |
1305 global $wpdb; |
1361 global $wpdb; |
1306 if (!$comment = get_comment($comment_id)) |
1362 if ( ! $comment = get_comment( $comment_id ) ) { |
1307 return false; |
1363 return false; |
1308 |
1364 } |
1309 if ( !$force_delete && EMPTY_TRASH_DAYS && !in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) ) |
1365 |
1310 return wp_trash_comment($comment_id); |
1366 if ( ! $force_delete && EMPTY_TRASH_DAYS && ! in_array( wp_get_comment_status( $comment ), array( 'trash', 'spam' ) ) ) { |
|
1367 return wp_trash_comment( $comment_id ); |
|
1368 } |
1311 |
1369 |
1312 /** |
1370 /** |
1313 * Fires immediately before a comment is deleted from the database. |
1371 * Fires immediately before a comment is deleted from the database. |
1314 * |
1372 * |
1315 * @since 1.2.0 |
1373 * @since 1.2.0 |
1319 * @param WP_Comment $comment The comment to be deleted. |
1377 * @param WP_Comment $comment The comment to be deleted. |
1320 */ |
1378 */ |
1321 do_action( 'delete_comment', $comment->comment_ID, $comment ); |
1379 do_action( 'delete_comment', $comment->comment_ID, $comment ); |
1322 |
1380 |
1323 // Move children up a level. |
1381 // Move children up a level. |
1324 $children = $wpdb->get_col( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID) ); |
1382 $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) ); |
1325 if ( !empty($children) ) { |
1383 if ( ! empty( $children ) ) { |
1326 $wpdb->update($wpdb->comments, array('comment_parent' => $comment->comment_parent), array('comment_parent' => $comment->comment_ID)); |
1384 $wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) ); |
1327 clean_comment_cache($children); |
1385 clean_comment_cache( $children ); |
1328 } |
1386 } |
1329 |
1387 |
1330 // Delete metadata |
1388 // Delete metadata |
1331 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); |
1389 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->commentmeta WHERE comment_id = %d", $comment->comment_ID ) ); |
1332 foreach ( $meta_ids as $mid ) |
1390 foreach ( $meta_ids as $mid ) { |
1333 delete_metadata_by_mid( 'comment', $mid ); |
1391 delete_metadata_by_mid( 'comment', $mid ); |
1334 |
1392 } |
1335 if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) |
1393 |
|
1394 if ( ! $wpdb->delete( $wpdb->comments, array( 'comment_ID' => $comment->comment_ID ) ) ) { |
1336 return false; |
1395 return false; |
|
1396 } |
1337 |
1397 |
1338 /** |
1398 /** |
1339 * Fires immediately after a comment is deleted from the database. |
1399 * Fires immediately after a comment is deleted from the database. |
1340 * |
1400 * |
1341 * @since 2.9.0 |
1401 * @since 2.9.0 |
1345 * @param WP_Comment $comment The deleted comment. |
1405 * @param WP_Comment $comment The deleted comment. |
1346 */ |
1406 */ |
1347 do_action( 'deleted_comment', $comment->comment_ID, $comment ); |
1407 do_action( 'deleted_comment', $comment->comment_ID, $comment ); |
1348 |
1408 |
1349 $post_id = $comment->comment_post_ID; |
1409 $post_id = $comment->comment_post_ID; |
1350 if ( $post_id && $comment->comment_approved == 1 ) |
1410 if ( $post_id && $comment->comment_approved == 1 ) { |
1351 wp_update_comment_count($post_id); |
1411 wp_update_comment_count( $post_id ); |
|
1412 } |
1352 |
1413 |
1353 clean_comment_cache( $comment->comment_ID ); |
1414 clean_comment_cache( $comment->comment_ID ); |
1354 |
1415 |
1355 /** This action is documented in wp-includes/comment.php */ |
1416 /** This action is documented in wp-includes/comment.php */ |
1356 do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' ); |
1417 do_action( 'wp_set_comment_status', $comment->comment_ID, 'delete' ); |
1357 |
1418 |
1358 wp_transition_comment_status('delete', $comment->comment_approved, $comment); |
1419 wp_transition_comment_status( 'delete', $comment->comment_approved, $comment ); |
1359 return true; |
1420 return true; |
1360 } |
1421 } |
1361 |
1422 |
1362 /** |
1423 /** |
1363 * Moves a comment to the Trash |
1424 * Moves a comment to the Trash |
1433 * @param WP_Comment $comment The comment to be untrashed. |
1496 * @param WP_Comment $comment The comment to be untrashed. |
1434 */ |
1497 */ |
1435 do_action( 'untrash_comment', $comment->comment_ID, $comment ); |
1498 do_action( 'untrash_comment', $comment->comment_ID, $comment ); |
1436 |
1499 |
1437 $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ); |
1500 $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ); |
1438 if ( empty($status) ) |
1501 if ( empty( $status ) ) { |
1439 $status = '0'; |
1502 $status = '0'; |
|
1503 } |
1440 |
1504 |
1441 if ( wp_set_comment_status( $comment, $status ) ) { |
1505 if ( wp_set_comment_status( $comment, $status ) ) { |
1442 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); |
1506 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); |
1443 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); |
1507 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); |
1444 /** |
1508 /** |
1527 * @param WP_Comment $comment The comment to be unmarked as spam. |
1591 * @param WP_Comment $comment The comment to be unmarked as spam. |
1528 */ |
1592 */ |
1529 do_action( 'unspam_comment', $comment->comment_ID, $comment ); |
1593 do_action( 'unspam_comment', $comment->comment_ID, $comment ); |
1530 |
1594 |
1531 $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ); |
1595 $status = (string) get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ); |
1532 if ( empty($status) ) |
1596 if ( empty( $status ) ) { |
1533 $status = '0'; |
1597 $status = '0'; |
|
1598 } |
1534 |
1599 |
1535 if ( wp_set_comment_status( $comment, $status ) ) { |
1600 if ( wp_set_comment_status( $comment, $status ) ) { |
1536 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); |
1601 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_status' ); |
1537 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); |
1602 delete_comment_meta( $comment->comment_ID, '_wp_trash_meta_time' ); |
1538 /** |
1603 /** |
1557 * @since 1.0.0 |
1622 * @since 1.0.0 |
1558 * |
1623 * |
1559 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object |
1624 * @param int|WP_Comment $comment_id Comment ID or WP_Comment object |
1560 * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure. |
1625 * @return false|string Status might be 'trash', 'approved', 'unapproved', 'spam'. False on failure. |
1561 */ |
1626 */ |
1562 function wp_get_comment_status($comment_id) { |
1627 function wp_get_comment_status( $comment_id ) { |
1563 $comment = get_comment($comment_id); |
1628 $comment = get_comment( $comment_id ); |
1564 if ( !$comment ) |
1629 if ( ! $comment ) { |
1565 return false; |
1630 return false; |
|
1631 } |
1566 |
1632 |
1567 $approved = $comment->comment_approved; |
1633 $approved = $comment->comment_approved; |
1568 |
1634 |
1569 if ( $approved == null ) |
1635 if ( $approved == null ) { |
1570 return false; |
1636 return false; |
1571 elseif ( $approved == '1' ) |
1637 } elseif ( $approved == '1' ) { |
1572 return 'approved'; |
1638 return 'approved'; |
1573 elseif ( $approved == '0' ) |
1639 } elseif ( $approved == '0' ) { |
1574 return 'unapproved'; |
1640 return 'unapproved'; |
1575 elseif ( $approved == 'spam' ) |
1641 } elseif ( $approved == 'spam' ) { |
1576 return 'spam'; |
1642 return 'spam'; |
1577 elseif ( $approved == 'trash' ) |
1643 } elseif ( $approved == 'trash' ) { |
1578 return 'trash'; |
1644 return 'trash'; |
1579 else |
1645 } else { |
1580 return false; |
1646 return false; |
|
1647 } |
1581 } |
1648 } |
1582 |
1649 |
1583 /** |
1650 /** |
1584 * Call hooks for when a comment status transition occurs. |
1651 * Call hooks for when a comment status transition occurs. |
1585 * |
1652 * |
1688 */ |
1759 */ |
1689 function wp_get_current_commenter() { |
1760 function wp_get_current_commenter() { |
1690 // Cookies should already be sanitized. |
1761 // Cookies should already be sanitized. |
1691 |
1762 |
1692 $comment_author = ''; |
1763 $comment_author = ''; |
1693 if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) ) |
1764 if ( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) ) { |
1694 $comment_author = $_COOKIE['comment_author_'.COOKIEHASH]; |
1765 $comment_author = $_COOKIE[ 'comment_author_' . COOKIEHASH ]; |
|
1766 } |
1695 |
1767 |
1696 $comment_author_email = ''; |
1768 $comment_author_email = ''; |
1697 if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) ) |
1769 if ( isset( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { |
1698 $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH]; |
1770 $comment_author_email = $_COOKIE[ 'comment_author_email_' . COOKIEHASH ]; |
|
1771 } |
1699 |
1772 |
1700 $comment_author_url = ''; |
1773 $comment_author_url = ''; |
1701 if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) ) |
1774 if ( isset( $_COOKIE[ 'comment_author_url_' . COOKIEHASH ] ) ) { |
1702 $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH]; |
1775 $comment_author_url = $_COOKIE[ 'comment_author_url_' . COOKIEHASH ]; |
|
1776 } |
1703 |
1777 |
1704 /** |
1778 /** |
1705 * Filters the current commenter's name, email, and URL. |
1779 * Filters the current commenter's name, email, and URL. |
1706 * |
1780 * |
1707 * @since 3.1.0 |
1781 * @since 3.1.0 |
1712 * @type string $comment_author The name of the author of the comment. Default empty. |
1786 * @type string $comment_author The name of the author of the comment. Default empty. |
1713 * @type string $comment_author_email The email address of the `$comment_author`. Default empty. |
1787 * @type string $comment_author_email The email address of the `$comment_author`. Default empty. |
1714 * @type string $comment_author_url The URL address of the `$comment_author`. Default empty. |
1788 * @type string $comment_author_url The URL address of the `$comment_author`. Default empty. |
1715 * } |
1789 * } |
1716 */ |
1790 */ |
1717 return apply_filters( 'wp_get_current_commenter', compact('comment_author', 'comment_author_email', 'comment_author_url') ); |
1791 return apply_filters( 'wp_get_current_commenter', compact( 'comment_author', 'comment_author_email', 'comment_author_url' ) ); |
|
1792 } |
|
1793 |
|
1794 /** |
|
1795 * Get unapproved comment author's email. |
|
1796 * |
|
1797 * Used to allow the commenter to see their pending comment. |
|
1798 * |
|
1799 * @since 5.1.0 |
|
1800 * |
|
1801 * @return string The unapproved comment author's email (when supplied). |
|
1802 */ |
|
1803 function wp_get_unapproved_comment_author_email() { |
|
1804 $commenter_email = ''; |
|
1805 |
|
1806 if ( ! empty( $_GET['unapproved'] ) && ! empty( $_GET['moderation-hash'] ) ) { |
|
1807 $comment_id = (int) $_GET['unapproved']; |
|
1808 $comment = get_comment( $comment_id ); |
|
1809 |
|
1810 if ( $comment && hash_equals( $_GET['moderation-hash'], wp_hash( $comment->comment_date_gmt ) ) ) { |
|
1811 $commenter_email = $comment->comment_author_email; |
|
1812 } |
|
1813 } |
|
1814 |
|
1815 if ( ! $commenter_email ) { |
|
1816 $commenter = wp_get_current_commenter(); |
|
1817 $commenter_email = $commenter['comment_author_email']; |
|
1818 } |
|
1819 |
|
1820 return $commenter_email; |
1718 } |
1821 } |
1719 |
1822 |
1720 /** |
1823 /** |
1721 * Inserts a comment into the database. |
1824 * Inserts a comment into the database. |
1722 * |
1825 * |
1754 */ |
1857 */ |
1755 function wp_insert_comment( $commentdata ) { |
1858 function wp_insert_comment( $commentdata ) { |
1756 global $wpdb; |
1859 global $wpdb; |
1757 $data = wp_unslash( $commentdata ); |
1860 $data = wp_unslash( $commentdata ); |
1758 |
1861 |
1759 $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author']; |
1862 $comment_author = ! isset( $data['comment_author'] ) ? '' : $data['comment_author']; |
1760 $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email']; |
1863 $comment_author_email = ! isset( $data['comment_author_email'] ) ? '' : $data['comment_author_email']; |
1761 $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url']; |
1864 $comment_author_url = ! isset( $data['comment_author_url'] ) ? '' : $data['comment_author_url']; |
1762 $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP']; |
1865 $comment_author_IP = ! isset( $data['comment_author_IP'] ) ? '' : $data['comment_author_IP']; |
1763 |
1866 |
1764 $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date']; |
1867 $comment_date = ! isset( $data['comment_date'] ) ? current_time( 'mysql' ) : $data['comment_date']; |
1765 $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt']; |
1868 $comment_date_gmt = ! isset( $data['comment_date_gmt'] ) ? get_gmt_from_date( $comment_date ) : $data['comment_date_gmt']; |
1766 |
1869 |
1767 $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID']; |
1870 $comment_post_ID = ! isset( $data['comment_post_ID'] ) ? 0 : $data['comment_post_ID']; |
1768 $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content']; |
1871 $comment_content = ! isset( $data['comment_content'] ) ? '' : $data['comment_content']; |
1769 $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; |
1872 $comment_karma = ! isset( $data['comment_karma'] ) ? 0 : $data['comment_karma']; |
1770 $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; |
1873 $comment_approved = ! isset( $data['comment_approved'] ) ? 1 : $data['comment_approved']; |
1771 $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; |
1874 $comment_agent = ! isset( $data['comment_agent'] ) ? '' : $data['comment_agent']; |
1772 $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type']; |
1875 $comment_type = ! isset( $data['comment_type'] ) ? '' : $data['comment_type']; |
1773 $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; |
1876 $comment_parent = ! isset( $data['comment_parent'] ) ? 0 : $data['comment_parent']; |
1774 |
1877 |
1775 $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; |
1878 $user_id = ! isset( $data['user_id'] ) ? 0 : $data['user_id']; |
1776 |
1879 |
1777 $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' ); |
1880 $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' ); |
1778 if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) { |
1881 if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) { |
1779 return false; |
1882 return false; |
1780 } |
1883 } |
1871 $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] ); |
1974 $commentdata['comment_author_IP'] = apply_filters( 'pre_comment_user_ip', $commentdata['comment_author_IP'] ); |
1872 /** This filter is documented in wp-includes/comment.php */ |
1975 /** This filter is documented in wp-includes/comment.php */ |
1873 $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] ); |
1976 $commentdata['comment_author_url'] = apply_filters( 'pre_comment_author_url', $commentdata['comment_author_url'] ); |
1874 /** This filter is documented in wp-includes/comment.php */ |
1977 /** This filter is documented in wp-includes/comment.php */ |
1875 $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] ); |
1978 $commentdata['comment_author_email'] = apply_filters( 'pre_comment_author_email', $commentdata['comment_author_email'] ); |
1876 $commentdata['filtered'] = true; |
1979 $commentdata['filtered'] = true; |
1877 return $commentdata; |
1980 return $commentdata; |
1878 } |
1981 } |
1879 |
1982 |
1880 /** |
1983 /** |
1881 * Whether a comment should be blocked because of comment flood. |
1984 * Whether a comment should be blocked because of comment flood. |
1962 $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID']; |
2067 $commentdata['user_id'] = $commentdata['user_ID'] = (int) $commentdata['user_ID']; |
1963 } elseif ( isset( $commentdata['user_id'] ) ) { |
2068 } elseif ( isset( $commentdata['user_id'] ) ) { |
1964 $commentdata['user_id'] = (int) $commentdata['user_id']; |
2069 $commentdata['user_id'] = (int) $commentdata['user_id']; |
1965 } |
2070 } |
1966 |
2071 |
1967 $commentdata['comment_parent'] = isset($commentdata['comment_parent']) ? absint($commentdata['comment_parent']) : 0; |
2072 $commentdata['comment_parent'] = isset( $commentdata['comment_parent'] ) ? absint( $commentdata['comment_parent'] ) : 0; |
1968 $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : ''; |
2073 $parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status( $commentdata['comment_parent'] ) : ''; |
1969 $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0; |
2074 $commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0; |
1970 |
2075 |
1971 if ( ! isset( $commentdata['comment_author_IP'] ) ) { |
2076 if ( ! isset( $commentdata['comment_author_IP'] ) ) { |
1972 $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; |
2077 $commentdata['comment_author_IP'] = $_SERVER['REMOTE_ADDR']; |
1973 } |
2078 } |
1974 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); |
2079 $commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '', $commentdata['comment_author_IP'] ); |
1975 |
2080 |
1976 if ( ! isset( $commentdata['comment_agent'] ) ) { |
2081 if ( ! isset( $commentdata['comment_agent'] ) ) { |
1977 $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT']: ''; |
2082 $commentdata['comment_agent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
1978 } |
2083 } |
1979 $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); |
2084 $commentdata['comment_agent'] = substr( $commentdata['comment_agent'], 0, 254 ); |
1980 |
2085 |
1981 if ( empty( $commentdata['comment_date'] ) ) { |
2086 if ( empty( $commentdata['comment_date'] ) ) { |
1982 $commentdata['comment_date'] = current_time('mysql'); |
2087 $commentdata['comment_date'] = current_time( 'mysql' ); |
1983 } |
2088 } |
1984 |
2089 |
1985 if ( empty( $commentdata['comment_date_gmt'] ) ) { |
2090 if ( empty( $commentdata['comment_date_gmt'] ) ) { |
1986 $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 ); |
2091 $commentdata['comment_date_gmt'] = current_time( 'mysql', 1 ); |
1987 } |
2092 } |
1988 |
2093 |
1989 $commentdata = wp_filter_comment($commentdata); |
2094 $commentdata = wp_filter_comment( $commentdata ); |
1990 |
2095 |
1991 $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die ); |
2096 $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die ); |
1992 if ( is_wp_error( $commentdata['comment_approved'] ) ) { |
2097 if ( is_wp_error( $commentdata['comment_approved'] ) ) { |
1993 return $commentdata['comment_approved']; |
2098 return $commentdata['comment_approved']; |
1994 } |
2099 } |
1995 |
2100 |
1996 $comment_ID = wp_insert_comment($commentdata); |
2101 $comment_ID = wp_insert_comment( $commentdata ); |
1997 if ( ! $comment_ID ) { |
2102 if ( ! $comment_ID ) { |
1998 $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' ); |
2103 $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' ); |
1999 |
2104 |
2000 foreach ( $fields as $field ) { |
2105 foreach ( $fields as $field ) { |
2001 if ( isset( $commentdata[ $field ] ) ) { |
2106 if ( isset( $commentdata[ $field ] ) ) { |
2179 * @global wpdb $wpdb WordPress database abstraction object. |
2285 * @global wpdb $wpdb WordPress database abstraction object. |
2180 * |
2286 * |
2181 * @param array $commentarr Contains information on the comment. |
2287 * @param array $commentarr Contains information on the comment. |
2182 * @return int Comment was updated if value is 1, or was not updated if value is 0. |
2288 * @return int Comment was updated if value is 1, or was not updated if value is 0. |
2183 */ |
2289 */ |
2184 function wp_update_comment($commentarr) { |
2290 function wp_update_comment( $commentarr ) { |
2185 global $wpdb; |
2291 global $wpdb; |
2186 |
2292 |
2187 // First, get all of the original fields |
2293 // First, get all of the original fields |
2188 $comment = get_comment($commentarr['comment_ID'], ARRAY_A); |
2294 $comment = get_comment( $commentarr['comment_ID'], ARRAY_A ); |
2189 if ( empty( $comment ) ) { |
2295 if ( empty( $comment ) ) { |
2190 return 0; |
2296 return 0; |
2191 } |
2297 } |
2192 |
2298 |
2193 // Make sure that the comment post ID is valid (if specified). |
2299 // Make sure that the comment post ID is valid (if specified). |
2194 if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { |
2300 if ( ! empty( $commentarr['comment_post_ID'] ) && ! get_post( $commentarr['comment_post_ID'] ) ) { |
2195 return 0; |
2301 return 0; |
2196 } |
2302 } |
2197 |
2303 |
2198 // Escape data pulled from DB. |
2304 // Escape data pulled from DB. |
2199 $comment = wp_slash($comment); |
2305 $comment = wp_slash( $comment ); |
2200 |
2306 |
2201 $old_status = $comment['comment_approved']; |
2307 $old_status = $comment['comment_approved']; |
2202 |
2308 |
2203 // Merge old and new fields with new fields overwriting old ones. |
2309 // Merge old and new fields with new fields overwriting old ones. |
2204 $commentarr = array_merge($comment, $commentarr); |
2310 $commentarr = array_merge( $comment, $commentarr ); |
2205 |
2311 |
2206 $commentarr = wp_filter_comment( $commentarr ); |
2312 $commentarr = wp_filter_comment( $commentarr ); |
2207 |
2313 |
2208 // Now extract the merged array. |
2314 // Now extract the merged array. |
2209 $data = wp_unslash( $commentarr ); |
2315 $data = wp_unslash( $commentarr ); |
2321 * @param bool $do_deferred Optional. Whether to process previously deferred |
2428 * @param bool $do_deferred Optional. Whether to process previously deferred |
2322 * post comment counts. Default false. |
2429 * post comment counts. Default false. |
2323 * @return bool|void True on success, false on failure or if post with ID does |
2430 * @return bool|void True on success, false on failure or if post with ID does |
2324 * not exist. |
2431 * not exist. |
2325 */ |
2432 */ |
2326 function wp_update_comment_count($post_id, $do_deferred=false) { |
2433 function wp_update_comment_count( $post_id, $do_deferred = false ) { |
2327 static $_deferred = array(); |
2434 static $_deferred = array(); |
2328 |
2435 |
2329 if ( empty( $post_id ) && ! $do_deferred ) { |
2436 if ( empty( $post_id ) && ! $do_deferred ) { |
2330 return false; |
2437 return false; |
2331 } |
2438 } |
2332 |
2439 |
2333 if ( $do_deferred ) { |
2440 if ( $do_deferred ) { |
2334 $_deferred = array_unique($_deferred); |
2441 $_deferred = array_unique( $_deferred ); |
2335 foreach ( $_deferred as $i => $_post_id ) { |
2442 foreach ( $_deferred as $i => $_post_id ) { |
2336 wp_update_comment_count_now($_post_id); |
2443 wp_update_comment_count_now( $_post_id ); |
2337 unset( $_deferred[$i] ); /** @todo Move this outside of the foreach and reset $_deferred to an array instead */ |
2444 unset( $_deferred[ $i ] ); |
|
2445 /** @todo Move this outside of the foreach and reset $_deferred to an array instead */ |
2338 } |
2446 } |
2339 } |
2447 } |
2340 |
2448 |
2341 if ( wp_defer_comment_counting() ) { |
2449 if ( wp_defer_comment_counting() ) { |
2342 $_deferred[] = $post_id; |
2450 $_deferred[] = $post_id; |
2343 return true; |
2451 return true; |
2344 } |
2452 } elseif ( $post_id ) { |
2345 elseif ( $post_id ) { |
2453 return wp_update_comment_count_now( $post_id ); |
2346 return wp_update_comment_count_now($post_id); |
|
2347 } |
2454 } |
2348 |
2455 |
2349 } |
2456 } |
2350 |
2457 |
2351 /** |
2458 /** |
2356 * @global wpdb $wpdb WordPress database abstraction object. |
2463 * @global wpdb $wpdb WordPress database abstraction object. |
2357 * |
2464 * |
2358 * @param int $post_id Post ID |
2465 * @param int $post_id Post ID |
2359 * @return bool True on success, false on '0' $post_id or if post with ID does not exist. |
2466 * @return bool True on success, false on '0' $post_id or if post with ID does not exist. |
2360 */ |
2467 */ |
2361 function wp_update_comment_count_now($post_id) { |
2468 function wp_update_comment_count_now( $post_id ) { |
2362 global $wpdb; |
2469 global $wpdb; |
2363 $post_id = (int) $post_id; |
2470 $post_id = (int) $post_id; |
2364 if ( !$post_id ) |
2471 if ( ! $post_id ) { |
2365 return false; |
2472 return false; |
|
2473 } |
2366 |
2474 |
2367 wp_cache_delete( 'comments-0', 'counts' ); |
2475 wp_cache_delete( 'comments-0', 'counts' ); |
2368 wp_cache_delete( "comments-{$post_id}", 'counts' ); |
2476 wp_cache_delete( "comments-{$post_id}", 'counts' ); |
2369 |
2477 |
2370 if ( !$post = get_post($post_id) ) |
2478 if ( ! $post = get_post( $post_id ) ) { |
2371 return false; |
2479 return false; |
|
2480 } |
2372 |
2481 |
2373 $old = (int) $post->comment_count; |
2482 $old = (int) $post->comment_count; |
2374 |
2483 |
2375 /** |
2484 /** |
2376 * Filters a post's comment count before it is updated in the database. |
2485 * Filters a post's comment count before it is updated in the database. |
2425 * @param string $url URL to ping. |
2538 * @param string $url URL to ping. |
2426 * @param int $deprecated Not Used. |
2539 * @param int $deprecated Not Used. |
2427 * @return false|string False on failure, string containing URI on success. |
2540 * @return false|string False on failure, string containing URI on success. |
2428 */ |
2541 */ |
2429 function discover_pingback_server_uri( $url, $deprecated = '' ) { |
2542 function discover_pingback_server_uri( $url, $deprecated = '' ) { |
2430 if ( !empty( $deprecated ) ) |
2543 if ( ! empty( $deprecated ) ) { |
2431 _deprecated_argument( __FUNCTION__, '2.7.0' ); |
2544 _deprecated_argument( __FUNCTION__, '2.7.0' ); |
|
2545 } |
2432 |
2546 |
2433 $pingback_str_dquote = 'rel="pingback"'; |
2547 $pingback_str_dquote = 'rel="pingback"'; |
2434 $pingback_str_squote = 'rel=\'pingback\''; |
2548 $pingback_str_squote = 'rel=\'pingback\''; |
2435 |
2549 |
2436 /** @todo Should use Filter Extension or custom preg_match instead. */ |
2550 /** @todo Should use Filter Extension or custom preg_match instead. */ |
2437 $parsed_url = parse_url($url); |
2551 $parsed_url = parse_url( $url ); |
2438 |
2552 |
2439 if ( ! isset( $parsed_url['host'] ) ) // Not a URL. This should never happen. |
2553 if ( ! isset( $parsed_url['host'] ) ) { // Not a URL. This should never happen. |
2440 return false; |
2554 return false; |
|
2555 } |
2441 |
2556 |
2442 //Do not search for a pingback server on our own uploads |
2557 //Do not search for a pingback server on our own uploads |
2443 $uploads_dir = wp_get_upload_dir(); |
2558 $uploads_dir = wp_get_upload_dir(); |
2444 if ( 0 === strpos($url, $uploads_dir['baseurl']) ) |
2559 if ( 0 === strpos( $url, $uploads_dir['baseurl'] ) ) { |
2445 return false; |
2560 return false; |
2446 |
2561 } |
2447 $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) ); |
2562 |
2448 |
2563 $response = wp_safe_remote_head( |
2449 if ( is_wp_error( $response ) ) |
2564 $url, |
|
2565 array( |
|
2566 'timeout' => 2, |
|
2567 'httpversion' => '1.0', |
|
2568 ) |
|
2569 ); |
|
2570 |
|
2571 if ( is_wp_error( $response ) ) { |
2450 return false; |
2572 return false; |
2451 |
2573 } |
2452 if ( wp_remote_retrieve_header( $response, 'x-pingback' ) ) |
2574 |
|
2575 if ( wp_remote_retrieve_header( $response, 'x-pingback' ) ) { |
2453 return wp_remote_retrieve_header( $response, 'x-pingback' ); |
2576 return wp_remote_retrieve_header( $response, 'x-pingback' ); |
|
2577 } |
2454 |
2578 |
2455 // Not an (x)html, sgml, or xml page, no use going further. |
2579 // Not an (x)html, sgml, or xml page, no use going further. |
2456 if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) ) |
2580 if ( preg_match( '#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' ) ) ) { |
2457 return false; |
2581 return false; |
|
2582 } |
2458 |
2583 |
2459 // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file) |
2584 // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file) |
2460 $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) ); |
2585 $response = wp_safe_remote_get( |
2461 |
2586 $url, |
2462 if ( is_wp_error( $response ) ) |
2587 array( |
|
2588 'timeout' => 2, |
|
2589 'httpversion' => '1.0', |
|
2590 ) |
|
2591 ); |
|
2592 |
|
2593 if ( is_wp_error( $response ) ) { |
2463 return false; |
2594 return false; |
|
2595 } |
2464 |
2596 |
2465 $contents = wp_remote_retrieve_body( $response ); |
2597 $contents = wp_remote_retrieve_body( $response ); |
2466 |
2598 |
2467 $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote); |
2599 $pingback_link_offset_dquote = strpos( $contents, $pingback_str_dquote ); |
2468 $pingback_link_offset_squote = strpos($contents, $pingback_str_squote); |
2600 $pingback_link_offset_squote = strpos( $contents, $pingback_str_squote ); |
2469 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { |
2601 if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { |
2470 $quote = ($pingback_link_offset_dquote) ? '"' : '\''; |
2602 $quote = ( $pingback_link_offset_dquote ) ? '"' : '\''; |
2471 $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote; |
2603 $pingback_link_offset = ( $quote == '"' ) ? $pingback_link_offset_dquote : $pingback_link_offset_squote; |
2472 $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset); |
2604 $pingback_href_pos = @strpos( $contents, 'href=', $pingback_link_offset ); |
2473 $pingback_href_start = $pingback_href_pos+6; |
2605 $pingback_href_start = $pingback_href_pos + 6; |
2474 $pingback_href_end = @strpos($contents, $quote, $pingback_href_start); |
2606 $pingback_href_end = @strpos( $contents, $quote, $pingback_href_start ); |
2475 $pingback_server_url_len = $pingback_href_end - $pingback_href_start; |
2607 $pingback_server_url_len = $pingback_href_end - $pingback_href_start; |
2476 $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len); |
2608 $pingback_server_url = substr( $contents, $pingback_href_start, $pingback_server_url_len ); |
2477 |
2609 |
2478 // We may find rel="pingback" but an incomplete pingback URL |
2610 // We may find rel="pingback" but an incomplete pingback URL |
2479 if ( $pingback_server_url_len > 0 ) { // We got it! |
2611 if ( $pingback_server_url_len > 0 ) { // We got it! |
2480 return $pingback_server_url; |
2612 return $pingback_server_url; |
2481 } |
2613 } |
2493 */ |
2625 */ |
2494 function do_all_pings() { |
2626 function do_all_pings() { |
2495 global $wpdb; |
2627 global $wpdb; |
2496 |
2628 |
2497 // Do pingbacks |
2629 // Do pingbacks |
2498 while ($ping = $wpdb->get_row("SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1")) { |
2630 while ( $ping = $wpdb->get_row( "SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_pingme' LIMIT 1" ) ) { |
2499 delete_metadata_by_mid( 'post', $ping->meta_id ); |
2631 delete_metadata_by_mid( 'post', $ping->meta_id ); |
2500 pingback( $ping->post_content, $ping->ID ); |
2632 pingback( $ping->post_content, $ping->ID ); |
2501 } |
2633 } |
2502 |
2634 |
2503 // Do Enclosures |
2635 // Do Enclosures |
2504 while ($enclosure = $wpdb->get_row("SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1")) { |
2636 while ( $enclosure = $wpdb->get_row( "SELECT ID, post_content, meta_id FROM {$wpdb->posts}, {$wpdb->postmeta} WHERE {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '_encloseme' LIMIT 1" ) ) { |
2505 delete_metadata_by_mid( 'post', $enclosure->meta_id ); |
2637 delete_metadata_by_mid( 'post', $enclosure->meta_id ); |
2506 do_enclose( $enclosure->post_content, $enclosure->ID ); |
2638 do_enclose( $enclosure->post_content, $enclosure->ID ); |
2507 } |
2639 } |
2508 |
2640 |
2509 // Do Trackbacks |
2641 // Do Trackbacks |
2510 $trackbacks = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'"); |
2642 $trackbacks = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE to_ping <> '' AND post_status = 'publish'" ); |
2511 if ( is_array($trackbacks) ) |
2643 if ( is_array( $trackbacks ) ) { |
2512 foreach ( $trackbacks as $trackback ) |
2644 foreach ( $trackbacks as $trackback ) { |
2513 do_trackbacks($trackback); |
2645 do_trackbacks( $trackback ); |
|
2646 } |
|
2647 } |
2514 |
2648 |
2515 //Do Update Services/Generic Pings |
2649 //Do Update Services/Generic Pings |
2516 generic_ping(); |
2650 generic_ping(); |
2517 } |
2651 } |
2518 |
2652 |
2519 /** |
2653 /** |
2520 * Perform trackbacks. |
2654 * Perform trackbacks. |
2521 * |
2655 * |
2522 * @since 1.5.0 |
2656 * @since 1.5.0 |
2523 * @since 4.7.0 $post_id can be a WP_Post object. |
2657 * @since 4.7.0 `$post_id` can be a WP_Post object. |
2524 * |
2658 * |
2525 * @global wpdb $wpdb WordPress database abstraction object. |
2659 * @global wpdb $wpdb WordPress database abstraction object. |
2526 * |
2660 * |
2527 * @param int|WP_Post $post_id Post object or ID to do trackbacks on. |
2661 * @param int|WP_Post $post_id Post object or ID to do trackbacks on. |
2528 */ |
2662 */ |
2534 } |
2668 } |
2535 |
2669 |
2536 $to_ping = get_to_ping( $post ); |
2670 $to_ping = get_to_ping( $post ); |
2537 $pinged = get_pung( $post ); |
2671 $pinged = get_pung( $post ); |
2538 if ( empty( $to_ping ) ) { |
2672 if ( empty( $to_ping ) ) { |
2539 $wpdb->update($wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) ); |
2673 $wpdb->update( $wpdb->posts, array( 'to_ping' => '' ), array( 'ID' => $post->ID ) ); |
2540 return; |
2674 return; |
2541 } |
2675 } |
2542 |
2676 |
2543 if ( empty($post->post_excerpt) ) { |
2677 if ( empty( $post->post_excerpt ) ) { |
2544 /** This filter is documented in wp-includes/post-template.php */ |
2678 /** This filter is documented in wp-includes/post-template.php */ |
2545 $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID ); |
2679 $excerpt = apply_filters( 'the_content', $post->post_content, $post->ID ); |
2546 } else { |
2680 } else { |
2547 /** This filter is documented in wp-includes/post-template.php */ |
2681 /** This filter is documented in wp-includes/post-template.php */ |
2548 $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt ); |
2682 $excerpt = apply_filters( 'the_excerpt', $post->post_excerpt ); |
2549 } |
2683 } |
2550 |
2684 |
2551 $excerpt = str_replace(']]>', ']]>', $excerpt); |
2685 $excerpt = str_replace( ']]>', ']]>', $excerpt ); |
2552 $excerpt = wp_html_excerpt($excerpt, 252, '…'); |
2686 $excerpt = wp_html_excerpt( $excerpt, 252, '…' ); |
2553 |
2687 |
2554 /** This filter is documented in wp-includes/post-template.php */ |
2688 /** This filter is documented in wp-includes/post-template.php */ |
2555 $post_title = apply_filters( 'the_title', $post->post_title, $post->ID ); |
2689 $post_title = apply_filters( 'the_title', $post->post_title, $post->ID ); |
2556 $post_title = strip_tags($post_title); |
2690 $post_title = strip_tags( $post_title ); |
2557 |
2691 |
2558 if ( $to_ping ) { |
2692 if ( $to_ping ) { |
2559 foreach ( (array) $to_ping as $tb_ping ) { |
2693 foreach ( (array) $to_ping as $tb_ping ) { |
2560 $tb_ping = trim($tb_ping); |
2694 $tb_ping = trim( $tb_ping ); |
2561 if ( !in_array($tb_ping, $pinged) ) { |
2695 if ( ! in_array( $tb_ping, $pinged ) ) { |
2562 trackback( $tb_ping, $post_title, $excerpt, $post->ID ); |
2696 trackback( $tb_ping, $post_title, $excerpt, $post->ID ); |
2563 $pinged[] = $tb_ping; |
2697 $pinged[] = $tb_ping; |
2564 } else { |
2698 } else { |
2565 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, |
2699 $wpdb->query( |
2566 '')) WHERE ID = %d", $tb_ping, $post->ID ) ); |
2700 $wpdb->prepare( |
|
2701 "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, |
|
2702 '')) WHERE ID = %d", |
|
2703 $tb_ping, |
|
2704 $post->ID |
|
2705 ) |
|
2706 ); |
2567 } |
2707 } |
2568 } |
2708 } |
2569 } |
2709 } |
2570 } |
2710 } |
2571 |
2711 |
2576 * |
2716 * |
2577 * @param int $post_id Post ID. |
2717 * @param int $post_id Post ID. |
2578 * @return int Same as Post ID from parameter |
2718 * @return int Same as Post ID from parameter |
2579 */ |
2719 */ |
2580 function generic_ping( $post_id = 0 ) { |
2720 function generic_ping( $post_id = 0 ) { |
2581 $services = get_option('ping_sites'); |
2721 $services = get_option( 'ping_sites' ); |
2582 |
2722 |
2583 $services = explode("\n", $services); |
2723 $services = explode( "\n", $services ); |
2584 foreach ( (array) $services as $service ) { |
2724 foreach ( (array) $services as $service ) { |
2585 $service = trim($service); |
2725 $service = trim( $service ); |
2586 if ( '' != $service ) |
2726 if ( '' != $service ) { |
2587 weblog_ping($service); |
2727 weblog_ping( $service ); |
|
2728 } |
2588 } |
2729 } |
2589 |
2730 |
2590 return $post_id; |
2731 return $post_id; |
2591 } |
2732 } |
2592 |
2733 |
2593 /** |
2734 /** |
2594 * Pings back the links found in a post. |
2735 * Pings back the links found in a post. |
2595 * |
2736 * |
2596 * @since 0.71 |
2737 * @since 0.71 |
2597 * @since 4.7.0 $post_id can be a WP_Post object. |
2738 * @since 4.7.0 `$post_id` can be a WP_Post object. |
2598 * |
2739 * |
2599 * @param string $content Post content to check for links. If empty will retrieve from post. |
2740 * @param string $content Post content to check for links. If empty will retrieve from post. |
2600 * @param int|WP_Post $post_id Post Object or ID. |
2741 * @param int|WP_Post $post_id Post Object or ID. |
2601 */ |
2742 */ |
2602 function pingback( $content, $post_id ) { |
2743 function pingback( $content, $post_id ) { |
2630 // http://dummy-weblog.org/post.php |
2771 // http://dummy-weblog.org/post.php |
2631 // We don't wanna ping first and second types, even if they have a valid <link/> |
2772 // We don't wanna ping first and second types, even if they have a valid <link/> |
2632 |
2773 |
2633 foreach ( (array) $post_links_temp as $link_test ) : |
2774 foreach ( (array) $post_links_temp as $link_test ) : |
2634 if ( ! in_array( $link_test, $pung ) && ( url_to_postid( $link_test ) != $post->ID ) // If we haven't pung it already and it isn't a link to itself |
2775 if ( ! in_array( $link_test, $pung ) && ( url_to_postid( $link_test ) != $post->ID ) // If we haven't pung it already and it isn't a link to itself |
2635 && !is_local_attachment($link_test) ) : // Also, let's never ping local attachments. |
2776 && ! is_local_attachment( $link_test ) ) : // Also, let's never ping local attachments. |
2636 if ( $test = @parse_url($link_test) ) { |
2777 if ( $test = @parse_url( $link_test ) ) { |
2637 if ( isset($test['query']) ) |
2778 if ( isset( $test['query'] ) ) { |
2638 $post_links[] = $link_test; |
2779 $post_links[] = $link_test; |
2639 elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) ) |
2780 } elseif ( isset( $test['path'] ) && ( $test['path'] != '/' ) && ( $test['path'] != '' ) ) { |
2640 $post_links[] = $link_test; |
2781 $post_links[] = $link_test; |
|
2782 } |
2641 } |
2783 } |
2642 endif; |
2784 endif; |
2643 endforeach; |
2785 endforeach; |
2644 |
2786 |
2645 $post_links = array_unique( $post_links ); |
2787 $post_links = array_unique( $post_links ); |
2646 /** |
2788 /** |
2647 * Fires just before pinging back links found in a post. |
2789 * Fires just before pinging back links found in a post. |
2648 * |
2790 * |
2649 * @since 2.0.0 |
2791 * @since 2.0.0 |
2650 * |
2792 * |
2651 * @param array $post_links An array of post links to be checked (passed by reference). |
2793 * @param string[] $post_links Array of link URLs to be checked (passed by reference). |
2652 * @param array $pung Whether a link has already been pinged (passed by reference). |
2794 * @param string[] $pung Array of link URLs already pinged (passed by reference). |
2653 * @param int $post_ID The post ID. |
2795 * @param int $post_ID The post ID. |
2654 */ |
2796 */ |
2655 do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) ); |
2797 do_action_ref_array( 'pre_ping', array( &$post_links, &$pung, $post->ID ) ); |
2656 |
2798 |
2657 foreach ( (array) $post_links as $pagelinkedto ) { |
2799 foreach ( (array) $post_links as $pagelinkedto ) { |
2658 $pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); |
2800 $pingback_server_url = discover_pingback_server_uri( $pagelinkedto ); |
2715 * @param string $title Title of post. |
2859 * @param string $title Title of post. |
2716 * @param string $excerpt Excerpt of post. |
2860 * @param string $excerpt Excerpt of post. |
2717 * @param int $ID Post ID. |
2861 * @param int $ID Post ID. |
2718 * @return int|false|void Database query from update. |
2862 * @return int|false|void Database query from update. |
2719 */ |
2863 */ |
2720 function trackback($trackback_url, $title, $excerpt, $ID) { |
2864 function trackback( $trackback_url, $title, $excerpt, $ID ) { |
2721 global $wpdb; |
2865 global $wpdb; |
2722 |
2866 |
2723 if ( empty($trackback_url) ) |
2867 if ( empty( $trackback_url ) ) { |
2724 return; |
2868 return; |
2725 |
2869 } |
2726 $options = array(); |
2870 |
|
2871 $options = array(); |
2727 $options['timeout'] = 10; |
2872 $options['timeout'] = 10; |
2728 $options['body'] = array( |
2873 $options['body'] = array( |
2729 'title' => $title, |
2874 'title' => $title, |
2730 'url' => get_permalink($ID), |
2875 'url' => get_permalink( $ID ), |
2731 'blog_name' => get_option('blogname'), |
2876 'blog_name' => get_option( 'blogname' ), |
2732 'excerpt' => $excerpt |
2877 'excerpt' => $excerpt, |
2733 ); |
2878 ); |
2734 |
2879 |
2735 $response = wp_safe_remote_post( $trackback_url, $options ); |
2880 $response = wp_safe_remote_post( $trackback_url, $options ); |
2736 |
2881 |
2737 if ( is_wp_error( $response ) ) |
2882 if ( is_wp_error( $response ) ) { |
2738 return; |
2883 return; |
2739 |
2884 } |
2740 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID) ); |
2885 |
2741 return $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID) ); |
2886 $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', %s) WHERE ID = %d", $trackback_url, $ID ) ); |
|
2887 return $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET to_ping = TRIM(REPLACE(to_ping, %s, '')) WHERE ID = %d", $trackback_url, $ID ) ); |
2742 } |
2888 } |
2743 |
2889 |
2744 /** |
2890 /** |
2745 * Send a pingback. |
2891 * Send a pingback. |
2746 * |
2892 * |
2747 * @since 1.2.0 |
2893 * @since 1.2.0 |
2748 * |
2894 * |
2749 * @param string $server Host of blog to connect to. |
2895 * @param string $server Host of blog to connect to. |
2750 * @param string $path Path to send the ping. |
2896 * @param string $path Path to send the ping. |
2751 */ |
2897 */ |
2752 function weblog_ping($server = '', $path = '') { |
2898 function weblog_ping( $server = '', $path = '' ) { |
2753 include_once( ABSPATH . WPINC . '/class-IXR.php' ); |
2899 include_once( ABSPATH . WPINC . '/class-IXR.php' ); |
2754 include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); |
2900 include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' ); |
2755 |
2901 |
2756 // using a timeout of 3 seconds should be enough to cover slow servers |
2902 // using a timeout of 3 seconds should be enough to cover slow servers |
2757 $client = new WP_HTTP_IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path)); |
2903 $client = new WP_HTTP_IXR_Client( $server, ( ( ! strlen( trim( $path ) ) || ( '/' == $path ) ) ? false : $path ) ); |
2758 $client->timeout = 3; |
2904 $client->timeout = 3; |
2759 $client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' ); |
2905 $client->useragent .= ' -- WordPress/' . get_bloginfo( 'version' ); |
2760 |
2906 |
2761 // when set to true, this outputs debug messages by itself |
2907 // when set to true, this outputs debug messages by itself |
2762 $client->debug = false; |
2908 $client->debug = false; |
2763 $home = trailingslashit( home_url() ); |
2909 $home = trailingslashit( home_url() ); |
2764 if ( !$client->query('weblogUpdates.extendedPing', get_option('blogname'), $home, get_bloginfo('rss2_url') ) ) // then try a normal ping |
2910 if ( ! $client->query( 'weblogUpdates.extendedPing', get_option( 'blogname' ), $home, get_bloginfo( 'rss2_url' ) ) ) { // then try a normal ping |
2765 $client->query('weblogUpdates.ping', get_option('blogname'), $home); |
2911 $client->query( 'weblogUpdates.ping', get_option( 'blogname' ), $home ); |
|
2912 } |
2766 } |
2913 } |
2767 |
2914 |
2768 /** |
2915 /** |
2769 * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI |
2916 * Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI |
2770 * |
2917 * |
2832 * cache using the comment group with the key using the ID of the comments. |
2980 * cache using the comment group with the key using the ID of the comments. |
2833 * |
2981 * |
2834 * @since 2.3.0 |
2982 * @since 2.3.0 |
2835 * @since 4.4.0 Introduced the `$update_meta_cache` parameter. |
2983 * @since 4.4.0 Introduced the `$update_meta_cache` parameter. |
2836 * |
2984 * |
2837 * @param array $comments Array of comment row objects |
2985 * @param WP_Comment[] $comments Array of comment objects |
2838 * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. |
2986 * @param bool $update_meta_cache Whether to update commentmeta cache. Default true. |
2839 */ |
2987 */ |
2840 function update_comment_cache( $comments, $update_meta_cache = true ) { |
2988 function update_comment_cache( $comments, $update_meta_cache = true ) { |
2841 foreach ( (array) $comments as $comment ) |
2989 foreach ( (array) $comments as $comment ) { |
2842 wp_cache_add($comment->comment_ID, $comment, 'comment'); |
2990 wp_cache_add( $comment->comment_ID, $comment, 'comment' ); |
|
2991 } |
2843 |
2992 |
2844 if ( $update_meta_cache ) { |
2993 if ( $update_meta_cache ) { |
2845 // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. |
2994 // Avoid `wp_list_pluck()` in case `$comments` is passed by reference. |
2846 $comment_ids = array(); |
2995 $comment_ids = array(); |
2847 foreach ( $comments as $comment ) { |
2996 foreach ( $comments as $comment ) { |
2858 * @access private |
3007 * @access private |
2859 * |
3008 * |
2860 * @see update_comment_cache() |
3009 * @see update_comment_cache() |
2861 * @global wpdb $wpdb WordPress database abstraction object. |
3010 * @global wpdb $wpdb WordPress database abstraction object. |
2862 * |
3011 * |
2863 * @param array $comment_ids Array of comment IDs. |
3012 * @param int[] $comment_ids Array of comment IDs. |
2864 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
3013 * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
2865 */ |
3014 */ |
2866 function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { |
3015 function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { |
2867 global $wpdb; |
3016 global $wpdb; |
2868 |
3017 |
2869 $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' ); |
3018 $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' ); |
2870 if ( !empty( $non_cached_ids ) ) { |
3019 if ( ! empty( $non_cached_ids ) ) { |
2871 $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); |
3020 $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", join( ',', array_map( 'intval', $non_cached_ids ) ) ) ); |
2872 |
3021 |
2873 update_comment_cache( $fresh_comments, $update_meta_cache ); |
3022 update_comment_cache( $fresh_comments, $update_meta_cache ); |
2874 } |
3023 } |
2875 } |
3024 } |
2876 |
3025 |
2887 * @param WP_Post $posts Post data object. |
3036 * @param WP_Post $posts Post data object. |
2888 * @param WP_Query $query Query object. |
3037 * @param WP_Query $query Query object. |
2889 * @return array |
3038 * @return array |
2890 */ |
3039 */ |
2891 function _close_comments_for_old_posts( $posts, $query ) { |
3040 function _close_comments_for_old_posts( $posts, $query ) { |
2892 if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) |
3041 if ( empty( $posts ) || ! $query->is_singular() || ! get_option( 'close_comments_for_old_posts' ) ) { |
2893 return $posts; |
3042 return $posts; |
|
3043 } |
2894 |
3044 |
2895 /** |
3045 /** |
2896 * Filters the list of post types to automatically close comments for. |
3046 * Filters the list of post types to automatically close comments for. |
2897 * |
3047 * |
2898 * @since 3.2.0 |
3048 * @since 3.2.0 |
2899 * |
3049 * |
2900 * @param array $post_types An array of registered post types. Default array with 'post'. |
3050 * @param string[] $post_types An array of post type names. |
2901 */ |
3051 */ |
2902 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); |
3052 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); |
2903 if ( ! in_array( $posts[0]->post_type, $post_types ) ) |
3053 if ( ! in_array( $posts[0]->post_type, $post_types ) ) { |
2904 return $posts; |
3054 return $posts; |
|
3055 } |
2905 |
3056 |
2906 $days_old = (int) get_option( 'close_comments_days_old' ); |
3057 $days_old = (int) get_option( 'close_comments_days_old' ); |
2907 if ( ! $days_old ) |
3058 if ( ! $days_old ) { |
2908 return $posts; |
3059 return $posts; |
|
3060 } |
2909 |
3061 |
2910 if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { |
3062 if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { |
2911 $posts[0]->comment_status = 'closed'; |
3063 $posts[0]->comment_status = 'closed'; |
2912 $posts[0]->ping_status = 'closed'; |
3064 $posts[0]->ping_status = 'closed'; |
2913 } |
3065 } |
2914 |
3066 |
2915 return $posts; |
3067 return $posts; |
2916 } |
3068 } |
2917 |
3069 |
2924 * @param bool $open Comments open or closed |
3076 * @param bool $open Comments open or closed |
2925 * @param int $post_id Post ID |
3077 * @param int $post_id Post ID |
2926 * @return bool $open |
3078 * @return bool $open |
2927 */ |
3079 */ |
2928 function _close_comments_for_old_post( $open, $post_id ) { |
3080 function _close_comments_for_old_post( $open, $post_id ) { |
2929 if ( ! $open ) |
3081 if ( ! $open ) { |
2930 return $open; |
3082 return $open; |
2931 |
3083 } |
2932 if ( !get_option('close_comments_for_old_posts') ) |
3084 |
|
3085 if ( ! get_option( 'close_comments_for_old_posts' ) ) { |
2933 return $open; |
3086 return $open; |
2934 |
3087 } |
2935 $days_old = (int) get_option('close_comments_days_old'); |
3088 |
2936 if ( !$days_old ) |
3089 $days_old = (int) get_option( 'close_comments_days_old' ); |
|
3090 if ( ! $days_old ) { |
2937 return $open; |
3091 return $open; |
2938 |
3092 } |
2939 $post = get_post($post_id); |
3093 |
|
3094 $post = get_post( $post_id ); |
2940 |
3095 |
2941 /** This filter is documented in wp-includes/comment.php */ |
3096 /** This filter is documented in wp-includes/comment.php */ |
2942 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); |
3097 $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) ); |
2943 if ( ! in_array( $post->post_type, $post_types ) ) |
3098 if ( ! in_array( $post->post_type, $post_types ) ) { |
2944 return $open; |
3099 return $open; |
|
3100 } |
2945 |
3101 |
2946 // Undated drafts should not show up as comments closed. |
3102 // Undated drafts should not show up as comments closed. |
2947 if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { |
3103 if ( '0000-00-00 00:00:00' === $post->post_date_gmt ) { |
2948 return $open; |
3104 return $open; |
2949 } |
3105 } |
2950 |
3106 |
2951 if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) |
3107 if ( time() - strtotime( $post->post_date_gmt ) > ( $days_old * DAY_IN_SECONDS ) ) { |
2952 return false; |
3108 return false; |
|
3109 } |
2953 |
3110 |
2954 return $open; |
3111 return $open; |
2955 } |
3112 } |
2956 |
3113 |
2957 /** |
3114 /** |
3108 if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] ) |
3264 if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] ) |
3109 || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID ) |
3265 || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID ) |
3110 ) { |
3266 ) { |
3111 kses_remove_filters(); // start with a clean slate |
3267 kses_remove_filters(); // start with a clean slate |
3112 kses_init_filters(); // set up the filters |
3268 kses_init_filters(); // set up the filters |
|
3269 remove_filter( 'pre_comment_content', 'wp_filter_post_kses' ); |
|
3270 add_filter( 'pre_comment_content', 'wp_filter_kses' ); |
3113 } |
3271 } |
3114 } |
3272 } |
3115 } else { |
3273 } else { |
3116 if ( get_option( 'comment_registration' ) ) { |
3274 if ( get_option( 'comment_registration' ) ) { |
3117 return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to comment.' ), 403 ); |
3275 return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to comment.' ), 403 ); |