Ticket #36901: 36901.diff
File 36901.diff, 9.5 KB (added by , 8 years ago) |
---|
-
src/wp-includes/comment.php
581 586 * Validates whether this comment is allowed to be made. 582 587 * 583 588 * @since 2.0.0 589 * @since 4.7.0 The `$avoid_die` parameter was added. 584 590 * 585 591 * @global wpdb $wpdb WordPress database abstraction object. 586 592 * 587 * @param array $commentdata Contains information on the comment 588 * @return int|string Signifies the approval status (0|1|'spam') 593 * @param array $commentdata Contains information on the comment. 594 * @param boolean $avoid_die Should errors be returned as WP_Error objects 595 * instead of executing wp_die()? Default false. 596 * @return int|string|WP_Error Allowed comments return the approval status (0|1|'spam'). 597 * If $avoid_die is true, unallowed comments return a WP_Error. 589 598 */ 590 function wp_allow_comment( $commentdata ) {599 function wp_allow_comment( $commentdata, $avoid_die = false ) { 591 600 global $wpdb; 592 601 593 602 // Simple duplicate check … … 632 641 * @param array $commentdata Comment data. 633 642 */ 634 643 do_action( 'comment_duplicate_trigger', $commentdata ); 635 if ( wp_doing_ajax() ) { 636 die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); 644 if ( true === $avoid_die ) { 645 return new WP_Error( 'comment_duplicate', __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); 646 } else { 647 if ( wp_doing_ajax() ) { 648 die( __('Duplicate comment detected; it looks as though you’ve already said that!') ); 649 } 650 651 wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 ); 637 652 } 638 wp_die( __( 'Duplicate comment detected; it looks as though you’ve already said that!' ), 409 );639 653 } 640 654 641 655 /** … … 656 670 $commentdata['comment_date_gmt'] 657 671 ); 658 672 673 $is_flood = check_comment_flood_db( $commentdata['comment_author_IP'], $commentdata['comment_author_email'], $commentdata['comment_date_gmt'], $avoid_die ); 674 if ( $is_flood ) { 675 return new WP_Error( 'comment_flood', __( 'You are posting comments too quickly. Slow down.' ), 429 ); 676 } 677 659 678 if ( ! empty( $commentdata['user_id'] ) ) { 660 679 $user = get_userdata( $commentdata['user_id'] ); 661 680 $post_author = $wpdb->get_var( $wpdb->prepare( … … 714 733 * administrators. 715 734 * 716 735 * @since 2.3.0 736 * @since 4.7.0 The `$avoid_die` parameter was added. 717 737 * 718 738 * @global wpdb $wpdb WordPress database abstraction object. 719 739 * 720 * @param string $ip Comment IP. 721 * @param string $email Comment author email address. 722 * @param string $date MySQL time string. 740 * @param string $ip Comment IP. 741 * @param string $email Comment author email address. 742 * @param string $date MySQL time string. 743 * @param boolean $avoid_die Prevent executing wp_die() or die() if a 744 * comment flood is occuring. Default is false. 745 * @return boolean|WP_Error True or a WP_Error if a comment flood is occuring, 746 * otherwise false. 723 747 */ 724 function check_comment_flood_db( $ip, $email, $date ) { 748 function check_comment_flood_db( $ip, $email, $date, $avoid_die = false ) { 749 725 750 global $wpdb; 726 751 // don't throttle admins or moderators 727 752 if ( current_user_can( 'manage_options' ) || current_user_can( 'moderate_comments' ) ) { … … 767 792 * @param int $time_newcomment Timestamp of when the new comment was posted. 768 793 */ 769 794 do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment ); 795 if ( true === $avoid_die ) { 796 return true; 797 } else { 798 if ( wp_doing_ajax() ) { 799 die( __('You are posting comments too quickly. Slow down.') ); 800 } 770 801 771 if ( wp_doing_ajax() ) 772 die( __('You are posting comments too quickly. Slow down.') ); 773 774 wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 ); 802 wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 ); 803 } 775 804 } 776 805 } 806 807 return false; 777 808 } 778 809 779 810 /** … … 1717 1748 * 1718 1749 * @since 1.5.0 1719 1750 * @since 4.3.0 'comment_agent' and 'comment_author_IP' can be set via `$commentdata`. 1751 * @since 4.7.0 The `$avoid_die` parameter was added. 1720 1752 * 1721 1753 * @see wp_insert_comment() 1722 1754 * @global wpdb $wpdb WordPress database abstraction object. … … 1740 1772 * @type string $comment_author_IP Comment author IP address in IPv4 format. Default is the value of 1741 1773 * 'REMOTE_ADDR' in the `$_SERVER` superglobal sent in the original request. 1742 1774 * } 1743 * @return int|false The ID of the comment on success, false on failure. 1775 * @param boolean $avoid_die Should errors be returned as WP_Error objects instead of 1776 * executing wp_die()? Default false. 1777 * @return int|false|WP_Error The ID of the comment on success, false or WP_Error on failure. 1744 1778 */ 1745 function wp_new_comment( $commentdata ) {1779 function wp_new_comment( $commentdata, $avoid_die = false ) { 1746 1780 global $wpdb; 1747 1781 1748 1782 if ( isset( $commentdata['user_ID'] ) ) { … … 1791 1825 1792 1826 $commentdata = wp_filter_comment($commentdata); 1793 1827 1794 $commentdata['comment_approved'] = wp_allow_comment($commentdata); 1828 $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die ); 1829 if ( is_wp_error( $commentdata['comment_approved'] ) ) { 1830 return $commentdata['comment_approved']; 1831 } 1795 1832 1796 1833 $comment_ID = wp_insert_comment($commentdata); 1797 1834 if ( ! $comment_ID ) { … … 1805 1842 1806 1843 $commentdata = wp_filter_comment( $commentdata ); 1807 1844 1808 $commentdata['comment_approved'] = wp_allow_comment( $commentdata ); 1845 $commentdata['comment_approved'] = wp_allow_comment( $commentdata, $avoid_die ); 1846 if ( is_wp_error( $commentdata['comment_approved'] ) ) { 1847 return $commentdata['comment_approved']; 1848 } 1809 1849 1810 1850 $comment_ID = wp_insert_comment( $commentdata ); 1811 1851 if ( ! $comment_ID ) { … … 2913 2953 'user_ID' 2914 2954 ); 2915 2955 2916 $comment_id = wp_new_comment( wp_slash( $commentdata ) ); 2956 $comment_id = wp_new_comment( wp_slash( $commentdata ), true ); 2957 if ( is_wp_error( $comment_id ) ) { 2958 return $comment_id; 2959 } 2960 2917 2961 if ( ! $comment_id ) { 2918 2962 return new WP_Error( 'comment_save_error', __( '<strong>ERROR</strong>: The comment could not be saved. Please try again later.' ), 500 ); 2919 2963 } 2920 2964 2921 2965 return get_comment( $comment_id ); 2922 2923 2966 } -
src/wp-includes/default-filters.php
197 197 add_filter( 'teeny_mce_before_init', '_mce_set_direction' ); 198 198 add_filter( 'pre_kses', 'wp_pre_kses_less_than' ); 199 199 add_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 ); 200 add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 3 );201 200 add_filter( 'comment_flood_filter', 'wp_throttle_comment_flood', 10, 3 ); 202 201 add_filter( 'pre_comment_content', 'wp_rel_nofollow', 15 ); 203 202 add_filter( 'comment_email', 'antispambot' ); -
tests/phpunit/tests/comment-submission.php
714 714 return $commentdata; 715 715 } 716 716 717 /** 718 * @ticket 36901 719 */ 720 public function test_submitting_duplicate_comments() { 721 $post = self::factory()->post->create_and_get( array( 722 'post_status' => 'publish', 723 ) ); 724 $data = array( 725 'comment_post_ID' => $post->ID, 726 'comment' => 'Did I say that?', 727 'author' => 'Repeat myself', 728 'email' => 'mail@example.com', 729 ); 730 $first_comment = wp_handle_comment_submission( $data ); 731 $second_comment = wp_handle_comment_submission( $data ); 732 $this->assertWPError( $second_comment ); 733 $this->assertSame( 'comment_duplicate', $second_comment->get_error_code() ); 734 } 735 736 /** 737 * @ticket 36901 738 */ 739 public function test_comments_flood() { 740 $post = self::factory()->post->create_and_get( array( 741 'post_status' => 'publish', 742 ) ); 743 $data = array( 744 'comment_post_ID' => $post->ID, 745 'comment' => 'Did I say that?', 746 'author' => 'Repeat myself', 747 'email' => 'mail@example.com', 748 ); 749 $first_comment = wp_handle_comment_submission( $data ); 750 751 $data['comment'] = 'Wow! I am quick!'; 752 $second_comment = wp_handle_comment_submission( $data ); 753 754 $this->assertWPError( $second_comment ); 755 $this->assertSame( 'comment_flood', $second_comment->get_error_code() ); 756 } 757 758 /** 759 * @ticket 36901 760 */ 761 public function test_comments_flood_user_is_admin() { 762 $user = self::factory()->user->create_and_get( array( 763 'role' => 'administrator', 764 ) ); 765 wp_set_current_user( $user->ID ); 766 767 $post = self::factory()->post->create_and_get( array( 768 'post_status' => 'publish', 769 ) ); 770 $data = array( 771 'comment_post_ID' => $post->ID, 772 'comment' => 'Did I say that?', 773 'author' => 'Repeat myself', 774 'email' => 'mail@example.com', 775 ); 776 $first_comment = wp_handle_comment_submission( $data ); 777 778 $data['comment'] = 'Wow! I am quick!'; 779 $second_comment = wp_handle_comment_submission( $data ); 780 781 $this->assertNotWPError( $second_comment ); 782 $this->assertEquals( $post->ID, $second_comment->comment_post_ID ); 783 } 784 717 785 }