Skip to content

Add user synchronization #156

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

Merged
merged 23 commits into from
Jun 3, 2025
Merged

Add user synchronization #156

merged 23 commits into from
Jun 3, 2025

Conversation

iamdharmesh
Copy link
Collaborator

@iamdharmesh iamdharmesh commented May 14, 2025

Description of the Change

This PR adds functionality to sync WordPress users to Mailchimp. It provides options to select user roles to sync and to choose the subscription status for users in the user sync settings.

The feature allows manual syncing of all users at once using background processing and also listens for new user registrations and updates to existing users. Currently, the sync is one-way (WordPress → Mailchimp). The feature also includes a UI to view users that failed to sync, along with the associated error messages. I have created #157, to handle update subscription status from user profile separately.

screencapture-mailchimp-local-wp-admin-admin-php-2025-05-15-15_23_15

Quick Demo:

Screen.Recording.2025-05-15.at.3.46.56.PM.mov

Closes #155

How to test the Change

Prerequisites:

  • Connect your Mailchimp account and select a list.
  • Create some users in WordPress to test user synchronization.
  1. Go to Mailchimp > Settings > User Sync tab. You will see a "Start Sync" CTA.

Manual Sync:

  1. Save the user sync settings (select appropriate user roles and subscriber status).
  2. Click on "Synchronize All Users" and verify that the sync process starts.
  3. Confirm that the total user count is displayed correctly based on the selected roles.
  4. Once the sync is complete, verify from the Mailchimp dashboard that users are synced correctly (with correct status).
  5. Enable "Sync Existing Contacts Only" and run the sync.
  6. Verify that only existing Mailchimp contacts are synced.
  7. Test the sync with a large number of users and confirm the process completes without issues.
  8. Add a user with a non-valid email (e.g., example.com or test.com) and run the sync.
  9. Confirm that the sync fails for that user, the error appears below the user sync settings, and you can delete the error entry if needed.

Auto Sync:

  1. Enable "Enable Auto User Sync".
  2. Create a new user and verify it is synced properly with Mailchimp.
  3. Update an existing user and confirm the changes are synced correctly to Mailchimp.

Changelog Entry

Added - User synchronization to allow syncing WordPress users to Mailchimp.

Credits

Props @dkotter @jeffpaul @iamdharmesh

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@iamdharmesh iamdharmesh self-assigned this May 14, 2025
@github-actions github-actions bot added this to the 1.9.0 milestone May 14, 2025
@iamdharmesh iamdharmesh changed the title [WIP] Add user synchronization Add user synchronization May 15, 2025
Comment on lines +277 to +297
if ( $current_status ) {
switch ( $current_status ) {
// If user is already subscribed, unsubscribed or transactional, don't change the status.
case 'subscribed':
case 'unsubscribed':
case 'transactional':
$subscribe_status = $current_status;
break;

// If user is cleaned, set the status as pending.
case 'cleaned':
$subscribe_status = 'pending';
break;

// If user is archived, pending or anything else, set the status as per the subscribe status in settings.
case 'archived':
case 'pending':
default:
break;
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For existing Mailchimp contacts, we mostly don't update the status according to the settings on the WP side. However, for statuses like cleaned, archived, and pending, we do consider updating them based on the user sync settings.

Please let me know if you think we should handle this differently. Thanks!

@iamdharmesh iamdharmesh requested a review from dkotter May 15, 2025 14:30
@iamdharmesh iamdharmesh marked this pull request as ready for review May 15, 2025 14:30
@github-actions github-actions bot added the needs:code-review This requires code review. label May 15, 2025
Copy link
Collaborator

@dkotter dkotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code here looks great! Only question I have is in regards to the inclusion of Action Scheduler. Would it make sense to only include that if either a sync is manually triggered or the option to keep users in sync is set? That way we aren't loading Action Scheduler unless we need it? Not sure how hard that would be to accomplish

@iamdharmesh
Copy link
Collaborator Author

Would it make sense to only include that if either a sync is manually triggered or the option to keep users in sync is set? That way we aren't loading Action Scheduler unless we need it? Not sure how hard that would be to accomplish

I tried setting up a custom option when we start the manual user sync and load the Action Scheduler only if that option is set (it gets cleared once the sync is complete) or if auto sync is enabled. I'm not sure if this is the best way to do it, but it seems to work fine. You can check the change at: 809b7a7

Thanks!

@iamdharmesh iamdharmesh requested a review from dkotter May 20, 2025 09:09
@dkotter dkotter requested a review from qasumitbagthariya May 20, 2025 14:38
@qasumitbagthariya
Copy link
Collaborator

QA Update ✅


I have verified this PR in the enhancement/user-sync branch, which has been fixed and is functioning as intended.

I tested the following on this branch:

  • Manual Sync
  • Auto Sync
Synchronize.All.Users.1.mov
Sync.existing.contacts.only.2.mov

Testing Environment

  • WordPress: 6.8
  • Theme: Storefront 4.6.1
  • Theme: Twenty Twenty-Four 1.3
  • WooCommerce - 9.8.5
  • PHP: 8.0.30
  • Web Server: Nginx 1.20.2
  • Browser: Chrome
  • OS: macOS 15.2
  • Branch: enhancement/user-sync

Steps to Test- As mentioned in the PR description.
Test Results - It is working as expected.
Functional Demo / Screencast -
Special Notes - Ready for UAT
Testing Document status:
Cases related to this Issue/PR are added to the Critical Flow Wiki pages:

  • Yes - pending
  • Not Required/Applicable for this PR

<?php esc_html_e( 'Sync as non-subscribed', 'mailchimp' ); ?>
</label>
<p class="description_small">
<?php esc_html_e( 'This status indicates you haven\'t gotten permission to market to these users. However, you can use Mailchimp to send ', 'mailchimp' ); ?><a href="https://mailchimp.com/help/about-non-subscribed-contacts/" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'non-subscribed contacts', 'mailchimp' ); ?></a> <?php esc_html_e( 'transactional emails and postcards and target them with ads.', 'mailchimp' ); ?>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps simpler to change the second sentence to:

However, you can use Mailchimp to message non-subscribed contacts.

?>
<input type="checkbox" name="<?php echo esc_attr( $this->option_name . '[existing_contacts_only]' ); ?>" value="1" <?php checked( $existing_contacts_only, 1, true ); ?> />
<p class="description">
<?php esc_html_e( 'Only WordPress users who are already in your Mailchimp audience will sync. You won\'t be able to send your other users postcards or target them with ads.', 'mailchimp' ); ?>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably simpler to just remove the second sentence here

<div>
<label for="subscriber_status_transactional" class="subscribe_status_label">
<input type="radio" id="subscriber_status_transactional" name="<?php echo esc_attr( $this->option_name . '[subscriber_status]' ); ?>" value="transactional" <?php checked( $settings, 'transactional' ); ?> />
<?php esc_html_e( 'Sync as non-subscribed', 'mailchimp' ); ?>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's capitalize here to:

Sync as Non-Subscribed

@jeffpaul
Copy link
Collaborator

jeffpaul commented Jun 3, 2025

@vikrampm1 can you please open a follow-up issue where we display in the WP Users table list view some sort of notation noting users that are synced to Mailchimp? Perhaps a column & icon similar to what we do in Distributor and then have that icon link to the specific audience?

@vikrampm1 vikrampm1 merged commit c59af15 into develop Jun 3, 2025
13 checks passed
@vikrampm1 vikrampm1 mentioned this pull request Jun 3, 2025
21 tasks
@iamdharmesh iamdharmesh mentioned this pull request Jun 4, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs:code-review This requires code review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add user synchronization feature
5 participants