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(); } } }
echo '<tr>'; echo '<td>' . $row->id . '</td><td>' . $row->name . '</td><td>' . $row->description . '</td><td>'; echo $row->recipient_list; echo '</td>'; echo '<td style="text-align: center;">' . $emails . '/' . $days . ' ' . __('days', 'ym_mailmanager') . '</td>'; echo '<td>' . ($row->enabled ? 'Yes' : 'No') . '</td>'; echo '<td>'; echo '<a href="' . $mm->page_root . '&mm_action=series&series_action=assoc&series=' . $row->id . '">' . __('Content Edit', 'ym_mailmanager') . '</a>'; echo ' / '; if ($emails) { echo '<a href="' . $mm->page_root . '&mm_action=series&series_action=enable&tseries=' . $row->id . '">' . ($row->enabled ? __('Disable', 'ym_mailmanager') : __('Enable', 'ym_mailmanager')) . '</a>'; echo ' / '; } echo '<a href="' . $mm->page_root . '&mm_action=series&series_action=delete&deleteid=' . $row->id . '" class="deletelink">' . __('Delete', 'ym_mailmanager') . '</a>'; echo '</td>'; echo '</tr>'; } echo '</tbody> <tfoot> <tr id="sourcerow"><td></td><td><input type="text" name="series_name" /></td><td><input type="text" name="series_description" /></td><td>'; echo '<select name="recipient_list">'; foreach (mailmanager_get_recipients() as $value => $text) { echo '<option value="' . $value . '">' . $text . '</option>'; } echo '</select>'; echo '</td><td><input type="submit" value="' . __('Add', 'ym_mailmanager') . '" /></td></tr> </tfoot>'; echo '</table></form>'; echo '<p><a href="' . $mm->page_root . '&mm_action=series&series_action=new" id="addlink">' . __('Create new series', 'ym_mailmanager') . '</a></p>'; } ym_box_bottom();
ym_box_bottom(); $passed_id = $email_id; include 'preview.php'; global $mm; echo '<meta http-equiv="refresh" content="10;' . $mm->page_root . '" />'; return; } } ym_box_top(__('Broadcast', 'ym_mailmanager')); echo '<form action="" method="post">'; echo '<p>' . __('Schedule or send immediately a message to selected Users', 'ym_mailmanager') . '</p>'; echo '<table class="form-table">'; define('BROADCAST_FORM_OPEN', TRUE); do_action('mailmanager_broadcast_form'); if (!defined('MAILMANAGER_FORM_REPLACED')) { echo $ym_formgen->render_combo_from_array_row(__('Recipients', 'ym_mailmanager'), 'recipient_list', mailmanager_get_recipients(), $recipient_list, __('Select a List to send to', 'ym_mailmanager')); $emails = mailmanager_get_emails(TRUE); if ($emails) { echo $ym_formgen->render_combo_from_array_row(__('Select Prior Email', 'ym_mailmanager'), 'email_id', $emails, $email_id, __('Select a previously created email', 'ym_mailmanager')); } echo $ym_formgen->render_form_table_text_row(__('Email Subject', 'ym_mailmanager'), 'email_subject', $email_subject, __('Subject of Message', 'ym_mailmanager')); $ym_formgen->render_form_table_wp_editor_row(__('Email Message', 'ym_mailmanager'), 'email_message', $email_message, __('Message to Send, you can use HTML. You can use [ym_mm_custom_field field=""] [ym_mm_if_custom_field field=""]content[/ym_mm_if_custom_field] where the "" is a Custom Profile Field', 'ym_mailmanager')); echo $ym_formgen->render_form_table_datetime_picker_row(__('Send At', 'ym_mailmanager'), 'email_date', $time, __('Date/Time to send Message', 'ym_mailmanager')); } if (!defined('STOP_VIEW_EMAILS')) { echo '</table>'; echo '<p class="submit" style="text-align: right;"> <input type="submit" name="submit" value="' . __('Schedule', 'ym_mailmanager') . ' »" /> </p>'; echo '</form>'; ym_box_bottom();
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>'; } }
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 mailmanager_list_stats() { ym_box_top('List Stats'); echo '<ul>'; foreach (mailmanager_get_recipients() as $value => $text) { echo '<li>'; echo $text; echo ' - '; echo mailmanager_get_size_of_list($value); echo ' ' . __('Subscribers', 'ym_mailmanager'); echo '</li>'; } echo '</ul>'; ym_box_bottom(); }