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

Sync User Profile Data with WPCOM on Admin Interface Change #37975

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/plugins/wpcomsh/changelog/add-sync-user-profile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Syncs the Atomic site user profile data with wpcom when the admin interface changes to Default
74 changes: 74 additions & 0 deletions projects/plugins/wpcomsh/feature-plugins/classic-sync-profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Synchronizes user profile data between WordPress.com and the local WordPress installation.
*
* @package wpcomsh
*/

use Automattic\Jetpack\Connection\Client;

/**
* Synchronize the current user's profile data from WordPress.com.
*
* Fetches the user profile data via WordPress.com's JSON API and updates the local user profile.
*
* @return void
*/
function wpcom_sync_user_profile_data() {
// Fetch the current user's profile data from wpcom.
$response = Client::wpcom_json_api_request_as_user(
'/me/profile',
'2',
array(
'method' => 'GET',
),
null,
'wpcom'
);

$response_code = wp_remote_retrieve_response_code( $response );
$response_body_content = wp_remote_retrieve_body( $response );
$profile_data = json_decode( $response_body_content, true );

// Check if the API call was successful.
if ( $response_code === 200 && is_array( $profile_data ) ) {
okmttdhr marked this conversation as resolved.
Show resolved Hide resolved
$user_id = get_current_user_id();
$user = get_userdata( $user_id );

if ( $user ) {
// Update the user's profile with the fetched data.
$userdata = array(
'ID' => $user_id,
'first_name' => $profile_data['first_name'],
'last_name' => $profile_data['last_name'],
'nickname' => $profile_data['nickname'],
'display_name' => $profile_data['display_name'],
'description' => $profile_data['description'],
'user_url' => $profile_data['user_url'],
'locale' => $profile_data['locale'],
'admin_color' => $profile_data['admin_color'],
'comment_shortcuts' => 'true',
'rich_editing' => 'true',
'syntax_highlighting' => 'true',
'show_admin_bar_front' => 'true',
);

wp_update_user( $userdata );
}
}
}

/**
* Trigger user profile data synchronization when the admin interface setting is updated.
*
* @param mixed $old_value The old value of the setting.
* @param mixed $new_value The new value of the setting.
* @return void
*/
function wpcom_admin_interface_updated( $old_value, $new_value ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
if ( $new_value === 'calypso' || empty( $new_value ) ) {
wpcom_sync_user_profile_data();
}
}
okmttdhr marked this conversation as resolved.
Show resolved Hide resolved

add_action( 'update_option_wpcom_admin_interface', 'wpcom_admin_interface_updated', 15, 2 );
1 change: 1 addition & 0 deletions projects/plugins/wpcomsh/wpcomsh.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
require_once __DIR__ . '/feature-plugins/additional-css.php';
require_once __DIR__ . '/feature-plugins/autosave-revision.php';
require_once __DIR__ . '/feature-plugins/blaze.php';
require_once __DIR__ . '/feature-plugins/classic-sync-profile.php';
require_once __DIR__ . '/feature-plugins/coblocks-mods.php';
require_once __DIR__ . '/feature-plugins/full-site-editing.php';
require_once __DIR__ . '/feature-plugins/google-fonts.php';
Expand Down