Exemplo n.º 1
0
 public function changeEmail(User $user, $oldEmail, $newEmail)
 {
     $list_ids = $this->getDi()->newsletterUserSubscriptionTable->getSubscribedIds($user->pk());
     $lists = array();
     foreach ($this->getDi()->newsletterListTable->loadIds($list_ids) as $list) {
         if ($list->plugin_id != $this->getId()) {
             continue;
         }
         $lists[] = $list->plugin_list_id;
     }
     if (empty($lists)) {
         return;
     }
     require_once 'lib/csrest_subscribers.php';
     $customFields = $this->getCustomFields($user);
     foreach ($lists as $listId) {
         $wrap = new CS_REST_Subscribers($listId, array('api_key' => $this->getConfig('api_key')));
         $result = $wrap->update($oldEmail, array('EmailAddress' => $newEmail, 'Name' => $user->getName(), 'Resubscribe' => true));
         if (!empty($customFields)) {
             $result['CustomFields'] = $customFields;
         }
         if (!$result->was_successful()) {
             $this->debugLog("Cannot update email user {$oldEmail}/{$newEmail} by reason: {$result->http_status_code}");
         }
     }
 }
 public static function donation_button_campaign_monitor_handler($posted)
 {
     $cm_api_key = get_option("campaignmonitor_api_key");
     $client_id = get_option("campaignmonitor_client_id");
     $cm_list_id = get_option("campaignmonitor_lists");
     $fname = isset($posted['first_name']) ? $posted['first_name'] : '';
     $lname = isset($posted['last_name']) ? $posted['last_name'] : '';
     $email = isset($posted['payer_email']) ? $posted['payer_email'] : $posted['receiver_email'];
     $debug = get_option('log_enable_campaignmonitor') == 'yes' ? 'yes' : 'no';
     if ('yes' == $debug) {
         $log = new Donation_Button_Logger();
     }
     if (isset($cm_api_key) && !empty($cm_api_key) && (isset($client_id) && !empty($client_id)) && (isset($cm_list_id) && !empty($cm_list_id))) {
         include_once DBP_PLUGIN_DIR_PATH . '/admin/partials/lib/campaign_monitor/csrest_subscribers.php';
         $wrap = new CS_REST_Subscribers($cm_list_id, $cm_api_key);
         try {
             $response = $wrap->get($email);
             if ($response->http_status_code == "200") {
                 $result = $wrap->update($email, array('EmailAddress' => $email, 'Name' => $fname . ' ' . $lname, 'CustomFields' => array(), 'Resubscribe' => true));
                 if ("yes" == $debug) {
                     if ($response->response->State == "Unsubscribed") {
                         $log->add('CampaignMonitor', ' CampaignMonitor new contact ' . $email . ' added to selected contact list');
                     } else {
                         $log->add('CampaignMonitor', ' CampaignMonitor update contact ' . $email . ' to selected contact list');
                     }
                 }
             } else {
                 $result = $wrap->add(array('EmailAddress' => $email, 'Name' => $fname . ' ' . $lname, 'CustomFields' => array(), 'Resubscribe' => true));
                 if (isset($result) && 'yes' == $debug) {
                     $log->add('CampaignMonitor', ' CampaignMonitor new contact ' . $email . ' added to selected contact list');
                 }
             }
         } catch (Exception $e) {
             if ('yes' == $debug) {
                 $log->add('CampaignMonitor', prin_r($e, true));
             }
         }
     } else {
         if ('yes' == $debug) {
             $log->add('CampaignMonitor', 'Campaign Monitor API Key OR Campaign Monitor Client ID does not set');
         }
     }
 }
 public static function cmdr_mass_update()
 {
     global $cmdr_fields_to_hide;
     $cmdr_user_fields = (array) unserialize(base64_decode(get_option('cmdr_user_fields')));
     if (!class_exists('CS_REST_Lists')) {
         require_once CMDR_PLUGIN_PATH . 'campaignmonitor-createsend-php/csrest_lists.php';
     }
     if (!class_exists('CS_REST_Subscribers')) {
         require_once CMDR_PLUGIN_PATH . 'campaignmonitor-createsend-php/csrest_subscribers.php';
     }
     $auth = array('api_key' => get_option('cmdr_api_key'));
     $wrap_l = new CS_REST_Lists(get_option('cmdr_list_id'), $auth);
     $wrap_s = new CS_REST_Subscribers(get_option('cmdr_list_id'), $auth);
     // Update custom fields
     $missing_fields = $cmdr_user_fields;
     $result = $wrap_l->get_custom_fields();
     if (!$result->was_successful()) {
         self::$error = $result->response;
         return false;
     }
     if (is_array($result->response)) {
         foreach ($result->response as $key => $field) {
             $k = str_replace('[', '', str_replace(']', '', $field->Key));
             if (in_array($k, $missing_fields)) {
                 unset($missing_fields[array_search($k, $missing_fields)]);
             }
         }
     }
     foreach ($missing_fields as $key => $field) {
         if (!in_array($field, $cmdr_fields_to_hide)) {
             $result = $wrap_l->create_custom_field(array('FieldName' => $field, 'Key' => $field, 'DataType' => CS_REST_CUSTOM_FIELD_TYPE_TEXT));
             if (!$result->was_successful()) {
                 self::$error = $result->response;
                 return false;
             }
         }
     }
     // Update users
     $missing_users = array();
     $u = get_users();
     foreach ($u as $key => $value) {
         $missing_users[] = $value->user_email;
     }
     $result = $wrap_l->get_active_subscribers('', 1, 1000);
     if (!$result->was_successful()) {
         self::$error = $result->response;
         return false;
     }
     $i = 2;
     while (count($result->response->Results)) {
         if (!empty($result->response->Results) && is_array($result->response->Results)) {
             foreach ($result->response->Results as $key => $subscriber) {
                 set_time_limit(60);
                 $user = get_user_by('email', $subscriber->EmailAddress);
                 if ($user) {
                     $args = array();
                     if (trim($user->first_name . ' ' . $user->last_name) != trim($subscriber->Name)) {
                         $args['Name'] = $user->first_name . ' ' . $user->last_name;
                     }
                     $custom_values = array();
                     foreach ($subscriber->CustomFields as $field) {
                         $k = str_replace('[', '', str_replace(']', '', $field->Key));
                         $custom_values[$k] = $field->Value;
                     }
                     foreach ($cmdr_user_fields as $key => $field) {
                         if (!in_array($field, $cmdr_fields_to_hide)) {
                             if (empty($custom_values[$field]) || trim($custom_values[$field]) != trim(get_user_meta($user->ID, $field, true))) {
                                 // We export scalar values only
                                 if (is_scalar(get_user_meta($user->ID, $field, true))) {
                                     $args['CustomFields'][] = array('Key' => $field, 'Value' => get_user_meta($user->ID, $field, true));
                                 } else {
                                     $args['CustomFields'][] = array('Key' => $field, 'Value' => '');
                                 }
                             }
                         }
                     }
                     if (count($args)) {
                         $result_tmp = $wrap_s->update($user->user_email, $args);
                         if (!$result_tmp->was_successful()) {
                             self::$error = $result_tmp->response;
                             return false;
                         }
                     }
                 }
                 if (in_array($subscriber->EmailAddress, $missing_users)) {
                     unset($missing_users[array_search($subscriber->EmailAddress, $missing_users)]);
                 }
             }
         }
         $result = $wrap_l->get_active_subscribers('', $i, 1000);
         if (!$result->was_successful()) {
             self::$error = $result->response;
             return false;
         }
         $i++;
     }
     // Get bounced and unsubscribed users
     $unsubscribed = array();
     $result = $wrap_l->get_unsubscribed_subscribers('', 1, 1000);
     if (!$result->was_successful()) {
         self::$error = $result->response;
         return false;
     }
     $i = 2;
     while (count($result->response->Results)) {
         foreach ($result->response->Results as $key => $subscriber) {
             $unsubscribed[] = $subscriber->EmailAddress;
         }
         $result = $wrap_l->get_unsubscribed_subscribers('', $i, 1000);
         if (!$result->was_successful()) {
             self::$error = $result->response;
             return false;
         }
         $i++;
     }
     $bounced = array();
     $result = $wrap_l->get_bounced_subscribers('', 1, 1000);
     if (!$result->was_successful()) {
         self::$error = $result->response;
         return false;
     }
     $i = 2;
     while (count($result->response->Results)) {
         foreach ($result->response->Results as $key => $subscriber) {
             $bounced[] = $subscriber->EmailAddress;
         }
         $result = $wrap_l->get_bounced_subscribers('', $i, 1000);
         if (!$result->was_successful()) {
             self::$error = $result->response;
             return false;
         }
         $i++;
     }
     $subscribers = array();
     foreach ($missing_users as $key => $user_email) {
         if (!in_array($user_email, $unsubscribed) && !in_array($user_email, $bounced)) {
             // Subscriber does not exist, let's add him
             $user = get_user_by('email', $user_email);
             if ($user) {
                 $args = array('EmailAddress' => $user->user_email, 'Name' => $user->first_name . ' ' . $user->last_name, 'CustomFields' => array());
                 foreach ($cmdr_user_fields as $key => $field) {
                     if (!in_array($field, $cmdr_fields_to_hide)) {
                         // We export scalar values only
                         if (is_scalar(get_user_meta($user->ID, $field, true))) {
                             $args['CustomFields'][] = array('Key' => $field, 'Value' => get_user_meta($user->ID, $field, true));
                         } else {
                             $args['CustomFields'][] = array('Key' => $field, 'Value' => '');
                         }
                     }
                 }
                 $subscribers[] = $args;
             }
         }
     }
     while (count($subscribers)) {
         set_time_limit(60);
         $subscribers1000 = array();
         for ($i = 0; $i < 1000; $i++) {
             $subscribers1000[] = array_shift($subscribers);
             if (!count($subscribers)) {
                 break;
             }
         }
         $result = $wrap_s->import($subscribers1000, true);
         if (!$result->was_successful()) {
             self::$error = $result->response;
             return false;
         }
     }
     return true;
 }
