|
| 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 | +} |
0 commit comments