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;
 }
 /** 
  * get mgm subscriptions
  */
 private function _get_user_subscriptions(&$member, $user)
 {
     // log
     // mgm_log($member, __FUNCTION__);
     // other_membership_types
     $other_membership_types = $member->other_membership_types;
     // unset prefilter
     unset($member->other_membership_types);
     // subscriptions
     $subscriptions = array();
     // set
     $subscriptions[] = $this->_get_user_subscription($member, $user);
     // parse other_membership_types as subscriptions tree
     if (!empty($other_membership_types)) {
         // mgm_log($other_membership_types, 'other_membership_types_'.$user->ID);
         foreach ($other_membership_types as $key => $member_oth) {
             // as object
             $o_mgm_member = mgm_convert_array_to_memberobj($member_oth, $user->ID);
             // set
             $subscriptions[] = $this->_get_user_subscription($o_mgm_member, $user);
             // unset
             unset($o_mgm_member);
         }
     }
     // unset
     unset($member);
     // return
     return $subscriptions;
 }
 function _do_export($export_format, $export_option)
 {
     global $wpdb;
     $status = 'error';
     $message = __('Error while exporting Coupons.', 'mgm');
     // users
     if ($export_option != 'unused') {
         //$users = mgm_get_all_userids(array('ID'), 'get_results');
         $start = 0;
         $limit = 1000;
         //user meta fields
         $fields = array('user_id', 'meta_value');
         // sql
         $user_sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
         $count = $wpdb->get_var($user_sql);
         $users = wp_cache_get('all_mgm_user_meta', 'users');
         // if empty read from db:
         if (empty($users)) {
             $users = array();
             if ($count) {
                 // read again
                 for ($i = $start; $i < $count; $i = $i + $limit) {
                     $users = array_merge($users, (array) mgm_patch_partial_user_member_options($i, $limit, $fields));
                     //a small delay of 0.01 second
                     usleep(10000);
                 }
                 wp_cache_set('all_mgm_user_meta', $users, 'users');
             }
         }
     }
     //mgm_log(mgm_array_dump($users,true),__FUNCTION__);
     //array
     $export_coupons = array();
     //export options issue #1494
     if ($export_option == 'used') {
         $query = " AND `used_count` > 0";
     } elseif ($export_option == 'unused') {
         $query = " AND `used_count` IS NULL";
     } else {
         $query = '';
     }
     // count
     $total = $wpdb->get_var("SELECT COUNT(*) FROM `" . TBL_MGM_COUPON . "` WHERE 1 " . $query);
     //
     $perpage = 50;
     // check
     if ($total > $perpage) {
         $pages = ceil($total / $perpage);
     } else {
         $pages = 1;
     }
     // loop pages
     for ($i = 1; $i <= $pages; $i++) {
         //@set_time_limit(300);//300s
         //@ini_set('memory_limit', 134217728);// 128M
         // offset
         $offset = ($i - 1) * $perpage;
         // coupons
         $sql = "SELECT `id`, `name` FROM `" . TBL_MGM_COUPON . "` WHERE 1 {$query} ORDER BY `create_dt` DESC LIMIT {$offset},{$perpage}";
         // coupons
         $coupons = $wpdb->get_results($sql);
         // log
         // mgm_log('Page: ' .$i. '. '. $wpdb->last_query, __FUNCTION__);
         // loop
         foreach ($coupons as $coupon) {
             // found
             if ($users && $export_option != 'unused') {
                 $coupon_used = false;
                 foreach ($users as $user) {
                     if ($user->user_id) {
                         // member
                         $user_id = $user->user_id;
                         $member = unserialize($user->meta_value);
                         $member = mgm_convert_array_to_memberobj($member, $user_id);
                         // show
                         $show = false;
                         // check
                         if (mgm_member_has_coupon($member, $coupon->id)) {
                             $show = true;
                         } else {
                             if (isset($member->other_membership_types) && !empty($member->other_membership_types) && count($member->other_membership_types) > 0) {
                                 // loop
                                 foreach ($member->other_membership_types as $key => $member_oth) {
                                     // as object
                                     if (is_array($member_oth)) {
                                         $o_mgm_member = mgm_convert_array_to_memberobj($member_oth, $user_id);
                                     } else {
                                         if (is_object($member_oth)) {
                                             $o_mgm_member = $member_oth;
                                         }
                                     }
                                     // check
                                     if (mgm_member_has_coupon($o_mgm_member, $coupon->id)) {
                                         $show = true;
                                         break;
                                     }
                                     // unset
                                     unset($o_mgm_member);
                                 }
                             }
                         }
                         // include
                         if ($show) {
                             //mgm_log(mgm_array_dump($member,true),__FUNCTION__);
                             // user
                             $user = get_userdata($user_id);
                             // int
                             $row = new stdClass();
                             // export fields
                             $row->coupon_id = $coupon->id;
                             $row->coupon_name = $coupon->name;
                             $row->user_id = $user->ID;
                             $row->user_login = $user->user_login;
                             $row->user_email = $user->user_email;
                             $row->user_membership_type = ucwords(str_replace('_', ' ', $member->membership_type));
                             // cache
                             $export_coupons[] = $row;
                             // unset
                             unset($row);
                             $coupon_used = true;
                         }
                         unset($user);
                         unset($member);
                     }
                 }
                 if (!$coupon_used && $export_option == 'all') {
                     // int
                     $row = new stdClass();
                     // export fields
                     $row->coupon_id = $coupon->id;
                     $row->coupon_name = $coupon->name;
                     $row->user_id = '';
                     $row->user_login = '';
                     $row->user_email = '';
                     $row->user_membership_type = '';
                     // cache
                     $export_coupons[] = $row;
                     // unset
                     unset($row);
                 }
             } else {
                 // int
                 $row = new stdClass();
                 // export fields
                 $row->coupon_id = $coupon->id;
                 $row->coupon_name = $coupon->name;
                 $row->user_id = '';
                 $row->user_login = '';
                 $row->user_email = '';
                 $row->user_membership_type = '';
                 // cache
                 $export_coupons[] = $row;
                 // unset
                 unset($row);
             }
         }
     }
     // log
     // mgm_log($export_coupons, 'export_coupons');
     // check
     if (count($export_coupons) > 0) {
         // success
         $success = count($export_coupons);
         // create
         if ($export_format == 'csv') {
             $filename = mgm_create_csv_file($export_coupons, 'export_coupons');
         } else {
             $filename = mgm_create_xls_file($export_coupons, 'export_coupons');
         }
         // src
         $file_src = MGM_FILES_EXPORT_URL . $filename;
         // message
         $message = sprintf(__('Successfully exported %d %s.', 'mgm'), $success, $success > 1 ? 'coupons' : 'coupon');
         //status
         $status = 'success';
         //return
         return array('status' => $status, 'message' => $message, 'action' => 'export', 'src' => $file_src);
     } else {
         $message .= ' ' . __('No coupons found with associated users.', 'mgm');
     }
     // return
     return array('status' => $status, 'message' => $message, 'action' => 'export');
 }
