<?php
class visitors_edits_EMAIL{
	var $subject;
	var $body;
	public function __construct($cpts,$template){
		$template=$this->loadTemplate($template);
		$this->subject=$this->inject($template["subject"],$cpts);
		$this->body=$this->inject($template["body"],$cpts);
	}
	public function send($destination){

		$options=get_option( "visitors_edits_options", [
			"admin_email"=>"",
			"notify_admin"=>null
		]);

		$headers= "MIME-Version: 1.0\r\n";
		$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

		wp_mail($destination, $this->subject, $this->body,$headers);
		/*
		subject : $this->subject
		body : $this->body
		*/
	}
	private function loadTemplate($template){
		ob_start();
		require("mail_templates/".$template.".html");
		$templateHtml=ob_get_clean();
		preg_match("|<subject>(.*)</subject>|",$templateHtml,$subject);
		$templateHtml=preg_replace("|<subject>.*</subject>|","",$templateHtml);
		return [
			"subject"=>$subject[1],
			"body"=>$templateHtml
		];	
	}
	private function inject($str,$body){
		foreach ($body as $key => $value) {
			$str=str_replace("#$key#",$value,$str);
		}
		return $str;
	}
	public function preview(){
		echo $this->subject;
		echo $this->body;
	}
}
?>