Skip to content

Commit 2b24857

Browse files
add mailchimp data to personal data export; patch supplied by @DavidAnderson684
1 parent d7eb3d6 commit 2b24857

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
#### In development
5+
6+
- Add Mailchimp data to Personal Data exporter. Contributed by [David Anderson from UpdraftPlus](https://updraftplus.com/).
7+
8+
49
#### 4.10.1 - Feb 06, 2025
510

611
- Fix JS error breaking Ninja Forms edit form page when not connected to a Mailchimp account or account has no audiences.

autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'MC4WP_Dynamic_Content_Tags' => '/includes/class-dynamic-content-tags.php',
3232
'MC4WP_Easy_Digital_Downloads_Integration' => '/integrations/easy-digital-downloads/class-easy-digital-downloads.php',
3333
'MC4WP_Events_Manager_Integration' => '/integrations/events-manager/class-events-manager.php',
34+
'MC4WP_Personal_Data_Exporter' => '/includes/class-personal-data-exporter.php',
3435
'MC4WP_Field_Formatter' => '/includes/class-field-formatter.php',
3536
'MC4WP_Field_Guesser' => '/includes/class-field-guesser.php',
3637
'MC4WP_Form' => '/includes/forms/class-form.php',
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* Class MC4WP_Exporter
5+
*/
6+
class MC4WP_Personal_Data_Exporter
7+
{
8+
/**
9+
* Registers the personal data exporter for comments.
10+
*
11+
* @param array[] $exporters An array of personal data exporters.
12+
* @return array[] An array of personal data exporters.
13+
*/
14+
public static function add_mailchimp_to_privacy_export($exporters)
15+
{
16+
$exporters['mailchimp-subscriptions'] = [
17+
'exporter_friendly_name' => __('Mailchimp Subscriptions'),
18+
'callback' => [self::class, 'get_mailchimp_subscription_data']
19+
];
20+
21+
return $exporters;
22+
}
23+
24+
/**
25+
* Retrieves the Mailchimp subscription data for a given email address.
26+
*
27+
* This method uses the Mailchimp for WordPress (MC4WP) API to search for members based on the provided
28+
* email address and returns a list of Mailchimp lists the user is subscribed to, if any.
29+
*
30+
* @param string $email_address The email address of the user to search for.
31+
*
32+
* @return array An array containing the user's Mailchimp subscription data:
33+
* - 'data' (array): The subscription information, including:
34+
* - 'group_id' (string): The group identifier for Mailchimp.
35+
* - 'group_label' (string): The label for the group ('Mailchimp Subscriptions').
36+
* - 'item_id' (string): The item identifier ('mailchimp-subscriptions').
37+
* - 'data' (array): The subscription details, with:
38+
* - 'name' (string): The label ('Mailchimp List').
39+
* - 'value' (string): A comma-separated list of Mailchimp lists the user is subscribed to.
40+
* - 'done' (bool): Indicates the completion of the process (always true).
41+
*/
42+
public static function get_mailchimp_subscription_data($email_address)
43+
{
44+
$api = mc4wp_get_api_v3();
45+
$client = $api->get_client();
46+
$data = $client->get('search-members?query=' . urlencode($email_address));
47+
48+
// Parse the API response to get the lists the user is subscribed to.
49+
$subscribed_lists = [];
50+
$data_to_export = [];
51+
52+
if (!empty($data->exact_matches->members)) {
53+
$lists = $api->get_lists();
54+
foreach ($data->exact_matches->members as $member) {
55+
// Fetch the user's subscribed lists.
56+
if (isset($member->list_id)) {
57+
foreach ($lists as $list) {
58+
if ($list->id == $member->list_id) {
59+
$subscribed_lists[] = $list->name;
60+
continue;
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
if ($subscribed_lists) {
68+
$data_to_export[] = [
69+
'group_id' => 'mailchimp',
70+
'group_label' => __('Mailchimp Subscriptions', 'mailchimp-for-wp'),
71+
'item_id' => 'mailchimp-subscriptions',
72+
'data' => [
73+
[
74+
'name' => __('Mailchimp Lists', 'mailchimp-for-wp'),
75+
'value' => implode(', ', $subscribed_lists),
76+
]
77+
]
78+
];
79+
}
80+
81+
return [
82+
'data' => $data_to_export,
83+
'done' => true,
84+
];
85+
}
86+
}

includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
add_filter('mc4wp_integration_data', '_mc4wp_update_groupings_data', PHP_INT_MAX);
1111
add_filter('mailchimp_sync_user_data', '_mc4wp_update_groupings_data', PHP_INT_MAX);
1212
add_filter('mc4wp_use_sslverify', '_mc4wp_use_sslverify', 1);
13+
add_filter('wp_privacy_personal_data_exporters', [MC4WP_Personal_Data_Exporter::class, 'add_mailchimp_to_privacy_export']);
1314

1415
mc4wp_apply_deprecated_filters('mc4wp_merge_vars', 'mc4wp_form_data');
1516
mc4wp_apply_deprecated_filters('mc4wp_form_merge_vars', 'mc4wp_form_data');

0 commit comments

Comments
 (0)