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)); }