public function run() { $result = false; switch ($this->importType()) { case 'subscribers': $Subscribers = new PerchMailChimp_Subscribers($this->api); $result = $Subscribers->import_next($this); break; case 'campaigns': $Campaigns = new PerchMailChimp_Campaigns($this->api); $result = $Campaigns->import_next($this); break; case 'webhooks': $Webhooks = new PerchMailChimp_Webhooks($this->api); $result = $Webhooks->import_next($this); break; } if ($result && $result['result'] == 'success') { if ($result['count'] < $this->importCount()) { // looks like that's all the pages, so self-delete. PerchUtil::debug('Result count is ' . $result['count']); PerchUtil::debug('Import count is ' . $this->importCount()); PerchUtil::debug('Deleting.'); $this->delete(); return; } $this->update(['importOffset' => $this->importOffset() + $this->importCount()]); return $result; } return $result; }
function perch_mailchimp_campaign_content($slug, $opts = array(), $return = false) { $API = new PerchAPI(1.0, 'perch_mailchimp'); $Campaigns = new PerchMailChimp_Campaigns($API); $Campaign = $Campaigns->get_one_by('campaignSlug', $slug); if ($Campaign && $Campaign->is_public()) { if ($return) { return $Campaign->get_html(); } echo $Campaign->get_html(); } return false; }
<?php $Form = $API->get('Form'); $Paging = $API->get('Paging'); $Paging->set_per_page(20); $Campaigns = new PerchMailChimp_Campaigns($API); // Run an import if there is one $Imports = new PerchMailChimp_Imports($API); $Import = $Imports->get_next_import('campaigns'); if (is_object($Import)) { $message = $HTML->warning_message('Campaigns are still updating.'); $Import->run(); } else { if ($Form->submitted()) { $Campaigns->import(); $campaigns = $Campaigns->all($Paging); } } $campaigns = $Campaigns->all($Paging); if (!PerchUtil::count($campaigns)) { $Campaigns->import(); $campaigns = $Campaigns->all($Paging); }
}); 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) { $Subscriber = $Subscribers->get_one_by('subscriberMailChimpID', $data['id']); if ($Subscriber) { $Subscriber->update_subscription($data['list_id'], 'cleaned'); } $Lists = new PerchMailChimp_Lists($API); $Lists->import(); }); MailChimpWebhook::subscribe('upemail', function ($data) use($API, $Subscribers) { $Subscriber = $Subscribers->get_one_by('subscriberEmail', $data['old_email']); if ($Subscriber) { $Subscriber->update(['subscriberEmail' => $data['new_email'], 'subscriberMailChimpID' => $data['new_id']]); } }); MailChimpWebhook::subscribe('campaign', function ($data) use($API) { $Campaigns = new PerchMailChimp_Campaigns($API); $Campaigns->import_one($data['id']); $Lists = new PerchMailChimp_Lists($API); $Lists->import(); }); # Uncomment for debug #file_put_contents(time().'_log.txt', print_r(MailChimpWebhook::receive(), 1));