<?php 
if(isset($_GET["edit"])){
	global $wpdb;
	$table_name = $wpdb->prefix . 'visitors_edits';
	$edit = $wpdb->get_row("SELECT * FROM ".$table_name." WHERE edit_id=".$_GET["edit"]);
	$edit->edit_content=stripcslashes($edit->edit_content);
	$edit->post=get_post($edit->post_id);
	$options=get_option( "visitors_edits_options", [
		"advanced_merge"=>null
	]);
	//Fix nl
	//$edit->post->post_content=str_replace(["\r\n", "\r", "\n"], "<br/>",$edit->post->post_content);
	//$edit->post_content=str_replace(["\r\n", "\r", "\n"], "<br/>",$edit->post_content);
	//$edit->edit_content=str_replace(["\r\n", "\r", "\n"], "<br/>",$edit->edit_content);

	$edit->post->post_content=nl2br($edit->post->post_content);
	$edit->post_content=nl2br($edit->post_content);
	$edit->edit_content=nl2br($edit->edit_content);

	if($options["advanced_merge"]!=null){
		$diff1=visitors_editsDIFF($edit->post_content,$edit->edit_content);
		$conv_diff1=encodeDiff($diff1);
		$diff=visitors_editsDIFF($conv_diff1["html"],$edit->post->post_content);
		$diff=cleanEncodedDiff($diff);
		$diff=decodeDiff($diff,$conv_diff1["codes"]);
	}else{
		$diff=visitors_editsDIFF($edit->post->post_content,$edit->edit_content);
	}

	$diff=cleanDiff($diff);
	echo '<div style="display:none" id="visitors_edits_diff">'.$diff.'</div>';

	showEditor($edit);
}else{
	noData("Select a visitor contribution");
	?>
	<script type="text/javascript">
		setTimeout(function(){
			window.location="<?php echo menu_page_url('visitors_edits_main',false);?>";
		},2000)
	</script>
	<?php
}

function showEditor($edit,$editor_content=""){
	$options=get_option( "visitors_edits_options", [
		"edit_notify_message"=>"Thanks for your contribution to our blog, your contribution was reviewed and approved."
	]);
	?>
	<div class="visitors_edits_review_editor">
		<h1>Review a contribution</h1><a class="delete_edit" href="<?php echo menu_page_url('visitors_edits_main',false);?>&delete=<?php echo $edit->edit_id;?>">Delete this contribution</a>
		<ul class="edit_info">
			<li><a target="_blank" href="<?php echo get_permalink($edit->post->ID);?>">View original post</a></li>
			<li>Author : <?php echo $edit->visitor_name; ?> <span class="mail"><?php echo $edit->visitor_email; ?></span></li>
			<li>Submited on : 
				<?php 
					$creationDate=date_create($edit->edit_time);
         			echo date_format($creationDate,"m/d/Y")." at ".date_format($creationDate,"h:i a");
				?>
			</li>
			<li>Author comment :</li>
			<p class="comment">
				<?php echo stripcslashes($edit->visitor_comment); ?>		
			</p>
		</ul>
		
		<div class="cb"></div>
		<form action="<?php echo menu_page_url('visitors_edits_main',false);?>" method="POST" onsubmit="return editSubmit()">
			<div class="editor">
				<?php 
				wp_editor($editor_content,"diff_editor",[
					"media_buttons"=>false,
					"quicktags"=>false,
					"textarea_name"=>"post_content",
					"tinymce"=>[
						"mode" => "textareas",
						"theme" => "modern"
				    ]
				]); 
				?>
			</div>
			<style>
				.diff_editor ins{
					background: #44B3FD !important;
				}
			</style>
			<div class="controls">
				<input type="hidden" value="<?php echo $edit->visitor_name; ?>" name="visitor_name">
				<input type="hidden" value="<?php echo $edit->visitor_email; ?>" name="visitor_email">
				<input type="hidden" value="<?php echo $edit->edit_id; ?>" name="edit_id">
				<input type="hidden" value="<?php echo $edit->post_id; ?>" name="ID">

				<div class="edit_notify">
					<label><input type="checkbox" name="notify_visitor" id="edit_notify_activate"> Notify the author.</label>
					<textarea name="admin_message" id="edit_notify_message"><?php echo $options["edit_notify_message"]; ?></textarea>
				</div>
				<button class="button-primary" type="submit">Save changes</button>
			</div>
		</form>
		<script src="<?php echo visitors_edits::scriptUrl('admin');?>"></script>
	</div>
	<?php
}

function noData($msg){
	?>
	<h1 class="visitors_edits_no_data" style="display:block">
	    <?php echo $msg; ?>
	</h1>
	<?php
}

function clearTag($tag,$content){
	return preg_replace("#<".$tag.".*?>.*?</".$tag.">#i","", $content);
}

function addAttrTag($tag,$attr,$content){
	return preg_replace("#<".$tag."(.*?)>#i","<".$tag." ".$attr."='1'$1>", $content);
}
function clearTagName($tag,$content){
	return preg_replace("#</*".$tag.".*?>#i", "", $content);
}
function replaceTag($origin,$replace,$content){
	return preg_replace("#(<(/*)(".$origin.")(.*?)>)#i", '[$2'.$replace.'$4]', $content);
}
function encodeDiff($diff){
	//composer require paquettg/php-html-parser
	require_once "parser.php";
	$html=str_get_html($diff);
	$encoded=[
		"html"=>"",
		"codes"=>[
			"INS"=>[],
			"DEL"=>[]
		]
	];
	$id=0;
	foreach($html->find('ins') as $ins){
    	$encoded["codes"]["INS"][$id]=$ins->outertext;
    	$ins->outertext="%INS".$id."%";
    	$id++;
	}
	$id=0;
	foreach($html->find('del') as $del){
    	$encoded["codes"]["DEL"][$id]=$del->outertext;
    	$del->outertext="%DEL".$id."%";
    	$id++;
	}
	$encoded["html"]=$html->outertext;
	return $encoded;
}
function cleanDiff($content){
	require_once "parser.php";
	$html=str_get_html($content);
	foreach($html->find('ul') as $ul){
		foreach($ul->find("br") as $br){
			$br->outertext="";
		}
	}
	foreach($html->find('ol') as $ol){
		foreach($ol->find("br") as $br){
			$br->outertext="";
		}
	} 
	return $html;
}
function cleanEncodedDiff($content){
	$html=str_get_html($content);
	$delete_next=false;
	$diffs = $html->find('ins, del');
	for ($i=0; $i < count($diffs); $i++) { 
		$diff=$diffs[$i];
		if($delete_next){
			$diff->outertext="";
			$delete_next=false;
			coutinue;
		}else{
			$isSpecial=preg_match("#%(INS|DEL)\d*?%#", $diff->innertext);
			if ($isSpecial){
		    	//is important
		    	if(preg_match("#diffmod#",$diff->outertext)){
		    		$delete_next=true;
		    	}
		    	$diff->outertext=$diff->innertext;
		    }else{
		    	if($diff->tag==="ins"){
		    		$diff->outertext=$diff->innertext;
		    	}else{
		    		$diff->outertext="";
		    	}
		    }
		}
	}
	return $html;
}
function decodeDiff($content,$codes){
	foreach ($codes["INS"] as $id => $code) {
		$content=str_replace("%INS".$id."%", $code, $content);
	}
	foreach ($codes["DEL"] as $id => $code) {
		$content=str_replace("%DEL".$id."%", $code, $content);
	}
	return $content;
}