function sync_with_gateway()
 {
     global $wpdb;
     if ($_GET['mm_action']) {
         echo '<pre>';
         echo 'sync' . "\n";
     }
     foreach (mailmanager_get_recipients() as $list => $text) {
         if ($_GET['mm_action']) {
             echo $list;
         }
         if ($listid = $this->associations->{$list}) {
             if ($_GET['mm_action']) {
                 echo ' has ';
             }
             $sql = 'SELECT id FROM ' . $wpdb->prefix . 'mm_series WHERE description = \'' . $listid . '\'';
             $series_id = $wpdb->get_var($sql);
             if (!$series_id) {
                 $sql = 'INSERT INTO ' . $wpdb->prefix . 'mm_series(name, description, recipient_list) VALUES (\'' . $this->name . ': ' . $text . '\', \'' . $listid . '\', \'' . $list . '\')';
                 $wpdb->query($sql);
                 $series_id = $wpdb->insert_id;
             }
             // its associated
             $users_sub = array();
             $users_unsub = array();
             // get lists
             $subscribed = $this->construct->active_subscribers($listid);
             $unsubscribed = $this->construct->unsubscribed_subscribers($listid);
             // hope no list is greater than 1000
             foreach ($subscribed->Results as $user) {
                 $users_sub[] = $user->EmailAddress;
                 $user_id = get_user_by_email($user->EmailAddress);
                 $user_id = $user_id->ID;
                 $timestamp = strtotime($user->Date);
                 mailmanager_get_user_in_series($user_id, $series_id, $list, $timestamp);
             }
             // unsub
             // hope no list is greater than 1000
             $sql = 'DELETE FROM ' . $wpdb->prefix . 'mm_list_unsubscribe WHERE list_name = \'' . $list . '\'';
             $wpdb->query($sql);
             foreach ($unsubscribed->Results as $user) {
                 $users_unsub[] = $user->EmailAddress;
                 $user = get_user_by_email($user->EmailAddress);
                 $user_id = $user_id->ID;
                 $timestamp = strtotime($user->Date);
                 // log unsub
                 mailmanager_unsubscribe_user($list, $user_id);
             }
             $users = array();
             foreach ($wpdb->get_results(mailmanager_get_sql($list)) as $row) {
                 $users[] = $row->email;
             }
             $users_to_add = array_diff($users, $users_sub, $users_unsub);
             if ($_GET['mm_action']) {
                 echo 'Live: ' . sizeof($users_sub) . ' Local: ' . sizeof($users) . ' To Add: ' . sizeof($users_to_add);
             }
             foreach ($users_to_add as $user) {
                 // fields
                 $data = get_user_by_email($user);
                 $theirname = $data->first_name . ' ' . $data->last_name;
                 // assemble and add custom fields
                 $custom = array();
                 $fields = ym_get_custom_field_array($data->ID);
                 if (sizeof($fields)) {
                     foreach ($fields as $field => $value) {
                         $custom[strtoupper($field)] = $value;
                     }
                 }
                 if ($_GET['mm_action']) {
                     echo "\n" . ' Adding ' . $user;
                 }
                 $this->construct->add_subscriber($listid, $user, $theirname, $custom);
             }
             if ($_GET['mm_action']) {
                 echo "\n";
             }
         }
         if ($_GET['mm_action']) {
             echo "\n";
         }
     }
     if ($_GET['mm_action']) {
         echo '</pre>';
     }
 }
