Make WordPress Core

Changeset 52218


Ignore:
Timestamp:
11/19/2021 06:58:14 PM (5 years ago)
Author:
hellofromTonya
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r52206 r52218  
    20182018                                $this->last_query = $query;
    20192019
    2020                                 if ( function_exists( '__' ) ) {
    2021                                         $this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' );
    2022                                 } else {
    2023                                         $this->last_error = 'WordPress database error: Could not perform query because it contains invalid data.';
    2024                                 }
     2020                                wp_load_translations_early();
     2021
     2022                                $this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' );
    20252023
    20262024                                return false;
    … …  
    25512549                        }
    25522550
     2551                        wp_load_translations_early();
     2552
    25532553                        if ( 1 === count( $problem_fields ) ) {
    2554                                 if ( function_exists( '__' ) ) {
    2555                                         /* translators: %s Database field where the error occurred. */
    2556                                         $message = __( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' );
    2557                                 } else {
    2558                                         $message = 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.';
    2559                                 }
     2554                                $this->last_error = sprintf(
     2555                                        /* translators: %s: Database field where the error occurred. */
     2556                                        __( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ),
     2557                                        reset( $problem_fields )
     2558                                );
    25602559                        } else {
    2561                                 if ( function_exists( '__' ) ) {
    2562                                         /* translators: %s Database fields where the error occurred. */
    2563                                         $message = __( 'WordPress database error: Processing the value for the following fields failed: %s. The supplied value may be too long or contains invalid data.' );
    2564                                 } else {
    2565                                         $message = 'WordPress database error: Processing the value for the following fields failed: %s. The supplied value may be too long or contains invalid data.';
    2566                                 }
    2567                         }
    2568 
    2569                         $this->last_error = sprintf( $message, implode( ', ', $problem_fields ) );
     2560                                $this->last_error = sprintf(
     2561                                        /* translators: %s: Database fields where the error occurred. */
     2562                                        __( 'WordPress database error: Processing the values for the following fields failed: %s. The supplied values may be too long or contain invalid data.' ),
     2563                                        implode( ', ', $problem_fields )
     2564                                );
     2565                        }
    25702566
    25712567                        return false;
  • trunk/tests/phpunit/tests/db.php

    r52176 r52218  
    11901190         */
    11911191        private function get_db_error_value_too_long( $errored_fields ) {
     1192                if ( str_contains( $errored_fields, ', ' ) ) {
     1193                        return sprintf(
     1194                                'WordPress database error: Processing the values for the following fields failed: %s. ' .
     1195                                'The supplied values may be too long or contain invalid data.',
     1196                                $errored_fields
     1197                        );
     1198                }
    11921199                return sprintf(
    1193                         'WordPress database error: Processing the value for the following field%s failed: %s. ' .
     1200                        'WordPress database error: Processing the value for the following field failed: %s. ' .
    11941201                        'The supplied value may be too long or contains invalid data.',
    1195                         str_contains( $errored_fields, ', ' ) ? 's' : '',
    11961202                        $errored_fields
    11971203                );
Note: See TracChangeset for help on using the changeset viewer.