<?php
$post=null;
if(!empty($_POST)){
	global $wpdb;
	$wpdb->show_errors();
	$table_name = $wpdb->prefix . 'visitors_edits';

	$post_id=$_POST["post_id"];
	$visitor_name=$_POST["visitor_name"];
	$visitor_email=$_POST["visitor_email"];
	$visitor_comment=$_POST["visitor_comment"];
	$edit_content=$_POST["edit_content"];

	$post = get_post($post_id);
	
	//Check if changed
	if(md5($post->post_content)!==md5(stripcslashes($edit_content))){
		
		$wpdb->insert($table_name,[
			"edit_time"=>date('Y-m-d H:i:s'),
			"visitor_name"=>$visitor_name,
			"visitor_email"=>$visitor_email,
			"visitor_comment"=>$visitor_comment,
			"edit_content"=>$edit_content,
			"post_id"=>$post_id,
			"post_content"=>$post->post_content
		]);
		require "mail.php";
		$options=get_option( "visitors_edits_options", [
			"admin_email"=>"",
			"notify_admin"=>null,
			"visitor_notif_message"=>"Your suggestion was submitted.",
			"admin_notif_message"=>"A new suggestion was submitted."
		]);

		$mail = [
			"post_title"=>$post->post_title,
			"post_url"=>get_permalink($post_id),
			"visitor_name"=>$visitor_name,
			"visitor_comment"=>$visitor_comment,
			"visitor_email"=>$visitor_email,
			"edit_time"=>date('m/d/Y')." at ".date('h:i a'),
			"blog_title" => get_bloginfo("name"),
			"visitor_notif_message"=>$options["visitor_notif_message"],
			"admin_notif_message"=>$options["admin_notif_message"]
		];

		$visitor_submitionMail=new visitors_edits_EMAIL($mail,"visitor_submition");
		$visitor_submitionMail->send($visitor_email);

		if($options["notify_admin"]!=null){
			$admin_email=$options["admin_email"];
			$admin_submitionMail=new visitors_edits_EMAIL($mail,"admin_submition");
			$admin_submitionMail->send($admin_email);
		}
	}
}else{
	global $wp;
	global $post;
	$post = get_posts([
		"name"=> $wp->query_vars['visitors_edits_post_name'],
		'post_type'   => 'post',
	  	'post_status' => 'publish',
	  	'numberposts' => 1
	]);
	$post=$post[0];
	if($post->post_name!=$wp->query_vars['visitors_edits_post_name']){
		header("Location:".get_site_url());
	}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w1.org/1998/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="<?php echo plugins_url( '/../css/grid.css',__FILE__ );?>">
	<link rel="stylesheet" type="text/css" href="<?php echo plugins_url( '/../css/editor.css',__FILE__ );?>">
	<script src="<?php echo visitors_edits::scriptUrl('jquery');?>"></script>
	<title>Submit an edit</title>
</head>
<body <?php if(!empty($_POST)){echo 'class="grey"';} ?>>
	<?php 
		if(!empty($_POST)){
			confirmSubmit("Edit Submitted!","Thanks for your contribution you will be notified once the edit reviewed.");
		}else{
			showForm();
		}
	?>
</body>
</html>
<?php 
function showForm(){
	global $post;
	?>
	<div class="header">
		<div class="header-title">Submit a contribution</div>
		<p class="header-content">
			<a href="<?php echo  get_permalink($post->ID) ?>">Original post : <?php echo $post->post_title; ?>.</a>
		</p>
	</div>
	<form action="" method="POST" class="editor_form row" id="editor_form" onsubmit="return validateEdit.run()">
		<div class="submit_fields col-12 col-l-4">
			<ul class="submit_fields_error" id="submit_fields_error">
			</ul>
			<label for="name">Name</label>
			<input class="text_field" type="text" name="visitor_name" placeholder="Name" id="name">
			<label for="email">Email</label>
			<input class="text_field" type="email" name="visitor_email" placeholder="Email" id="email">
			<label for="comment">Description</label>
			<textarea class="area_field" type="text" name="visitor_comment" value=" " id="comment">
				
			</textarea>
			<input type="hidden" name="post_id" value="<?php echo $post->ID;?>">
			<input type="hidden" name="post_url" value="<?php echo  get_permalink($post->ID);?>">
			<input type="submit" class="btn" value="Submit for review">
		</div>
		<div class="editor_field col-12 col-l-8">
			<?php
				wp_editor($post->post_content,"edit_content",[
					"media_buttons"=>false,
					"quicktags"=>false,
					"textarea_name"=>"edit_content",
					"tinymce"=>[
						"mode" => "textareas",
						"theme" => "modern"
				    ]
				]);
				_WP_Editors::enqueue_scripts();
				print_footer_scripts();
				_WP_Editors::editor_js();
			?>
		</div>
		<div class="cb"></div>
	</form>
	<script src="<?php echo visitors_edits::scriptUrl('editor');?>"></script>
	<?php
}
function confirmSubmit($title,$message){
	?>
	<div class="submit_success col-10 col-l-6 col-center">
		<strong class="alert_title">
			<?php echo $title;?>
		</strong>
		<p class="alert_content">
			<?php echo $message;?>
		</p>
		<p class="alert_footer">
			<a href="<?php echo $_POST['post_url']?>">Click here to continue back to the post</a>
		</p>
		</div>
	</div>
	<?php
}
?>