Example #2
0
 function sync_with_gateway()
 {
     global $wpdb;
     if ($_GET['mm_action']) {
         if (!strpos($_SERVER['SERVER_SOFTWARE'], 'Debian')) {
             ob_end_flush();
             // echo as we go
             ob_implicit_flush(true);
         }
         echo '<pre>';
         echo 'Syncing with Gateway' . "\n";
     }
     if ($_GET['mm_action']) {
         echo 'Syncing YM Custom Fields with MailChimp Merge Vars' . "\n";
     }
     $local_fields = $live_fields = $fields_to_add = array();
     $fields = get_option('ym_custom_fields');
     $order = explode(';', $fields->order);
     $words = $custom_field_data = array();
     foreach ($order as $index) {
         $field = ym_get_custom_field_by_id($index);
         if ($field['name'] != 'subscription_introduction' && $field['name'] != 'subscription_options' && strlen($field['name']) <= 10 && $field['name'] != 'first_name' && $field['name'] != 'last_name' && $field['name'] != 'user_email' && $field['name'] != 'ym_password' && $field['name'] != 'user_url') {
             $word = strtoupper($field['name']);
             $local_fields[] = $word;
             $words[$word] = $field['name'];
             if ($field['name'] == 'birthdate') {
                 $field['type'] = 'birthday';
             }
             $custom_field_data[$word] = $field;
         }
     }
     $live_fieldsd = $this->mailchimp->listMergeVars($this->options->list);
     if ($live_fieldsd->error) {
         echo 'An Error Occured';
         return;
     }
     // process
     foreach ($live_fieldsd as $field) {
         $live_fields[] = $field->tag;
     }
     $fields_to_add = array_diff($local_fields, $live_fields);
     foreach ($fields_to_add as $field) {
         switch ($custom_field_data[$field]['type']) {
             case 'birthday':
                 $type = 'birthday';
                 break;
             case 'text':
             default:
                 $type = 'text';
         }
         $r = $this->mailchimp->listMergeVarAdd($this->options->list, $field, $words[$field], array('field_type' => $type));
         if ($_GET['mm_action']) {
             echo 'Result for: ' . $field . ' - ' . $r . "\n";
         }
     }
     if ($_GET['mm_action']) {
         echo 'Syncing All Users to list' . "\n";
     }
     $all_local_users = $all_live_users_subscribed = $all_live_users_unsubscribed = array();
     $list = 'wordpress_users';
     $subd = $this->mailchimp->listMembers($this->options->list, 'subscribed', FALSE, FALSE, 15000);
     $unsubd = $this->mailchimp->listMembers($this->options->list, 'unsubscribed', FALSE, FALSE, 15000);
     if ($subd->error || $unsubd->error) {
         // error
         echo 'An Error Occured';
         return;
     }
     foreach ($subd->data as $user) {
         $all_live_users_subscribed[] = $user->email;
     }
     foreach ($unsubd->data as $user) {
         $all_live_users_unsubscribed[] = $user->email;
     }
     foreach ($wpdb->get_results(mailmanager_get_sql($list)) as $row) {
         $all_local_users[] = strtolower($row->email);
     }
     if ($_GET['mm_action']) {
         echo 'There are locally: ' . sizeof($all_local_users) . "\n";
         echo 'There are live: ' . sizeof($all_live_users_subscribed) . " Subscribed\n";
         echo 'There are live: ' . sizeof($all_live_users_unsubscribed) . " Unsubcribed\n";
     }
     // sync delete
     // umsubed
     // clean all in order to handle resubscribe.....
     $sql = 'DELETE FROM ' . $wpdb->prefix . 'mm_list_unsubscribe WHERE list_name = \'' . $this->options->list . '\'';
     $wpdb->query($sql);
     foreach ($all_live_users_unsubscribed as $user_email) {
         $user_id = get_user_by_email($user_email);
         $user_id = $user_id->ID;
         // log unsub
         mailmanager_unsubscribe_user($this->options->list, $user_id);
     }
     $users_to_add = array_diff($all_local_users, $all_live_users_subscribed, $all_live_users_unsubscribed);
     if ($_GET['mm_action']) {
         echo 'There are ' . sizeof($users_to_add) . ' Users to add' . "\n";
     }
     include YM_MM_INCLUDES_DIR . 'countries.inc.php';
     foreach ($users_to_add as $user_email) {
         $data = get_user_by_email($user_email);
         $merge = array();
         $merge = array('FNAME' => $data->first_name, 'LNAME' => $data->last_name);
         $fields = ym_get_custom_field_array($data->ID);
         foreach ((array) $fields as $field => $value) {
             $merge[strtoupper(str_replace(' ', '_', $field))] = $value;
         }
         if ($merge['COUNTRY']) {
             $merge['COUNTRY'] = $countries[$merge['COUNTRY']];
         }
         if (isset($merge['BIRTHDATE']) && $merge['BIRTHDATE']) {
             $merge['BIRTHDATE'] = explode('-', $merge['BIRTHDATE']);
             // m d y
             //				$merge['BIRTHDATE'] = $merge['BIRTHDATE'][2] . '-' . $merge['BIRTHDATE'][0] . '-' . $merge['BIRTHDATE'][1];
             $merge['BIRTHDATE'] = $merge['BIRTHDATE'][0] . '/' . $merge['BIRTHDATE'][1];
         }
         if ($_GET['mm_action']) {
             echo 'Adding ' . $user_email . "\n";
         }
         $this->mailchimp->listSubscribe($this->options->list, $user_email, $merge, $this->options->double_opt_in, $this->options->welcome_message);
     }
     // for exitinst subscribed update fields
     if ($_GET['mm_action']) {
         echo 'Updating existing users' . "\n";
     }
     $counter = 0;
     foreach ($all_live_users_subscribed as $index => $user_email) {
         $data = get_user_by_email($user_email);
         $merge = array();
         $merge = array('FNAME' => $data->first_name, 'LNAME' => $data->last_name);
         $fields = ym_get_custom_field_array($data->ID);
         foreach ((array) $fields as $field => $value) {
             $merge[strtoupper($field)] = $value;
         }
         if (isset($merge['COUNTRY'])) {
             $merge['COUNTRY'] = $countries[$merge['COUNTRY']];
         }
         if (isset($merge['BIRTHDATE']) && $merge['BIRTHDATE']) {
             $merge['BIRTHDATE'] = explode('-', $merge['BIRTHDATE']);
             // m d y
             //				$merge['BIRTHDATE'] = $merge['BIRTHDATE'][2] . '-' . $merge['BIRTHDATE'][0] . '-' . $merge['BIRTHDATE'][1];
             $merge['BIRTHDATE'] = $merge['BIRTHDATE'][0] . '/' . $merge['BIRTHDATE'][1];
         }
         echo $this->mailchimp->listUpdateMember($this->options->list, $user_email, $merge);
         $counter++;
         if ($counter >= 20) {
             $counter = 0;
             echo ' ' . ($index + 1) . '/' . count($all_live_users_subscribed);
             echo "\n";
         }
     }
     echo "\n";
     // hadnle lists and segments
     if ($_GET['mm_action']) {
         echo 'General Sync Complete - Moving on to YM MM List/Segmentation' . "\n";
     }
     // what segments exists
     // if list associations are to a segment.....
     // validate segemetns
     // in case deleted server side
     // server side
     $segments = $this->mailchimp->listSegments($this->options->list);
     $this->associations = new StdClass();
     foreach ($segments as $segment) {
         $name = $segment->name;
         $this->associations->{$name} = $segment->id;
     }
     $this->saveassociations();
     // any segments left to make?
     foreach (mailmanager_get_recipients() as $nice_key => $word_name) {
         $assoc = $this->associations->{$nice_key};
         if (!$assoc) {
             if ($_GET['mm_action']) {
                 echo 'Creating Segment: ' . $nice_key . "\n";
             }
             // the ym list needs a segment
             if ($id = $this->mailchimp->listStaticSegmentAdd($this->options->list, $nice_key)) {
                 $this->associations->{$nice_key} = $id;
                 $assoc = $id;
                 // just to be safe in case of crash
                 $this->saveassociations();
             } else {
                 if ($_GET['mm_action']) {
                     echo 'There was an error, creating a list segment' . "\n";
                 }
                 return;
             }
             // ought to error catch.....
         }
     }
     // just in case
     $this->saveassociations();
     $emails = array();
     foreach ($this->associations as $nice_key => $word_name) {
         foreach ($wpdb->get_results(mailmanager_get_sql($nice_key)) as $row) {
             if ($row->email) {
                 $emails[$row->email][] = $this->associations->{$nice_key};
             }
         }
     }
     // member iterate
     $subd = $this->mailchimp->listMembers($this->options->list, 'subscribed', FALSE, FALSE, 15000);
     $counter = 0;
     foreach ($subd->data as $index => $user) {
         $email = $user->email;
         // ping
         // should generally do nothing
         // but in case admin switch the level
         // or the ipn was missed
         // do this first to stop multiple existings
         $user_data = get_user_by_email($email);
         if ($user_data) {
             $this->change_user_list(array('user_id' => $user_data->ID));
         }
         // obeslete below now?
         $data = $this->mailchimp->listMemberInfo($this->options->list, $email);
         $segments = array();
         foreach ((array) $data->data[0]->static_segments as $segment) {
             $segments[] = $segment->id;
         }
         $diff = array_diff((array) $emails[$email], $segments);
         // add or remove a user from a segment
         foreach ($diff as $item) {
             $r = $this->mailchimp->listStaticSegmentMembersAdd($this->options->list, $item, array($email));
             if ($_GET['mm_action']) {
                 echo 'a';
                 //					echo 'Adding ' . $email . ' to ' . $item . "\n";
                 if ($r->success) {
                     echo $r->success;
                 } else {
                     echo $r->error;
                 }
             }
         }
         $counter++;
         if ($counter >= 20) {
             $counter = 0;
             echo ' ' . ($index + 1) . '/' . count($subd->data);
             echo "\n";
         }
     }
     // complete
     if ($_GET['mm_action']) {
         echo 'Segmentation Complete' . "\n";
         echo 'WebHook check' . "\n";
     }
     $url = site_url() . '/?mm_webhook=1';
     if (FALSE === stripos($url, 'localhost')) {
         $hooks = $this->mailchimp->listWebHooks($this->options->list);
         $found = FALSE;
         foreach ($hooks as $hook) {
             if ($hook->url == $url) {
                 $found = TRUE;
             }
         }
         if (!$found) {
             // can't handle upemail properly
             $this->mailchimp->listWebhookAdd($this->options->list, $url, '11111');
         }
     } else {
         if ($_GET['mm_action']) {
             echo 'On Localhost' . "\n";
         }
     }
     if ($_GET['mm_action']) {
         echo 'Checking for abuse/spam reports' . "\n";
     }
     // cron job, runs daily....
     // so get the last day only
     $reports = $this->mailchimp->listAbuseReports($this->options->list, 0, 1000, time() - 86400);
     if ($reports->total) {
         foreach ($reports->data as $report) {
             $email = $report['email'];
             $user = get_user_by_email($email);
             $user_id = $user->ID;
             @ym_log_transaction(YM_USER_STATUS_UPDATE, 'Inactive', $user_id);
             update_user_option($user_id, 'ym_status', 'Inactive', true);
             if ($_GET['mm_action']) {
                 echo $email . ' reported spam removing/inactivising' . "\n";
             }
         }
     }
     if ($_GET['mm_action']) {
         echo 'All Done' . "\n";
         echo '</pre>';
         if (!strpos($_SERVER['SERVER_SOFTWARE'], 'Debian')) {
             ob_flush();
             // restart wordpress ob
             ob_start();
         }
     }
 }
