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(); } } }
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; include YM_MM_INCLUDES_DIR . 'countries.inc.php'; $language_code = 'en'; $fp = fopen('/Users/barrycarlyon/WebWork/CodingFutures/yourmembers/wordpress_dev/wordpress/wp-content/plugins/ym_mailmanager/mailgateway/getresponse/log.log', 'w'); fwrite($fp, 'a'); fclose($fp); if ($_GET['mm_action']) { ob_end_flush(); // echo as we go ob_implicit_flush(true); echo '<pre>'; echo 'Syncing with Gateway' . "\n"; } // from check if ($_GET['mm_action']) { echo 'From Check '; } $from_email_id = $this->from_id(); if (!$from_email_id) { if ($_GET['mm_action']) { echo 'No From Email '; } return; } if ($_GET['mm_action']) { echo 'Confirmations'; } $confirm_subjects = $this->getresponse->get_confirmation_subjects(); $confirm_subject_id = FALSE; $confirm_subjects = get_object_vars($confirm_subjects->result); foreach ($confirm_subjects as $id => $details) { if ($details->language_code == $language_code) { $confirm_subject_id = $id; } } if (!$confirm_subject_id) { if ($_GET['mm_action']) { echo 'AN ERROR ORRCURED: Subject'; } return; } $confirm_bodies = $this->getresponse->get_confirmation_bodies(); $confirm_bodies_id = FALSE; $confirm_bodies = get_object_vars($confirm_bodies->result); foreach ($confirm_bodies as $id => $details) { if ($details->language_code == $language_code) { $confirm_bodies_id = $id; } } if (!$confirm_bodies_id) { if ($_GET['mm_action']) { echo 'AN ERROR ORRCURED: Body'; } return; } echo ' values ' . $from_email_id . '-' . $from_email_id . '-' . $confirm_subject_id . '-' . $confirm_bodies_id; if ($_GET['mm_action']) { echo "\n" . 'Syncing All Users to list. Insert/update' . "\n"; } $campaignid = $this->options->campaign_id; $local_users = array(); foreach ($wpdb->get_results(mailmanager_get_sql('wordpress_users')) as $row) { $local_users[] = $row->email; } if ($_GET['mm_action']) { echo 'There are locally: ' . count($local_users) . "\n"; } if ($_GET['mm_action']) { echo 'There are ' . count($local_users) . ' Users to sync' . "\n"; } foreach ($local_users as $user_email) { $data = get_user_by_email($user_email); if ($data->first_name || $data->last_name) { $name = $data->first_name . ' ' . $data->last_name; } else { $name = $data->display_name; $data->first_name = ''; $data->last_name = $name; } $person_name = $name; $merge = array('LNAME' => $data->last_name); if ($data->first_name) { $merge['FNAME'] = $data->first_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']]; } // list $id = $data->ID; $data = get_user_meta($id, 'ym_user'); $account_type = $data->account_type; $pack_id = $data->pack_id; $status = get_user_meta($id, 'ym_status', TRUE); // new data $segment_account_type = 'ym_ac_' . strtolower($account_type); $segment_pack_id = 'ym_pack_' . $pack_id; $segment_all = 'ym_all_' . strtolower($status); $merge['segment_account_type'] = $segment_account_type; $merge['segment_pack_id'] = $segment_pack_id; $merge['segment_all'] = $segment_all; if ($_GET['mm_action']) { echo 'Syncing ' . $user_email . ' '; } // build customs $customs = array(); foreach ($merge as $name => $value) { if ($value) { $customs[] = array('name' => $name, 'content' => $value); } else { $customs[] = array('name' => $name, 'content' => 'empty'); } } // standard does insert or update if ($this->getresponse->add_contact($campaignid, 'standard', $person_name, $user_email, FALSE, FALSE, $customs)) { if ($_GET['mm_action']) { echo 'OK queued/pending'; } } else { if ($_GET['mm_action']) { echo 'Failed'; } } if ($_GET['mm_action']) { echo "\n"; } } if ($_GET['mm_action']) { echo 'All Done' . "\n"; 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_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); } } } } } } }