$arr_users = mgm_get_all_userids(); if (!empty($arr_users)) { $arr_remove = array('ID', 'id', 'name', 'code', 'description', 'saving', 'custom_fields', 'other_membership_types', 'setting'); foreach ($arr_users as $user_id) { $member = mgm_get_member($user_id); if (isset($member->other_membership_types) && !empty($member->other_membership_types)) { foreach ($member->other_membership_types as $key => $member_obj) { //skip if already an array: if (is_array($member_obj)) { continue; } //remove unwanted fields: foreach ($arr_remove as $remove) { if (isset($member_obj->{$remove})) { unset($member_obj->{$remove}); } } //reassign aas array: $member->other_membership_types[$key] = mgm_convert_memberobj_to_array($member_obj); } //save mgm_member object //mgm_log('resaved:' . mgm_array_dump($member, true)); } else { $member->other_membership_types = array(); } //save $member->save(); } } } // end file
function _do_import_users() { //test global $wpdb; // extract extract($_POST); // import f*g define('MGM_DOING_USERS_IMPORT', TRUE); // parese $i_response = $this->_parse_import_file(); // log // mgm_log($response, __FUNCTION__); // stop // $response['status'] = 'error'; // process data: if ($i_response['status'] == 'success' && in_array('user_email', $i_response['header'])) { // success // extract $i_users = $i_response['users']; $i_header = $i_response['header']; // object $mgm_packs = mgm_get_class('mgm_subscription_packs'); $mgm_roles = mgm_get_class('mgm_roles'); // types $membership_types = mgm_get_class('membership_types')->get_membership_types(); $memtypes = mgm_get_class('membership_types'); // check users if (!empty($i_users)) { // users found // init $row_count = count($i_users); $col_count = count($i_header); // limits $row_limit = 2000; $user_count = 0; // log // mgm_log(sprintf('Rows: [%d] Cols: [%d]',$row_count, $col_count), __FUNCTION__); // user fields $user_fields = array('first_name', 'last_name', 'user_nicename', 'user_url', 'display_name', 'nickname', 'user_firstname', 'user_lastname', 'user_description', 'user_registered'); // flag $update_count = $user_count = 0; // new users $new_users = array(); $specialchars = array(',', '\'', '"', "\n\r", "\n", '\\', '/', '$', '`', '(', ')', ' ', " "); // custom fields $cf_register_page = mgm_get_class('member_custom_fields')->get_fields_where(array('display' => array('on_register' => true, 'on_profile' => true))); // count $cf_count = count($cf_register_page); // exclude $cf_exclude_names = array('subscription_introduction', 'subscription_options', 'terms_conditions', 'privacy_policy', 'description', 'payment_gateways', 'password_conf', 'autoresponder'); // types $cf_exclude_types = array('html', 'label', 'captcha'); // loop foreach ($i_users as $i_user) { // init $update_user = $insert_user = $is_membership_update = $is_multiple_membership_update = $multiple_membership_exists = $update_role = false; // pack $pack = array(); // increment $user_count++; // remove N/A ? // $i_user = str_ireplace('N/A', '', $i_user); // init // id $id = isset($i_user['ID']) && is_numeric($i_user['ID']) ? $i_user['ID'] : ''; $email = str_replace($specialchars, '', sanitize_email($i_user['user_email'])); $user_login = str_replace($specialchars, '', sanitize_user($i_user['user_login'])); $user_password = isset($i_user['user_password']) ? $i_user['user_password'] : ''; // Issue #1559: Standardize membership type name to use machine name $membership_type = $memtypes->get_type_code($i_user['membership_type']); $pack_id = isset($i_user['pack_id']) ? sanitize_user($i_user['pack_id']) : ''; // log // mgm_log(sprintf('step 1: user_count: [%d] id: [%s] email: [%s] user_login: [%s] user_password: [%s]', $user_count, $id, $email, $user_login, $user_password), __FUNCTION__); // user id not valid if (!is_numeric($id)) { // check login/email if (!empty($user_login) && !empty($email)) { // user $_user = get_user_by('login', $user_login); // if update and different email if (isset($_user->ID) && $_user->user_email != $email) { // log // mgm_log(sprintf('step 2: %s %s', $_user->user_email, $email), __FUNCTION__); // continue continue; } // fresh insert/registration: if (!$_user) { // password $user_password = !empty($user_password) ? $user_password : wp_generate_password(); $user_password = str_replace($specialchars, '', $user_password); // trim $user_password = trim($user_password); // log // mgm_log('step 2.1 user_password: '******'step 3: %s, %s ', $user_password, $id), __FUNCTION__); // check error if (is_wp_error($id)) { // log // mgm_log(sprintf('step 4: %s', print_r($id->get_error_messages(),1)), __FUNCTION__); // unset unset($id); // continue continue; } // set new user $new_users[$id]['email'] = $email; $new_users[$id]['user_login'] = $user_login; // update option update_user_option($id, 'default_password_nag', true, true); // flag $insert_user = true; } else { // set id to user $id = $_user->ID; } // unset unset($_user); } else { // login/email not present // log // mgm_log(sprintf('step 5: %s, %s ', $user_login, $email), __FUNCTION__); // continue continue; //skip the record } } else { // update $update_user = true; } // get User object: $user = new WP_user($id); // log // mgm_log(sprintf('step 6: %s ', print_r($user,1)), __FUNCTION__); //issue #700 $format = mgm_get_date_format('date_format_short'); // ------------------------------------------ // user to mgm member if (isset($user->ID) && $user->ID > 0) { // get mgm object: $member = mgm_get_member($user->ID); // update custom fields: if (!empty($member)) { // update pack id if not supplied and already exists // This is to make pack_id optional: issue#: 807 if (!is_numeric($pack_id) && isset($member->pack_id) && $member->pack_id > 0) { $pack_id = $member->pack_id; } // update misc fields: if (!isset($member->rss_token) || isset($member->rss_token) && empty($member->rss_token)) { $member->rss_token = mgm_create_rss_token(); } // init $user_password_enc = mgm_encrypt_password($user_password, $user->ID, $member->rss_token); // mgm_log($user_password_enc, __FUNCTION__); // check if ($cf_count > 0) { // loop custom fields foreach ($cf_register_page as $field) { // key $key = $field['name']; // mgm_log($key, __FUNCTION__); // skip unwanted fields if (in_array($field['name'], $cf_exclude_names) || in_array($field['type'], $cf_exclude_types)) { // log // mgm_log(sprintf('step 7: %s ', $field['name']), __FUNCTION__); // continue continue; } // init $val = ''; //issue #700 // check if (isset($i_user[$key]) && !empty($i_user[$key]) && preg_match('/date/i', $key)) { // validate date if (mgm_is_valid_date($i_user[$key]) && mgm_format_inputdate_to_mysql($i_user[$key], $format)) { $val = $i_user[$key]; } } elseif ($key == 'email') { // email and username custom fields $val = $email; } elseif ($key == 'username') { // username $val = $user_login; } elseif ($key == 'password') { // password if (!empty($user_password_enc)) { // set $val = $user_password_enc; // log // mgm_log(sprintf('step 7.1: %s ', $user_password_enc), __FUNCTION__); } } else { $val = isset($i_user[$key]) ? $i_user[$key] : ''; } // If checkbox, then serialize the value: Issue #1070 if ($field['type'] == 'checkbox' && !empty($val)) { $val = serialize(explode("|", $val)); } // update fields: if (!empty($val) || !isset($member->custom_fields->{$key})) { // set $member->custom_fields->{$key} = $val; } // unset unset($field, $val); } } // custom fields updated // log // mgm_log(sprintf('step 8: %s ', print_r($member,1)), __FUNCTION__); // update membership: main mgm_member object if (!empty($membership_type) && is_numeric($pack_id)) { // pack if ($pack = $mgm_packs->get_pack($pack_id)) { // valid pack $member->pack_id = $pack_id; } else { // log // mgm_log(sprintf('step 9: %s ', print_r($pack,1)), __FUNCTION__); // error: continue; } // membership types: $sel_type = ''; // loop foreach ($membership_types as $key => $type) { // check if ($membership_type == $key || $membership_type == $type) { // match $sel_type = $key; break; } } // check if (!empty($sel_type)) { $membership_type = $sel_type; } else { // log // mgm_log(sprintf('step 10: %s ', $sel_type), __FUNCTION__); // continue continue; } // to distinguish between primary membership and other membership(Y/N) if (!isset($i_user['other_membership']) || isset($i_user['other_membership']) && $i_user['other_membership'] != 'Y') { // set $member->membership_type = $membership_type; // update current membership: $_response = $this->_update_member_object($member, $pack, $i_user); // check if (!$_response['status']) { // log // mgm_log(sprintf('step 11: %s ', print_r($_response,1)), __FUNCTION__); // skip the row continue; } // set $member = $_response['mgm_member']; // check guest if (strtolower($member->membership_type) == 'guest') { // default $member->other_membership_types = array(); } else { // flag $update_role = true; } } else { // init $multiple_updated = false; // if multiple mgm_member object: if (isset($member->other_membership_types) && !empty($member->other_membership_types)) { // loop foreach ((array) $member->other_membership_types as $key => $member) { // convert $member = mgm_convert_array_to_memberobj($member, $user->ID); // type if ($member->membership_type == $membership_type) { // check $_response = $this->_update_member_object($member, $pack, $i_user); // check if (!$_response['status']) { // log // mgm_log(sprintf('step 12: %s ', print_r($_response,1)), __FUNCTION__); // skip the row: continue; } // make sure array is saved: $_response['mgm_member'] = mgm_convert_memberobj_to_array($_response['mgm_member']); // set $member->other_membership_types[$key] = $_response['mgm_member']; // flag $multiple_updated = true; // break break; } } } //else {mgm_log('skip other_memberships 3', __FUNCTION__);} // add new to mother_membership_types object: if (!$multiple_updated) { // update $_response = $this->_update_member_object(new stdClass(), $pack, $i_user); // check if (!$_response['status']) { // log // mgm_log(sprintf('step 13: %s ', print_r($_response,1)), __FUNCTION__); // skip the row: continue; } // set $_response['mgm_member'] = mgm_convert_memberobj_to_array($_response['mgm_member']); // set $member->other_membership_types[] = $_response['mgm_member']; // flag $update_role = true; } } } // payment type: if (!isset($member->payment_type) || isset($member->payment_type) && empty($member->payment_type)) { $member->payment_type = 'subscription'; } // update password: if (!empty($user_password)) { // issue#: 672 // generate iss#688 /*if(empty($user_password_enc)) { // set $user_password_enc = mgm_encrypt_password($user_password, $user->ID); // log // mgm_log(sprintf('step 7.2: regenarete password: %s ', $user_password_enc), __FUNCTION__); }*/ // set $member->user_password = $user_password_enc; // md5 // $user_password_md5 = wp_hash_password($user_password); //mgm_log($wpdb->prepare("UPDATE ".$wpdb->users." SET user_pass = %s WHERE ID = %d", $user_password_md5, $user->ID), __FUNCTION__); // db update //$wpdb->query( $wpdb->prepare("UPDATE ".$wpdb->users." SET user_pass = %s WHERE ID = %d", $user_password_md5, $user->ID) ); // new user if ($insert_user) { $new_users[$id]['user_password'] = $user_password; } } // save mgm_member object: $member->save(); // update role: if ($update_role) { // update role/change order $mgm_roles->add_user_role($user->ID, $pack['role']); } // log // mgm_log(sprintf('step 14: %s ', print_r($member,1)), __FUNCTION__); } // update other user fields: $user_extra = array(); if (!empty($user_password)) { $user_extra['user_pass'] = $user_password; } // loop foreach ($i_user as $key => $value) { // check if (in_array($key, $user_fields) && !empty($value)) { if ($key == 'user_registered') { if (mgm_is_valid_date($value) && ($mysql_date = mgm_format_inputdate_to_mysql($value))) { $user_extra[$key] = $mysql_date; } } else { // set $user_extra[$key] = $value; } } } // update if (!empty($user_extra)) { // set $user_extra['ID'] = $user->ID; // update wp_update_user($user_extra); } // update $update_count++; // check here: unset($member, $user, $user_extra); } // check limit reached: if ($user_count >= $row_limit) { // check if ($row_count > $row_limit) { // set $response['message'] = sprintf(__('( Import stopped at: %s as limit( %d ) reached. )', 'mgm'), $email, $row_limit); } // break; break; } // ------------------------------------------ // unset unset($i_user); // debug // if(function_exists('gc_collect_cycles')) gc_collect_cycles(); // wait if (!($user_count % 25)) { sleep(1); } } // end imported users loop // mgm_log('IMPORT MEMORY PEAK2.5: ' . memory_get_peak_usage(true)/(1024*1024)); // free unwanted resources unset($cf_register_page, $cf_exclude_names, $user_fields, $mgm_packs, $mgm_roles, $user_count); // debug // if(function_exists('gc_collect_cycles')) gc_collect_cycles(); // done importing, mail and notify if ($update_count) { // unset unset($update_count); // set response $response['status'] = true; $response['users_count'] = count($new_users); // send admin notification: // send to admin if (!empty($new_users)) { $system_obj = mgm_get_class('system'); // message $message = sprintf('(%d) %s %s: <br/><br/>', count($new_users), __('New user registration on your blog', 'mgm'), get_option('blogname')); // loop foreach ($new_users as $user_id => $n_user) { // set $message .= sprintf('%s: %s <br/>', __('Username', 'mgm'), $n_user['user_login']); $message .= sprintf('%s: %s <br/>', __('E-mail', 'mgm'), $n_user['email']); $message .= "-----------------------------------<br/><br/>"; // unset unset($n_user); // send email to the user: // mgm_new_user_notification($user_id, $new['user_password'],false); } // unset unset($new_users); // check if (isset($response['message'])) { // set $message .= $response['message']; $message .= "-----------------------------------<br/><br/>"; } // admin email: @mgm_mail(get_option('admin_email'), sprintf('[%s] %s', __('New User Registration', 'mgm'), get_option('blogname')), $message); // unset unset($message); // debug // if(function_exists('gc_collect_cycles')) gc_collect_cycles(); } } else { // none updated $response['error'] = __('No users imported', 'mgm'); } } else { // no users $response['error'] = __('Empty records', 'mgm'); } } else { // no users $response['error'] = __('Error in processing users', 'mgm'); } // mgm_log('IMPORT MEMORY PEAK3: ' . memory_get_peak_usage(true)/(1024*1024)); // mgm_log('$response:' . mgm_array_dump($response, true)); // return return $response; }
/** * saves multiple mgm_member objects(inner mgm_objects) * Every primary mgm_member object will have an array other_membership_types[] to hold multiple mgm_member objects. * * @param $user_id: user id * @param $fields: array/object of mgm_member fields * @param update_index: index of inner mgm_member object in other_membership_types array, if specifically passed other_membership_types arrayt will be updated directly */ function mgm_save_another_membership_fields($fields, $user_id, $update_index = null) { $member = mgm_get_member($user_id); //make sure each membership object is an array: $fields = mgm_convert_memberobj_to_array($fields); $arr_remove = array('ID', 'id', 'name', 'code', 'description', 'saving', 'custom_fields', 'other_membership_types', 'setting'); foreach ($arr_remove as $remove) { if (isset($fields[$remove])) { unset($fields[$remove]); } } //checks if it is a new entry in other_membership_types array if (!isset($member->other_membership_types) || isset($member->other_membership_types) && empty($member->other_membership_types)) { $member->other_membership_types[] = $fields; } else { //looping through multiple membership object $saved = false; foreach ($member->other_membership_types as $key => $memtypes) { //this is to treat each member array(old objects) as member object:(considering backward compatibility) $memtypes = mgm_convert_array_to_memberobj($memtypes, $user_id); //if supplied mgm_object's membership type == existing member object's membership type //OR //supplied index == $key if ($memtypes->membership_type == $fields['membership_type'] || is_numeric($update_index) && $update_index == $key) { //reset if already saved $member->other_membership_types[$key] = $fields; $saved = true; break; } } //if didn't find any match insert as new if (!$saved) { $member->other_membership_types[] = $fields; } } // save $member->save(); }