diff --git a/public/class-reuna-mailchimp-public.php b/public/class-reuna-mailchimp-public.php index 69e9fe9..614b4dd 100644 --- a/public/class-reuna-mailchimp-public.php +++ b/public/class-reuna-mailchimp-public.php @@ -42,6 +42,24 @@ class Reuna_Mailchimp_Public { */ private $version; + /** + * The MailChimp API key + * + * @since 1.0.0 + * @access private + * @var string $api_key The MailChimp API key + */ + private $api_key; + + /** + * The MailChimp list ID + * + * @since 1.0.0 + * @access private + * @var string $list_id The MailChimp list ID + */ + private $list_id; + /** * Initialize the class and set its properties. * @@ -77,14 +95,9 @@ class Reuna_Mailchimp_Public { */ public function register_rest_api_routes() { - register_rest_route($this->plugin_name . '/v1', '/subscribe/(?P\w+)', [ + register_rest_route($this->plugin_name . '/v1', '/subscribe', [ 'methods' => 'POST', - 'callback' => [$this, 'subscribe_user_to_list'], - 'args' => [ - 'list_id' => [ - 'required' => true, - ], - ], + 'callback' => [$this, 'handle_rest_api_request'], 'permission_callback' => function () { return current_user_can('read'); } @@ -92,31 +105,108 @@ class Reuna_Mailchimp_Public { } /** - * Add a subscriber + * Handle the REST API Request + * + * @since 1.0.0 + */ + public function handle_rest_api_request(WP_REST_Request $request) + { + $this->setup_environment(); + $user = wp_get_current_user(); + + if (! $this->subscriber_exists($user)) { + $this->create_subscriber($user); + } + + $tags = $request->get_param('tags'); + if (! is_array($tags)) { + return; + } + + $this->add_subscriber_tags($user, $tags); + } + + /** + * Setup class properties from environment variables * * @since 1.0.0 */ - public function subscribe_user_to_list(WP_REST_Request $request) + protected function setup_environment() { if (! function_exists('env')) { return new WP_Error('missing_env'); } - $api_key = env('MAILCHIMP_API_KEY'); - if (! $api_key) { + $this->api_key = env('MAILCHIMP_API_KEY'); + if (! $this->api_key) { return new WP_Error('missing_mailchimp_api_key'); } - $user = wp_get_current_user(); + $this->list_id = env('MAILCHIMP_LIST_ID'); + if (! $this->list_id) { + return new WP_Error('missing_mailchimp_list_id'); + } + } + + /** + * Check if a user is already subscribed to a list + * + * @since 1.0.0 + */ + protected function subscriber_exists($user) + { + $mailchimp = new Mailchimp($this->api_key); + $subscriber_hash = MailChimp::subscriberHash($user->user_email); + + $mailchimp->get('lists/' . $this->list_id . '/members/' . $subscriber_hash); + + return $mailchimp->success(); + } + + /** + * Add tags to an existing subscriber + * + * @since 1.0.0 + */ + protected function add_subscriber_tags(WP_User $user, array $tags) + { + $mailchimp = new Mailchimp($this->api_key); + $subscriber_hash = MailChimp::subscriberHash($user->user_email); + + $existing_tags = $mailchimp->get('lists/' . $this->list_id . '/members/' . $subscriber_hash . '/tags'); + $existing_tags = array_column($existing_tags, 'name'); + + $new_tags = array_values(array_unique(array_merge($existing_tags, $tags))); + $new_tags = array_map(function($tag) { + return [ + 'name' => $tag, + 'status' => 'active', + ]; + }, $new_tags); + + return $mailchimp->post('lists/' . $this->list_id . '/members/' . $subscriber_hash . '/tags', [ + 'tags' => $new_tags, + ]); + } - $mailchimp = new MailChimp($api_key); - $mailchimp->post( - 'lists/' . $request->get_param('list_id') . '/members', - [ - 'email_address' => $user->user_email, - ] - ); + /** + * Subscribe user to list + * + * @since 1.0.0 + */ + protected function create_subscriber(WP_User $user) + { + $mailchimp = new Mailchimp($this->api_key); + + $options = [ + 'email_address' => $user->user_email, + 'status' => 'subscribed', + 'merge_fields' => [ + 'FNAME' => $user->first_name, + 'LNAME' => $user->last_name, + ], + ]; - return true; + return $mailchimp->post('lists/' . $this->list_id . '/members', $options); } } diff --git a/public/js/reuna-mailchimp-public.js b/public/js/reuna-mailchimp-public.js index 41aeb98..33415d8 100644 --- a/public/js/reuna-mailchimp-public.js +++ b/public/js/reuna-mailchimp-public.js @@ -3,12 +3,14 @@ $(window).on('reuna-mailchimp-subscribe', function(event) { $.ajax({ - url: wpApiSettings.root + 'reuna-mailchimp/v1/subscribe/' + event.detail.list_id, + url: wpApiSettings.root + 'reuna-mailchimp/v1/subscribe/', method: 'POST', beforeSend: function (xhr) { xhr.setRequestHeader('X-WP-Nonce', wpApiSettings.nonce); }, - data: {} + data: { + tags: event.detail.tags, + }, }).done(function(response) {}); });