| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Email. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @category Widgets |
| 7 |
* @package Learnpress/Email |
| 8 |
* @since 3.0.0 |
| 9 |
* @version 3.0.2 |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Prevent loading this file directly |
| 14 |
*/ |
| 15 |
defined( 'ABSPATH' ) || exit(); |
| 16 |
|
| 17 |
if ( ! class_exists( 'LP_Email' ) ) { |
| 18 |
|
| 19 |
/** |
| 20 |
* Class LP_Email |
| 21 |
*/ |
| 22 |
class LP_Email extends LP_Abstract_Settings { |
| 23 |
/** |
| 24 |
* Email method ID. |
| 25 |
* |
| 26 |
* @var String |
| 27 |
*/ |
| 28 |
public $id; |
| 29 |
|
| 30 |
/** |
| 31 |
* Email method title. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $title; |
| 36 |
|
| 37 |
/** |
| 38 |
* 'yes' if the method is enabled. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $enabled; |
| 43 |
|
| 44 |
/** |
| 45 |
* Description for the email. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public $description; |
| 50 |
|
| 51 |
/** |
| 52 |
* Plain text template path. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
public $template_plain; |
| 57 |
|
| 58 |
/** |
| 59 |
* HTML template path. |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
public $template_html = ''; |
| 64 |
|
| 65 |
/** |
| 66 |
* Template path. |
| 67 |
* |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
public $template_base = ''; |
| 71 |
|
| 72 |
/** |
| 73 |
* Recipients for the email. |
| 74 |
* |
| 75 |
* @var string |
| 76 |
*/ |
| 77 |
public $recipient; |
| 78 |
|
| 79 |
/** |
| 80 |
* For display the field to setting specific emails. |
| 81 |
* . |
| 82 |
* |
| 83 |
* @var string |
| 84 |
*/ |
| 85 |
public $recipients = ''; |
| 86 |
|
| 87 |
/** |
| 88 |
* Heading for the email content. |
| 89 |
* |
| 90 |
* @var string |
| 91 |
*/ |
| 92 |
public $heading = ''; |
| 93 |
|
| 94 |
/** |
| 95 |
* Subject for the email. |
| 96 |
* |
| 97 |
* @var string |
| 98 |
*/ |
| 99 |
public $subject; |
| 100 |
|
| 101 |
/** |
| 102 |
* Default heading for the email content. |
| 103 |
* |
| 104 |
* @var string |
| 105 |
*/ |
| 106 |
public $default_heading; |
| 107 |
|
| 108 |
/** |
| 109 |
* Default subject for the email. |
| 110 |
* |
| 111 |
* @var string |
| 112 |
*/ |
| 113 |
public $default_subject; |
| 114 |
|
| 115 |
/** |
| 116 |
* Default content for the email. |
| 117 |
* |
| 118 |
* @var string |
| 119 |
*/ |
| 120 |
public $content; |
| 121 |
|
| 122 |
/** |
| 123 |
* Object this email is for, for example a customer, product, or email. |
| 124 |
* |
| 125 |
* @var object |
| 126 |
*/ |
| 127 |
public $object; |
| 128 |
|
| 129 |
/** |
| 130 |
* Strings to find in subjects/headings. |
| 131 |
* |
| 132 |
* @var array |
| 133 |
*/ |
| 134 |
public $find; |
| 135 |
|
| 136 |
/** |
| 137 |
* Strings to replace in subjects/headings. |
| 138 |
* |
| 139 |
* @var array |
| 140 |
*/ |
| 141 |
public $replace; |
| 142 |
|
| 143 |
/** |
| 144 |
* Mime boundary (for multipart emails). |
| 145 |
* |
| 146 |
* @var string |
| 147 |
*/ |
| 148 |
public $mime_boundary; |
| 149 |
|
| 150 |
/** |
| 151 |
* Mime boundary header (for multipart emails). |
| 152 |
* |
| 153 |
* @var string |
| 154 |
*/ |
| 155 |
public $mime_boundary_header; |
| 156 |
|
| 157 |
/** |
| 158 |
* True when email is being sent. |
| 159 |
* |
| 160 |
* @var bool |
| 161 |
*/ |
| 162 |
public $sending; |
| 163 |
|
| 164 |
public $debug = false; |
| 165 |
|
| 166 |
protected $_object_loaded = false; |
| 167 |
|
| 168 |
/** |
| 169 |
* List of preg* regular expression patterns to search for, |
| 170 |
* used in conjunction with $replace. |
| 171 |
* https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc |
| 172 |
* |
| 173 |
* @var array $search |
| 174 |
* @see $replace |
| 175 |
*/ |
| 176 |
public $plain_search = array( |
| 177 |
"/\r/", // Non-legal carriage return |
| 178 |
'/&(nbsp|#160);/i', // Non-breaking space |
| 179 |
'/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', // Double quotes |
| 180 |
'/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes |
| 181 |
'/>/i', // Greater-than |
| 182 |
'/</i', // Less-than |
| 183 |
'/&/i', // Ampersand |
| 184 |
'/&/i', // Ampersand |
| 185 |
'/&/i', // Ampersand |
| 186 |
'/&(copy|#169);/i', // Copyright |
| 187 |
'/&(trade|#8482|#153);/i', // Trademark |
| 188 |
'/&(reg|#174);/i', // Registered |
| 189 |
'/&(mdash|#151|#8212);/i', // mdash |
| 190 |
'/&(ndash|minus|#8211|#8722);/i', // ndash |
| 191 |
'/&(bull|#149|#8226);/i', // Bullet |
| 192 |
'/&(pound|#163);/i', // Pound sign |
| 193 |
'/&(euro|#8364);/i', // Euro sign |
| 194 |
'/$/', // Dollar sign |
| 195 |
'/&[^&\s;]+;/i', // Unknown/unhandled entities |
| 196 |
'/[ ]{2,}/', // Runs of spaces, post-handling |
| 197 |
); |
| 198 |
|
| 199 |
/** |
| 200 |
* List of pattern replacements corresponding to patterns searched. |
| 201 |
* |
| 202 |
* @var array $replace |
| 203 |
* @see $search |
| 204 |
*/ |
| 205 |
public $plain_replace = array( |
| 206 |
'', // Non-legal carriage return |
| 207 |
' ', // Non-breaking space |
| 208 |
'"', // Double quotes |
| 209 |
"'", // Single quotes |
| 210 |
'>', // Greater-than |
| 211 |
'<', // Less-than |
| 212 |
'&', // Ampersand |
| 213 |
'&', // Ampersand |
| 214 |
'&', // Ampersand |
| 215 |
'(c)', // Copyright |
| 216 |
'(tm)', // Trademark |
| 217 |
'(R)', // Registered |
| 218 |
'--', // mdash |
| 219 |
'-', // ndash |
| 220 |
'*', // Bullet |
| 221 |
'�', // Pound sign |
| 222 |
'EUR', // Euro sign. � ? |
| 223 |
'$', // Dollar sign |
| 224 |
'', // Unknown/unhandled entities |
| 225 |
' ', // Runs of spaces, post-handling |
| 226 |
); |
| 227 |
|
| 228 |
/** |
| 229 |
* List of pattern search corresponding to patterns replace. |
| 230 |
* |
| 231 |
* @var array text shortcode |
| 232 |
*/ |
| 233 |
public $text_search = array(); |
| 234 |
|
| 235 |
/** |
| 236 |
* List of pattern to replace |
| 237 |
* |
| 238 |
* @var array text replace |
| 239 |
*/ |
| 240 |
public $text_replace = array(); |
| 241 |
|
| 242 |
/** |
| 243 |
* Text message description |
| 244 |
* |
| 245 |
* @var string |
| 246 |
*/ |
| 247 |
public $email_text_message_description = ''; |
| 248 |
|
| 249 |
/** |
| 250 |
* @var string |
| 251 |
*/ |
| 252 |
public $template_path = ''; |
| 253 |
|
| 254 |
/** |
| 255 |
* @var null |
| 256 |
*/ |
| 257 |
public $variables = array(); |
| 258 |
|
| 259 |
/** |
| 260 |
* @var array|null |
| 261 |
*/ |
| 262 |
//public $basic_variables = array(); |
| 263 |
|
| 264 |
/** |
| 265 |
* @var null |
| 266 |
*/ |
| 267 |
public $general_variables = []; |
| 268 |
|
| 269 |
/** |
| 270 |
* @var null |
| 271 |
*/ |
| 272 |
public $support_variables = []; |
| 273 |
|
| 274 |
/** |
| 275 |
* @var LP_Settings |
| 276 |
*/ |
| 277 |
public $settings = null; |
| 278 |
|
| 279 |
/** |
| 280 |
* @var string |
| 281 |
*/ |
| 282 |
public $email_format = 'html'; |
| 283 |
|
| 284 |
/** |
| 285 |
* @var bool |
| 286 |
*/ |
| 287 |
public $enable = false; |
| 288 |
|
| 289 |
/** |
| 290 |
* @var string |
| 291 |
*/ |
| 292 |
public $group = ''; |
| 293 |
|
| 294 |
/** |
| 295 |
* @var string |
| 296 |
*/ |
| 297 |
protected $_option_id = ''; |
| 298 |
|
| 299 |
/** |
| 300 |
* LP_Email constructor. |
| 301 |
*/ |
| 302 |
public function __construct() { |
| 303 |
// Set template base path to LP templates path if it is not set. |
| 304 |
if ( empty( $this->template_base ) ) { |
| 305 |
$this->template_base = LP()->plugin_path( 'templates/' ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Set template folder if it is not set. Default is 'learnpress' |
| 310 |
*/ |
| 311 |
if ( empty( $this->template_path ) ) { |
| 312 |
$this->template_path = learn_press_template_path(); |
| 313 |
} |
| 314 |
|
| 315 |
if ( empty( $this->template_html ) ) { |
| 316 |
$this->template_html = "emails/{$this->id}.php"; |
| 317 |
} |
| 318 |
|
| 319 |
if ( empty( $this->template_plain ) ) { |
| 320 |
$this->template_plain = "emails/plain/{$this->id}.php"; |
| 321 |
} |
| 322 |
|
| 323 |
if ( ! $this->object ) { |
| 324 |
$this->object = array(); |
| 325 |
} |
| 326 |
|
| 327 |
$this->_option_id = 'emails_' . $this->id; |
| 328 |
|
| 329 |
$this->settings = LP_Settings::instance()->get_group( $this->_option_id, '' ); |
| 330 |
|
| 331 |
/** |
| 332 |
* Init general options |
| 333 |
*/ |
| 334 |
$this->heading = $this->settings->get( 'heading', $this->default_heading ); |
| 335 |
$this->subject = $this->settings->get( 'subject', $this->default_subject ); |
| 336 |
$this->enable = $this->settings->get( 'enable', 'no' ) === 'yes'; |
| 337 |
|
| 338 |
if ( $this->settings->get( 'email_content.format' ) ) { |
| 339 |
$this->email_format = ( $this->settings->get( 'email_content.format' ) == 'plain_text' ) ? 'plain' : 'html'; |
| 340 |
} else { |
| 341 |
if ( LP_Settings::instance()->get( 'emails_general.default_email_content' ) ) { |
| 342 |
$this->email_format = LP_Settings::instance()->get( 'emails_general.default_email_content' ); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
$email_formats = array( 'plain', 'html' ); |
| 347 |
if ( ! in_array( $this->email_format, $email_formats ) ) { |
| 348 |
$this->email_format = 'html'; |
| 349 |
} |
| 350 |
|
| 351 |
// Set type variables can click add to content of email setting. |
| 352 |
$this->support_variables = $this->general_variables = [ |
| 353 |
'{{site_url}}', |
| 354 |
'{{site_title}}', |
| 355 |
'{{login_url}}', |
| 356 |
'{{email_heading}}', |
| 357 |
'{{site_admin_email}}', |
| 358 |
'{{site_admin_name}}', |
| 359 |
'{{header}}', |
| 360 |
'{{footer}}', |
| 361 |
'{{footer_text}}', |
| 362 |
]; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* @param null $value |
| 367 |
* |
| 368 |
* @return bool |
| 369 |
*/ |
| 370 |
public function enable( $value = null ): bool { |
| 371 |
if ( is_bool( $value ) ) { |
| 372 |
$this->enable = $value; |
| 373 |
|
| 374 |
// Load default settings if the email is not configured |
| 375 |
if ( ! $this->is_configured() ) { |
| 376 |
$settings = $this->get_settings(); |
| 377 |
|
| 378 |
foreach ( $settings as $field ) { |
| 379 |
if ( $field['type'] == 'heading' || $field['type'] == 'title' || $field['type'] == 'sectionend' ) { |
| 380 |
continue; |
| 381 |
} |
| 382 |
|
| 383 |
$id = str_replace( $this->_option_id, '', $field['id'] ); |
| 384 |
$id = str_replace( array( '[', ']' ), '', $id ); |
| 385 |
|
| 386 |
if ( $id == 'email_content' ) { |
| 387 |
$this->settings->set( |
| 388 |
'email_content', |
| 389 |
array( |
| 390 |
'format' => 'html', |
| 391 |
'plain' => lp_get_email_content( 'plain', '', $field ), |
| 392 |
'html' => lp_get_email_content( 'html', '', $field ), |
| 393 |
) |
| 394 |
); |
| 395 |
} else { |
| 396 |
$this->settings->set( $id, $field['default'] ); |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
$this->settings->set( 'enable', $value ? 'yes' : 'no' ); |
| 401 |
$this->settings->update( 'learn_press_' . $this->_option_id, $this->settings->get() ); |
| 402 |
} |
| 403 |
|
| 404 |
return $this->enable; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* @return mixed |
| 409 |
*/ |
| 410 |
public function is_configured() { |
| 411 |
return LP_Settings::instance()->get( $this->_option_id ); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Get variables support in mail. |
| 416 |
* |
| 417 |
* @return mixed |
| 418 |
*/ |
| 419 |
public function get_variables_support() { |
| 420 |
return apply_filters( 'learn-press/email-variables-support', $this->support_variables, $this ); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Magic function |
| 425 |
* |
| 426 |
* @param $key |
| 427 |
* |
| 428 |
* @return mixed |
| 429 |
*/ |
| 430 |
public function __get( $key ) { |
| 431 |
if ( ! empty( $this->{$key} ) ) { |
| 432 |
return $this->{$key}; |
| 433 |
} else { |
| 434 |
return $this->settings->get( $key ); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* @return bool |
| 440 |
*/ |
| 441 |
private function is_current() { |
| 442 |
return ! empty( $_REQUEST['section'] ) && sanitize_text_field( $_REQUEST['section'] ) == $this->id; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* @param $options |
| 447 |
* @param $key |
| 448 |
* |
| 449 |
* @return bool |
| 450 |
*/ |
| 451 |
public function _remove_email_content_from_option( $options, $key ) { |
| 452 |
if ( ! $this->is_current() ) { |
| 453 |
return false; |
| 454 |
} |
| 455 |
|
| 456 |
if ( is_array( $options ) && ( array_key_exists( 'email_content_html', $options ) || array_key_exists( 'email_content_plain', $options ) ) ) { |
| 457 |
|
| 458 |
if ( array_key_exists( 'email_content_html', $options ) ) { |
| 459 |
$this->save_template( $options['email_content_html'], $this->template_html ); |
| 460 |
unset( $options['email_content_html'] ); |
| 461 |
} |
| 462 |
|
| 463 |
if ( array_key_exists( 'email_content_plain', $options ) ) { |
| 464 |
$this->save_template( $options['email_content_plain'], $this->template_plain ); |
| 465 |
unset( $options['email_content_plain'] ); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
return $options; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Apply the value of variables for string |
| 474 |
* |
| 475 |
* @param string $string |
| 476 |
* |
| 477 |
* @return string |
| 478 |
* @editor tungnx |
| 479 |
*/ |
| 480 |
public function format_string( string $string ): string { |
| 481 |
//$this->_maybe_get_object(); |
| 482 |
|
| 483 |
$search = $replace = array(); |
| 484 |
if ( is_array( $this->variables ) ) { |
| 485 |
$search = array_keys( $this->variables ); |
| 486 |
$replace = array_values( $this->variables ); |
| 487 |
} |
| 488 |
$search = apply_filters( 'learn-press/email-format-string-find', $search, $this ); |
| 489 |
$replace = apply_filters( 'learn-press/email-format-string-replace', $replace, $this ); |
| 490 |
|
| 491 |
return str_replace( $search, $replace, $string ); |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Get email recipient. |
| 496 |
* |
| 497 |
* @return string |
| 498 |
*/ |
| 499 |
public function get_recipient() { |
| 500 |
return apply_filters( 'learn-press/email-recipient-' . $this->id, $this->recipient, $this->object ); |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Get email subject. |
| 505 |
* |
| 506 |
* @return string |
| 507 |
*/ |
| 508 |
public function get_subject(): string { |
| 509 |
return $this->format_string( $this->subject ); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Get email content. |
| 514 |
* |
| 515 |
* @return string |
| 516 |
* @editor tungnx |
| 517 |
*/ |
| 518 |
public function get_content(): string { |
| 519 |
$email_format = $this->get_email_format(); |
| 520 |
|
| 521 |
if ( 'plain' == $email_format ) { |
| 522 |
$email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) ); |
| 523 |
} elseif ( in_array( $email_format, array( 'html', 'multipart' ) ) ) { |
| 524 |
$email_content = $this->get_content_html(); |
| 525 |
} else { |
| 526 |
$email_content = preg_replace( $this->text_search, $this->text_replace, $this->get_content_text_message() ); |
| 527 |
} |
| 528 |
|
| 529 |
$email_content = $this->format_string( $email_content ); |
| 530 |
|
| 531 |
return wordwrap( $email_content, 70 ); |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Try to get object if it is null. |
| 536 |
* |
| 537 |
* @editor tungnx |
| 538 |
* @reason comment - not use |
| 539 |
*/ |
| 540 |
/*protected function _maybe_get_object() { |
| 541 |
try { |
| 542 |
if ( ! $this->_object_loaded && empty( $this->object ) ) { |
| 543 |
$this->_object_loaded = true; |
| 544 |
$this->get_object(); |
| 545 |
} |
| 546 |
} catch ( Exception $ex ) { |
| 547 |
} |
| 548 |
}*/ |
| 549 |
|
| 550 |
/** |
| 551 |
* Get email heading. |
| 552 |
* |
| 553 |
* @return string |
| 554 |
*/ |
| 555 |
public function get_heading(): string { |
| 556 |
return $this->format_string( $this->heading ); |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Get email footer text. |
| 561 |
* |
| 562 |
* @return string |
| 563 |
*/ |
| 564 |
public function get_footer_text(): string { |
| 565 |
$text = LP_Settings::instance()->get( 'emails_general.footer_text', 'LearnPress' ); |
| 566 |
|
| 567 |
return LP_Helper::sanitize_params_submitted( $text, 'html' ); |
| 568 |
} |
| 569 |
|
| 570 |
/** |
| 571 |
* Get email content HTML. |
| 572 |
* |
| 573 |
* @return string |
| 574 |
*/ |
| 575 |
public function get_content_html(): string { |
| 576 |
$template = $this->get_template( 'template_html' ); |
| 577 |
$template_file = $this->template_base . $template; |
| 578 |
$content = $this->settings->get( 'email_content.html', file_get_contents( $template_file ) ); |
| 579 |
|
| 580 |
return stripslashes( $content ); |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* Get email content plain. |
| 585 |
* |
| 586 |
* @return string |
| 587 |
*/ |
| 588 |
public function get_content_plain(): string { |
| 589 |
$template = $this->get_template( 'template_plain' ); |
| 590 |
$template_file = $this->template_base . $template; |
| 591 |
$content_tmp = LP_WP_Filesystem::instance()->file_get_contents( $template_file ); |
| 592 |
$content = $this->settings->get( 'email_content.plain', $content_tmp ); |
| 593 |
$content = stripslashes( $content ); |
| 594 |
|
| 595 |
return $content; |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Get content context message. |
| 600 |
* |
| 601 |
* @return string |
| 602 |
*/ |
| 603 |
public function get_content_text_message(): string { |
| 604 |
return apply_filters( 'learn-press/email-text-message-' . $this->id, $this->settings->get( 'content_text_message' ) ); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Get email headers. |
| 609 |
* |
| 610 |
* @return string|array |
| 611 |
*/ |
| 612 |
public function get_headers() { |
| 613 |
return apply_filters( 'learn-press/email-headers', 'Content-Type: ' . $this->get_content_format() . "\r\n", $this->id, $this->object ); |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Get email attachments. |
| 618 |
* |
| 619 |
* @return array |
| 620 |
*/ |
| 621 |
public function get_attachments() { |
| 622 |
return apply_filters( 'learn-press/email-attachments', array(), $this->id, $this->object ); |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* Get FROM address. Default is admin email. |
| 627 |
* |
| 628 |
* @return string |
| 629 |
* @editor tungnx |
| 630 |
* @version 1.0.1 |
| 631 |
* @editor tungnx |
| 632 |
* @modify 4.1.4 - comment - not override hook |
| 633 |
*/ |
| 634 |
/*public function get_from_address(): string { |
| 635 |
$email = LP_Settings::instance()->get( 'emails_general.from_email', get_option( 'admin_email' ) ); |
| 636 |
|
| 637 |
return sanitize_email( $email ); |
| 638 |
}*/ |
| 639 |
|
| 640 |
/** |
| 641 |
* Get FROM name. Default is Blog name. |
| 642 |
* |
| 643 |
* @return string |
| 644 |
* @editor tungnx |
| 645 |
* @version 1.0.1 |
| 646 |
*/ |
| 647 |
public function get_from_name(): string { |
| 648 |
$name = LP_Settings::instance()->get( 'emails_general.from_name', get_option( 'blogname' ) ); |
| 649 |
|
| 650 |
return LP_Helper::sanitize_params_submitted( $name ); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Get image header in general settings. |
| 655 |
* |
| 656 |
* @return string |
| 657 |
* @editor tungnx |
| 658 |
* @reason comment - write new |
| 659 |
*/ |
| 660 |
/*public function get_image_header() { |
| 661 |
$image = LP_Emails::instance()->get_image_header(); |
| 662 |
|
| 663 |
return apply_filters( 'learn-press/email-image-header-' . $this->id, $image ); |
| 664 |
}*/ |
| 665 |
|
| 666 |
/** |
| 667 |
* Get WP Blog name. |
| 668 |
* |
| 669 |
* @return string |
| 670 |
*/ |
| 671 |
public function get_blogname() { |
| 672 |
return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
| 673 |
} |
| 674 |
|
| 675 |
/** |
| 676 |
* Get email content format. |
| 677 |
* |
| 678 |
* @return string |
| 679 |
*/ |
| 680 |
public function get_content_format() { |
| 681 |
switch ( $this->get_email_format() ) { |
| 682 |
case 'text_message': |
| 683 |
case 'html': |
| 684 |
return 'text/html'; |
| 685 |
case 'multipart': |
| 686 |
return 'multipart/alternative'; |
| 687 |
default: |
| 688 |
return 'text/plain'; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
* @return string |
| 694 |
*/ |
| 695 |
public function get_email_format() { |
| 696 |
return $this->email_format && class_exists( 'DOMDocument' ) ? $this->email_format : 'plain'; |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* Apply css from external to inline. |
| 701 |
* |
| 702 |
* @param string $content |
| 703 |
* |
| 704 |
* @return string |
| 705 |
*/ |
| 706 |
public function apply_style_inline( $content ) { |
| 707 |
if ( in_array( $this->get_content_format(), array( 'text/html', 'multipart/alternative' ) ) && class_exists( 'DOMDocument' ) ) { |
| 708 |
|
| 709 |
// get CSS styles |
| 710 |
ob_start(); |
| 711 |
learn_press_get_template( 'emails/email-styles.php' ); |
| 712 |
$css = apply_filters( 'learn_press_email_styles', ob_get_clean(), $this->id, $this ); |
| 713 |
|
| 714 |
try { |
| 715 |
if ( ! class_exists( 'Emogrifier' ) ) { |
| 716 |
include_once LP_PLUGIN_PATH . 'inc/libraries/class-emogrifier.php'; |
| 717 |
} |
| 718 |
// apply CSS styles inline for picky email clients |
| 719 |
$emogrifier = new Emogrifier( $content, $css ); |
| 720 |
$content = $emogrifier->emogrify(); |
| 721 |
|
| 722 |
} catch ( Exception $e ) { |
| 723 |
|
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
return apply_filters( 'learn-press/email-content-inline-style', $content, $this->id ); |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Get template file from type. |
| 732 |
* |
| 733 |
* @param string $type |
| 734 |
* |
| 735 |
* @return string |
| 736 |
*/ |
| 737 |
public function get_template( $type ) { |
| 738 |
$type = esc_attr( basename( $type ) ); |
| 739 |
|
| 740 |
if ( 'template_html' == $type ) { |
| 741 |
return $this->template_html; |
| 742 |
} elseif ( 'template_plain' == $type ) { |
| 743 |
return $this->template_plain; |
| 744 |
} |
| 745 |
|
| 746 |
return ''; |
| 747 |
} |
| 748 |
|
| 749 |
protected function save_template( $code, $path ) { |
| 750 |
return; |
| 751 |
} |
| 752 |
|
| 753 |
/** |
| 754 |
* Get template file. |
| 755 |
* |
| 756 |
* @param string $template |
| 757 |
* @param string $template_path |
| 758 |
* |
| 759 |
* @return string |
| 760 |
*/ |
| 761 |
public function get_theme_template_file( $template, $template_path = null ) { |
| 762 |
$template_dir = apply_filters( 'learn-press/template-directory', $template_path ? $template_path : learn_press_template_path(), $template ); |
| 763 |
|
| 764 |
return join( |
| 765 |
'', |
| 766 |
array( |
| 767 |
trailingslashit( get_stylesheet_directory() ), |
| 768 |
trailingslashit( $template_dir ), |
| 769 |
$template, |
| 770 |
) |
| 771 |
); |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* Send email. |
| 776 |
* |
| 777 |
* @param string|array $to |
| 778 |
* @param string $subject |
| 779 |
* @param string $message |
| 780 |
* @param string|string[] $headers |
| 781 |
* @param array $attachments |
| 782 |
* |
| 783 |
* @editor tungnx |
| 784 |
* @version 1.0.1 |
| 785 |
* |
| 786 |
* @return bool |
| 787 |
*/ |
| 788 |
public function send( $to, string $subject, string $message, $headers, array $attachments ): bool { |
| 789 |
$return = false; |
| 790 |
$message = apply_filters( 'learn-press/email-content', $this->apply_style_inline( $message ), $this ); |
| 791 |
|
| 792 |
if ( is_string( $to ) ) { |
| 793 |
$to = preg_split( '~\s?,\s?~', $to ); |
| 794 |
|
| 795 |
$return = wp_mail( $to, $subject, $message, $headers, $attachments ); |
| 796 |
} elseif ( is_array( $to ) ) { |
| 797 |
foreach ( $to as $t ) { |
| 798 |
$return = wp_mail( $t, $subject, $message, $headers, $attachments ); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
return $return; |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Get all values set and send email |
| 807 |
* |
| 808 |
* @author tungnx |
| 809 |
* @since 4.1.3 |
| 810 |
*/ |
| 811 |
public function send_email(): bool { |
| 812 |
add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) ); |
| 813 |
add_filter( 'wp_mail_content_type', array( $this, 'get_content_format' ) ); |
| 814 |
|
| 815 |
return $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 816 |
} |
| 817 |
|
| 818 |
/** |
| 819 |
* Get common variables. |
| 820 |
* Variables use for click add to content of Email |
| 821 |
* |
| 822 |
* @param string $format |
| 823 |
* @return array |
| 824 |
* @author tungnx |
| 825 |
* @since 4.1.1 |
| 826 |
*/ |
| 827 |
public function get_common_variables( string $format = 'plain' ): array { |
| 828 |
$heading = strip_tags( $this->get_heading() ); |
| 829 |
$footer_text = strip_tags( $this->get_footer_text() ); |
| 830 |
|
| 831 |
$header = $heading; |
| 832 |
$footer = $footer_text; |
| 833 |
|
| 834 |
if ( $format != 'plain' ) { |
| 835 |
$header = $this->email_header( $heading, false ); |
| 836 |
$footer = $this->email_footer( $footer_text, false ); |
| 837 |
} |
| 838 |
|
| 839 |
$admin_user = get_user_by( 'email', get_option( 'admin_email' ) ); |
| 840 |
return apply_filters( |
| 841 |
'email_variables_common', |
| 842 |
[ |
| 843 |
'{{header}}' => $header, |
| 844 |
'{{footer}}' => $footer, |
| 845 |
'{{footer_text}}' => $footer_text, |
| 846 |
'{{site_url}}' => get_home_url(), |
| 847 |
'{{site_title}}' => $this->get_blogname(), |
| 848 |
'{{site_admin_email}}' => get_option( 'admin_email' ), |
| 849 |
'{{site_admin_name}}' => learn_press_get_profile_display_name( $admin_user ), |
| 850 |
'{{login_url}}' => learn_press_get_login_url(), |
| 851 |
'{{plain_text}}' => $format == 'plain', |
| 852 |
] |
| 853 |
); |
| 854 |
} |
| 855 |
|
| 856 |
/** |
| 857 |
* @param string $name |
| 858 |
* |
| 859 |
* @return string |
| 860 |
*/ |
| 861 |
public function get_field_name( $name ) { |
| 862 |
return $this->_option_id . "[$name]"; |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* Default options for all emails. |
| 867 |
* Almost the emails are the same with options. |
| 868 |
* |
| 869 |
* @return array |
| 870 |
*/ |
| 871 |
protected function _default_settings(): array { |
| 872 |
/** |
| 873 |
* In case the email is not for sending to specific admin (like user who has bought course or author of course, etc..) |
| 874 |
* So, we do not need this field. |
| 875 |
*/ |
| 876 |
|
| 877 |
$enable_recipients = apply_filters( 'learn-press/enable-email-recipients', ! empty( $this->recipients ), $this ); |
| 878 |
|
| 879 |
$default = array_merge( |
| 880 |
array( |
| 881 |
array( |
| 882 |
'type' => 'title', |
| 883 |
), |
| 884 |
array( |
| 885 |
'title' => esc_html__( 'Enable/Disable', 'learnpress' ), |
| 886 |
'type' => 'checkbox', |
| 887 |
'default' => 'no', |
| 888 |
'id' => $this->get_field_name( 'enable' ), |
| 889 |
'desc' => $this->description, |
| 890 |
), |
| 891 |
), |
| 892 |
$enable_recipients ? array( |
| 893 |
array( |
| 894 |
'title' => esc_html__( 'Recipient(s)', 'learnpress' ), |
| 895 |
'type' => 'text', |
| 896 |
'default' => get_option( 'admin_email' ), |
| 897 |
'id' => $this->get_field_name( 'recipients' ), |
| 898 |
'desc' => esc_html__( 'Separate other recipients by comma.', 'learnpress' ), |
| 899 |
), |
| 900 |
) : array(), |
| 901 |
array( |
| 902 |
array( |
| 903 |
'title' => esc_html__( 'Subject', 'learnpress' ), |
| 904 |
'type' => 'text', |
| 905 |
'default' => $this->default_subject, |
| 906 |
'id' => $this->get_field_name( 'subject' ), |
| 907 |
'css' => 'width:400px', |
| 908 |
), |
| 909 |
array( |
| 910 |
'title' => esc_html__( 'Email heading', 'learnpress' ), |
| 911 |
'type' => 'text', |
| 912 |
'default' => $this->default_heading, |
| 913 |
'id' => $this->get_field_name( 'heading' ), |
| 914 |
'css' => 'width:400px', |
| 915 |
), |
| 916 |
array( |
| 917 |
'title' => esc_html__( 'Content type', 'learnpress' ), |
| 918 |
'type' => 'email-content', |
| 919 |
'default' => '', |
| 920 |
'id' => $this->get_field_name( 'email_content' ), |
| 921 |
'template_base' => $this->template_base, |
| 922 |
'template_path' => $this->template_path, |
| 923 |
'template_html' => $this->template_html, |
| 924 |
'template_plain' => $this->template_plain, |
| 925 |
'template_html_local' => $this->get_theme_template_file( 'html', $this->template_path ), |
| 926 |
'template_plain_local' => $this->get_theme_template_file( 'plain', $this->template_path ), |
| 927 |
'support_variables' => $this->get_variables_support(), |
| 928 |
), |
| 929 |
array( |
| 930 |
'type' => 'sectionend', |
| 931 |
), |
| 932 |
) |
| 933 |
); |
| 934 |
|
| 935 |
return $default; |
| 936 |
} |
| 937 |
|
| 938 |
/** |
| 939 |
* Get settings in admin. |
| 940 |
* |
| 941 |
* @since 3.0.0 |
| 942 |
* |
| 943 |
* @return bool|mixed |
| 944 |
*/ |
| 945 |
public function get_settings() { |
| 946 |
return apply_filters( 'learn-press/email-settings/' . $this->id . '/settings', $this->_default_settings() ); |
| 947 |
} |
| 948 |
|
| 949 |
/** |
| 950 |
* Get instructors to send mail. |
| 951 |
* |
| 952 |
* @since 3.0.0 |
| 953 |
* |
| 954 |
* @param null $order_id |
| 955 |
* |
| 956 |
* @return array |
| 957 |
*/ |
| 958 |
public function get_order_instructors( $order_id ) { |
| 959 |
if ( ! $order_id ) { |
| 960 |
return array(); |
| 961 |
} |
| 962 |
|
| 963 |
$order = learn_press_get_order( $order_id ); |
| 964 |
|
| 965 |
$items = $order->get_items(); |
| 966 |
$instructors = array(); |
| 967 |
|
| 968 |
if ( count( $items ) ) { |
| 969 |
foreach ( $items as $item ) { |
| 970 |
if ( ! isset( $item['course_id'] ) ) { |
| 971 |
continue; |
| 972 |
} |
| 973 |
|
| 974 |
$user_id = get_post_field( 'post_author', $item['course_id'] ); |
| 975 |
if ( $user_id ) { |
| 976 |
$instructors[] = $user_id; |
| 977 |
} |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
return $instructors; |
| 982 |
} |
| 983 |
|
| 984 |
protected function _get_admin_email() { |
| 985 |
return apply_filters( 'learn-press/email/admin-email', get_option( 'admin_email' ) ); |
| 986 |
} |
| 987 |
|
| 988 |
/** |
| 989 |
* @return string |
| 990 |
*/ |
| 991 |
public function __toString() { |
| 992 |
return $this->title; |
| 993 |
} |
| 994 |
|
| 995 |
/** |
| 996 |
* Get image header in general settings. |
| 997 |
* |
| 998 |
* @return string |
| 999 |
*/ |
| 1000 |
public function get_image_header(): string { |
| 1001 |
$image = LP_Settings::instance()->get( 'emails_general.header_image', '' ); |
| 1002 |
|
| 1003 |
if ( ! empty( $image ) ) { |
| 1004 |
$image = wp_get_attachment_image_url( $image, 'full' ); |
| 1005 |
} |
| 1006 |
|
| 1007 |
return $image; |
| 1008 |
} |
| 1009 |
|
| 1010 |
/** |
| 1011 |
* Email header. |
| 1012 |
* |
| 1013 |
* @param string $heading |
| 1014 |
* @param bool $echo |
| 1015 |
* |
| 1016 |
* @return string |
| 1017 |
*/ |
| 1018 |
public function email_header( string $heading, bool $echo = true ): string { |
| 1019 |
ob_start(); |
| 1020 |
learn_press_get_template( |
| 1021 |
'emails/email-header.php', |
| 1022 |
[ |
| 1023 |
'email_heading' => $heading, |
| 1024 |
'image_header' => $this->get_image_header(), |
| 1025 |
] |
| 1026 |
); |
| 1027 |
$header = ob_get_clean(); |
| 1028 |
|
| 1029 |
if ( $echo ) { |
| 1030 |
echo wp_kses_post( $header ); |
| 1031 |
} |
| 1032 |
|
| 1033 |
return $header; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/** |
| 1037 |
* Email footer. |
| 1038 |
* |
| 1039 |
* @param string $footer_text |
| 1040 |
* @param bool $echo |
| 1041 |
* |
| 1042 |
* @return string |
| 1043 |
*/ |
| 1044 |
public function email_footer( string $footer_text, bool $echo = true ): string { |
| 1045 |
ob_start(); |
| 1046 |
learn_press_get_template( 'emails/email-footer.php', array( 'footer_text' => $footer_text ) ); |
| 1047 |
$footer = ob_get_clean(); |
| 1048 |
|
| 1049 |
if ( $echo ) { |
| 1050 |
echo wp_kses_post( $footer ); |
| 1051 |
} |
| 1052 |
|
| 1053 |
return $footer; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/** |
| 1057 |
* Set receive email |
| 1058 |
* |
| 1059 |
* @param string $receive_email |
| 1060 |
*/ |
| 1061 |
public function set_receive( string $receive_email ) { |
| 1062 |
$this->recipient = $receive_email; |
| 1063 |
} |
| 1064 |
|
| 1065 |
/** |
| 1066 |
* Method called by background on LP_Background_Single_Email |
| 1067 |
* @see LP_Background_Single_Email::handle() |
| 1068 |
* |
| 1069 |
* @param array $params |
| 1070 |
*/ |
| 1071 |
public function handle( array $params ) { |
| 1072 |
|
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|