function mgm_patch_get_users($start, $limit) { global $wpdb; $qry = 'SELECT ID from ' . $wpdb->users . ' WHERE ID <> 1 ORDER BY ID LIMIT ' . $start . ',' . $limit; $uids = $wpdb->get_col($qry); if (!count($uids)) { return false; } foreach ($uids as $user_id) { mgm_remove_excess_user_roles($user_id, true); $member = mgm_get_member($user_id); $pack = mgm_get_option('subscription_packs')->get_pack($member->pack_id); if (!empty($pack['membership_type']) && $pack['membership_type'] != $member->membership_type && $member->status == MGM_STATUS_ACTIVE) { $member->membership_type = $pack['membership_type']; // update_user_option($user_id, 'mgm_member', $member, true); $member->save(); } } return true; }
function subscription_packages_update() { // get object $obj_sp = mgm_get_class('subscription_packs'); // roles obj $obj_role = new mgm_roles(); // init $_packs = array(); // init $arr_new_role_users = $arr_users_main_role = array(); // loop foreach ($_POST['packs'] as $pack) { // set modules if (!isset($pack['modules'])) { $pack['modules'] = array(); } // check role changed: $prev_pack = $obj_sp->get_pack($pack['id']); // check if (isset($prev_pack['role']) && isset($pack['role']) && $prev_pack['role'] != $pack['role']) { // find users with the package: if (!isset($uid_all)) { $uid_all = mgm_get_all_userids(); } // cap $arr_users = mgm_get_users_with_package($pack['id'], $uid_all); // check if (!empty($arr_users)) { // loop foreach ($arr_users as $uid) { //add role to old users $user = new WP_User($uid); // check if (in_array($user->roles[0], array($prev_pack['role']))) { $arr_users_main_role[$uid] = $pack['role']; } else { $arr_users_main_role[$uid] = $user->roles[0]; } // add new role: $obj_role->add_user_role($uid, $pack['role'], false, false); $arr_new_role_users[] = $uid; } } } // num_cycles $pack['num_cycles'] = (int) $pack['num_cycles']; // limited flag if (isset($pack['num_cycles']) && $pack['num_cycles'] == 2) { // set limited $pack['num_cycles'] = (int) $pack['num_cycles_limited']; // unset unset($pack['num_cycles_limited']); } // duration_type if ($pack['duration_type'] == 'dr') { $pack['duration_range_start_dt'] = (int) $pack['duration_range_start_dt'] > 0 ? date('Y-m-d', strtotime($pack['duration_range_start_dt'])) : ''; $pack['duration_range_end_dt'] = (int) $pack['duration_range_end_dt'] > 0 ? date('Y-m-d', strtotime($pack['duration_range_end_dt'])) : ''; } else { $pack['duration_range_start_dt'] = $pack['duration_range_end_dt'] = ''; } // unset for safety if (empty($pack['duration_range_start_dt'])) { unset($pack['duration_range_start_dt']); } if (empty($pack['duration_range_end_dt'])) { unset($pack['duration_range_end_dt']); } // switch back if ($pack['duration_type'] == 'dr' && (!isset($pack['duration_range_start_dt']) && !isset($pack['duration_range_end_dt']))) { $pack['duration_type'] = 'l'; // make it lifetime } // val int $pack['duration'] = (int) $pack['duration']; $pack['sort'] = (int) $pack['sort']; $pack['preference'] = (int) $pack['preference']; // lifetime EL if (isset($pack['duration_type']) && $pack['duration_type'] == 'l') { //lifetime: $pack['duration'] = $pack['num_cycles'] = 1; } // trial if (isset($pack['trial_on']) && (bool) $pack['trial_on'] == true) { $pack['trial_num_cycles'] = (int) $pack['trial_num_cycles']; $pack['trial_duration'] = (int) $pack['trial_duration']; } // update active on pages: foreach ($obj_sp->get_active_options() as $option => $val) { // check if (!isset($pack['active'][$option]) || empty($pack['active'][$option])) { $pack['active'][$option] = 0; } } // set $_packs[] = $pack; } // save $obj_sp->set_packs($_packs); // save to database $obj_sp->save(); //remove excess roles from user if updated role if (count($arr_new_role_users) > 0) { // users $arr_new_role_users = array_unique($arr_new_role_users); // loop foreach ($arr_new_role_users as $uid) { // move mgm_remove_excess_user_roles($uid); // highlight role: if (isset($arr_users_main_role[$uid])) { $obj_role->highlight_role($uid, $arr_users_main_role[$uid]); } } } // message $message = sprintf(__('Successfully updated %d subscription packages.', 'mgm'), count($_packs)); $status = 'success'; // return response echo json_encode(array('status' => $status, 'message' => $message)); }
function add_user_role($user_id, $role, $update_order = true, $remove_role = true) { global $wp_roles; // db $wp_roles->use_db = $this->use_db; // user $user = new WP_User($user_id); // check if (!empty($role)) { // not exist if (!in_array($role, $user->roles)) { // assign $user->roles = $this->_assign_true_to_keys($user->roles); // add $user->add_role($role); // set role #789 to execute spf hook called by action "set_user_role", is it double? $user->set_role($role); } // check to remove any unwanted roles if ($remove_role) { mgm_remove_excess_user_roles($user_id); } // change role order if ($update_order) { $this->highlight_role($user_id, $role); } } }