<?php 
/**
 * The create new post function.
 * 
 * @Dependancy postform.php
 * @package WordPress
 * @subpackage evol
 * @since eVol 1.0.0
 */
?>
<?php
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'new-post-handler.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');

if( $_SERVER['REQUEST_METHOD'] == 'POST' && !empty( $_POST['add_new_post'] ) && current_user_can('publish_posts')) {

				if (!wp_verify_nonce($_POST['new-post'],'add-new-post')) {
					print __('Could not verified','evol');
					exit;
				}
				if (!empty($_POST['post_format'])) {
					$post_format = $_POST['post_format'];	
				} else {
					$post_format = '';
				}					
				if (!empty($_POST['post_content'])) {
					$post_content = htmlentities(trim(stripcslashes($_POST['post_content'])));
				}
				if (!empty($_POST['post_title'])) {			
					$post_title =  htmlentities(trim(stripcslashes($_POST['post_title'])));
				} elseif (($post_format == 'link') || ($post_format =='video') || ($post_format =='image')) {
					$linkurl = evol_extract_url($post_content);
						$post_title = htmlentities(trim(get_website_title($linkurl)));						
					} else {
					$post_title = htmlentities(trim(stripcslashes(evol_auto_title($_POST['post_content'],15))));
				}	
				if (!empty ($_POST['post_category'])) {
					$post_category = array($_POST['post_category']);
				} else {
					$post_category = 1;
				}
				if (!empty($_POST['post_tags'])) {
					$post_tags = $_POST['post_tags'];
				} else {
					$post_tags = '';
				}
				if (!empty($_POST['post_type'])) {
					$post_type = $_POST['post_type'];
				}
		
				$post = array(
					'post_title'	=> $post_title,
					'post_content'	=> $post_content,
					'post_category'	=> $post_category,
					'tags_input'	=> $post_tags,
					'post_status'	=> 'publish',
					'post_type'	=> $post_type
				);
			
			$post_id = wp_insert_post($post);
			if (!empty($post_format)) {
					$post_format = $_POST['post_format'];
					set_post_format($post_id, $post_format);
				}
			if (!empty($_POST['evol_manual_content'])) {
				update_post_meta($post_id, 'evol_manual_content', 'yes');
			}	
			if (!empty($_FILES['file_0']['name'])) {
			  foreach ($_FILES as $file_id => $array) {
			  	if($file_id=="file_0") {
			  		$featuredImage = true;
			  	  	$attachment_id = insert_attachment($file_id,$post_id,$featuredImage);
			  	} else {
			  		$featuredImage = false;
			  	  	$attachment_id = insert_attachment($file_id,$post_id,$featuredImage);
			  	}
			  }
			
			} 
			unset($_POST);
			$location = $_SERVER["REQUEST_URI"];
			echo "<meta http-equiv='refresh' content='0;url=$location' />";
} 

?> 