Beispiel #1
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>';
     }
 }
 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>';
     }
 }
Beispiel #3
0
function mailmanager_check_series_queue()
{
    $mm_action = $_GET['mm_action'];
    if ($mm_action) {
        echo __('The MM Check Series Queue is being manually run', 'ym_mailmanager') . '<br />';
    }
    global $wpdb;
    // contains a element with target email_id
    $emails_to_send = array();
    //get series
    $sql = 'SELECT id, recipient_list FROM ' . $wpdb->prefix . 'mm_series WHERE enabled = 1';
    foreach ($wpdb->get_results($sql) as $row) {
        //get target users
        if ($sql = mailmanager_get_sql($row->recipient_list)) {
            if ($users = $wpdb->get_results($sql)) {
                foreach ($users as $i => $user) {
                    if ($mm_action) {
                        echo '<br />' . __('Found', 'ym_mailmanager') . ' ' . $user->email;
                    }
                    //add users not in table
                    $user_id = mailmanager_get_user_id($user->email);
                    // when did user join series
                    $user_join = mailmanager_get_user_in_series($user_id, $row->id, $row->recipient_list);
                    if ($user_join) {
                        if ($mm_action) {
                            echo ' ' . __('user is subscribed', 'ym_mailmanager');
                        }
                        // get what has been sent to this users
                        $sql = 'SELECT email_id FROM ' . $wpdb->prefix . 'mm_email_sent WHERE user_id = ' . $user_id;
                        $ignore = array();
                        foreach ($wpdb->get_results($sql) as $sent) {
                            $ignore[] = $sent->email_id;
                        }
                        // series emails
                        $sql = 'SELECT email_id, delay_days FROM ' . $wpdb->prefix . 'mm_email_in_series WHERE series_id = ' . $row->id;
                        $emails = array();
                        $one_day = 86400;
                        foreach ($wpdb->get_results($sql) as $email) {
                            $offset = $email->delay_days * $one_day;
                            $send_email = $user_join + $offset;
                            // already sent this email id?
                            if (!in_array($email->email_id, $ignore)) {
                                // check if need senting
                                if ($send_email <= time()) {
                                    $emails[] = $email->email_id;
                                }
                            }
                        }
                        // emails now contains the emails in this series that are due to be sent
                        // in theory this should only be a array of size 1
                        // depends how many emails of day delay 0 there are
                        foreach ($emails as $email_id) {
                            if ($mm_action) {
                                echo ' ' . __('sending EID:', 'ym_mailmanager') . $email_id;
                            }
                            list($subject, $body) = mailmanager_process_hooks($email_id, $row->id, $user_id);
                            mailmanager_send_email($user->email, $subject, $body);
                            mailmanager_log_email_send($user_id, $email_id);
                        }
                    }
                }
            }
        }
    }
}