<?php use DrewM\MailChimp\Webhook as MailChimpWebhook; include __DIR__ . '/../../env_runtime.php'; $API = new PerchAPI(1.0, 'perch_mailchimp'); $Settings = $API->get('Settings'); $secret = $Settings->get('perch_mailchimp_secret')->val(); if ($secret != perch_get('secret')) { die('Unauthorised.'); } $Subscribers = new PerchMailChimp_Subscribers($API); MailChimpWebhook::subscribe('subscribe', function ($data) use($API, $Subscribers) { $Subscriber = $Subscribers->get_one_by('subscriberMailChimpID', $data['id']); if (!$Subscriber) { $Subscriber = $Subscribers->lookup_and_create($data); } if ($Subscriber) { $Subscriber->update_subscription($data['list_id'], 'subscribe'); } $Lists = new PerchMailChimp_Lists($API); $Lists->import(); }); MailChimpWebhook::subscribe('unsubscribe', function ($data) use($API, $Subscribers) { $Subscriber = $Subscribers->get_one_by('subscriberMailChimpID', $data['id']); if ($Subscriber) { $Subscriber->update_subscription($data['list_id'], 'unsubscribed'); } $Lists = new PerchMailChimp_Lists($API); $Lists->import(); }); MailChimpWebhook::subscribe('cleaned', function ($data) use($API, $Subscribers) {