Make WordPress Core


Ignore:
Timestamp:
01/13/2016 01:24:46 AM (9 years ago)
Author:
rachelbaker
Message:

Comments: Restrict the maximum characters for input fields within the comments template.

Added hardcoded maxlength attributes on the author, author_email, author_url, and comment_field input markup. These can be modified via the comment_form_defaults filter. Added logic in wp_handle_comment_submission() to return a WP_Error when the comment_author, comment_author_url, or comment_content values exceed the max length of their columns. Introduces wp_get_comment_column_max_length() which returns the max column length for a given column name, and is filterable. Unit tests included for the error conditions in wp_handle_comment_submission()

Fixes #10377.

Props westonruter rachelbaker.

File:
1 edited

Legend:

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

    r36215 r36272  
    949949
    950950/**
     951 * Calculate the maximum character length of a column from the comments table.
     952 *
     953 * @since 4.5.0
     954 *
     955 * @global wpdb $wpdb WordPress database abstraction object.
     956 *
     957 * @param string $column Name of a column in the comments table.
     958 * @return int Maximum column character length.
     959 */
     960function wp_get_comment_column_max_length( $column ) {
     961    global $wpdb;
     962
     963    $col_length = $wpdb->get_col_length( $wpdb->comments, $column );
     964    if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
     965        $max_length = (int) $col_length;
     966    } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && intval( $col_length['length'] ) > 0 ) {
     967        $max_length = (int) $col_length['length'];
     968    } else {
     969        $max_length = 255;
     970    }
     971
     972    if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
     973        $max_length = $max_length - 10;
     974    }
     975
     976    /**
     977     * Filters the calculated length for a given column of the comments table.
     978     *
     979     * @since 4.5.0
     980     *
     981     * @param int    $max_length Maximum column character length.
     982     * @param string $column     Column name.
     983     */
     984    return apply_filters( 'wp_get_comment_column_max_length', $max_length, $column );
     985}
     986
     987/**
    951988 * Does comment contain blacklisted characters or words.
    952989 *
     
    27792816    }
    27802817
     2818    if ( isset( $comment_author ) && wp_get_comment_column_max_length( 'comment_author' ) < mb_strlen( $comment_author, '8bit' ) ) {
     2819        return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
     2820    }
     2821
     2822    if ( isset( $comment_author_email ) && wp_get_comment_column_max_length( 'comment_author_email' ) < strlen( $comment_author_email ) ) {
     2823        return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
     2824    }
     2825
     2826    if ( isset( $comment_author_url ) && wp_get_comment_column_max_length( 'comment_author_url' ) < strlen( $comment_author_url ) ) {
     2827        return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
     2828    }
     2829
    27812830    if ( '' == $comment_content ) {
    27822831        return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
     2832    } elseif ( wp_get_comment_column_max_length( 'comment_content' ) < mb_strlen( $comment_content, '8bit' ) ) {
     2833        return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
    27832834    }
    27842835
Note: See TracChangeset for help on using the changeset viewer.