<?php


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



add_action( 'init', 'create_post_typess' );	

function create_post_typess() {
	global $czpf_plugin;
	$current_user = wp_get_current_user();

	if ( ! is_user_logged_in() ) {
		send_error_die( __( 'You are not logged in.', 'rhc' ) );
	}
	
	if(isset($_REQUEST['tmp_postid']) and is_numeric($_REQUEST['tmp_postid'])){
		
		$content_post = get_post($_REQUEST['tmp_postid']);
		$typevalue = $_REQUEST['tmp_save'];
		
		
		
		if($typevalue=='post_title'){
			$post_exist_title = $czpf_plugin->wp_exist_post_by_title($_REQUEST['tmp_value']);
			if($post_exist_title and $post_exist_title != $_REQUEST['tmp_postid']){
				send_error_die(__('Title is already used, try another.','rhc'));
			}
		}
	
		if(!empty($content_post) && $content_post->post_status == 'publish' and $content_post->post_type == 'groupsteams' and $content_post->post_author == $current_user->ID and ($typevalue=='post_title' || $typevalue == 'post_content' )){
			  $my_post = array(
			      'ID' => $_REQUEST['tmp_postid'],
			      $typevalue => $_REQUEST['tmp_value'],
			      'editdate' => date('Y-m-d H:i:s')
			  );
			  wp_update_post( $my_post );
		}
		
		
	}

	die(json_encode(array('R'=>'OK','MSG'=>__('Save done','rhc'))));
};




?>