Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WordPress.com Features: Calypso Locale Sync from Calypso to wp-admin #37352

Merged
merged 10 commits into from
May 15, 2024
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

WordPress.com Features: Add wp-admin sync with Calypso locale
5 changes: 3 additions & 2 deletions projects/packages/jetpack-mu-wpcom/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"automattic/jetpack-redirect": "@dev",
"automattic/jetpack-stats-admin": "@dev",
"automattic/jetpack-status": "@dev",
"automattic/scheduled-updates": "@dev"
"automattic/scheduled-updates": "@dev",
"automattic/jetpack-compat": "@dev"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down Expand Up @@ -57,7 +58,7 @@
},
"autotagger": true,
"branch-alias": {
"dev-trunk": "5.30.x-dev"
"dev-trunk": "5.31.x-dev"
},
"textdomain": "jetpack-mu-wpcom",
"version-constants": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/jetpack-mu-wpcom/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-mu-wpcom",
"version": "5.30.0",
"version": "5.31.0-alpha",
"description": "Enhances your site with features powered by WordPress.com",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/jetpack-mu-wpcom/#readme",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Jetpack_Mu_Wpcom main class.
*/
class Jetpack_Mu_Wpcom {
const PACKAGE_VERSION = '5.30.0';
const PACKAGE_VERSION = '5.31.0-alpha';
const PKG_DIR = __DIR__ . '/../';
const BASE_DIR = __DIR__ . '/';
const BASE_FILE = __FILE__;
Expand Down Expand Up @@ -88,6 +88,7 @@ public static function load_features() {
require_once __DIR__ . '/features/wpcom-site-menu/wpcom-site-menu.php';
require_once __DIR__ . '/features/wpcom-themes/wpcom-themes.php';
require_once __DIR__ . '/features/calypso-locale-sync/sync-wp-admin-to-calypso.php';
require_once __DIR__ . '/features/calypso-locale-sync/sync-calypso-to-wp-admin.php';

// Initializers, if needed.
\Marketplace_Products_Updater::init();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* Makes sure Calypso and wp-admin locales are in sync.
*
* @package automattic/jetpack-mu-wpcom
*/

use Automattic\Jetpack\Connection\Manager as Connection_Manager;

/**
* Sync locale updated via Calypso /me/account to wp-admin.
*/
function sync_calypso_locale_to_wp_admin() {
// If we are on the profile update page.
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ), '/wp-admin/profile.php' ) !== false ) {
// Check if the 'updated' query parameter is set to '1'
if ( isset( $_GET['updated'] ) && $_GET['updated'] === '1' ) { // phpcs:disable WordPress.Security.NonceVerification.Recommended
return;
}
}

// Get user connection
$connection_manager = new Connection_Manager( 'jetpack' );
if ( ! $connection_manager->is_user_connected( get_current_user_id() ) ) {
return;
}
// Get user locale
$user_data = $connection_manager->get_connected_user_data( get_current_user_id() );
$locale = get_jetpack_locale( $user_data['user_locale'] );

// Check for changes
if ( $locale && $locale !== get_user_option( 'locale' ) ) {
// Install
install_locale( $locale );

// Update user meta
update_user_option( get_current_user_id(), 'locale', $locale, true );

// Redirect to the same page to refresh changes.
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
wp_safe_redirect( wp_unslash( $_SERVER['REQUEST_URI'] ) );
exit;
}
}
}

if ( function_exists( 'wpcom_is_nav_redesign_enabled' ) && wpcom_is_nav_redesign_enabled() ) {
// Not for POST requests because this syncing needs to be avoided when the locale is updated via /wp-admin/profile.php
if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
add_filter( 'admin_init', 'sync_calypso_locale_to_wp_admin' );
}
}

/**
* Get Jetpack locale name.
*
* @param string $slug Locale slug.
* @return string Jetpack locale.
*/
function get_jetpack_locale( $slug = '' ) {
$jetpack_locale = '';

if ( ! class_exists( 'GP_Locales' ) ) {
if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( (string) JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
require_once 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';
}
}

return $jetpack_locale;
}

/**
* 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 );
}
}
}
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.


2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
]
},
"config": {
"autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ2_1_25"
"autoloader-suffix": "d9d132a783958a00a2c7cccff60ca42d_jetpack_mu_wpcom_pluginⓥ2_1_26_alpha"
}
}
42 changes: 40 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.

2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/mu-wpcom-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Plugin Name: WordPress.com Features
* Description: Test plugin for the jetpack-mu-wpcom package
* Version: 2.1.25
* Version: 2.1.26-alpha
* Author: Automattic
* License: GPLv2 or later
* Text Domain: jetpack-mu-wpcom-plugin
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/mu-wpcom-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-mu-wpcom-plugin",
"version": "2.1.25",
"version": "2.1.26-alpha",
"description": "Test plugin for the jetpack-mu-wpcom package",
"homepage": "https://jetpack.com",
"bugs": {
Expand Down