public function import_one($campaignMailChimpID) { $MailChimpAPI = $this->get_api_instance(); $campaign = $MailChimpAPI->get("campaigns/{$campaignMailChimpID}"); if ($MailChimpAPI->success()) { $data = $this->map_fields($campaign); $Lists = new PerchMailChimp_Lists($this->api); $List = $Lists->get_one_by('listMailChimpID', $campaign['recipients']['list_id']); if ($List) { $data['listID'] = $List->id(); } if (!$this->remote_campaign_exists_locally($campaign['id'])) { $Campaign = $this->create($data); } else { // Campaign exists $Campaign = $this->get_one_by('campaignMailChimpID', $campaign['id']); $Compaign->update($data); } if ($Campaign) { $Campaign->import_content($MailChimpAPI); } } else { PerchUtil::debug($MailChimpAPI->getLastResponse(), 'error'); } return false; }
public function import() { $MailChimpAPI = $this->get_api_instance(); $lists = $MailChimpAPI->get("lists"); if ($MailChimpAPI->success()) { if (isset($lists['lists']) && PerchUtil::count($lists['lists'])) { $all_lists = $lists['lists']; foreach ($all_lists as $list) { $data = $this->map_fields($list); if (!$this->remote_list_exists_locally($list['id'])) { PerchUtil::debug('Importing list: ' . $list['id']); $this->create($data); } else { $Lists = new PerchMailChimp_Lists($this->api); $List = $Lists->get_one_by('listMailChimpID', $list['id']); if ($List) { $List->update($data); } } } } } else { PerchUtil::debug($MailChimpAPI->getLastResponse(), 'error'); } }
public function is_public() { $Lists = new PerchMailChimp_Lists($this->api); $List = $Lists->find((int) $this->listID()); if ($List) { if ($List->listPublic()) { return true; } } return false; }
public function update_subscription($listMailChimpID, $subStatus) { $Lists = new PerchMailChimp_Lists($this->api); $List = $Lists->get_one_by('listMailChimpID', $listMailChimpID); if (!is_object($List)) { return false; } $Subscriptions = new PerchMailChimp_Subscriptions($this->api); $Subscription = $Subscriptions->find_subscription($List, $this); if (is_object($Subscription)) { $Subscription->update(['subStatus' => $subStatus, 'subUpdated' => date('Y-m-d H:i:s')]); return true; } return false; }
public function import_next(PerchMailChimp_Import $Import) { $Lists = new PerchMailChimp_Lists($this->api); $List = $Lists->find($Import->importSourceID()); if (!is_object($List)) { return false; } $MailChimpAPI = $this->get_api_instance(); $listID = $List->listMailChimpID(); $webhooks = $MailChimpAPI->get("lists/{$listID}/webhooks", ['count' => $Import->importCount(), 'offset' => $Import->importOffset()]); if ($MailChimpAPI->success()) { if (isset($webhooks['webhooks']) && PerchUtil::count($webhooks['webhooks'])) { $all_webhooks = $webhooks['webhooks']; foreach ($all_webhooks as $webhook) { $data = $this->map_fields($webhook); $data['listID'] = $List->id(); if (!$this->remote_webhook_exists_locally($webhook['id'])) { $Webhook = $this->create($data); } else { // Webhook exists $Webhook = $this->get_one_by('webhookMailChimpID', $webhook['id']); if ($Webhook) { $Webhook->update($data); } } } // Return so that the cursor within the import is moved forward return ['result' => 'success', 'count' => PerchUtil::count($webhooks['webhooks']), 'message' => sprintf('Imported %d webhooks to list %s.', PerchUtil::count($webhooks['webhooks']), $List->listTitle())]; } else { return ['result' => 'success', 'count' => 0, 'message' => sprintf('Imported all webhooks to list %s.', $List->listTitle())]; } } else { PerchUtil::debug($MailChimpAPI->getLastResponse(), 'error'); } return false; }
<?php $Form = $API->get('Form'); $Paging = $API->get('Paging'); $Paging->set_per_page(20); $Lists = new PerchMailChimp_Lists($API); $lists = $Lists->all(); $Subscribers = new PerchMailChimp_Subscribers($API); // Run an import if there is one $Imports = new PerchMailChimp_Imports($API); $Import = $Imports->get_next_import('subscribers'); if (is_object($Import)) { $message = $HTML->warning_message('Subscribers are still updating.'); $Import->run(); } else { if ($Form->submitted()) { foreach ($lists as $List) { $Subscribers->import($List); } } } $subscribers = $Subscribers->all_subscribed($Paging); if (!PerchUtil::count($subscribers)) { foreach ($lists as $List) { $Subscribers->import($List); } $subscribers = $Subscribers->all_subscribed($Paging); }
<?php $Lists = new PerchMailChimp_Lists($API); $edit_mode = false; $List = false; $list_id = false; $message = false; $details = false; if (PerchUtil::get('id')) { if (!$CurrentUser->has_priv('perch_mailchimp.lists.edit')) { PerchUtil::redirect($API->app_path()); } $list_id = PerchUtil::get('id'); $List = $Lists->find($list_id); $edit_mode = true; } else { if (!$CurrentUser->has_priv('perch_mailchimp.lists.create')) { PerchUtil::redirect($API->app_path()); } } // Template $Template = $API->get('Template'); $Template->set('mailchimp/lists/list.html', 'mailchimp'); $tags = $Template->find_all_tags_and_repeaters(); $Form = $API->get('Form'); $Form->handle_empty_block_generation($Template); $Form->set_required_fields_from_template($Template, $details); if ($Form->submitted()) { $data = $Form->get_posted_content($Template, $Lists, $List); if ($List) { $List->update($data);
}); 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));
<?php $Form = $API->get('Form'); $Paging = $API->get('Paging'); $Paging->set_per_page(20); $Lists = new PerchMailChimp_Lists($API); $lists = $Lists->all($Paging); if (!PerchUtil::count($lists)) { // No lists! gasp! // Do we have a license key? $Settings = PerchSettings::fetch(); $api_key = $Settings->get('perch_mailchimp_api_key')->val(); if ($api_key) { $Lists->attempt_install(); $Lists->import(); $lists = $Lists->all(); } else { $message = $HTML->warning_message('Please add your MailChimp API key on the Settings page.'); } } else { if ($Form->submitted()) { $Lists->import(); $lists = $Lists->all(); $message = $HTML->success_message('Lists updated.'); } }
public function lookup_and_create(array $data) { if ($this->remote_subscriber_exists_locally($data['id'])) { return $this->get_one_by('subscriberMailChimpID', $data['id']); } $Lists = new PerchMailChimp_Lists($this->api); $List = $Lists->get_one_by('listMailChimpID', $data['list_id']); if (!is_object($List)) { return false; } $MailChimpAPI = $this->get_api_instance(); $listID = $List->listMailChimpID(); $hash = $MailChimpAPI->subscriberHash($data['email']); $member = $MailChimpAPI->get("lists/{$listID}/members/{$hash}"); if ($MailChimpAPI->success()) { $Subscriptions = new PerchMailChimp_Subscriptions($this->api); $sub_data = $this->map_fields($member); $sub_data['subscriberCreated'] = date('Y-m-d H:i:s'); $Subscriber = $this->create($sub_data); if ($Subscriber) { // create subscription $Subscriptions->create_from_import($List, $Subscriber, $member); } } }