Skip to content

Commit

Permalink
Refactor filter and profile in preparation for reverse sync PR
Browse files Browse the repository at this point in the history
  • Loading branch information
fushar committed May 14, 2024
1 parent 239747f commit 100a220
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function load_features() {
require_once __DIR__ . '/features/site-editor-dashboard-link/site-editor-dashboard-link.php';
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/calypso-locale-sync.php';
require_once __DIR__ . '/features/calypso-locale-sync/sync-wp-admin-to-calypso.php';

// Initializers, if needed.
\Marketplace_Products_Updater::init();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Makes sure Calypso and wp-admin locales are in sync.
*
* @package automattic/jetpack-mu-wpcom
*/

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;

/**
* Sync locale updated via /wp-admin/profile.php to Calypso.
*
* @param array $meta Meta values and keys for the user.
* @param WP_User $user User object.
* @param boolean $update Whether the user is being updated rather than created.
*/
function sync_wp_admin_locale_to_calypso( $meta, $user, $update ) {
$locale = $meta['locale'];
$old_locale = get_user_locale( $user );
$is_user_connected = ( new Connection_Manager( 'jetpack' ) )->is_user_connected();

if ( ! $update || $old_locale === $locale || ! $is_user_connected ) {
// Only proceed for locale changes on an existing connected WPCOM user.
return $meta;
}

if ( ! $locale ) {
// No locale means the "Site Default" option, which is the Site language (WPLANG).
$locale = get_option( 'WPLANG', '' );
}

Client::wpcom_json_api_request_as_user(
'/me/locale',
'2',
array(
'method' => 'POST',
),
array( 'locale' => $locale ),
'wpcom'
);

return $meta;
}

if ( function_exists( 'wpcom_is_nav_redesign_enabled' ) && wpcom_is_nav_redesign_enabled() ) {
add_filter( 'insert_user_meta', 'sync_wp_admin_locale_to_calypso', 8, 3 );
}

0 comments on commit 100a220

Please sign in to comment.