| 1 |
<?php |
| 2 |
/** |
| 3 |
* Emails |
| 4 |
* |
| 5 |
* This class handles all emails sent through Give |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @subpackage Classes/Emails |
| 9 |
* @copyright Copyright (c) 2016, GiveWP |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
* @since 1.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Give_Emails Class. |
| 21 |
* |
| 22 |
* @property $from_address |
| 23 |
* @property $from_name |
| 24 |
* @property $content_type |
| 25 |
* @property $headers |
| 26 |
* @property $html |
| 27 |
* @property $template |
| 28 |
* @property $heading |
| 29 |
* |
| 30 |
* @since 1.0 |
| 31 |
*/ |
| 32 |
// __set() accepts any key, so keys without a declared property become dynamic ones. PHP 8.2 deprecates |
| 33 |
// those, and the notice is printed mid-request, which breaks any redirect that follows. |
| 34 |
// PHP 7.4 reads this line as a comment, so it is safe on the minimum supported version. |
| 35 |
#[AllowDynamicProperties] |
| 36 |
class Give_Emails { |
| 37 |
|
| 38 |
/** |
| 39 |
* Holds the from address. |
| 40 |
* |
| 41 |
* @since 1.0 |
| 42 |
*/ |
| 43 |
private $from_address; |
| 44 |
|
| 45 |
/** |
| 46 |
* Holds the from name. |
| 47 |
* |
| 48 |
* @since 1.0 |
| 49 |
*/ |
| 50 |
private $from_name; |
| 51 |
|
| 52 |
/** |
| 53 |
* Holds the email content type. |
| 54 |
* |
| 55 |
* @since 1.0 |
| 56 |
*/ |
| 57 |
private $content_type; |
| 58 |
|
| 59 |
/** |
| 60 |
* Holds the email headers. |
| 61 |
* |
| 62 |
* @since 1.0 |
| 63 |
*/ |
| 64 |
private $headers; |
| 65 |
|
| 66 |
/** |
| 67 |
* Whether to send email in HTML. |
| 68 |
* |
| 69 |
* @since 1.0 |
| 70 |
*/ |
| 71 |
private $html = true; |
| 72 |
|
| 73 |
/** |
| 74 |
* The email template to use. |
| 75 |
* |
| 76 |
* @since 1.0 |
| 77 |
*/ |
| 78 |
private $template; |
| 79 |
|
| 80 |
/** |
| 81 |
* The header text for the email. |
| 82 |
* |
| 83 |
* @since 1.0 |
| 84 |
*/ |
| 85 |
private $heading = ''; |
| 86 |
|
| 87 |
/** |
| 88 |
* Email template tags argument. |
| 89 |
* This helps to decode email template tags, |
| 90 |
* |
| 91 |
* @since 1.0 |
| 92 |
*/ |
| 93 |
public $tag_args = array(); |
| 94 |
|
| 95 |
/** |
| 96 |
* Form ID |
| 97 |
* |
| 98 |
* @since 1.0 |
| 99 |
*/ |
| 100 |
public $form_id = 0; |
| 101 |
|
| 102 |
/** |
| 103 |
* Get things going. |
| 104 |
* |
| 105 |
* @since 1.0 |
| 106 |
*/ |
| 107 |
public function __construct() { |
| 108 |
|
| 109 |
if ( 'none' === $this->get_template() ) { |
| 110 |
$this->html = false; |
| 111 |
} |
| 112 |
|
| 113 |
add_action( 'give_email_send_before', array( $this, 'send_before' ) ); |
| 114 |
add_action( 'give_email_send_after', array( $this, 'send_after' ) ); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Set a property. |
| 120 |
* |
| 121 |
* @since 1.0 |
| 122 |
* |
| 123 |
* @param $key |
| 124 |
* @param $value |
| 125 |
*/ |
| 126 |
public function __set( $key, $value ) { |
| 127 |
$this->$key = $value; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the email from name. |
| 132 |
* |
| 133 |
* @since 1.0 |
| 134 |
*/ |
| 135 |
public function get_from_name() { |
| 136 |
if ( ! $this->from_name ) { |
| 137 |
$this->from_name = give_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) ); |
| 138 |
} |
| 139 |
|
| 140 |
return apply_filters( 'give_email_from_name', wp_specialchars_decode( $this->from_name ), $this ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get the email from address. |
| 145 |
* |
| 146 |
* @since 1.0 |
| 147 |
*/ |
| 148 |
public function get_from_address() { |
| 149 |
if ( ! $this->from_address ) { |
| 150 |
$this->from_address = give_get_option( 'from_email', get_option( 'admin_email' ) ); |
| 151 |
} |
| 152 |
|
| 153 |
return apply_filters( 'give_email_from_address', $this->from_address, $this ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get the email content type. |
| 158 |
* |
| 159 |
* @since 1.0 |
| 160 |
*/ |
| 161 |
public function get_content_type() { |
| 162 |
if ( ! $this->content_type ) { |
| 163 |
$this->content_type = $this->html |
| 164 |
? apply_filters( 'give_email_default_content_type', 'text/html', $this ) |
| 165 |
: 'text/plain'; |
| 166 |
} |
| 167 |
|
| 168 |
return apply_filters( 'give_email_content_type', $this->content_type, $this ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the email headers. |
| 173 |
* |
| 174 |
* @since 1.0 |
| 175 |
*/ |
| 176 |
public function get_headers() { |
| 177 |
if ( ! $this->headers ) { |
| 178 |
$this->headers = "From: {$this->get_from_name()} <{$this->get_from_address()}>\r\n"; |
| 179 |
$this->headers .= "Reply-To: {$this->get_from_address()}\r\n"; |
| 180 |
$this->headers .= "Content-Type: {$this->get_content_type()}; charset=utf-8\r\n"; |
| 181 |
} |
| 182 |
|
| 183 |
return apply_filters( 'give_email_headers', $this->headers, $this ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Retrieve email templates. |
| 188 |
* |
| 189 |
* @since 1.0 |
| 190 |
*/ |
| 191 |
public function get_templates() { |
| 192 |
$templates = array( |
| 193 |
'default' => esc_html__( 'Default Template', 'give' ), |
| 194 |
'none' => esc_html__( 'No template, plain text only', 'give' ), |
| 195 |
); |
| 196 |
|
| 197 |
return apply_filters( 'give_email_templates', $templates ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Get the enabled email template. |
| 202 |
* |
| 203 |
* @since 1.0 |
| 204 |
*/ |
| 205 |
public function get_template() { |
| 206 |
if ( ! $this->template ) { |
| 207 |
$this->template = give_get_option( 'email_template', 'default' ); |
| 208 |
} |
| 209 |
|
| 210 |
return apply_filters( 'give_email_template', $this->template ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get the header text for the email. |
| 215 |
* |
| 216 |
* @since 1.0 |
| 217 |
*/ |
| 218 |
public function get_heading() { |
| 219 |
return apply_filters( 'give_email_heading', $this->heading ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Parse email template tags. |
| 224 |
* |
| 225 |
* @param $content |
| 226 |
* |
| 227 |
* @return mixed |
| 228 |
*/ |
| 229 |
public function parse_tags( $content ) { |
| 230 |
return $content; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Build the final email. |
| 235 |
* |
| 236 |
* @since 1.0 |
| 237 |
* |
| 238 |
* @param $message |
| 239 |
* |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public function build_email( $message ) { |
| 243 |
|
| 244 |
if ( false === $this->html ) { |
| 245 |
|
| 246 |
// Added Replacement check to simply behaviour of anchor tags. |
| 247 |
$pattern = '/<a.+?href\=(?:["|\'])(.+?)(?:["|\']).*?>(.+?)<\/a>/i'; |
| 248 |
$message = preg_replace_callback( |
| 249 |
$pattern, |
| 250 |
function ( $return ) { |
| 251 |
if ( $return[1] !== $return[2] ) { |
| 252 |
return "{$return[2]} ( {$return[1]} )"; |
| 253 |
} |
| 254 |
|
| 255 |
return trailingslashit( $return[1] ); |
| 256 |
}, |
| 257 |
$message |
| 258 |
); |
| 259 |
|
| 260 |
return apply_filters( 'give_email_message', wp_strip_all_tags( $message ), $this ); |
| 261 |
} |
| 262 |
|
| 263 |
$message = $this->text_to_html( $message ); |
| 264 |
|
| 265 |
$template = $this->get_template(); |
| 266 |
|
| 267 |
ob_start(); |
| 268 |
|
| 269 |
give_get_template_part( 'emails/header', $template, true ); |
| 270 |
|
| 271 |
/** |
| 272 |
* Fires in the email head. |
| 273 |
* |
| 274 |
* @since 1.0 |
| 275 |
* |
| 276 |
* @param Give_Emails $this The email object. |
| 277 |
*/ |
| 278 |
do_action( 'give_email_header', $this ); |
| 279 |
|
| 280 |
if ( has_action( 'give_email_template_' . $template ) ) { |
| 281 |
/** |
| 282 |
* Fires in a specific email template. |
| 283 |
* |
| 284 |
* @since 1.0 |
| 285 |
*/ |
| 286 |
do_action( "give_email_template_{$template}" ); |
| 287 |
} else { |
| 288 |
give_get_template_part( 'emails/body', $template, true ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Fires in the email body. |
| 293 |
* |
| 294 |
* @since 1.0 |
| 295 |
* |
| 296 |
* @param Give_Emails $this The email object. |
| 297 |
*/ |
| 298 |
do_action( 'give_email_body', $this ); |
| 299 |
|
| 300 |
give_get_template_part( 'emails/footer', $template, true ); |
| 301 |
|
| 302 |
/** |
| 303 |
* Fires in the email footer. |
| 304 |
* |
| 305 |
* @since 1.0 |
| 306 |
* |
| 307 |
* @param Give_Emails $this The email object. |
| 308 |
*/ |
| 309 |
do_action( 'give_email_footer', $this ); |
| 310 |
|
| 311 |
$body = ob_get_clean(); |
| 312 |
|
| 313 |
// Email tag. |
| 314 |
$message = str_replace( '{email}', $message, $body ); |
| 315 |
|
| 316 |
$header_img = Give_Email_Notification_Util::get_email_logo( $this->form_id ); |
| 317 |
|
| 318 |
if ( ! empty( $header_img ) ) { |
| 319 |
$header_img = sprintf( |
| 320 |
'<div id="template_header_image"><p style="margin-top:0;"><img style="max-width:450px;" src="%1$s" alt="%2$s" /></p></div>', |
| 321 |
esc_url( $header_img ), |
| 322 |
get_bloginfo( 'name' ) |
| 323 |
); |
| 324 |
} |
| 325 |
|
| 326 |
$message = str_replace( '{email_logo}', $header_img, $message ); |
| 327 |
|
| 328 |
return apply_filters( 'give_email_message', $message, $this ); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Send the email. |
| 333 |
* |
| 334 |
* @param string $to The To address to send to. |
| 335 |
* @param string $subject The subject line of the email to send. |
| 336 |
* @param string $message The body of the email to send. |
| 337 |
* @param string|array $attachments Attachments to the email in a format supported by wp_mail(). |
| 338 |
* |
| 339 |
* @return bool |
| 340 |
*/ |
| 341 |
public function send( $to, $subject, $message, $attachments = '' ) { |
| 342 |
|
| 343 |
if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) { |
| 344 |
give_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send email with Give_Emails until init/admin_init has been reached.', 'give' ) ); |
| 345 |
|
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Fires before sending an email. |
| 351 |
* |
| 352 |
* @since 1.0 |
| 353 |
* |
| 354 |
* @param Give_Emails $this The email object. |
| 355 |
*/ |
| 356 |
do_action( 'give_email_send_before', $this ); |
| 357 |
|
| 358 |
$subject = $this->parse_tags( $subject ); |
| 359 |
$message = $this->parse_tags( $message ); |
| 360 |
|
| 361 |
$message = $this->build_email( $message ); |
| 362 |
|
| 363 |
$attachments = apply_filters( 'give_email_attachments', $attachments, $this ); |
| 364 |
|
| 365 |
$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $attachments ); |
| 366 |
|
| 367 |
/** |
| 368 |
* Fires after sending an email. |
| 369 |
* |
| 370 |
* @since 1.0 |
| 371 |
* |
| 372 |
* @param Give_Emails $this The email object. |
| 373 |
*/ |
| 374 |
do_action( 'give_email_send_after', $this ); |
| 375 |
|
| 376 |
return $sent; |
| 377 |
|
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Add filters / actions before the email is sent. |
| 382 |
* |
| 383 |
* @since 1.0 |
| 384 |
*/ |
| 385 |
public function send_before() { |
| 386 |
add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
| 387 |
add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 388 |
add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Remove filters / actions after the email is sent. |
| 393 |
* |
| 394 |
* @since 1.0 |
| 395 |
*/ |
| 396 |
public function send_after() { |
| 397 |
remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) ); |
| 398 |
remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 399 |
remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) ); |
| 400 |
|
| 401 |
// Reset email related params. |
| 402 |
$this->heading = ''; |
| 403 |
$this->from_name = ''; |
| 404 |
$this->from_address = ''; |
| 405 |
$this->form_id = 0; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Converts text to formatted HTML. This is primarily for turning line breaks into <p> and <br/> tags. |
| 410 |
* |
| 411 |
* @since 1.0 |
| 412 |
* |
| 413 |
* @param string $message |
| 414 |
* |
| 415 |
* @return string |
| 416 |
*/ |
| 417 |
public function text_to_html( $message ) { |
| 418 |
/** |
| 419 |
* Filter the flag which decide to process email message with wpautop or not. |
| 420 |
* |
| 421 |
* @since 2.3.0 |
| 422 |
*/ |
| 423 |
$disable_wpautop = apply_filters( 'give_email_message_disable_wpautop', false ); |
| 424 |
|
| 425 |
if ( |
| 426 |
( 'text/html' == $this->content_type || true === $this->html ) |
| 427 |
&& ! $disable_wpautop |
| 428 |
) { |
| 429 |
$message = wpautop( $message ); |
| 430 |
} |
| 431 |
|
| 432 |
return $message; |
| 433 |
} |
| 434 |
|
| 435 |
} |
| 436 |
|