<?php
global $czpf_plugin;

function send_error_die($msg){
	die(json_encode(array('R'=>'ERR','MSG'=>$msg)));
}

if(!is_user_logged_in()){
	send_error_die('You are not logged in.');
}

if(!isset($_REQUEST['czpf_container']) ){
	send_error_die('Error in data');
}

if(isset($_REQUEST['czpf_container']) and  isset($_REQUEST['czpf_resettodefault']) and $_REQUEST['czpf_resettodefault'] == true) {
	update_option('czpf_container', $_REQUEST['czpf_container'] );
	die(json_encode(array('R'=>'OK','MSG'=>__('Reset done, reloading page now','rhc'))));
}



$tmp_czpf_advancesettings = $_REQUEST['czpf_advancesettings'];
$tmp_czpf_advancesettings_retrun = array();
if(!empty($tmp_czpf_advancesettings)){
	$tmp_czpf_advancesettings = json_decode(urldecode($tmp_czpf_advancesettings),true);

	foreach($tmp_czpf_advancesettings as $tmp_data){
		$tmp_czpf_advancesettings_retrun[$tmp_data['name']] = $tmp_data['value'];
	}
}

$czpf_sortable_button_list = array();

if(isset($_REQUEST['czpf_sortable_button_list'])){
	$czpf_sortable_button_list = explode('|', $_REQUEST['czpf_sortable_button_list']);
}

update_option('czpf_sort_order',$czpf_sortable_button_list);
update_option('czpf_advancede', $tmp_czpf_advancesettings_retrun);
update_option('czpf_container', $_REQUEST['czpf_container'] );

$czpf_plugin->czpf_advancede = get_option('czpf_advancede', '');


add_action( 'init', 'create_post_typess' );	

function create_post_typess(){
	global $czpf_plugin,$wp_rewrite;
	$return_url = $czpf_plugin->get_profil_url();
	
	$memeber_tmp = '';
	
	if(!empty($czpf_plugin->czpf_advancede['members_page'])){
		$memeber_tmp = $czpf_plugin->czpf_advancede['members_page'];
		$memeber_tmp = get_permalink( $memeber_tmp );	
	}
	
	if(!empty($czpf_plugin->czpf_advancede['groupsteams_page'])){
		$groups_tmp = $czpf_plugin->czpf_advancede['groupsteams_page'];
		$groups_tmp = get_permalink( $groups_tmp );	
	}
	
	
	
	$wp_rewrite->flush_rules();	
	die(json_encode(array('R'=>'OK','GROUPS_URL'=>'<a target="_blank" href="'.$groups_tmp.'">'.$groups_tmp.'</a>','MEMBERS_URL'=>'<a target="_blank" href="'.$memeber_tmp.'">'.$memeber_tmp.'</a>','RETURN_URL'=>'<a target="_blank" href="'.$return_url.'">'.$return_url.'</a>','MSG'=>__('Save done','rhc'))));
}
?>