<?php /** * Objects merge/update */ //Restoring custom fileds to user meta $arr_users = mgm_get_all_userids(); if (!empty($arr_users)) { foreach ($arr_users as $user_id) { //member $member = mgm_get_member($user_id); // save if (isset($member->custom_fields)) { foreach ($member->custom_fields as $key => $value) { // key $options_key = '_mgm_cf_' . $key; // verify if ($user_id) { // update update_user_option($user_id, $options_key, $value, true); } } } } } // end file
<?php /** * Patch for Membership Level's rename */ // get object $membership_types_obj = mgm_get_class('membership_types'); // ge all users $users = mgm_get_all_userids(array('ID'), 'get_results'); // loop types foreach ($membership_types_obj->get_membership_types() as $type_code => $type_name) { $new_type_code = $membership_types_obj->get_type_code($type_name); //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;
/** * This will update user expirey date/status if any issues * @param $user_id */ function updateUserDetails($user_id = null) { if (is_numeric($user_id)) { $users[] = $user_id; } else { $users = mgm_get_all_userids(); } //mgm_log(mgm_array_dump($users, true)); $arr_skip = array('guest', 'free', 'trial'); foreach ($users as $user_id) { $member = mgm_get_member($user_id); if (!isset($member->membership_type) || isset($member->membership_type) && in_array(strtolower($member->membership_type), $arr_skip)) { continue; } if (strtolower($member->status) == 'cancelled') { //mgm_log($user_id, 'not_updated_cacncelled', true); } if (isset($member->payment_info->subscr_id) && !empty($member->payment_info->subscr_id)) { $update = $this->getSaleDetails($member->payment_info->subscr_id); $update['user_id'] = $user_id; if ($update['status'] == 'active') { if (strtotime($update['expire_date']) >= strtotime(date('Y-m-d'))) { $update['user_id'] = $user_id; //mgm_log(mgm_array_dump($update, true), 'updated-users', true); $member->expire_date = $update['expire_date']; $member->status = MGM_STATUS_ACTIVE; $member->status_str = __('Last payment was successful', 'mgm'); $member->last_pay_date = $update['last_pay']; $member->save(); } else { //mgm_log(mgm_array_dump($update, true), 'not_updated_past_date', true); } } else { //mgm_log(mgm_array_dump($update, true), 'not_updated_no_details', true); } } else { //mgm_log($user_id, 'not_updated_no_saleid', true); } } }
function mgm_get_membershiptype_access_post($post_id = "") { $users = array(); $post_obj = mgm_get_post($post_id); $access_types = $post_obj->get_access_membership_types(); $users = wp_cache_get('all_user_ids', 'users'); //if empty read from db: if (empty($users)) { $users = mgm_get_all_userids(array('ID'), 'get_results'); //update cache with user ids: wp_cache_set('all_user_ids', $users, 'users'); } $members = array(); // check if ($users) { // loop foreach ($users as $user) { // member object $member = mgm_get_member($user->ID); if (in_array($member->membership_type, $access_types)) { if ($member->status == MGM_STATUS_ACTIVE) { $members[] = $member; } } } } return $members; }
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; }
private function _get_user_ids() { global $wpdb; //from cache $uids = wp_cache_get('all_user_ids', 'users'); if (!$uids) { //$uids = $wpdb->get_col('SELECT ID from ' . $wpdb->users); $uids = mgm_get_all_userids(); wp_cache_set('all_user_ids', $uids, 'users'); } return $uids; }
/** * find users with the given package */ function mgm_get_users_with_package($pack_id, $uids = array()) { global $wpdb; if (empty($uids)) { //from cache $uids = wp_cache_get('all_user_ids', 'users'); if (!$uids) { //$uids = $wpdb->get_col('SELECT ID from ' . $wpdb->users. ' WHERE ID <> 1'); $uids = mgm_get_all_userids(); wp_cache_set('all_user_ids', $uids, 'users'); } } $arr_pack_users = array(); foreach ($uids as $uid) { $user = mgm_get_member($uid); if (isset($user->pack_id) && $user->pack_id == $pack_id) { $arr_pack_users[] = $uid; } } return $arr_pack_users; }