Exemplo n.º 4
0
 foreach ($users as $user) {
     // get
     $member = mgm_get_member($user->ID);
     // if users with same membershiptype as that of selected
     if (isset($member->membership_type) && $member->membership_type == $type_code) {
         // set
         $member->membership_type = $new_type_code;
         // save
         $member->save();
     }
     // check if any multiple levels exist:
     if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
         // loop
         foreach ($member->other_membership_types as $key => $memtypes) {
             // make sure its an object:
             $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
             // verify
             if ($memtypes->membership_type == $type_code) {
                 // set
                 $memtypes->membership_type = $new_type_code;
                 // save
                 mgm_save_another_membership_fields($memtypes, $user->ID, $key);
             }
         }
     }
     // unset
     unset($member);
 }
 //unset
 $membership_types_obj->unset_membership_type($type_code);
 // set
Exemplo n.º 5
0
         }
         // update
         update_user_option($user_id, sprintf('mgm_user_billing_num_cycles_%d', $member->pack_id), $user_billing_num_cycles, true);
     }
 }
 // othe membership level
 if (isset($member->other_membership_types) && !empty($member->other_membership_types) && is_object($member)) {
     //loop
     foreach ($member->other_membership_types as $key => $other_membership_type) {
         //filter for empty values
         $other_membership_type = array_filter($other_membership_type);
         //check
         if (!empty($other_membership_type)) {
             // convet
             if (is_array($other_membership_type)) {
                 $other_membership_type = mgm_convert_array_to_memberobj($other_membership_type, $user_id);
             }
             //check
             if (mgm_get_user_option(sprintf('mgm_user_billing_num_cycles_%d', $other_membership_type->pack_id), $user_id) === FALSE) {
                 //othermembership level wise user meta - issue #1681
                 if (isset($other_membership_type->pack_id) && (int) $other_membership_type->pack_id > 0) {
                     // init
                     $user_billing_num_cycles = 'ongoing';
                     // ongoing
                     // check limited
                     if (isset($other_membership_type->active_num_cycles) && (int) $other_membership_type->active_num_cycles >= 1) {
                         // set
                         $user_billing_num_cycles = (int) $other_membership_type->active_num_cycles;
                     } else {
                         // check pack
                         if (isset($other_membership_type->pack_id) && (int) $other_membership_type->pack_id > 0) {
 function get_expected_recurring_earning($date_start, $date_end, $member_type)
 {
     global $wpdb;
     $start = 0;
     $limit = 1000;
     //user meta fields
     $fields = array('user_id', 'meta_value');
     // sql
     $sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
     $count = $wpdb->get_var($sql);
     // init
     $members = array();
     // packs
     $packs = mgm_get_class('subscription_packs');
     $sformat = mgm_get_date_format('date_format_short');
     $curr_date = mgm_get_current_datetime();
     $current_date = $curr_date['timestamp'];
     $on_going = array();
     $recuring_cycles = array();
     $dt = array();
     //setting the default dates
     if (empty($date_end)) {
         $end_date = date('Y-m-d', $current_date);
         $end_date = date('Y-m-d', strtotime("{$end_date} + 6 months"));
     } else {
         $end_date = mgm_format_inputdate_to_mysql($date_end, $sformat);
         //$end_date=str_replace('/','-',$date_end);
     }
     if (!empty($date_start)) {
         $start_date = mgm_format_inputdate_to_mysql($date_start, $sformat);
         //$start_date=str_replace('/','-',$date_start);
         if (strtotime($start_date) > $current_date) {
             $current_date = strtotime($start_date);
         }
     }
     //count
     if ($count) {
         for ($k = $start; $k < $count; $k = $k + $limit) {
             $users = mgm_patch_partial_user_member_options($k, $limit, $fields);
             foreach ($users as $user) {
                 $user_id = $user->user_id;
                 $member = unserialize($user->meta_value);
                 // convert
                 $member = mgm_convert_array_to_memberobj($member, $user_id);
                 $expire_date = $member->expire_date;
                 $membership_type = $member->membership_type;
                 if (!empty($expire_date) && $member->status == 'Active' && $member->amount > 0) {
                     if ($membership_type == $member_type || $member_type == 'all') {
                         if (strtotime($expire_date) >= $current_date) {
                             //echo "<br/>Expire_date : ".$expire_date;
                             //echo "<br/>Current_date : ".date('Y-m-d',$current_date);
                             // check pack
                             $subs_pack = null;
                             if ($member->pack_id) {
                                 $subs_pack = $packs->get_pack($member->pack_id);
                             }
                             if (empty($subs_pack)) {
                                 if ($member_type != 'all') {
                                     $subs_pack = $packs->validate_pack($member->amount, $member->duration, $member->duration_type, $member_type);
                                 } else {
                                     $subs_pack = $packs->validate_pack($member->amount, $member->duration, $member->duration_type, $membership_type);
                                 }
                             }
                             $num_cycles = isset($member->active_num_cycles) && !empty($member->active_num_cycles) ? $member->active_num_cycles : $subs_pack['num_cycles'];
                             $num_cycles = trim($num_cycles);
                             $rebilled = 0;
                             if ($num_cycles > 0) {
                                 $rebilled = $member->rebilled;
                                 if ($num_cycles > $rebilled) {
                                     $num = $num_cycles - $rebilled;
                                     for ($i = 1; $i <= $num; $i++) {
                                         $duration = $i * $subs_pack['duration'];
                                         //days
                                         if ($member->duration_type == 'd') {
                                             $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                         }
                                         //months
                                         if ($member->duration_type == 'm') {
                                             $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                         }
                                         //years
                                         if ($member->duration_type == 'y') {
                                             $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                         }
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($recuring_cycles[$d])) {
                                                 $dt[] = $d;
                                                 $recuring_cycles[$d]['cost'] = $subs_pack['cost'];
                                                 $recuring_cycles[$d]['expire_date'] = $d;
                                             } else {
                                                 $recuring_cycles[$d]['cost'] = $test[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                             }
                             //num_cycles >0
                             if ($num_cycles == 0 && $num_cycles != '' && !empty($subs_pack)) {
                                 $exp_date = $expire_date;
                                 //days
                                 if ($member->duration_type == 'd') {
                                     $i = 1;
                                     while (strtotime($exp_date) <= strtotime($end_date)) {
                                         $duration = $i * $subs_pack['duration'];
                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                         $exp_date = $d;
                                         $i++;
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($on_going[$d])) {
                                                 $dt[] = $d;
                                                 $on_going[$d]['cost'] = $subs_pack['cost'];
                                                 $on_going[$d]['expire_date'] = $d;
                                             } else {
                                                 $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                                 //days end
                                 //months
                                 if ($member->duration_type == 'm') {
                                     $i = 1;
                                     while (strtotime($exp_date) <= strtotime($end_date)) {
                                         $duration = $i * $subs_pack['duration'];
                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                         $exp_date = $d;
                                         $i++;
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($on_going[$d])) {
                                                 $dt[] = $d;
                                                 $on_going[$d]['cost'] = $subs_pack['cost'];
                                                 $on_going[$d]['expire_date'] = $d;
                                             } else {
                                                 $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                                 //moths end
                                 //years
                                 if ($member->duration_type == 'y') {
                                     $i = 1;
                                     while (strtotime($exp_date) <= strtotime($end_date)) {
                                         $duration = $i * $subs_pack['duration'];
                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                         $exp_date = $d;
                                         $i++;
                                         if (strtotime($d) <= strtotime($end_date)) {
                                             if (!isset($on_going[$d])) {
                                                 $dt[] = $d;
                                                 $on_going[$d]['cost'] = $subs_pack['cost'];
                                                 $on_going[$d]['expire_date'] = $d;
                                             } else {
                                                 $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                             }
                                         }
                                     }
                                 }
                                 //years end
                             }
                             //$num_cycles ==0
                         }
                         //EXPIRE DATE GRATER THAN CURRENT DATE
                     }
                     //Membership Type
                 }
                 //EMPTY EXPIRE DATE
                 //checking recurring payments for other membership levels of a user
                 if (isset($member->other_membership_types) && is_object($member->other_membership_types) && !empty($member->other_membership_types)) {
                     foreach ($member->other_membership_types as $key => $other_member) {
                         if (!empty($other_member)) {
                             $other_member = mgm_convert_array_to_memberobj($other_member, $user_id);
                             $membership_type = $other_member->membership_type;
                             $expire_date = $other_member->expire_date;
                             if (!empty($expire_date) && $other_member->status == 'Active' && $other_member->amount > 0) {
                                 if ($membership_type == $member_type || $member_type == 'all') {
                                     if (strtotime($expire_date) >= $current_date) {
                                         //echo "<br/>Expire_date : ".$expire_date;
                                         //echo "<br/>Current_date : ".date('Y-m-d',$current_date);
                                         // check pack
                                         $subs_pack = null;
                                         if ($other_member->pack_id) {
                                             $subs_pack = $packs->get_pack($other_member->pack_id);
                                         }
                                         if (empty($subs_pack)) {
                                             if ($member_type != 'all') {
                                                 $subs_pack = $packs->validate_pack($other_member->amount, $other_member->duration, $other_member->duration_type, $member_type);
                                             } else {
                                                 $subs_pack = $packs->validate_pack($other_member->amount, $other_member->duration, $other_member->duration_type, $membership_type);
                                             }
                                         }
                                         $num_cycles = isset($other_member->active_num_cycles) && !empty($other_member->active_num_cycles) ? $other_member->active_num_cycles : $subs_pack['num_cycles'];
                                         $num_cycles = trim($num_cycles);
                                         $rebilled = 0;
                                         if ($num_cycles > 0) {
                                             $rebilled = $member->rebilled;
                                             if ($num_cycles > $rebilled) {
                                                 $num = $num_cycles - $rebilled;
                                                 for ($i = 1; $i <= $num; $i++) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     //days
                                                     if ($other_member->duration_type == 'd') {
                                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                                     }
                                                     //months
                                                     if ($other_member->duration_type == 'm') {
                                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                                     }
                                                     //years
                                                     if ($other_member->duration_type == 'y') {
                                                         $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                                     }
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($recuring_cycles[$d])) {
                                                             $dt[] = $d;
                                                             $recuring_cycles[$d]['cost'] = $subs_pack['cost'];
                                                             $recuring_cycles[$d]['expire_date'] = $d;
                                                         } else {
                                                             $recuring_cycles[$d]['cost'] = $test[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         //num_cycles >0
                                         if ($num_cycles == 0 && $num_cycles != '' && !empty($subs_pack)) {
                                             $exp_date = $expire_date;
                                             //days
                                             if ($other_member->duration_type == 'd') {
                                                 $i = 1;
                                                 while (strtotime($exp_date) <= strtotime($end_date)) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " days"));
                                                     $exp_date = $d;
                                                     $i++;
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($on_going[$d])) {
                                                             $dt[] = $d;
                                                             $on_going[$d]['cost'] = $subs_pack['cost'];
                                                             $on_going[$d]['expire_date'] = $d;
                                                         } else {
                                                             $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                             //days end
                                             //months
                                             if ($other_member->duration_type == 'm') {
                                                 $i = 1;
                                                 while (strtotime($exp_date) <= strtotime($end_date)) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " months"));
                                                     $exp_date = $d;
                                                     $i++;
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($on_going[$d])) {
                                                             $dt[] = $d;
                                                             $on_going[$d]['cost'] = $subs_pack['cost'];
                                                             $on_going[$d]['expire_date'] = $d;
                                                         } else {
                                                             $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                             //moths end
                                             //years
                                             if ($other_member->duration_type == 'y') {
                                                 $i = 1;
                                                 while (strtotime($exp_date) <= strtotime($end_date)) {
                                                     $duration = $i * $subs_pack['duration'];
                                                     $d = date('Y-m-d', strtotime("{$expire_date} + " . $duration . " years"));
                                                     $exp_date = $d;
                                                     $i++;
                                                     if (strtotime($d) <= strtotime($end_date)) {
                                                         if (!isset($on_going[$d])) {
                                                             $dt[] = $d;
                                                             $on_going[$d]['cost'] = $subs_pack['cost'];
                                                             $on_going[$d]['expire_date'] = $d;
                                                         } else {
                                                             $on_going[$d]['cost'] = $on_going[$d]['cost'] + $subs_pack['cost'];
                                                         }
                                                     }
                                                 }
                                             }
                                             //years end
                                         }
                                         //$num_cycles ==0
                                     }
                                     //EXPIRE DATE GRATER THAN CURRENT DATE
                                 }
                                 //Membership Type
                             }
                         }
                     }
                 }
             }
             //users
         }
     }
     //exit;
     sort($dt);
     $dt = array_unique($dt);
     $dcount = count($dt);
     $data[0]['date_start'] = $date_start;
     $data[0]['date_end'] = $date_end;
     $data[0]['member_type'] = $member_type;
     // Migrating the recuring  and on going data to array
     for ($i = 0; $i < $dcount; $i++) {
         $udt = $dt[$i];
         if (!empty($udt)) {
             $data[$i]['date'] = $udt;
             //issue #1311
             if (isset($recuring_cycles[$udt]['cost']) && array_key_exists($udt, $recuring_cycles)) {
                 $data[$i]['cost'] = $data[$i]['cost'] + $recuring_cycles[$udt]['cost'];
             }
             if (isset($on_going[$udt]['cost']) && array_key_exists($udt, $on_going)) {
                 $data[$i]['cost'] = $data[$i]['cost'] + $on_going[$udt]['cost'];
             }
         }
     }
     return $data;
 }
/**
 * check memberships to extend/expire
 *
 * @params array $users
 * @return int $offset
 */
function mgm_check_memberships_to_extend($a_users)
{
    // found
    if (count($a_users) > 0) {
        // current date: timezone formatted
        $current_date = mgm_get_current_datetime('Y-m-d H:i:s');
        // with time part #1023 issue
        // packs
        $spacks_obj = mgm_get_class('subscription_packs');
        // process_inactive_users
        $process_inactive_users = bool_from_yn(mgm_get_class('system')->get_setting('enable_process_inactive_users'));
        // set
        $data['current_date'] = $current_date['date'];
        $data['current_timestamp'] = $current_date['timestamp'];
        $data['duration_exprs'] = $spacks_obj->get_duration_exprs();
        $checked = $offset = 0;
        // loop
        foreach ($a_users as $user) {
            // set limit
            @set_time_limit(300);
            //300s
            @ini_set('memory_limit', 134217728);
            // 128M
            //mem
            // get member
            $member = mgm_get_member($user->ID);
            // reset primary subscripton
            mgm_check_ongoing_member($user, $member, $spacks_obj, $data, false, $process_inactive_users);
            // check other memberships as well
            if (isset($member->other_membership_types) && is_array($member->other_membership_types) && !empty($member->other_membership_types)) {
                // loop
                foreach ($member->other_membership_types as $key => $val) {
                    // convert
                    $val = mgm_convert_array_to_memberobj($val, $user->ID);
                    // check
                    if (isset($val->membership_type) && !empty($val->membership_type) && !in_array($val->membership_type, array('guest'))) {
                        //skip if default value
                        mgm_check_ongoing_member($user, $val, $spacks_obj, $data, true, $process_inactive_users);
                    }
                }
            }
            // increment
            $offset++;
        }
        // return
        return $offset;
    }
    // return
    return 0;
}
/**
 * find members with selected custom filed critera
 * @param string $field
 * @return mixed array|int  
 */
function mgm_get_members_with_customfiled($field, $value)
{
    global $wpdb;
    $start = 0;
    $limit = 1000;
    //user meta fields
    $fields = array('user_id', 'meta_value');
    // sql
    $sql = "SELECT count(*) FROM `{$wpdb->usermeta}` WHERE `meta_key` = 'mgm_member_options' AND `user_id` <> 1";
    $count = $wpdb->get_var($sql);
    // init
    $members = array();
    //count
    if ($count) {
        //for
        for ($i = $start; $i < $count; $i = $i + $limit) {
            //users
            $users = mgm_patch_partial_user_member_options($i, $limit, $fields);
            // check
            if ($users) {
                // loop
                foreach ($users as $user) {
                    // vlid
                    $valid = false;
                    //name
                    $name = '';
                    $user_id = $user->user_id;
                    $member = unserialize($user->meta_value);
                    // convert
                    $member = mgm_convert_array_to_memberobj($member, $user_id);
                    // check
                    if (!empty($value) && isset($member->status) && $member->status == 'Active') {
                        // from custom field
                        if (isset($member->custom_fields->{$field}) && !empty($member->custom_fields->{$field})) {
                            $name = $member->custom_fields->{$field};
                        }
                        // clean
                        $value = mgm_stripslashes_deep($value);
                        // if there is a match
                        if (!empty($name) && strpos(strtolower($name), strtolower($value)) !== false) {
                            $valid = true;
                        } else {
                            $valid = false;
                        }
                    }
                    // store
                    if ($valid) {
                        $members[] = $user_id;
                    }
                    // unset object
                    unset($member);
                    unset($user);
                }
            }
        }
    }
    // return
    return $members;
}
Exemplo n.º 9
0
            ?>
						
			<ul id="membership_tree_<?php 
            echo $user->ID;
            ?>
">
				<li>
					<strong class="underline"><?php 
            _e('Other Memberships', 'mgm');
            ?>
</strong>
					<ul>
						<?php 
            foreach ($member->other_membership_types as $key => $member_oth) {
                // other
                $member_oth = mgm_convert_array_to_memberobj($member_oth, $user->ID);
                // check
                if (isset($member_oth->membership_type) && !empty($member_oth->membership_type) && !in_array($member_oth->membership_type, array('trial', 'guest'))) {
                    //issue #: 509
                    if (strtolower($member_oth->membership_type) == 'guest') {
                        $pack_desc_oth = __('N/A', 'mgm');
                    } else {
                        // member data
                        $amount_oth = esc_html($member_oth->amount);
                        $currency_oth = esc_html($member_oth->currency);
                        $duration_oth = esc_html($member_oth->duration);
                        $duration_type_oth = $member_oth->duration_type;
                        $membership_type_oth = esc_html($member_oth->membership_type);
                        $pack_id_oth = $member_oth->pack_id;
                        $num_cycles_oth = isset($member_oth->active_num_cycles) ? $member_oth->active_num_cycles : null;
                        // get pack desc
/**
 * user unsubscribe
 */
function mgm_user_unsubscribe_info($user_id = NULL, $args = array())
{
    // current user
    if (!$user_id) {
        $user = wp_get_current_user();
    } else {
        // by user id
        $user = get_userdata($user_id);
    }
    // return when no user
    if (!isset($user->ID) || isset($user->ID) && (int) $user->ID == 0) {
        return sprintf(__('Please <a href="%s">login</a> to see your unsubscribe button.', 'mgm'), mgm_get_custom_url('login'));
    }
    // member
    $member = mgm_get_member($user->ID);
    // init
    $html = '';
    // button
    $html .= mgm_get_unsubscribe_status_button($member, $user);
    //other members count
    $subs_count = 0;
    // check
    if (isset($member->other_membership_types) && !empty($member->other_membership_types)) {
        // loop
        foreach ($member->other_membership_types as $key => $other_member) {
            //check
            if (!empty($other_member)) {
                //check
                if (is_array($other_member)) {
                    $other_member = mgm_convert_array_to_memberobj($other_member, $user->ID);
                }
                // status button
                $html .= mgm_get_other_unsubscribe_status_button($other_member, $user, $subs_count);
                //check
                if ($subs_count == 0) {
                    $subs_count++;
                }
            }
        }
    }
    //return
    return $html;
}
Exemplo n.º 11
0
/**
 * authenticate user
 * 
 * @param object $user 
 * @param boolean $return 
 * @return mixed object or boolean
 */
function mgm_authenticate_user($user, $return = false)
{
    // user name
    $user_name = $user->user_login;
    // user is administrator, no check applied
    if (is_super_admin($user->ID)) {
        return $return ? true : $user;
    }
    // issue #1783- user is administrator, no check applied for multisite also
    if (is_multisite()) {
        //check
        if ($user->allcaps['delete_users']) {
            return $return ? true : $user;
        }
    }
    // issue #1711
    if (isset($_REQUEST['pwd']) && !wp_check_password($_REQUEST['pwd'], $user->user_pass, $user->ID)) {
        return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: The password you entered for the username <strong>%s</strong> is incorrect.'), $user_name));
    }
    // apply action
    do_action('mgm_pre_authenticate_user', $user->ID);
    // get member
    $member = mgm_get_member($user->ID);
    // check pack access
    if ($pack = mgm_get_class('subscription_packs')->get_pack($member->pack_id)) {
        // range
        if ($pack['duration_type'] == 'dr') {
            if (time() < strtotime($pack['duration_range_start_dt']) || time() > strtotime($pack['duration_range_end_dt'])) {
                // error
                return mgm_get_login_error($user, 'date_range', 'upgrade', $return, $pack);
            }
        }
        /// multiple user(IP checking)
        if (mgm_check_multiple_logins_violation($user, $member, $pack)) {
            // error
            return mgm_get_login_error($user, 'multiple_logins', 'upgrade', $return, $pack);
        }
    }
    // no status
    if ($member->status === FALSE) {
        return $return ? true : $user;
    }
    // allowed statuses
    $allowed_statuses = array(MGM_STATUS_ACTIVE, MGM_STATUS_AWAITING_CANCEL);
    // active, awaiting cancelled
    if (in_array($member->status, $allowed_statuses)) {
        // never expire
        if (empty($member->expire_date)) {
            return $return ? true : $user;
        }
        // check expire
        if (!empty($member->expire_date) && time() > strtotime($member->expire_date)) {
            // old status
            $old_status = $member->status;
            // set new status
            $member->status = $new_status = strtolower($member->membership_type) == 'trial' ? MGM_STATUS_TRIAL_EXPIRED : MGM_STATUS_EXPIRED;
            // update
            $member->save();
            // action
            do_action('mgm_user_status_change', $user->ID, $new_status, $old_status, 'authenticate_user', $member->pack_id);
        } else {
            // return
            return $return ? true : $user;
            // account is current. Let the user login.
        }
    } else {
        // multiple membership (issue#: 400) modification
        $others_active = 0;
        // check any other membership exists with active status
        if (isset($member->other_membership_types) && is_array($member->other_membership_types) && !empty($member->other_membership_types)) {
            // loop
            foreach ($member->other_membership_types as $key => $mem_obj) {
                // object
                $mem_obj = mgm_convert_array_to_memberobj($mem_obj, $user->ID);
                // check
                if (is_numeric($mem_obj->pack_id) && in_array($mem_obj->status, $allowed_statuses)) {
                    // check for expiry
                    if (!empty($mem_obj->expire_date) && time() > strtotime($mem_obj->expire_date)) {
                        // old status
                        $old_status = $mem_obj->status;
                        // set new status
                        $mem_obj->status = $new_status = MGM_STATUS_EXPIRED;
                        // update member object
                        mgm_save_another_membership_fields($mem_obj, $user->ID);
                        // action
                        do_action('mgm_user_status_change', $user->ID, $new_status, $old_status, 'authenticate_user', $mem_obj->pack_id);
                    } else {
                        $others_active++;
                    }
                }
            }
            // one of the other memberships is active. Let the user login.
            if ($others_active > 0) {
                // return
                return $return ? true : $user;
            }
        }
        // Force upgrade if status: Expired and free user
        $action = mgm_force_upgrade_if_freepack($member);
    }
    // if reached this, then there is error ---------------------------------------
    // set temp login cookie for editing user data
    if (isset($_POST['pwd']) && wp_check_password($_POST['pwd'], $user->user_pass, $user->ID)) {
        setcookie('wp_tempuser_login', $user->ID, time() + 60 * 60, SITECOOKIEPATH);
        // 1 hr
    }
    // default action
    if (!isset($action)) {
        $action = 'complete_payment';
    }
    // error
    return mgm_get_login_error($user, $member->status, $action, $return);
}
 function membership_type_update()
 {
     global $wpdb;
     extract($_POST);
     // init
     $message = $status = '';
     // new type -------------------------------------------------------------------
     if (isset($new_membership_type) && !empty($new_membership_type)) {
         // new type
         $new_membership_type = trim($new_membership_type);
         // allowed only
         if (strtolower($new_membership_type) != 'none') {
             // set
             $membership_types_obj = mgm_get_class('membership_types');
             // set type, check duplicate
             $success = $membership_types_obj->set_membership_type($new_membership_type);
             // update
             if ($success) {
                 // add redirect url
                 $n_login_redirect_url = isset($new_login_redirect_url) ? $new_login_redirect_url : '';
                 $n_logout_redirect_url = isset($new_logout_redirect_url) ? $new_logout_redirect_url : '';
                 $n_type_code = $membership_types_obj->get_type_code($new_membership_type);
                 // set url
                 $membership_types_obj->set_login_redirect($n_type_code, $n_login_redirect_url);
                 $membership_types_obj->set_logout_redirect($n_type_code, $n_logout_redirect_url);
                 // update
                 $membership_types_obj->save();
                 // message
                 $message = sprintf(__('Successfully created new membership type: %s.', 'mgm'), mgm_stripslashes_deep($new_membership_type));
                 $status = 'success';
             } else {
                 $message = sprintf(__('Error while creating new membership type: %s. Duplicate type.', 'mgm'), mgm_stripslashes_deep($new_membership_type));
                 $status = 'error';
             }
         } else {
             $message = sprintf(__('Error while creating new membership type: %s. Not allowed.', 'mgm'), mgm_stripslashes_deep($new_membership_type));
             $status = 'error';
         }
     }
     // delete/move account ------------------------------------------------------------
     if (isset($remove_membership_type) && count($remove_membership_type) > 0) {
         // get object
         $membership_types_obj = mgm_get_class('membership_types');
         // users
         $users = mgm_get_all_userids(array('ID'), 'get_results');
         // how many removed
         $removed = 0;
         // loop
         foreach ($remove_membership_type as $type_code) {
             // unset
             $membership_types_obj->unset_membership_type($type_code);
             // move
             if (isset($move_membership_type_to[$type_code]) && $move_membership_type_to[$type_code] != 'none') {
                 // loop
                 foreach ($users as $user) {
                     // get
                     $member = mgm_get_member($user->ID);
                     // if users with same membershiptype as that of selected
                     if ($member->membership_type == $type_code) {
                         // set
                         $member->membership_type = $move_membership_type_to[$type_code];
                         // save
                         $member->save();
                     } else {
                         // check if any multiple levels exist:
                         if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
                             // loop
                             foreach ($member->other_membership_types as $key => $memtypes) {
                                 // make sure its an object:
                                 $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
                                 // verify
                                 if ($memtypes->membership_type == $type_code) {
                                     // set
                                     $memtypes->membership_type = $move_membership_type_to[$type_code];
                                     // save
                                     mgm_save_another_membership_fields($memtypes, $user->ID, $key);
                                     break;
                                 }
                             }
                         }
                     }
                     // unset
                     unset($member);
                 }
             }
             // remove packs
             $subscription_packs = mgm_get_class('subscription_packs');
             // empty
             $packs = array();
             // set
             foreach ($subscription_packs->packs as $i => $pack) {
                 // if membership_type is same as being deleted
                 if ($pack['membership_type'] == $type_code) {
                     continue;
                     // skip
                 }
                 // filtered
                 $packs[] = $pack;
             }
             // set
             $subscription_packs->set_packs($packs);
             // update
             $subscription_packs->save();
             // removed
             $removed++;
         }
         // ends remove pack:
         // save
         $membership_types_obj->save();
         // message
         $message .= (!empty($message) ? '<br>' : '') . sprintf(__('Successfully removed %d membership type(s).', 'mgm'), $removed);
         // set status
         $status = 'success';
     }
     // update name/redirects ------------------------------------------------------------------------
     // get object
     $membership_types_obj = mgm_get_class('membership_types');
     // ge all users
     $users = mgm_get_all_userids(array('ID'), 'get_results');
     // init
     $updated = 0;
     // loop types
     foreach ($membership_types_obj->get_membership_types() as $type_code => $type_name) {
         // skip new type, in edit otherwise overwritten
         if (isset($n_type_code) && !empty($n_type_code) && $n_type_code == $type_code) {
             continue;
         }
         // urls
         $_login_redirect_url = isset($login_redirect_url[$type_code]) ? $login_redirect_url[$type_code] : '';
         $_logout_redirect_url = isset($logout_redirect_url[$type_code]) ? $logout_redirect_url[$type_code] : '';
         // set urls
         $membership_types_obj->set_login_redirect($type_code, $_login_redirect_url);
         $membership_types_obj->set_logout_redirect($type_code, $_logout_redirect_url);
         // set name
         if (isset($membership_type_names[$type_code]) && !empty($membership_type_names[$type_code]) && $membership_type_names[$type_code] != $type_name) {
             //issue #1127
             $new_type_code = $membership_types_obj->get_type_code($membership_type_names[$type_code]);
             //check
             if ($new_type_code != $type_code) {
                 // get object
                 $obj_sp = mgm_get_class('subscription_packs');
                 //update new
                 foreach ($obj_sp->packs as $key => $pack) {
                     if ($obj_sp->packs[$key]['membership_type'] == $type_code) {
                         $obj_sp->packs[$key]['membership_type'] = $new_type_code;
                         $obj_sp->save();
                     }
                 }
                 // loop
                 foreach ($users as $user) {
                     // get
                     $member = mgm_get_member($user->ID);
                     // if users with same membershiptype as that of selected
                     if (isset($member->membership_type) && $member->membership_type == $type_code) {
                         // set
                         $member->membership_type = $new_type_code;
                         // save
                         $member->save();
                     }
                     // check if any multiple levels exist:
                     if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
                         // loop
                         foreach ($member->other_membership_types as $key => $memtypes) {
                             // make sure its an object:
                             $memtypes = mgm_convert_array_to_memberobj($memtypes, $user->ID);
                             // verify
                             if ($memtypes->membership_type == $type_code) {
                                 // set
                                 $memtypes->membership_type = $new_type_code;
                                 // save
                                 mgm_save_another_membership_fields($memtypes, $user->ID, $key);
                             }
                         }
                     }
                     // unset
                     unset($member);
                 }
                 //issue #1336
                 $membership_posts = mgm_get_posts_for_level($type_code);
                 if (isset($membership_posts['total']) && $membership_posts['total'] > 0) {
                     foreach ($membership_posts['posts'] as $id => $obj) {
                         $post_id = $obj->ID;
                         // get object
                         $post_obj = mgm_get_post($post_id);
                         // if access set
                         if (is_array($post_obj->access_membership_types)) {
                             $access_membership_types = $post_obj->access_membership_types;
                             foreach ($post_obj->access_membership_types as $key => $access_membership_type) {
                                 if ($access_membership_type == $type_code) {
                                     //update rename
                                     $access_membership_types[$key] = $new_type_code;
                                     // set
                                     $post_obj->access_membership_types = $access_membership_types;
                                 }
                             }
                         }
                         // if access delay set
                         if (is_array($post_obj->access_delay)) {
                             $access_delay = $post_obj->access_delay;
                             if (isset($access_delay[$type_code])) {
                                 $access_delay[$new_type_code] = $access_delay[$type_code];
                                 unset($access_delay[$type_code]);
                                 $post_obj->access_delay = $access_delay;
                             }
                         }
                         // apply filter
                         $post_obj = apply_filters('mgm_post_update', $post_obj, $post_id);
                         // save meta
                         $post_obj->save();
                         // unset
                         unset($post_obj);
                     }
                 }
                 //unset
                 $membership_types_obj->unset_membership_type($type_code);
                 // set
                 $membership_types_obj->set_name($membership_type_names[$type_code], $new_type_code);
             } else {
                 // set
                 $membership_types_obj->set_name($membership_type_names[$type_code], $type_code);
             }
         }
         // update
         $updated++;
     }
     // update
     $membership_types_obj->save();
     // notify
     if (empty($message)) {
         // message
         $message = sprintf(__('Successfully updated %d membership type(s).', 'mgm'), $updated);
         // set status
         $status = 'success';
     }
     // return response
     echo json_encode(array('status' => $status, 'message' => $message));
     exit;
 }
/**
 * Save member object after Multiple Membership Upgrade
 * replace upgrade mgm_member with old mgm_member
 * @param array $data
 * @param int $trans_id
 */
function mgm_multiple_upgrade_save_memberobject($data, $trans_id)
{
    if (empty($data['multiple_upgrade_prev_packid'])) {
        return;
    }
    if (isset($data['user_id'])) {
        $member = mgm_get_member($data['user_id']);
        if (isset($member->other_membership_types) && is_array($member->other_membership_types) && count($member->other_membership_types) > 0) {
            // loop
            $prev_index = null;
            $new_index = null;
            foreach ($member->other_membership_types as $index => $member) {
                $member = mgm_convert_array_to_memberobj($member, $data['user_id']);
                //skip default values:
                //if(strtolower($memtypes->membership_type) == 'guest' || $memtypes->status == MGM_STATUS_NULL )	continue;
                if (strtolower($member->membership_type) == 'guest') {
                    continue;
                }
                // find previous member object index to be replaced
                if ($data['multiple_upgrade_prev_packid'] == $member->pack_id) {
                    $prev_index = $index;
                }
                // find newsly inserted member object index
                // consider only for success state
                if ($member->status == MGM_STATUS_ACTIVE && $data['pack_id'] == $member->pack_id) {
                    $new_index = $index;
                }
            }
            if (is_numeric($prev_index) && is_numeric($new_index)) {
                // swap old with new
                $member->other_membership_types[$prev_index] = $member->other_membership_types[$new_index];
                // remove old member object as it is an upgrade
                unset($member->other_membership_types[$new_index]);
                // save all
                $member->save();
                // update transacton - reset multiple_upgrade_prev_packid
                // to not consider next time
                // get
                $trans = mgm_get_transaction($trans_id);
                // reset
                $trans['data']['multiple_upgrade_prev_packid'] = '';
                // update
                mgm_update_transaction(array('data' => json_encode($trans['data'])), $trans_id);
            }
        }
    }
}