Exemplo n.º 4
0
 public function email_list_update($old_email, $email, $name = NULL)
 {
     $wrap = new CS_REST_Subscribers(CM_MEMBERLIST, CM_APIKEY);
     error_log($old_email, 0);
     if (isset($name)) {
         // assume we are updating name
         $result = $wrap->update($old_email, array('Name' => $name, 'Resubscribe' => false));
     } else {
         // assume we are updating email address
         $result = $wrap->update($old_email, array('EmailAddress' => $email, 'Resubscribe' => false));
     }
     error_log("EMAIL LIST UPDATE: Result of POST /api/v3/subscribers/{list id}.{format}\n<br />", 0);
     if ($result->was_successful()) {
         error_log("Subscribed with code " . $result->http_status_code, 0);
     } else {
         error_log('Failed with code ' . $result->http_status_code . "\n<br /><pre>", 0);
         error_log(serialize($result->response), 0);
     }
 }
Exemplo n.º 5
0
<?php

require_once '../../csrest_subscribers.php';
$auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token');
$wrap = new CS_REST_Subscribers('Your list ID', $auth);
$result = $wrap->update('Old Email Address', array('EmailAddress' => 'New Email Address', 'Name' => 'Subscriber name', 'CustomFields' => array(array('Key' => 'Field Key', 'Value' => 'Field Value')), 'Resubscribe' => true));
echo "Result of PUT /api/v3.1/subscribers/{list id}.{format}?email={email}\n<br />";
if ($result->was_successful()) {
    echo "Subscribed with code " . $result->http_status_code;
} else {
    echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>";
    var_dump($result->response);
    echo '</pre>';
}