From 7ab2a968563cfe05a8c2caff0f063d6ef6ac44f8 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Fri, 19 Nov 2021 18:58:14 +0000 Subject: [PATCH] WPDB: Call `wp_load_translations_early()` in `wpdb::query()` and `wpdb::process_fields()`. For consistency and simplification, replaces the `function_exists( '__' )` checks with `wp_load_translations_early()` to make sure i18n functions are available. This change removes the extra code introduced in [52176] for using non-translated error messages when `__()` is not available. Improves the plural versions of the error messages. For performance, when there are more than one problem field, uses `reset()` to populate the field in the error message. Follow-up to [52176], [52195]. Props sergeybiryukov, hellofromTonya. Fixes #32315. git-svn-id: https://develop.svn.wordpress.org/trunk@52218 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/wp-db.php | 38 +++++++++++++++++--------------------- tests/phpunit/tests/db.php | 10 ++++++++-- 2 files changed, 25 insertions(+), 23 deletions(-) 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 ); }