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
This commit is contained in:
Tonya Mork
2021-11-19 18:58:14 +00:00
parent 36463dfee8
commit 7ab2a96856
2 changed files with 25 additions and 23 deletions

View File

@@ -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;
}

View File

@@ -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
);
}