Make WordPress Core

Ticket #10377: 10377.8.diff

File 10377.8.diff, 5.3 KB (added by azaozz, 8 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                                if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) {
     985                                        $max_length = $max_length - 10;
     986                                }
     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/**
     
    28332850        }
    28342851
    28352852        $comment_type = '';
     2853        $max_lengths = wp_get_comment_fields_max_lengths();
    28362854
    28372855        if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
    28382856                if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
     
    28422860                }
    28432861        }
    28442862
    2845         if ( isset( $comment_author ) && wp_get_comment_column_max_length( 'comment_author' ) < mb_strlen( $comment_author, '8bit' ) ) {
     2863        if ( isset( $comment_author ) && $max_lengths['comment_author'] < mb_strlen( $comment_author, '8bit' ) ) {
    28462864                return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 );
    28472865        }
    28482866
    2849         if ( isset( $comment_author_email ) && wp_get_comment_column_max_length( 'comment_author_email' ) < strlen( $comment_author_email ) ) {
     2867        if ( isset( $comment_author_email ) && $max_lengths['comment_author_email'] < strlen( $comment_author_email ) ) {
    28502868                return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 );
    28512869        }
    28522870
    2853         if ( isset( $comment_author_url ) && wp_get_comment_column_max_length( 'comment_author_url' ) < strlen( $comment_author_url ) ) {
     2871        if ( isset( $comment_author_url ) && $max_lengths['comment_author_url'] < strlen( $comment_author_url ) ) {
    28542872                return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 );
    28552873        }
    28562874
    28572875        if ( '' == $comment_content ) {
    28582876                return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
    2859         } elseif ( wp_get_comment_column_max_length( 'comment_content' ) < mb_strlen( $comment_content, '8bit' ) ) {
     2877        } elseif ( $max_lengths['comment_content'] < mb_strlen( $comment_content, '8bit' ) ) {
    28602878                return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 );
    28612879        }
    28622880
  • tests/phpunit/tests/comment.php

     
    673673                $this->assertSame( 'SHIELD_AGENT', $updated->comment_agent );
    674674        }
    675675
    676         public function test_wp_get_comment_column_max_length() {
    677                 $columns = array(
    678                         'comment_author' => 245,
    679                         'comment_author_email' => 100,
    680                         'comment_author_url' => 200,
    681                         'comment_author_IP' => 100,
    682                         'comment_content' => 65525,
    683                         'comment_approved' => 20,
    684                         'comment_agent' => 255,
    685                         'comment_type' => 20,
    686                 );
    687 
    688                 foreach ( $columns as $column => $expected ) {
    689                         $max_length = wp_get_comment_column_max_length( $column );
    690                         $this->assertSame( $expected, $max_length );
    691                 }
    692         }
    693676}