WordPress.org

Make WordPress Core

Ticket #10377: 10377.7.diff

File 10377.7.diff, 4.5 KB (added by azaozz, 5 years ago)
  • src/wp-includes/comment.php

     
    948948}
    949949
    950950/**
    951  * Calculate the maximum character length of a column from the comments table.
     951 * Get the maximum character lengths for the comment form fields.
    952952 *
    953953 * @since 4.5.0
    954954 *
    955955 * @global wpdb $wpdb WordPress database abstraction object.
    956956 *
    957  * @param string $column Name of a column in the comments table.
    958  * @return int Maximum column character length.
     957 * @return array Maximum character length for the comment form fields.
    959958 */
    960 function wp_get_comment_column_max_length( $column ) {
     959function wp_get_comment_fields_max_lengths() {
    961960        global $wpdb;
    962961
    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                 // Assume a TEXT column, 65535 - 10.
    970                 $max_length = 65525;
    971         }
     962        $lengths = array(
     963                'comment_author'       => 245,
     964                'comment_author_email' => 100,
     965                'comment_author_url'   => 200,
     966                'comment_content'      => 65525,
     967        );
    972968
    973         if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
    974                 $max_length = $max_length - 10;
     969        if ( $wpdb->is_mysql ) {
     970                foreach ( $lengths as $column => $length ) {
     971                        $col_length = $wpdb->get_col_length( $wpdb->comments, $column );
     972                        $max_length = 0;
     973
     974                        // No point if we can't get the DB column lengths
     975                        if ( $col_length === false ) {
     976                                break;
     977                        }
     978
     979                        if ( ! is_array( $col_length ) && (int) $col_length > 0 ) {
     980                                $max_length = (int) $col_length;
     981                        } elseif ( is_array( $col_length ) && isset( $col_length['length'] ) && intval( $col_length['length'] ) > 0 ) {
     982                                $max_length = (int) $col_length['length'];
     983                        }
     984
     985                        if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
     986                                $max_length = $max_length - 10;
     987                        }
     988
     989                        if ( $max_length > 0 ) {
     990                                $lengths[ $column ] = $max_length;
     991                        }
     992                }
    975993        }
    976994
    977995        /**
    978          * Filters the calculated length for a given column of the comments table.
     996         * Filters the lengths for the comment form fields.
    979997         *
    980998         * @since 4.5.0
    981999         *
    982          * @param int    $max_length Maximum column character length.
    983          * @param string $column     Column name.
     1000         * @param array  $lengths Associative array 'field_name' => 'maximum length'.
    9841001         */
    985         return apply_filters( 'wp_get_comment_column_max_length', $max_length, $column );
     1002        return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths );
    9861003}
    9871004
    9881005/**
     
    28242841        }
    28252842
    28262843        $comment_type = '';
     2844        $max_lengths = wp_get_comment_fields_max_lengths();
    28272845
    28282846        if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
    28292847                if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
     
    28332851                }
    28342852        }
    28352853
    2836         if ( isset( $comment_author ) && wp_get_comment_column_max_length( 'comment_author' ) < mb_strlen( $comment_author, '8bit' ) ) {
     2854        if ( isset( $comment_author ) && $max_lengths['comment_author'] < mb_strlen( $comment_author, '8bit' ) ) {
    28372855                return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
    28382856        }
    28392857
    2840         if ( isset( $comment_author_email ) && wp_get_comment_column_max_length( 'comment_author_email' ) < strlen( $comment_author_email ) ) {
     2858        if ( isset( $comment_author_email ) && $max_lengths['comment_author_email'] < strlen( $comment_author_email ) ) {
    28412859                return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
    28422860        }
    28432861
    2844         if ( isset( $comment_author_url ) && wp_get_comment_column_max_length( 'comment_author_url' ) < strlen( $comment_author_url ) ) {
     2862        if ( isset( $comment_author_url ) && $max_lengths['comment_author_url'] < strlen( $comment_author_url ) ) {
    28452863                return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
    28462864        }
    28472865
    28482866        if ( '' == $comment_content ) {
    28492867                return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
    2850         } elseif ( wp_get_comment_column_max_length( 'comment_content' ) < mb_strlen( $comment_content, '8bit' ) ) {
     2868        } elseif ( $max_lengths['comment_content'] < mb_strlen( $comment_content, '8bit' ) ) {
    28512869                return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
    28522870        }
    28532871