Changeset 36514
- Timestamp:
- 02/12/2016 01:54:50 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment.php
r36405 r36514 949 949 950 950 /** 951 * Calculate the maximum character length of a column from the comments table.951 * Get the maximum character lengths for the comment form fields. 952 952 * 953 953 * @since 4.5.0 … … 955 955 * @global wpdb $wpdb WordPress database abstraction object. 956 956 * 957 * @param string $column Name of a column in the comments table. 958 * @return int Maximum column character length. 959 */ 960 function wp_get_comment_column_max_length( $column ) { 957 * @return array Maximum character length for the comment form fields. 958 */ 959 function wp_get_comment_fields_max_lengths() { 961 960 global $wpdb; 962 961 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 } 972 973 if ( ! empty( $col_length['type'] ) && 'byte' === $col_length['type'] ) { 974 $max_length = $max_length - 10; 975 } 976 977 /** 978 * Filters the calculated length for a given column of the comments table. 962 $lengths = array( 963 'comment_author' => 245, 964 'comment_author_email' => 100, 965 'comment_author_url' => 200, 966 'comment_content' => 65525, 967 ); 968 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 } 993 } 994 995 /** 996 * Filters the lengths for the comment form fields. 979 997 * 980 998 * @since 4.5.0 981 999 * 982 * @param int $max_length Maximum column character length. 983 * @param string $column Column name. 984 */ 985 return apply_filters( 'wp_get_comment_column_max_length', $max_length, $column ); 1000 * @param array $lengths Associative array 'field_name' => 'maximum length'. 1001 */ 1002 return apply_filters( 'wp_get_comment_fields_max_lengths', $lengths ); 986 1003 } 987 1004 … … 2834 2851 2835 2852 $comment_type = ''; 2853 $max_lengths = wp_get_comment_fields_max_lengths(); 2836 2854 2837 2855 if ( get_option( 'require_name_email' ) && ! $user->exists() ) { … … 2843 2861 } 2844 2862 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' ) ) { 2846 2864 return new WP_Error( 'comment_author_column_length', __( '<strong>ERROR</strong>: your name is too long.' ), 200 ); 2847 2865 } 2848 2866 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 ) ) { 2850 2868 return new WP_Error( 'comment_author_email_column_length', __( '<strong>ERROR</strong>: your email address is too long.' ), 200 ); 2851 2869 } 2852 2870 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 ) ) { 2854 2872 return new WP_Error( 'comment_author_url_column_length', __( '<strong>ERROR</strong>: your url is too long.' ), 200 ); 2855 2873 } … … 2857 2875 if ( '' == $comment_content ) { 2858 2876 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' ) ) { 2860 2878 return new WP_Error( 'comment_content_column_length', __( '<strong>ERROR</strong>: your comment is too long.' ), 200 ); 2861 2879 } -
trunk/tests/phpunit/tests/comment.php
r36325 r36514 674 674 } 675 675 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 }693 676 }
Note: See TracChangeset
for help on using the changeset viewer.