Example #3
0
 function sync_with_gateway()
 {
     global $wpdb;
     if ($_GET['mm_action']) {
         echo '<pre>';
         echo 'sync' . "\n";
     }
     //print_r($this->associations);
     // this function will iterate thru all the local lists
     // it the list is associated
     // if will go get all the subscribed/unsubscribed users from aweber
     // then update the local tables
     // if it hits a user email that doth not exists locally it will? (skip it)
     foreach (mailmanager_get_recipients() as $list => $text) {
         if ($_GET['mm_action']) {
             echo $list;
         }
         if ($aweberid = $this->associations->{$list}) {
             if ($_GET['mm_action']) {
                 echo ' has ';
             }
             $sql = 'SELECT id FROM ' . $wpdb->prefix . 'mm_series WHERE description = \'' . $aweberid . '\'';
             $series_id = $wpdb->get_var($sql);
             if (!$series_id) {
                 $sql = 'INSERT INTO ' . $wpdb->prefix . 'mm_series(name, description, recipient_list) VALUES (\'Aweber: ' . $text . '\', \'' . $aweberid . '\', \'' . $list . '\')';
                 $wpdb->query($sql);
                 $series_id = $wpdb->insert_id;
             }
             // its associated
             $users_sub = array();
             $users_unsub = array();
             // go aweber
             // will contain all subed and unsubbed users
             $list_members = $this->construct->subscribers($aweberid);
             if ($this->construct_check()) {
                 // has d/c
                 if ($_GET['mm_action']) {
                     echo 'DISCONNECTED';
                 }
                 return;
             }
             $subscribed = $unsubscribed = array();
             // sort
             foreach ($list_members->entries as $entry) {
                 $status = $entry->status;
                 switch ($status) {
                     case 'unsubscribed':
                         $unsubscribed[] = $entry;
                         break;
                     case 'subscribed':
                     case 'unconfirmed':
                     default:
                         $subscribed[] = $entry;
                         break;
                 }
             }
             // le run
             foreach ($subscribed as $user) {
                 $users_sub[] = $user->email;
                 $user_id = get_user_by_email($user->email);
                 $user_id = $user_id->ID;
                 $timestamp = strtotime($user->subscribed_at);
                 // return timestamp....
                 mailmanager_get_user_in_series($user_id, $series_id, $list, $timestamp);
             }
             // umsubed
             // clean all in order to handle resubscribe.....
             $sql = 'DELETE FROM ' . $wpdb->prefix . 'mm_list_unsubscribe WHERE list_name = \'' . $list . '\'';
             $wpdb->query($sql);
             foreach ($unsubscribed as $user) {
                 $users_unsub[] = $user->email;
                 $user_id = get_user_by_email($user->email);
                 $user_id = $user_id->ID;
                 $timestamp = strtotime($user->unsubscribed_at);
                 // log unsub
                 mailmanager_unsubscribe_user($list, $user_id);
             }
             $users = array();
             foreach ($wpdb->get_results(mailmanager_get_sql($list)) as $row) {
                 $users[] = $row->email;
             }
             $users_to_add = array_diff($users, $users_sub, $users_unsub);
             if ($_GET['mm_action']) {
                 echo ' Live: ' . sizeof($users_sub) . ' Local: ' . sizeof($users) . ' To Add: ' . sizeof($users_to_add);
                 //print_r($mc_users_sub);
                 //print_r($mc_users_unsub);
                 //print_r($users);
                 //print_r($users_to_add);
             }
             // need list name
             $list_data = $this->construct->alist($aweberid);
             $awebername = $list_data->name;
             // get the web form
             $forms = $this->construct->web_forms($aweberid);
             foreach ($forms->entries as $entry) {
                 if ($entry->is_active) {
                     $form_id = $entry->id;
                 }
             }
             foreach ($users_to_add as $user) {
                 // get fname lname
                 $data = get_user_by_email($user);
                 $theirname = $data->first_name . ' ' . $data->last_name;
                 if ($theirname == ' ') {
                     list($theirname, $null) = explode('@', $user);
                 }
                 // assemble and add custom fields
                 $fields = ym_get_custom_field_array($data->ID);
                 /*
                 					foreach ($fields as $field => $value) {
                 						$custom[strtoupper($field)] = $value;
                 					}
                 */
                 if ($_GET['mm_action']) {
                     echo "\n" . ' Adding ' . $user;
                 }
                 $aweber_data = array('listname' => $awebername, 'redirect' => get_bloginfo('siteurl'), 'meta_adtracking' => 'MailManager', 'meta_required' => 'name,email', 'meta_forward_vars' => '0', 'name' => $theirname, 'email' => $user, 'submit' => 'Subscribe');
                 // prepare to hack the form
                 $this->construct->add_subscriber($aweberid, $aweber_data);
             }
             /*
             update
             foreach ($mc_users_sub as $user) {
             	// get fname lname
             	$data = get_user_by_email($user);
             	$theirname = array(
             		'FNAME'	=> $data->first_name,
             		'LNAME'	=> $data->last_name
             	);
             	// assemble and add custom fields
             	$fields = ym_get_custom_field_array($data->ID);
             	foreach ($fields as $field => $value) {
             		$theirname[strtoupper($field)] = $value;
             	}
             	if ($_GET['mm_action']) {
             		echo "\n" . ' Updating ' . $user;
             	}
             	$this->mailchimp->listUpdateMember($mcid, $user, $theirname);
             }
             */
             if ($_GET['mm_action']) {
                 echo "\n";
             }
         }
         if ($_GET['mm_action']) {
             echo "\n";
         }
     }
     if ($_GET['mm_action']) {
         echo '</pre>';
     }
 }