From 7a4c5007b6667c6a77fd28c0832cde20d69e8bde Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Tue, 16 Nov 2021 02:59:00 +0000 Subject: [PATCH] WPDB: Capture error in `wpdb::$last_error` when insert fails instead of silently failing for invalid data or value too long. Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the `wpdb::$last_error` property. Sets `last_error` with an error message if: * `wpdb::query()` fails for invalid data * `wpdb::process_fields()` fails to process the value(s) for the field(s) where the value could be too long or contain invalid data Sets `last_query` if `wpdb::query()` fails for invalid data. If `__()` is not available, uses non-translated error message to ensure the error is captured. There is no change to wpdb aborting when an error occurs. Adds tests. Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec. Fixes #37267. Built from https://develop.svn.wordpress.org/trunk@52176 git-svn-id: http://core.svn.wordpress.org/trunk@51768 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/version.php | 2 +- wp-includes/wp-db.php | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/wp-includes/version.php b/wp-includes/version.php index 2b5001dfdd..ead1098ee5 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-52175'; +$wp_version = '5.9-alpha-52176'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 53ba207203..57c3bd76f6 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -2015,7 +2015,15 @@ class wpdb { // to flush again, just to make sure everything is clear. $this->flush(); if ( $stripped_query !== $query ) { - $this->insert_id = 0; + $this->insert_id = 0; + $this->last_query = $query; + + if ( function_exists( '__' ) ) { + $this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' ); + } else { + $this->last_error = 'WordPress database error: Could not perform query because it contains invalid data.'; + } + return false; } } @@ -2535,6 +2543,32 @@ class wpdb { $converted_data = $this->strip_invalid_text( $data ); if ( $data !== $converted_data ) { + + $problem_fields = array(); + foreach ( $data as $field => $value ) { + if ( $value !== $converted_data[ $field ] ) { + $problem_fields[] = $field; + } + } + + if ( 1 === count( $problem_fields ) ) { + if ( function_exists( '__' ) ) { + /* translators: %s Database field where the error occurred. */ + $message = __( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ); + } else { + $message = 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.'; + } + } else { + if ( function_exists( '__' ) ) { + /* translators: %s Database fields where the error occurred. */ + $message = __( 'WordPress database error: Processing the value for the following fields failed: %s. The supplied value may be too long or contains invalid data.' ); + } else { + $message = 'WordPress database error: Processing the value for the following fields failed: %s. The supplied value may be too long or contains invalid data.'; + } + } + + $this->last_error = sprintf( $message, implode( ', ', $problem_fields ) ); + return false; }