WordPress.org

Make WordPress Core

Changeset 36514


Ignore:
Timestamp:
02/12/2016 01:54:50 PM (5 years ago)
Author:
rachelbaker
Message:

Comments: Change wp_get_comment_column_max_length() function to wp_get_comment_fields_max_lengths() for consolidation and better fallbacks.

Instead of returning a value for each of the related table column lengths, return an array of all of the column lengths used in the comment form.
Better fallback handling, where each field falls back to the expected max_length instead of an arbitrary number.

Props azaozz.

Fixes #10377.

Location:
trunk
Files:
2 edited

Legend:

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

    r36405 r36514  
    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
     
    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.
    959  */
    960 function wp_get_comment_column_max_length( $column ) {
     957 * @return array Maximum character length for the comment form fields.
     958 */
     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     }
    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.
    979997     *
    980998     * @since 4.5.0
    981999     *
    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 );
    9861003}
    9871004
     
    28342851
    28352852    $comment_type = '';
     2853    $max_lengths = wp_get_comment_fields_max_lengths();
    28362854
    28372855    if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
     
    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    }
     
    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    }
  • trunk/tests/phpunit/tests/comment.php

    r36325 r36514  
    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}
Note: See TracChangeset for help on using the changeset viewer.