public function updateWebhooks($vars) { require_once 'lib/csrest_lists.php'; $listIds = array_keys($this->getLists()); $wh = array(); $whActive = array(); $whDeactive = array(); foreach ($listIds as $listId) { if ($savedWh = $this->getDi()->store->get(self::CM_STORE_KEY_LIST . $listId)) { $wh[] = $savedWh; continue; } $wrap = new CS_REST_Lists($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->create_webhook(array('Events' => array(CS_REST_LIST_WEBHOOK_SUBSCRIBE, CS_REST_LIST_WEBHOOK_DEACTIVATE, CS_REST_LIST_WEBHOOK_UPDATE), 'Url' => ROOT_SURL . '/misc/misc-' . $this->getId(), 'PayloadFormat' => CS_REST_WEBHOOK_FORMAT_JSON)); if ($result->was_successful()) { $this->getDi()->store->set(self::CM_STORE_KEY_LIST . $listId, $result->response); $wh[] = $result->response; } else { throw new Am_Exception_InternalError("Cannot create webhook for {$listId} by reason: {$result->http_status_code}"); } } $this->debugLog("updateWebhooks: webhooks created " . join(',', $wh) . " for lists " . join(',', $listIds)); foreach ($vars as $k => $v) { if (!preg_match("/^newsletter.campaignmonitor.webhook_(.*)\$/", $k, $match)) { continue; } $listId = $match[1]; $webhookId = $this->getDi()->store->get(self::CM_STORE_KEY_LIST . $listId); if ($v) { if ($this->getDi()->store->get(self::CM_STORE_KEY_WEBHOOK . $webhookId)) { $whActive[] = $webhookId; continue; } $wrap = new CS_REST_Lists($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->activate_webhook($webhookId); if ($result->was_successful()) { $this->getDi()->store->set(self::CM_STORE_KEY_WEBHOOK . $webhookId, 1); $whActive[] = $webhookId; } else { throw new Am_Exception_InternalError("Cannot activate webhook {$webhookId} for {$listId} by reason: {$result->http_status_code}"); } } else { if (!$this->getDi()->store->get(self::CM_STORE_KEY_WEBHOOK . $webhookId)) { $whDeactive[] = $webhookId; continue; } $wrap = new CS_REST_Lists($listId, array('api_key' => $this->getConfig('api_key'))); $result = $wrap->deactivate_webhook($webhookId); if ($result->was_successful()) { $this->getDi()->store->delete(self::CM_STORE_KEY_WEBHOOK . $webhookId); $whDeactive[] = $webhookId; } else { throw new Am_Exception_InternalError("Cannot deactivate webhook {$webhookId} for {$listId} by reason: {$result->http_status_code}"); } } } $this->debugLog("updateWebhooks: webhooks activated " . join(',', $whActive) . "; deactivated " . join(',', $whDeactive)); return null; }
<?php require_once '../../csrest_lists.php'; $auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token'); $wrap = new CS_REST_Lists('List ID', $auth); /* * The Events array must contain a combination of * CS_REST_LIST_WEBHOOK_SUBSCRIBE * CS_REST_LIST_WEBHOOK_DEACTIVATE * CS_REST_LIST_WEBHOOK_UPDATE * * The payload format must be one of * CS_REST_WEBHOOK_FORMAT_JSON or * CS_REST_WEBHOOK_FORMAT_XML */ $result = $wrap->create_webhook(array('Events' => array(CS_REST_LIST_WEBHOOK_SUBSCRIBE, CS_REST_LIST_WEBHOOK_DEACTIVATE), 'Url' => 'http://www.example.com/webhook_receiver.php', 'PayloadFormat' => CS_REST_WEBHOOK_FORMAT_JSON)); echo "Result of POST /api/v3.1/lists/{ID}/webhooks\n<br />"; if ($result->was_successful()) { echo "Created with ID\n<br />" . $result->response; } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; }
function cmdr_plugin_menu() { if (basename($_SERVER['SCRIPT_FILENAME']) == 'plugins.php' && isset($_GET['page']) && $_GET['page'] == 'cm-dual-registration') { // Check permissions if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.', 'cm-dual-registration')); } if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['cmdr_user_fields'])) { update_option('cmdr_user_fields', base64_encode(serialize((array) $_POST['cmdr_user_fields']))); } else { update_option('cmdr_user_fields', base64_encode(serialize(array()))); } update_option('cmdr_api_key', $_POST['cmdr_api_key']); update_option('cmdr_list_id', $_POST['cmdr_list_id']); if (!class_exists('CS_REST_Lists')) { require_once CMDR_PLUGIN_PATH . 'campaignmonitor-createsend-php/csrest_lists.php'; } $auth = array('api_key' => get_option('cmdr_api_key')); $wrap_l = new CS_REST_Lists(get_option('cmdr_list_id'), $auth); if ($_POST['cmdr_cm_sync']) { update_option('cmdr_cm_sync', 1); // Create the webhook if needed $c = true; $result = $wrap_l->get_webhooks(); if (!$result->was_successful()) { wp_redirect(home_url('/wp-admin/plugins.php?page=cm-dual-registration&error=' . urlencode($result->response->Message))); die; } foreach ($result->response as $hook) { if ($hook->Url == admin_url('admin-ajax.php?action=cmdr-cm-sync')) { $c = false; break; } } if ($c) { $result = $wrap_l->create_webhook(array('Events' => array(CS_REST_LIST_WEBHOOK_SUBSCRIBE, CS_REST_LIST_WEBHOOK_UPDATE), 'Url' => admin_url('admin-ajax.php?action=cmdr-cm-sync'), 'PayloadFormat' => CS_REST_WEBHOOK_FORMAT_JSON)); if (!$result->was_successful()) { wp_redirect(home_url('/wp-admin/plugins.php?page=cm-dual-registration&error=' . urlencode($result->response->Message))); die; } } } else { delete_option('cmdr_cm_sync'); // Remove the webhook if needed $c = false; $result = $wrap_l->get_webhooks(); if (!$result->was_successful()) { wp_redirect(home_url('/wp-admin/plugins.php?page=cm-dual-registration&error=' . urlencode($result->response->Message))); die; } foreach ($result->response as $hook) { if ($hook->Url == admin_url('admin-ajax.php?action=cmdr-cm-sync')) { $c = $hook->WebhookID; break; } } if ($c) { $result = $wrap_l->delete_webhook($c); if (!$result->was_successful()) { wp_redirect(home_url('/wp-admin/plugins.php?page=cm-dual-registration&error=' . urlencode($result->response->Message))); die; } } } // Make forced sync $result = CMDR_Dual_Synchronizer::cmdr_mass_update(); if ($result) { wp_redirect(home_url('/wp-admin/plugins.php?page=cm-dual-registration&saved=true')); } else { wp_redirect(home_url('/wp-admin/plugins.php?page=cm-dual-registration&error=' . urlencode(CMDR_Dual_Synchronizer::$error->Message . (!empty(CMDR_Dual_Synchronizer::$error->ResultData) ? '<br />Error details: ' . json_encode(CMDR_Dual_Synchronizer::$error->ResultData) : '')))); } } } add_plugins_page('Campaign Monitor Dual Registration Options', 'CM Dual Registration', 'manage_options', 'cm-dual-registration', 'cmdr_plugin_page'); }