diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index dfca1acdbf..0eaede8811 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -2017,11 +2017,9 @@ class wpdb { $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.'; - } + wp_load_translations_early(); + + $this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' ); return false; } @@ -2550,23 +2548,21 @@ class wpdb { } } - 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.'; - } - } + wp_load_translations_early(); - $this->last_error = sprintf( $message, implode( ', ', $problem_fields ) ); + if ( 1 === count( $problem_fields ) ) { + $this->last_error = sprintf( + /* translators: %s: Database field where the error occurred. */ + __( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ), + reset( $problem_fields ) + ); + } else { + $this->last_error = sprintf( + /* translators: %s: Database fields where the error occurred. */ + __( 'WordPress database error: Processing the values for the following fields failed: %s. The supplied values may be too long or contain invalid data.' ), + implode( ', ', $problem_fields ) + ); + } return false; } diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 4ad6a2ad0a..25c674510c 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -1189,10 +1189,16 @@ class Tests_DB extends WP_UnitTestCase { * @param string $errored_fields Expected fields in the error message. */ private function get_db_error_value_too_long( $errored_fields ) { + if ( str_contains( $errored_fields, ', ' ) ) { + return sprintf( + 'WordPress database error: Processing the values for the following fields failed: %s. ' . + 'The supplied values may be too long or contain invalid data.', + $errored_fields + ); + } return sprintf( - 'WordPress database error: Processing the value for the following field%s failed: %s. ' . + 'WordPress database error: Processing the value for the following field failed: %s. ' . 'The supplied value may be too long or contains invalid data.', - str_contains( $errored_fields, ', ' ) ? 's' : '', $errored_fields ); }