Make WordPress Core


Ignore:
Timestamp:
11/03/2016 01:11:30 AM (10 years ago)
Author:
rachelbaker
Message:

REST API: Return an error when the length of a comment field is too long.

Introduces wp_check_comment_data_max_lengths() which allows both the REST API comments endpoints and wp_handle_comment_submission() to check the length of the comment content, author name, author url, and author email fields against their respective database columns.

Props rachelbaker, mangeshp, salcode, pento.
Fixes #38477.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment.php

    r38925 r39101  
    11231123
    11241124/**
     1125 * Compares the lengths of comment data against the maximum character limits.
     1126 *
     1127 * @since 4.7.0
     1128 *
     1129 * @param array $comment_data Array of arguments for inserting a comment.
     1130 * @return WP_Error|true WP_Error when a comment field exceeds the limit,
     1131 *                       otherwise true.
     1132 */
     1133function wp_check_comment_data_max_lengths( $comment_data ) {
     1134        $max_lengths = wp_get_comment_fields_max_lengths();
     1135
     1136        if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) {
     1137                return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
     1138        }
     1139
     1140        if ( isset( $comment_data['comment_author_email'] ) && strlen( $comment_data['comment_author_email'] ) > $max_lengths['comment_author_email'] ) {
     1141                return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
     1142        }
     1143
     1144        if ( isset( $comment_data['comment_author_url'] ) && strlen( $comment_data['comment_author_url'] ) > $max_lengths['comment_author_url'] ) {
     1145                return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
     1146        }
     1147
     1148        if ( isset( $comment_data['comment_content'] ) && mb_strlen( $comment_data['comment_content'], '8bit' ) > $max_lengths['comment_content'] ) {
     1149                return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
     1150        }
     1151
     1152        return true;
     1153}
     1154
     1155/**
    11251156 * Does comment contain blacklisted characters or words.
    11261157 *
     
    30333064
    30343065        $comment_type = '';
    3035         $max_lengths = wp_get_comment_fields_max_lengths();
    30363066
    30373067        if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
     
    30433073        }
    30443074
    3045         if ( isset( $comment_author ) && $max_lengths['comment_author'] < mb_strlen( $comment_author, '8bit' ) ) {
    3046                 return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
    3047         }
    3048 
    3049         if ( isset( $comment_author_email ) && $max_lengths['comment_author_email'] < strlen( $comment_author_email ) ) {
    3050                 return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
    3051         }
    3052 
    3053         if ( isset( $comment_author_url ) && $max_lengths['comment_author_url'] < strlen( $comment_author_url ) ) {
    3054                 return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
    3055         }
    3056 
    30573075        if ( '' == $comment_content ) {
    30583076                return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
    3059         } elseif ( $max_lengths['comment_content'] < mb_strlen( $comment_content, '8bit' ) ) {
    3060                 return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
    30613077        }
    30623078
     
    30723088        );
    30733089
     3090        $check_max_lengths = wp_check_comment_data_max_lengths( $commentdata );
     3091        if ( is_wp_error( $check_max_lengths ) ) {
     3092                return $check_max_lengths;
     3093        }
     3094
    30743095        $comment_id = wp_new_comment( wp_slash( $commentdata ), true );
    30753096        if ( is_wp_error( $comment_id ) ) {
Note: See TracChangeset for help on using the changeset viewer.