Skip to content

Commit

Permalink
Sync Calypso locale to Atomic Classic & Atomic Default (#39009)
Browse files Browse the repository at this point in the history
* Sync locale on Atomic Classic

* changelog

* Update versions

* Revert "Sync locale on Atomic Classic"

This reverts commit 9a79265.

* Extract locale sync logic

* changelog

* Bump versions

* Fix phan errors

* Remove return type

* Add guard clause

* Revert version bump in masterbar

* Revert jetpack change

* Update composer.lock

* Change the directory name

* Load wpcom-profile-settings only for wpcom users

* Update composer.lock

* Remove locale sync from Masterbar package

* changelog

* Bump versions
  • Loading branch information
okmttdhr authored Aug 22, 2024
1 parent 34fa923 commit ba84857
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Sync Calypso locale to Atomic Classic
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public static function load_features() {
require_once __DIR__ . '/features/wpcom-block-editor/class-jetpack-wpcom-block-editor.php';
require_once __DIR__ . '/features/wpcom-block-editor/functions.editor-type.php';
require_once __DIR__ . '/features/wpcom-logout/wpcom-logout.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-theme-fixes.php';

// Initializers, if needed.
Expand Down Expand Up @@ -143,7 +141,10 @@ public static function load_wpcom_user_features() {
require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
require_once __DIR__ . '/features/wpcom-admin-menu/wpcom-admin-menu.php';
require_once __DIR__ . '/features/wpcom-command-palette/wpcom-command-palette.php';
require_once __DIR__ . '/features/wpcom-locale/sync-locale-from-calypso-to-atomic.php';
require_once __DIR__ . '/features/wpcom-plugins/wpcom-plugins.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-link-to-wpcom.php';
require_once __DIR__ . '/features/wpcom-profile-settings/profile-settings-notices.php';
require_once __DIR__ . '/features/wpcom-sidebar-notice/wpcom-sidebar-notice.php';
require_once __DIR__ . '/features/wpcom-site-management-widget/class-wpcom-site-management-widget.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Sync locale from Calypso to Atomic sites.
*
* @package automattic/jetpack-mu-wpcom
*/

use Automattic\Jetpack\Connection\Manager as Connection_Manager;

/**
* Get Jetpack locale name.
*
* @param string $slug Locale slug.
* @return string Jetpack locale.
*/
function _get_jetpack_locale( $slug = '' ) {
if ( ! class_exists( 'GP_Locales' ) ) {
if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
require JETPACK__GLOTPRESS_LOCALES_PATH;
}
}

if ( class_exists( 'GP_Locales' ) ) {
$jetpack_locale_object = GP_Locales::by_field( 'slug', $slug );
if ( $jetpack_locale_object instanceof GP_Locale ) {
$jetpack_locale = $jetpack_locale_object->wp_locale ? $jetpack_locale_object->wp_locale : 'en_US';
}
}

if ( isset( $jetpack_locale ) ) {
return $jetpack_locale;
}

return 'en_US';
}

/**
* Install locale if not yet available.
*
* @param string $locale The new locale slug.
*/
function _install_locale( $locale = '' ) {
if ( ! in_array( $locale, get_available_languages(), true )
&& ! empty( $locale ) && current_user_can( 'install_languages' ) ) {

if ( ! function_exists( 'wp_download_language_pack' ) ) {
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
}

if ( ! function_exists( 'request_filesystem_credentials' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}

if ( wp_can_install_language_pack() ) {
wp_download_language_pack( $locale );
load_default_textdomain( $locale );
}
}
}

/**
* Trigger reloading of all non-default textdomains if the user just changed
* their locale on WordPress.com.
*
* @param string $wpcom_locale The user's detected WordPress.com locale.
*/
function _unload_non_default_textdomains_on_wpcom_user_locale_switch( $wpcom_locale ) {
$user_switched_locale = get_user_locale() !== $wpcom_locale;
if ( ! $user_switched_locale ) {
return;
}

global $l10n;
$loaded_textdomains = array_keys( $l10n );
$non_default_textdomains = array_diff( $loaded_textdomains, array( 'default' ) );
foreach ( $non_default_textdomains as $textdomain ) {
// Using $reloadable = true makes sure the correct locale's
// translations are loaded just-in-time.
unload_textdomain( $textdomain, true );
}
}

/**
* Handles the locale setup for Atomic sites.
*
* @param string $user_locale The user's locale.
*/
function wpcom_sync_locale_from_calypso_to_atomic( $user_locale ) {
$is_atomic_site = ( new Automattic\Jetpack\Status\Host() )->is_woa_site();
if ( ! $is_atomic_site ) {
return;
}

$user_id = get_current_user_id();
$connection_manager = new Connection_Manager( 'jetpack' );
if ( ! $connection_manager->is_user_connected( $user_id ) ) {
return;
}
$user_data = $connection_manager->get_connected_user_data( $user_id );
$user_locale = $user_data['user_locale'] ?? '';

$jetpack_locale = _get_jetpack_locale( $user_locale );
_install_locale( $jetpack_locale );
_unload_non_default_textdomains_on_wpcom_user_locale_switch( $jetpack_locale );

if ( get_user_option( 'locale', $user_id ) !== $jetpack_locale ) {
update_user_option( $user_id, 'locale', $jetpack_locale, true );
}
}
add_filter( 'init', 'wpcom_sync_locale_from_calypso_to_atomic' );
4 changes: 4 additions & 0 deletions projects/packages/masterbar/changelog/update-sync-locale
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Remove locale sync
2 changes: 1 addition & 1 deletion projects/packages/masterbar/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"extra": {
"autotagger": true,
"branch-alias": {
"dev-trunk": "0.7.x-dev"
"dev-trunk": "0.8.x-dev"
},
"changelogger": {
"link-template": "https://github.com/Automattic/jetpack-masterbar/compare/v${old}...v${new}"
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/masterbar/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-masterbar",
"version": "0.7.1-alpha",
"version": "0.8.0-alpha",
"description": "The WordPress.com Toolbar feature replaces the default admin bar and offers quick links to the Reader, all your sites, your WordPress.com profile, and notifications.",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/masterbar/#readme",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/masterbar/src/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Main {

const PACKAGE_VERSION = '0.7.1-alpha';
const PACKAGE_VERSION = '0.8.0-alpha';

/**
* Initializer.
Expand Down
7 changes: 0 additions & 7 deletions projects/packages/masterbar/src/masterbar/class-masterbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ public function __construct() {
if ( isset( $this->user_data['use_wp_admin_links'] ) ) {
update_user_option( $this->user_id, 'jetpack_admin_menu_link_destination', $this->user_data['use_wp_admin_links'] ? '1' : '0' );
}
// If Atomic, store and install user locale.
if ( $this->site_woa && 'wp-admin' !== get_option( 'wpcom_admin_interface' ) ) {
$this->user_locale = $this->get_jetpack_locale( $this->user_locale );
$this->install_locale( $this->user_locale );
$this->unload_non_default_textdomains_on_wpcom_user_locale_switch( $this->user_locale );
update_user_option( $this->user_id, 'locale', $this->user_locale, true );
}

add_action( 'admin_bar_init', array( $this, 'init' ) );

Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/update-sync-locale
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions projects/plugins/mu-wpcom-plugin/changelog/update-sync-locale
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/mu-wpcom-plugin/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions projects/plugins/wpcomsh/changelog/update-sync-locale
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


5 changes: 5 additions & 0 deletions projects/plugins/wpcomsh/changelog/update-sync-locale#2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


5 changes: 5 additions & 0 deletions projects/plugins/wpcomsh/changelog/update-sync-locale#3
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/wpcomsh/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba84857

Please sign in to comment.