| 1 |
<?php |
| 2 |
/** |
| 3 |
* Core email class |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Timetics\Core\Emails; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class Email |
| 14 |
*/ |
| 15 |
abstract class Email { |
| 16 |
/** |
| 17 |
* Constructor for core email class |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
add_action( 'timetics-email-header', [$this, 'add_email_header'] ); |
| 23 |
add_action( 'timetics-email-footer', [$this, 'add_email_footer'] ); |
| 24 |
add_action( 'timetics-email-body', [$this, 'add_email_body'] ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get email subject |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
abstract public function get_subject(); |
| 33 |
|
| 34 |
/** |
| 35 |
* Get email html template |
| 36 |
* |
| 37 |
* @return string |
| 38 |
*/ |
| 39 |
abstract public function get_template(); |
| 40 |
|
| 41 |
/** |
| 42 |
* Get email recipent |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
abstract public function get_recipient(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Get email header |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function get_headers() { |
| 54 |
$charset = get_bloginfo( 'charset' ); |
| 55 |
$charset = $charset ? $charset : 'UTF-8'; |
| 56 |
|
| 57 |
$headers = 'MIME-Version: 1.0' . "\r\n"; |
| 58 |
$headers .= 'Content-type: text/html; charset=' . $charset . "\r\n"; |
| 59 |
$from = get_bloginfo( 'name' ); |
| 60 |
$from_email = timetics_get_option('booking_created_customer_email_from'); |
| 61 |
|
| 62 |
if ( function_exists( 'mb_encode_mimeheader' ) ) { |
| 63 |
$from = mb_encode_mimeheader( $from, $charset ); |
| 64 |
} |
| 65 |
|
| 66 |
// Append the "From" header |
| 67 |
$headers .= 'From: ' .$from. ' <' . $from_email . '> ' . "\r\n"; |
| 68 |
|
| 69 |
return $headers; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Add email header |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function add_email_header() { |
| 78 |
include TIMETICS_PLUGIN_DIR . '/templates/emails/email-header.php'; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Add email body |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function add_email_body() { |
| 87 |
$template = $this->get_template(); |
| 88 |
|
| 89 |
if ( file_exists( $template ) ) { |
| 90 |
ob_start(); // Start output buffering |
| 91 |
include $template; |
| 92 |
$body = ob_get_clean(); // Retrieve and clean output buffer |
| 93 |
echo wp_kses_post( $body ); // Output the email body |
| 94 |
} |
| 95 |
} |
| 96 |
/** |
| 97 |
* Add email footer |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function add_email_footer() { |
| 102 |
include TIMETICS_PLUGIN_DIR . '/templates/emails/email-footer.php'; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get email content |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public function get_template_content() { |
| 111 |
ob_start(); |
| 112 |
$this->add_email_body(); |
| 113 |
$template = ob_get_clean(); |
| 114 |
|
| 115 |
return $template; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Send email using email template |
| 121 |
* |
| 122 |
* @return bool |
| 123 |
*/ |
| 124 |
public function send() { |
| 125 |
$headers = $this->get_headers(); |
| 126 |
$subject = $this->get_subject(); |
| 127 |
$to = $this->get_recipient(); |
| 128 |
$message = $this->get_template_content(); |
| 129 |
|
| 130 |
/** |
| 131 |
* Added temporary for leagacy sass. It will remove in future. |
| 132 |
*/ |
| 133 |
$data = apply_filters( 'timetics/admin/email/send', [$to, $subject, $message, $headers] ); |
| 134 |
|
| 135 |
return wp_mail( ...$data ); |
| 136 |
} |
| 137 |
|
| 138 |
public function sanitize_content( $content ) { |
| 139 |
// Define the allowed HTML tags and attributes |
| 140 |
$allowed_tags = array( |
| 141 |
'a' => array( |
| 142 |
'href' => true, |
| 143 |
'title' => true, |
| 144 |
), |
| 145 |
'abbr' => array( |
| 146 |
'title' => true, |
| 147 |
), |
| 148 |
'acronym' => array( |
| 149 |
'title' => true, |
| 150 |
), |
| 151 |
'b' => array(), |
| 152 |
'blockquote' => array( |
| 153 |
'cite' => true, |
| 154 |
), |
| 155 |
'cite' => array(), |
| 156 |
'code' => array(), |
| 157 |
'del' => array( |
| 158 |
'datetime' => true, |
| 159 |
), |
| 160 |
'em' => array(), |
| 161 |
'i' => array(), |
| 162 |
'q' => array( |
| 163 |
'cite' => true, |
| 164 |
), |
| 165 |
'img' => array ( |
| 166 |
'src' => true, |
| 167 |
'data-*' => true |
| 168 |
), |
| 169 |
'strike' => array(), |
| 170 |
'strong' => array(), |
| 171 |
); |
| 172 |
|
| 173 |
$sanitized_content = wp_kses( $content, $allowed_tags ); |
| 174 |
|
| 175 |
return $sanitized_content; |
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
|