@@ -1521,11 +1521,20 @@ function delete( $table, $where, $where_format = null ) {
15211521 */
15221522 protected function process_fields ( $ table , $ data , $ format ) {
15231523 $ data = $ this ->process_field_formats ( $ data , $ format );
1524+ if ( false === $ data ) {
1525+ return false ;
1526+ }
1527+
15241528 $ data = $ this ->process_field_charsets ( $ data , $ table );
15251529 if ( false === $ data ) {
15261530 return false ;
15271531 }
15281532
1533+ $ data = $ this ->process_field_lengths ( $ data , $ table );
1534+ if ( false === $ data ) {
1535+ return false ;
1536+ }
1537+
15291538 $ converted_data = $ this ->strip_invalid_text ( $ data );
15301539
15311540 if ( $ data !== $ converted_data ) {
@@ -1606,6 +1615,40 @@ protected function process_field_charsets( $data, $table ) {
16061615 return $ data ;
16071616 }
16081617
1618+ /**
1619+ * For string fields, record the maximum string length that field can safely save.
1620+ *
1621+ * @since 4.2.1
1622+ * @access protected
1623+ *
1624+ * @param array $data As it comes from the wpdb::process_field_charsets() method.
1625+ * @param string $table Table name.
1626+ * @return array|False The same array as $data with additional 'length' keys, or false if
1627+ * any of the values were too long for their corresponding field.
1628+ */
1629+ protected function process_field_lengths ( $ data , $ table ) {
1630+ foreach ( $ data as $ field => $ value ) {
1631+ if ( '%d ' === $ value ['format ' ] || '%f ' === $ value ['format ' ] ) {
1632+ // We can skip this field if we know it isn't a string.
1633+ // This checks %d/%f versus ! %s because it's sprintf() could take more.
1634+ $ value ['length ' ] = false ;
1635+ } else {
1636+ $ value ['length ' ] = $ this ->get_col_length ( $ table , $ field );
1637+ if ( is_wp_error ( $ value ['length ' ] ) ) {
1638+ return false ;
1639+ }
1640+ }
1641+
1642+ if ( false !== $ value ['length ' ] && strlen ( $ value ['value ' ] ) > $ value ['length ' ] ) {
1643+ return false ;
1644+ }
1645+
1646+ $ data [ $ field ] = $ value ;
1647+ }
1648+
1649+ return $ data ;
1650+ }
1651+
16091652 /**
16101653 * Retrieve one variable from the database.
16111654 *
@@ -1921,6 +1964,77 @@ public function get_col_charset( $table, $column ) {
19211964 return $ charset ;
19221965 }
19231966
1967+ /**
1968+ * Retrieve the maximum string length allowed in a given column.
1969+ *
1970+ * @since 4.2.1
1971+ * @access public
1972+ *
1973+ * @param string $table Table name.
1974+ * @param string $column Column name.
1975+ * @return mixed Max column length as an int. False if the column has no
1976+ * length. WP_Error object if there was an error.
1977+ */
1978+ public function get_col_length ( $ table , $ column ) {
1979+ $ tablekey = strtolower ( $ table );
1980+ $ columnkey = strtolower ( $ column );
1981+
1982+ // Skip this entirely if this isn't a MySQL database.
1983+ if ( false === $ this ->is_mysql ) {
1984+ return false ;
1985+ }
1986+
1987+ if ( empty ( $ this ->col_meta [ $ tablekey ] ) ) {
1988+ // This primes column information for us.
1989+ $ table_charset = $ this ->get_table_charset ( $ table );
1990+ if ( is_wp_error ( $ table_charset ) ) {
1991+ return $ table_charset ;
1992+ }
1993+ }
1994+
1995+ if ( empty ( $ this ->col_meta [ $ tablekey ][ $ columnkey ] ) ) {
1996+ return false ;
1997+ }
1998+
1999+ $ typeinfo = explode ( '( ' , $ this ->col_meta [ $ tablekey ][ $ columnkey ]->Type );
2000+
2001+ $ type = strtolower ( $ typeinfo [0 ] );
2002+ if ( ! empty ( $ typeinfo [1 ] ) ) {
2003+ $ length = trim ( $ typeinfo [1 ], ') ' );
2004+ } else {
2005+ $ length = false ;
2006+ }
2007+
2008+ switch ( $ type ) {
2009+ case 'binary ' :
2010+ case 'char ' :
2011+ case 'varbinary ' :
2012+ case 'varchar ' :
2013+ return $ length ;
2014+ break ;
2015+ case 'tinyblob ' :
2016+ case 'tinytext ' :
2017+ return 255 ; // 2^8 - 1
2018+ break ;
2019+ case 'blob ' :
2020+ case 'text ' :
2021+ return 65535 ; // 2^16 - 1
2022+ break ;
2023+ case 'mediumblob ' :
2024+ case 'mediumtext ' :
2025+ return 16777215 ; // 2^24 - 1
2026+ break ;
2027+ case 'longblob ' :
2028+ case 'longtext ' :
2029+ return 4294967295 ; // 2^32 - 1
2030+ break ;
2031+ default :
2032+ return false ;
2033+ }
2034+
2035+ return false ;
2036+ }
2037+
19242038 /**
19252039 * Check if a string is ASCII.
19262040 *
0 commit comments