| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
class Yeemail_Builder_Frontend { |
| 4 |
function __construct(){ |
| 5 |
add_filter( 'template_include',array($this,'template_include'),99); |
| 6 |
add_filter('wp_mail',array($this,"yeemail_template")); |
| 7 |
add_filter( 'wp_mail_content_type',array($this,'set_content_type') ); |
| 8 |
add_filter('upload_mimes', array($this,'mime_types')); |
| 9 |
add_filter("yeemail_email_id",array($this,"set_id_send_email"),10); |
| 10 |
} |
| 11 |
function set_id_send_email($emails){ |
| 12 |
preg_match_all('/data-yeemail-id="([^"]*)"/',$emails["message"],$matches); |
| 13 |
if( isset($matches[1][0]) && is_numeric($matches[1][0]) ){ |
| 14 |
$emails["apply"] = false; |
| 15 |
} |
| 16 |
if(strpos($emails["message"], "yeemail_disable") !== false) { |
| 17 |
$emails["apply"] = false; |
| 18 |
} |
| 19 |
return $emails; |
| 20 |
} |
| 21 |
function get_nl2br($string){ |
| 22 |
if(!preg_match("/<[^<]+>/",$string)){ |
| 23 |
return nl2br($string); |
| 24 |
} |
| 25 |
return $string; |
| 26 |
} |
| 27 |
function template_include($template) { |
| 28 |
if( isset($_GET['email_preview']) ){ |
| 29 |
if( $_GET['email_preview'] == "preview") { |
| 30 |
$template = YEEMAIL_PLUGIN_PATH."preview.php"; |
| 31 |
}else{ |
| 32 |
$template = YEEMAIL_PLUGIN_PATH."preview-inner.php"; |
| 33 |
} |
| 34 |
if ( file_exists( $template ) ) { |
| 35 |
return $template; |
| 36 |
} |
| 37 |
}else{ |
| 38 |
return $template; |
| 39 |
} |
| 40 |
} |
| 41 |
public static function get_email_id_template_by_type($id_email ="default",$status = "enable", $show_all = false){ |
| 42 |
if($show_all){ |
| 43 |
$templates_post = get_posts(array("numberposts"=>1,"post_type"=>"yeemail_template","meta_query"=>array(array('key'=>"_mail_template",'value' => $id_email)))); |
| 44 |
}else{ |
| 45 |
$templates_post = get_posts(array("numberposts"=>1,"post_type"=>"yeemail_template","meta_query"=>array(array('key'=>"_status",'value' => $status),array('key'=>"_mail_template",'value' => $id_email)))); |
| 46 |
} |
| 47 |
foreach ( $templates_post as $post ) { |
| 48 |
return $post->ID; |
| 49 |
break; |
| 50 |
} |
| 51 |
return false; |
| 52 |
} |
| 53 |
function yeemail_template($args) { |
| 54 |
$message = $args["message"]; |
| 55 |
$mails = apply_filters( "yeemail_email_id", array("apply"=>true,"message"=>$message)); |
| 56 |
if( $mails["apply"] ) { |
| 57 |
//check mail template |
| 58 |
$template_id = self::get_email_id_template_by_type("default"); |
| 59 |
if( is_numeric($template_id) && $template_id > 0 ) { |
| 60 |
//remove header and |
| 61 |
preg_match("/<body[^>]*>(.*?)<\/body>/is", $message, $matches); |
| 62 |
if(isset($matches[1])){ |
| 63 |
$message = $matches[1]; |
| 64 |
} |
| 65 |
$content = Yeemail_Builder_Frontend_Functions::creator_template(array("id_template"=>$template_id,"type"=>"full")); |
| 66 |
//check no use html |
| 67 |
$message = $this->get_nl2br($message); |
| 68 |
$args["message"] = str_replace('<div class="builder-content">Content Email</div>','<div class="builder-content-email">'.$message.'</div>',$content); |
| 69 |
} |
| 70 |
}else{ |
| 71 |
//Another template has already used this email template. |
| 72 |
} |
| 73 |
return $args; |
| 74 |
} |
| 75 |
function set_content_type(){ |
| 76 |
return "text/html"; |
| 77 |
} |
| 78 |
function mime_types($mimes) { |
| 79 |
$mimes['json'] = 'text/plain'; |
| 80 |
return $mimes; |
| 81 |
} |
| 82 |
} |
| 83 |
new Yeemail_Builder_Frontend; |