WordPress.org

Make WordPress Core

Ticket #4332: comment-validation.diff

File comment-validation.diff, 4.5 KB (added by shamess, 9 years ago)
  • wp-comments-post.php

     
    1717
    1818nocache_headers();
    1919
     20function redirectBackToPost ($comment_post_ID, $errorCode) {
     21  //  Work out the link we're redirecting back to
     22  $postURL = get_permalink ($comment_post_ID);
     23  if (strstr ($postURL, '?') !== false) $postURL .= "&"; else $postURL .= "?";
     24  $postURL .= "commentErrorCode=".$errorCode;
     25        //  Redirect to the post's comment section, but add the comment error message
     26  header ("Location: ".$postURL);
     27  die;
     28}
     29
    2030$comment_post_ID = (int) $_POST['comment_post_ID'];
    2131
    2232$status = $wpdb->get_row( $wpdb->prepare("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );
     
    2636        exit;
    2737} elseif ( !comments_open($comment_post_ID) ) {
    2838        do_action('comment_closed', $comment_post_ID);
    29         wp_die( __('Sorry, comments are closed for this item.') );
     39  redirectBackToPost ($comment_post_ID, 4);
    3040} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) {
    3141        do_action('comment_on_draft', $comment_post_ID);
    3242        exit;
     
    5969$comment_type = '';
    6070
    6171if ( 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).') );
     72        if ( '' == $comment_author_email || '' == $comment_author )
     73                redirectBackToPost ($comment_post_ID, 3);
    6474        elseif ( !is_email($comment_author_email))
    65                 wp_die( __('Error: please enter a valid email address.') );
     75                redirectBackToPost ($comment_post_ID, 2);
    6676}
    6777
     78//  Comment blank?
    6879if ( '' == $comment_content )
    69         wp_die( __('Error: please type a comment.') );
     80  redirectBackToPost ($comment_post_ID, 1);
    7081
    7182$comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;
    7283
  • wp-content/themes/default/comments.php

     
    6666
    6767<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>
    6868
    69 <?php else : ?>
     69<?php else :
    7070
     71do_action ('comment_form_errors'); ?>
     72
    7173<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
    7274<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>
    7375
  • 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.7.1
     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\">Something went wrong. No idea what.</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_errors', 'check_errors_on_comment_post');
     127
    126128add_filter('list_cats', 'wptexturize');
    127129add_filter('single_post_title', 'wptexturize');
    128130