Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
_get_jetpack_locale
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
72
_install_locale
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
_unload_non_default_textdomains_on_wpcom_user_locale_switch
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
wpcom_sync_locale_from_calypso_to_atomic
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Sync locale from Calypso to Atomic sites.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8use Automattic\Jetpack\Connection\Manager as Connection_Manager;
9
10/**
11 * Get Jetpack locale name.
12 *
13 * @param  string $slug Locale slug.
14 * @return string Jetpack locale.
15 */
16function _get_jetpack_locale( $slug = '' ) {
17    if ( ! class_exists( 'GP_Locales' ) ) {
18        if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
19            require JETPACK__GLOTPRESS_LOCALES_PATH;
20        }
21    }
22
23    if ( class_exists( 'GP_Locales' ) ) {
24        $jetpack_locale_object = GP_Locales::by_field( 'slug', $slug );
25        if ( $jetpack_locale_object instanceof GP_Locale ) {
26            $jetpack_locale = $jetpack_locale_object->wp_locale ? $jetpack_locale_object->wp_locale : 'en_US';
27        }
28    }
29
30    if ( isset( $jetpack_locale ) ) {
31        return $jetpack_locale;
32    }
33
34    return 'en_US';
35}
36
37/**
38 * Install locale if not yet available.
39 *
40 * @param string $locale The new locale slug.
41 */
42function _install_locale( $locale = '' ) {
43    if ( ! in_array( $locale, get_available_languages(), true )
44        && ! empty( $locale ) && current_user_can( 'install_languages' ) ) {
45
46        if ( ! function_exists( 'wp_download_language_pack' ) ) {
47            require_once ABSPATH . 'wp-admin/includes/translation-install.php';
48        }
49
50        if ( ! function_exists( 'request_filesystem_credentials' ) ) {
51            require_once ABSPATH . 'wp-admin/includes/file.php';
52        }
53
54        if ( wp_can_install_language_pack() ) {
55            wp_download_language_pack( $locale );
56            load_default_textdomain( $locale );
57        }
58    }
59}
60
61/**
62 * Trigger reloading of all non-default textdomains if the user just changed
63 * their locale on WordPress.com.
64 *
65 * @param string $wpcom_locale The user's detected WordPress.com locale.
66 */
67function _unload_non_default_textdomains_on_wpcom_user_locale_switch( $wpcom_locale ) {
68    $user_switched_locale = get_user_locale() !== $wpcom_locale;
69    if ( ! $user_switched_locale ) {
70        return;
71    }
72
73    global $l10n;
74    $loaded_textdomains      = array_keys( $l10n );
75    $non_default_textdomains = array_diff( $loaded_textdomains, array( 'default' ) );
76    foreach ( $non_default_textdomains as $textdomain ) {
77        // Using $reloadable = true makes sure the correct locale's
78        // translations are loaded just-in-time.
79        unload_textdomain( $textdomain, true );
80    }
81}
82
83/**
84 * Handles the locale setup for Atomic sites.
85 *
86 * @param string $user_locale The user's locale.
87 */
88function wpcom_sync_locale_from_calypso_to_atomic( $user_locale ) {
89    $is_atomic_site = ( new Automattic\Jetpack\Status\Host() )->is_woa_site();
90    if ( ! $is_atomic_site ) {
91        return;
92    }
93
94    $user_id            = get_current_user_id();
95    $connection_manager = new Connection_Manager( 'jetpack' );
96    if ( ! $connection_manager->is_user_connected( $user_id ) ) {
97        return;
98    }
99    $user_data   = $connection_manager->get_connected_user_data( $user_id );
100    $user_locale = $user_data['user_locale'] ?? '';
101
102    $jetpack_locale = _get_jetpack_locale( $user_locale );
103    _install_locale( $jetpack_locale );
104    _unload_non_default_textdomains_on_wpcom_user_locale_switch( $jetpack_locale );
105
106    if ( get_user_option( 'locale', $user_id ) !== $jetpack_locale ) {
107        update_user_option( $user_id, 'locale', $jetpack_locale, true );
108    }
109}
110add_filter( 'init', 'wpcom_sync_locale_from_calypso_to_atomic' );