From 51dc8d8bfe2df74af54029d771bfd63921f3bf4c Mon Sep 17 00:00:00 2001 From: Jaydeep Das Date: Fri, 30 Oct 2026 16:51:45 +0530 Subject: [PATCH] I18N: Store an empty `WPLANG` option on single sites. Sites that never changed their language have no `WPLANG` row, so `get_locale()` ran an options query on every request without a persistent object cache. `populate_options()` now adds an autoloaded empty `WPLANG` on single sites. This covers new installs and, through `upgrade_all()`, existing sites on the next database upgrade. A `WPLANG` constant set to a locale keeps taking precedence. When a single site is converted to a network, an empty `WPLANG` is removed from the main site so that it keeps falling back to the network language. --- src/wp-admin/includes/schema.php | 10 +++ tests/phpunit/tests/admin/includesSchema.php | 85 ++++++++++++++++++++ tests/qunit/fixtures/wp-api-generated.js | 2 +- 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/schema.php b/src/wp-admin/includes/schema.php index d175c65e3649c..92cef756cd4fb 100644 --- a/src/wp-admin/includes/schema.php +++ b/src/wp-admin/includes/schema.php @@ -579,6 +579,11 @@ function populate_options( array $options = array() ) { $defaults['permalink_structure'] = '/%year%/%monthnum%/%day%/%postname%/'; } + // 7.3.0 + if ( ! is_multisite() && ( ! defined( 'WPLANG' ) || '' === WPLANG ) ) { + $defaults['WPLANG'] = ''; + } + $options = wp_parse_args( $options, $defaults ); // Set autoload to no for these options. @@ -1138,6 +1143,11 @@ function populate_network( $network_id = 1, $domain = '', $email = '', $site_nam update_user_meta( $site_user_id, 'source_domain', $domain ); update_user_meta( $site_user_id, 'primary_blog', $current_site->blog_id ); + // Let the main site fall back to the network language instead of an empty site language. + if ( '' === get_option( 'WPLANG' ) ) { + delete_option( 'WPLANG' ); + } + // Unable to use update_network_option() while populating the network. $wpdb->insert( $wpdb->sitemeta, diff --git a/tests/phpunit/tests/admin/includesSchema.php b/tests/phpunit/tests/admin/includesSchema.php index f6c96169659a5..a96c947cc4d71 100644 --- a/tests/phpunit/tests/admin/includesSchema.php +++ b/tests/phpunit/tests/admin/includesSchema.php @@ -249,6 +249,91 @@ public function test_populate_options_when_locale_uses_deprecated_timezone_strin $this->assertSame( 'America/Buenos_Aires', $result ); } + /** + * @ticket 50980 + * @group ms-excluded + */ + public function test_populate_options_adds_autoloaded_empty_wplang_on_single_site() { + global $wpdb; + + $orig_options = $wpdb->options; + $wpdb->options = self::$options; + + populate_options(); + + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); + wp_load_alloptions(); + + $num_queries = get_num_queries(); + $result = get_option( 'WPLANG' ); + $queries = get_num_queries() - $num_queries; + + $wpdb->query( "TRUNCATE TABLE {$wpdb->options}" ); + $wpdb->options = $orig_options; + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); + + $this->assertSame( '', $result, 'WPLANG should default to an empty string.' ); + $this->assertSame( 0, $queries, 'WPLANG should be autoloaded.' ); + } + + /** + * @ticket 50980 + * @group ms-excluded + */ + public function test_populate_options_keeps_existing_wplang() { + global $wpdb; + + $orig_options = $wpdb->options; + $wpdb->options = self::$options; + + $wpdb->insert( + $wpdb->options, + array( + 'option_name' => 'WPLANG', + 'option_value' => 'de_DE', + ) + ); + + populate_options(); + + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); + + $result = get_option( 'WPLANG' ); + + $wpdb->query( "TRUNCATE TABLE {$wpdb->options}" ); + $wpdb->options = $orig_options; + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); + + $this->assertSame( 'de_DE', $result ); + } + + /** + * @ticket 50980 + * @group multisite + * @group ms-required + */ + public function test_populate_options_does_not_add_wplang_on_multisite() { + global $wpdb; + + $orig_options = $wpdb->options; + $wpdb->options = self::$options; + + populate_options(); + + $result = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", 'WPLANG' ) ); + + $wpdb->query( "TRUNCATE TABLE {$wpdb->options}" ); + $wpdb->options = $orig_options; + wp_cache_delete( 'alloptions', 'options' ); + wp_cache_delete( 'notoptions', 'options' ); + + $this->assertNull( $result ); + } + /** * @ticket 44896 * @group multisite diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 807eda9d2cf92..641528308bb50 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -15021,7 +15021,7 @@ mockedApiResponse.settings = { "date_format": "F j, Y", "time_format": "g:i a", "start_of_week": 1, - "language": "en_US", + "language": "", "use_smilies": true, "default_category": 1, "default_post_format": "0",