<?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.');
}


add_action( 'init', 'create_post_typess' );	


function profile_get_image_scalse($type = ''){
	global $_wp_additional_image_sizes;
	
	$image_sizes = $_wp_additional_image_sizes;
	
	
	//if(empty($image_sizes['post-thumbnail'])){
		$image_sizes['post-thumbnail'] = array('width'=>get_option( 'thumbnail_size_w' ), 'height'=>get_option( 'thumbnail_size_h' ), 'crop' => true);
	//}
	if(empty($image_sizes['medium-feature'])){
		$image_sizes['medium-feature'] = array('width'=>get_option( 'medium_size_w' ), 'height'=>get_option( 'medium_size_h' ),  'crop' => false);
	}	
	if(empty($image_sizes['large-feature'])){
		$image_sizes['large-feature'] = array('width'=>get_option( 'large_size_w' ), 'height'=>get_option( 'large_size_h' ),  'crop' => false);
	}

	if(!empty($type)){
		return $image_sizes[$type];
	} else {
		return $image_sizes;	
	}

}



function create_post_typess(){
    global $czpf_plugin,$wpdb,$wp_version;
    
	if($czpf_plugin->wp_exist_post_by_title($_REQUEST['tmp_title'])){
		send_error_die(__('Title is already used, try another.','rhc'));
	}
	
    	
	$my_post = array(
	  'post_title'    => $_REQUEST['tmp_title'],
	  'post_content'  => $_REQUEST['tmp_content'],
	  'post_status'   => 'publish',
	  'post_type'     => 'groupsteams'
	);
	
	// Insert the post into the database
	$post_ID = wp_insert_post( $my_post );

	update_post_meta($post_ID, 'allow_all_users' , $_REQUEST['tmp_check']);

	$upload_dir = wp_upload_dir();
	$uploadDirectory = $upload_dir['path'].'/';
	$uploadUrl = $upload_dir['url'].'/';
	$create_scales = profile_get_image_scalse();
	$return_img = '';
	$post_attacement_meta = '';
	$filename = date('YmdHis');
        
    $ext = substr(basename($_REQUEST['tmp_img']), -3);    

	foreach($create_scales as $key_tmp => $create_scale){
		$tmp_standart = '';
		
		if((float)$wp_version >= 3.5){
			$tmp_standart = wp_get_image_editor($_REQUEST['tmp_img']);
			if ( ! is_wp_error( $tmp_standart ) ) {
			    $tmp_standart->resize( $create_scale['width'], $create_scale['height'], $create_scale['crop'] );
			    $tmp_standart->save( $uploadDirectory . $filename .'-'.$create_scale['width'].'x'.$create_scale['height']. '.' . $ext );


				$tmp_name = $filename .'-'.$create_scale['width'].'x'.$create_scale['height']. '.' . $ext ;
    			$tmp_size[0] = $create_scale['width'];
    			$tmp_size[1] = $create_scale['height'];
    			$tmp_size[2] = $create_scale['crop'];
    			
    			
    			if($key_tmp == 'post-thumbnail'){
        			$post_attacement_meta['sizes']['thumbnail'] = array('file'=>$tmp_name,
        															 'width'=>$tmp_size[0],
        															 'height'=>$tmp_size[1]);
    			} else if($key_tmp == 'medium-feature'){
        			$post_attacement_meta['sizes']['medium'] = array('file'=>$tmp_name,
        															 'width'=>$tmp_size[0],
        															 'height'=>$tmp_size[1]);
    			} else if($key_tmp == 'large-feature'){
        			$post_attacement_meta['sizes']['large'] = array('file'=>$tmp_name,
        															 'width'=>$tmp_size[0],
        															 'height'=>$tmp_size[1]);
    			}
    		
    			$post_attacement_meta['sizes'][$key_tmp] = array('file'=>$tmp_name,
    															 'width'=>$tmp_size[0],
    															 'height'=>$tmp_size[1]);
    			$return_img[] = array('name'=>$key_tmp,
    								  'url'=>$uploadUrl.$tmp_name,
    								  'filename'=> $filename,
    								  'width'=>$tmp_size[0],
    								  'height'=>$tmp_size[1]);




			}
		} else {
    		$tmp_standart = image_resize( $_REQUEST['tmp_img'], $create_scale['width'],
    									 $create_scale['height'], 
    									 $create_scale['crop']);
    		
    		if(!is_object($tmp_standart)){
    			$tmp_name = basename($tmp_standart);
    			$tmp_size = explode("-", $tmp_name);
    			$tmp_size = explode(".", $tmp_size[(count($tmp_size)-1)]);
    			$tmp_size = explode("x", $tmp_size[(count($tmp_size)-2)]);
    		
    			$post_attacement_meta['sizes'][$key_tmp] = array('file'=>$tmp_name,
    															 'width'=>$tmp_size[0],
    															 'height'=>$tmp_size[1]);
										 
									 
    															 
    															 
    															 
    		
    			$return_img[] = array('name'=>$key_tmp,
    								  'url'=>$uploadUrl.$tmp_name,
    								  'filename'=>$filename,
    								  'width'=>$tmp_size[0],
    								  'height'=>$tmp_size[1]);
    		}	
		}
	}
	
	$tmp_size = '';
	$tmp_size = getimagesize($_REQUEST['tmp_img']);
	

	$return_img[] = array('name'=>'Full Size',
						  'url'=>$uploadUrl.$filename . '.' . $ext,
						  'filename'=>$filename,
						  'width'=>$tmp_size[0],
						  'height'=>$tmp_size[1]);
						  
				
	$post_attacement_meta['width'] = $tmp_size[0];			
	$post_attacement_meta['height'] = $tmp_size[1];	        				
	$post_attacement_meta['file'] = $upload_dir['subdir'].'/'.$filename . '.' . $ext;	 
	        		
	$post_attacement_meta['image_meta']['aperture'] = 0;
	$post_attacement_meta['image_meta']['credit'] = '';
	$post_attacement_meta['image_meta']['camera'] = '';
	$post_attacement_meta['image_meta']['caption'] = '';
	$post_attacement_meta['image_meta']['created_timestamp'] = 0;
	$post_attacement_meta['image_meta']['copyright'] = '';
	$post_attacement_meta['image_meta']['focal_length'] = 0;
	$post_attacement_meta['image_meta']['iso'] = 0;
	$post_attacement_meta['image_meta']['shutter_speed'] = 0;
	$post_attacement_meta['image_meta']['title'] = '';					  
					  
				
		$my_post = array(
			'post_title' => wp_strip_all_tags($filename),
			'post_status' => 'inherit',
			'post_author' => 1,
			'post_mime_type' => $tmp_size['mime'],
			'post_type' =>'attachment',
			'post_parent' => 0,
			'guid' => $uploadUrl.$filename . '.' . $ext,
			'post_name' => wp_strip_all_tags($filename)
		);
		
		
		$tmp_post_id = wp_insert_post( $my_post );					
	if(!empty($tmp_post_id)){
		update_post_meta($tmp_post_id, '_wp_attached_file', $upload_dir['subdir'].'/'.$filename . '.' . $ext);
		update_post_meta($tmp_post_id, '_wp_attachment_metadata', $post_attacement_meta);
		
	}		


	set_post_thumbnail( $post_ID, $tmp_post_id );

	die(json_encode(array('R'=>'OK')));
}
?>