diff --git a/includes/dashboard.php b/includes/dashboard.php index 9a11e5eb5..5533ef449 100644 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -722,6 +722,8 @@ function action_admin_menu() { * @return string The updated language. */ function use_language_in_setting( $language = 'english', $context = '' ) { + global $locale, $wp_local_package; + // Get the currently set language. $ep_language = Utils\get_language(); @@ -730,6 +732,17 @@ function use_language_in_setting( $language = 'english', $context = '' ) { return $language; } + /** + * WordPress does not reset the language when switch_blog() is called. + * + * @see https://core.trac.wordpress.org/ticket/49263 + */ + if ( 'ep_site_default' === $ep_language ) { + $locale = null; + $wp_local_package = null; + $ep_language = get_locale(); + } + require_once ABSPATH . 'wp-admin/includes/translation-install.php'; $translations = wp_get_available_translations(); @@ -815,6 +828,10 @@ function use_language_in_setting( $language = 'english', $context = '' ) { 'Turkish', ]; + $es_snowball_similar = [ + 'Brazilian' => 'Portuguese', + ]; + foreach ( $es_languages as $analyzer_name => $analyzer_language_codes ) { if ( in_array( $wp_language, $analyzer_language_codes, true ) ) { $language = $analyzer_name; @@ -823,11 +840,12 @@ function use_language_in_setting( $language = 'english', $context = '' ) { } if ( 'filter_ewp_snowball' === $context ) { - if ( in_array( ucfirst( $language ), $es_snowball_languages, true ) ) { - return ucfirst( $language ); + $uc_first_language = ucfirst( $language ); + if ( in_array( $uc_first_language, $es_snowball_languages, true ) ) { + return $uc_first_language; } - return 'English'; + return $es_snowball_similar[ $uc_first_language ] ?? 'English'; } if ( 'filter_ep_stop' === $context ) { diff --git a/includes/partials/settings-page.php b/includes/partials/settings-page.php index f59e708a9..26c537812 100644 --- a/includes/partials/settings-page.php +++ b/includes/partials/settings-page.php @@ -165,13 +165,21 @@ 'ep_language', 'name' => 'ep_language', 'selected' => $ep_language, + 'echo' => false, ] ); + + $default_site_option = sprintf( + "", + __( 'Default to Site Language', 'elasticpress' ) + ); + + echo preg_replace( '//', "{$default_site_option}", $dropdown ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>

diff --git a/includes/utils.php b/includes/utils.php index 420811769..f56b70d70 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -495,13 +495,8 @@ function get_term_tree( $all_terms, $orderby = 'count', $order = 'desc', $flat = * @return string Default EP language. */ function get_language() { - if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { - $ep_language = get_site_option( 'ep_language' ); - } else { - $ep_language = get_option( 'ep_language' ); - } - - $ep_language = ! empty( $ep_language ) ? $ep_language : get_locale(); + $ep_language = get_option( 'ep_language' ); + $ep_language = ! empty( $ep_language ) ? $ep_language : 'ep_site_default'; /** * Filter the default language to use at index time diff --git a/tests/php/TestDashboard.php b/tests/php/TestDashboard.php index b9b697e76..f8253c139 100644 --- a/tests/php/TestDashboard.php +++ b/tests/php/TestDashboard.php @@ -19,6 +19,8 @@ class TestDashboard extends BaseTestCase { */ public function set_up() { remove_filter( 'translations_api', __NAMESPACE__ . '\skip_translations_api' ); + + parent::set_up(); } /** @@ -26,6 +28,8 @@ public function set_up() { */ public function tear_down() { tests_add_filter( 'translations_api', __NAMESPACE__ . '\skip_translations_api' ); + + parent::tear_down(); } /** @@ -68,6 +72,15 @@ public function test_use_language_in_setting_for_snowball() { }; add_filter( 'ep_default_language', $existing_lang ); $this->assertSame( 'English', Dashboard\use_language_in_setting( '', 'filter_ewp_snowball' ) ); + + /** + * Test similar languages + */ + $existing_lang = function () { + return 'pt_BR'; + }; + add_filter( 'ep_default_language', $existing_lang ); + $this->assertSame( 'Portuguese', Dashboard\use_language_in_setting( '', 'filter_ewp_snowball' ) ); } /** @@ -90,4 +103,40 @@ public function test_use_language_in_setting_for_stop() { add_filter( 'ep_default_language', $existing_lang ); $this->assertSame( '_english_', Dashboard\use_language_in_setting( '', 'filter_ep_stop' ) ); } + + /** + * Test the `use_language_in_setting` function when on multisite using `ep_site_default` + * + * @group skip-on-single-site + * @group dashboard + */ + public function test_use_language_in_setting_for_multisite() { + $site_pt_br = wp_insert_site( + [ + 'domain' => 'example.org', + 'path' => '/pt_BR', + 'options' => [ + 'WPLANG' => 'pt_BR', + ], + ] + ); + $site_he_il = wp_insert_site( + [ + 'domain' => 'example.org', + 'path' => '/he_IL', + 'options' => [ + 'WPLANG' => 'he_IL', + ], + ] + ); + + switch_to_blog( $site_pt_br ); + $this->assertSame( 'brazilian', Dashboard\use_language_in_setting() ); + + /* + * Hebrew is not a language supported by Elasticsearch out-of-the-box, so it should fallback to English. + */ + switch_to_blog( $site_he_il ); + $this->assertSame( 'english', Dashboard\use_language_in_setting() ); + } } diff --git a/tests/php/TestUtils.php b/tests/php/TestUtils.php index ed356b6ba..5fc54f1f9 100644 --- a/tests/php/TestUtils.php +++ b/tests/php/TestUtils.php @@ -443,4 +443,36 @@ public function test_delete_transient() { $this->assertSame( 1, did_action( $filter_name ) ); } + + /** + * Test the `get_language()` method + * + * @since 4.7.0 + * @group utils + */ + public function test_get_language() { + $this->assertSame( 'ep_site_default', Utils\get_language() ); + + $set_lang_via_option = function() { + return 'custom_via_option'; + }; + if ( is_multisite() ) { + add_filter( 'pre_site_option_ep_language', $set_lang_via_option ); + } else { + add_filter( 'pre_option_ep_language', $set_lang_via_option ); + } + + $this->assertSame( 'custom_via_option', Utils\get_language() ); + + /** + * Test the `ep_default_language` filter + */ + $set_lang_via_filter = function( $ep_language ) { + $this->assertSame( 'custom_via_option', $ep_language ); + return 'custom_via_filter'; + }; + add_filter( 'ep_default_language', $set_lang_via_filter ); + + $this->assertSame( 'custom_via_filter', Utils\get_language() ); + } }