WordPress.org

Make WordPress Core

Ticket #4332: comment-validation2.diff

File comment-validation2.diff, 3.6 KB (added by shamess, 6 years ago)

Updated version of my older patch (old one kept for comparison)

  • wp-comments-post.php

     
    1717 
    1818nocache_headers(); 
    1919 
     20function redirectBackToPost ($comment_post_ID, $errorCode) { 
     21        //  Redirect to the post's comment section, but add the comment error message 
     22  wp_redirect (add_query_arg ("commentErrorCode", $errorCode, get_permalink ($comment_post_ID))); 
     23  die; 
     24} 
     25 
    2026$comment_post_ID = (int) $_POST['comment_post_ID']; 
    2127 
    2228$status = $wpdb->get_row( $wpdb->prepare("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) ); 
     
    2632        exit; 
    2733} elseif ( !comments_open($comment_post_ID) ) { 
    2834        do_action('comment_closed', $comment_post_ID); 
    29         wp_die( __('Sorry, comments are closed for this item.') ); 
     35  redirectBackToPost ($comment_post_ID, 4); 
    3036} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) { 
    3137        do_action('comment_on_draft', $comment_post_ID); 
    3238        exit; 
     
    5965$comment_type = ''; 
    6066 
    6167if ( get_option('require_name_email') && !$user->ID ) { 
    62         if ( 6 > strlen($comment_author_email) || '' == $comment_author ) 
    63                 wp_die( __('Error: please fill the required fields (name, email).') ); 
     68        if ( '' == $comment_author_email || '' == $comment_author ) 
     69                redirectBackToPost ($comment_post_ID, 3); 
    6470        elseif ( !is_email($comment_author_email)) 
    65                 wp_die( __('Error: please enter a valid email address.') ); 
     71                redirectBackToPost ($comment_post_ID, 2); 
    6672} 
    6773 
     74//  Comment blank? 
    6875if ( '' == $comment_content ) 
    69         wp_die( __('Error: please type a comment.') ); 
     76  redirectBackToPost ($comment_post_ID, 1); 
    7077 
    7178$comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0; 
    7279 
  • wp-includes/comment.php

     
    16381638        return $open; 
    16391639} 
    16401640 
     1641/** 
     1642 * Echos error messages given when trying to post a comment. Hooked to comment_form 
     1643 * 
     1644 * @since 2.8 
     1645 */ 
     1646function check_errors_on_comment_post () { 
     1647  /*  Error codes are as follows: 
     1648   *  1     Comment was left blank 
     1649   *  2     Email address wasn't valid 
     1650   *  3     Name or email address weren't set 
     1651   *  4     Comments are closed 
     1652   */ 
     1653   
     1654  //  Only bother with anything if there's an error code set 
     1655  if (isset ($_GET['commentErrorCode'])) { 
     1656    switch ($_GET['commentErrorCode']) { 
     1657      case 1: 
     1658        echo "<p id=\"commentError\">".__("Message can't be blank.")."</p>\n"; 
     1659        break; 
     1660      case 2: 
     1661        echo "<p id=\"commentError\">".__("Invalid email address.")."</p>\n"; 
     1662        break; 
     1663      case 3: 
     1664        echo "<p id=\"commentError\">".__("Name and email address are required fields.")."</p>\n"; 
     1665        break; 
     1666      case 4: 
     1667        echo "<p id=\"commentError\">".__("Sorry, comments are closed for this item.")."</p>\n"; 
     1668        break; 
     1669      default: 
     1670        //  We should never get here, but just in case 
     1671        echo "<p id=\"commentError\">".__("Received unknown error value.")."</p>\n"; 
     1672        break; 
     1673    } 
     1674  } 
     1675} 
     1676 
    16411677?> 
  • wp-includes/default-filters.php

     
    123123 
    124124add_filter('comment_excerpt', 'convert_chars'); 
    125125 
     126add_action ('comment_form', 'check_errors_on_comment_post', 11); 
     127 
    126128add_filter('list_cats', 'wptexturize'); 
    127129add_filter('single_post_title', 'wptexturize'); 
    128130