function mailchimp_import_process() { global $wpdb; check_ajax_referer('mailchimp-import', 'nonce'); $mailchimp_stats = $_POST['mailchimp_stats']; $mailchimp_import_mailing_list = $_POST['mailchimp_import_mailing_list']; $mailchimp_import_auto_opt_in = $_POST['mailchimp_import_auto_opt_in']; if (!empty($mailchimp_import_mailing_list)) { $mailchimp_ignore_plus = get_site_option('mailchimp_ignore_plus'); $users_count = absint($_POST['import_user_counts']); $import_users_batch = apply_filters('mailchimp_import_users_batch', 200); $query = "SELECT u.ID FROM {$wpdb->users} u LIMIT {$users_count}, {$import_users_batch}"; $existing_users = $wpdb->get_col($query); $add_list = array(); foreach ($existing_users as $user_id) { $user = get_user_by('id', $user_id); if (!$user || !empty($user->spam) || !empty($user->deleted)) { continue; } $item = array('email' => array('email' => $user->user_email)); $merge_vars = array(); if ($first_name = get_user_meta($user_id, 'first_name', true)) { $merge_vars['FNAME'] = $first_name; } if ($last_name = get_user_meta($user_id, 'last_name', true)) { $merge_vars['LNAME'] = $last_name; } $merge_groups = mailchimp_get_interest_groups(); if (!empty($merge_groups)) { $merge_groups = array('groupings' => $merge_groups); } $merge_vars = array_merge($merge_vars, $merge_groups); $merge_vars = apply_filters('mailchimp_bulk_merge_vars', $merge_vars, $item, $user_id); $item['merge_vars'] = $merge_vars; $add_list[] = $item; } if (!empty($add_list)) { $results = mailchimp_bulk_subscribe_users($add_list, $mailchimp_import_mailing_list, $mailchimp_import_auto_opt_in, true); $mailchimp_stats['added'] = $mailchimp_stats['added'] + count($results['added']); $mailchimp_stats['updated'] = $mailchimp_stats['updated'] + count($results['updated']); $mailchimp_stats['errors'] = $mailchimp_stats['errors'] + count($results['errors']); } $data = array('processed' => $import_users_batch, 'mailchimp_stats' => $mailchimp_stats); wp_send_json_success($data); } else { $data = array('error' => __('Please, select a list to subscribe users', MAILCHIMP_LANG_DOMAIN)); wp_send_json_error($data); } }
function mailchimp_add_user($uid) { if (is_integer($uid)) { $user = get_userdata($uid); } elseif (is_array($uid)) { $user = new stdClass(); $user->spam = false; $user->deleted = false; $user->user_email = $uid['email']; $user->user_firstname = $uid['first_name']; $user->user_lastname = $uid['last_name']; } else { return false; } //check for spam if ($user->spam || $user->deleted) { return false; } //remove + sign emails if (get_site_option('mailchimp_ignore_plus') == 'yes' && strstr($user->user_email, '+')) { return false; } $mailchimp_mailing_list = get_site_option('mailchimp_mailing_list'); $mailchimp_auto_opt_in = get_site_option('mailchimp_auto_opt_in'); $autopt = $mailchimp_auto_opt_in == 'yes' ? true : false; $merge_vars = array('FNAME' => $user->user_firstname, 'LNAME' => $user->user_lastname); $merge_groups = mailchimp_get_interest_groups(); if (!empty($merge_groups)) { $merge_groups = array('groupings' => $merge_groups); } $merge_vars = array_merge($merge_vars, $merge_groups); $merge_vars = apply_filters('mailchimp_merge_vars', $merge_vars, $user); do_action('mailchimp_subscribe_user', $merge_vars, $user); $results = mailchimp_subscribe_user($user->user_email, $mailchimp_mailing_list, $autopt, $merge_vars, true); return $results; }