| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email Notification |
| 4 |
* |
| 5 |
* This class handles all email notification settings. |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @subpackage Classes/Emails |
| 9 |
* @copyright Copyright (c) 2016, WordImpress |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
* @since 2.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if access directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! class_exists( 'Give_Email_Notification' ) ) : |
| 20 |
|
| 21 |
/** |
| 22 |
* Give_Email_Notification |
| 23 |
* |
| 24 |
* @abstract |
| 25 |
* @since 2.0 |
| 26 |
*/ |
| 27 |
abstract class Give_Email_Notification { |
| 28 |
/** |
| 29 |
* Array of instances |
| 30 |
* |
| 31 |
* @since 2.0 |
| 32 |
* @access private |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
private static $singleton = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Array of notification settings. |
| 39 |
* |
| 40 |
* @since 2.0 |
| 41 |
* @access public |
| 42 |
* @var array |
| 43 |
*/ |
| 44 |
public $config = array( |
| 45 |
'id' => '', |
| 46 |
'label' => '', |
| 47 |
'description' => '', |
| 48 |
'has_recipient_field' => false, |
| 49 |
'recipient_group_name' => '', |
| 50 |
'notification_status' => 'disabled', |
| 51 |
'notification_status_editable' => true, |
| 52 |
'notices' => array(), |
| 53 |
'content_type_editable' => true, |
| 54 |
'has_preview' => true, |
| 55 |
'has_preview_header' => true, |
| 56 |
'preview_email_tags_values' => array(), |
| 57 |
'email_tag_context' => 'all', |
| 58 |
'form_metabox_setting' => true, |
| 59 |
'content_type' => '', |
| 60 |
'email_template' => '', |
| 61 |
'default_email_subject' => '', |
| 62 |
'default_email_message' => '', |
| 63 |
'default_email_header' => '', |
| 64 |
// This setting page will appear under core setting. |
| 65 |
'show_on_emails_setting_page' => true, |
| 66 |
'form_metabox_id' => 'give_email_notification_options_metabox_fields', |
| 67 |
); |
| 68 |
|
| 69 |
/** |
| 70 |
* @var string $recipient_email Donor email. |
| 71 |
* @access protected |
| 72 |
* @since 2.0 |
| 73 |
*/ |
| 74 |
protected $recipient_email = ''; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Setup email notification. |
| 79 |
* |
| 80 |
* @since 2.0 |
| 81 |
*/ |
| 82 |
public function init() { |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Get instance. |
| 89 |
* |
| 90 |
* @since 2.0 |
| 91 |
* @access public |
| 92 |
* |
| 93 |
* @param string $email_notification_id |
| 94 |
* |
| 95 |
* @return Give_Email_Notification |
| 96 |
*/ |
| 97 |
public static function get_instance( $email_notification_id = '' ) { |
| 98 |
$class = ''; |
| 99 |
|
| 100 |
if ( ! empty( $email_notification_id ) ) { |
| 101 |
/* @var Give_Email_Notification $class */ |
| 102 |
foreach ( self::$singleton as $class ) { |
| 103 |
if ( $email_notification_id === $class->config['id'] ) { |
| 104 |
$class = get_class( $class ); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
} else { |
| 109 |
$class = get_called_class(); |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! empty( $class ) && ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) ) { |
| 113 |
self::$singleton[ $class ] = new $class(); |
| 114 |
} |
| 115 |
|
| 116 |
return ( isset( self::$singleton[ $class ] ) ? self::$singleton[ $class ] : null ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Setup action and filters. |
| 121 |
* |
| 122 |
* @access public |
| 123 |
* @since 2.0 |
| 124 |
* |
| 125 |
* @param array $config |
| 126 |
*/ |
| 127 |
public function load( $config ) { |
| 128 |
// Set notification configuration. |
| 129 |
$this->config = wp_parse_args( $config, $this->config ); |
| 130 |
|
| 131 |
// Set email preview header status. |
| 132 |
$this->config['has_preview_header'] = $this->config['has_preview'] && $this->config['has_preview_header'] ? true : false; |
| 133 |
|
| 134 |
// Set email content type |
| 135 |
$this->config['content_type'] = empty( $this->config['content_type'] ) || ! in_array( $this->config['content_type'], array( |
| 136 |
'text/html', |
| 137 |
'text/plain', |
| 138 |
) ) |
| 139 |
? Give()->emails->get_content_type() |
| 140 |
: $this->config['content_type']; |
| 141 |
$this->config['content_type'] = give_get_option( Give_Email_Setting_Field::get_prefix( $this ) . 'email_content_type', $this->config['content_type'] ); |
| 142 |
|
| 143 |
// Set email template type. |
| 144 |
$this->config['email_template'] = empty( $this->config['email_template'] ) |
| 145 |
? give_get_option( 'email_template' ) |
| 146 |
: $this->config['email_template']; |
| 147 |
|
| 148 |
// Set recipient group name. |
| 149 |
$this->config['recipient_group_name'] = empty( $this->config['recipient_group_name'] ) |
| 150 |
? ( ! Give_Email_Notification_Util::has_recipient_field( $this ) ? __( 'Donor', 'give' ) : '' ) |
| 151 |
: $this->config['recipient_group_name']; |
| 152 |
|
| 153 |
// Non notification status editable notice. |
| 154 |
$this->config['notices']['non-notification-status-editable'] = empty( $this->config['notices']['non-notification-status-editable'] ) |
| 155 |
? __( 'You can not edit notification status from here.', 'give' ) |
| 156 |
: $this->config['notices']['non-notification-status-editable']; |
| 157 |
|
| 158 |
/** |
| 159 |
* Filter the notification config. |
| 160 |
* |
| 161 |
* @since 2.0 |
| 162 |
* |
| 163 |
* @param array Give_Email_Notification::config |
| 164 |
* @param Give_Email_Notification $this |
| 165 |
*/ |
| 166 |
$this->config = apply_filters( 'give_email_api_notification_config', $this->config, $this ); |
| 167 |
|
| 168 |
// Setup filters. |
| 169 |
$this->setup_filters(); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Setup filters. |
| 175 |
* |
| 176 |
* @since 2.0 |
| 177 |
* @access public |
| 178 |
*/ |
| 179 |
private function setup_filters() { |
| 180 |
// Apply filter only for current email notification section. |
| 181 |
if ( give_get_current_setting_section() === $this->config['id'] ) { |
| 182 |
// Initialize email context for email notification. |
| 183 |
$this->config['email_tag_context'] = apply_filters( |
| 184 |
"give_{$this->config['id']}_email_tag_context", |
| 185 |
$this->config['email_tag_context'], |
| 186 |
$this |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
// Setup setting fields. |
| 191 |
if( $this->config['show_on_emails_setting_page'] ) { |
| 192 |
add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 ); |
| 193 |
} |
| 194 |
|
| 195 |
if ( $this->config['form_metabox_setting'] && ! empty( $this->config['form_metabox_id'] ) ) { |
| 196 |
add_filter( |
| 197 |
$this->config['form_metabox_id'], |
| 198 |
array( $this, 'add_metabox_setting_field' ), |
| 199 |
10, |
| 200 |
2 |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
if( $this->config['has_recipient_field'] ) { |
| 205 |
add_action( |
| 206 |
"give_save__give_{$this->config['id']}_recipient", |
| 207 |
array( $this, 'validate_form_recipient_field_value' ), |
| 208 |
10, |
| 209 |
3 |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Filter the default email subject. |
| 215 |
* |
| 216 |
* @since 2.0 |
| 217 |
*/ |
| 218 |
$this->config['default_email_subject'] = apply_filters( |
| 219 |
"give_{$this->config['id']}_get_default_email_subject", |
| 220 |
$this->config['default_email_subject'], |
| 221 |
$this |
| 222 |
); |
| 223 |
|
| 224 |
/** |
| 225 |
* Filter the default email message. |
| 226 |
* |
| 227 |
* @since 2.0 |
| 228 |
*/ |
| 229 |
$this->config['default_email_message'] = apply_filters( |
| 230 |
"give_{$this->config['id']}_get_default_email_message", |
| 231 |
$this->config['default_email_message'], |
| 232 |
$this |
| 233 |
); |
| 234 |
|
| 235 |
/** |
| 236 |
* Filter the default email header. |
| 237 |
* |
| 238 |
* @since 2.1.3 |
| 239 |
*/ |
| 240 |
$this->config['default_email_header'] = apply_filters( |
| 241 |
"give_{$this->config['id']}_get_default_email_header", |
| 242 |
$this->config['default_email_header'], |
| 243 |
$this |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Add sections. |
| 249 |
* |
| 250 |
* @since 2.0 |
| 251 |
* |
| 252 |
* @param array $sections |
| 253 |
* |
| 254 |
* @return array |
| 255 |
*/ |
| 256 |
public function add_section( $sections ) { |
| 257 |
$sections[ $this->config['id'] ] = $this->config['label']; |
| 258 |
|
| 259 |
return $sections; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Add sections. |
| 264 |
* |
| 265 |
* @since 2.0 |
| 266 |
* |
| 267 |
* @param bool $hide_section |
| 268 |
* |
| 269 |
* @return bool |
| 270 |
*/ |
| 271 |
public function hide_section( $hide_section ) { |
| 272 |
$hide_section = true; |
| 273 |
|
| 274 |
return $hide_section; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Register email settings. |
| 279 |
* |
| 280 |
* @since 2.0 |
| 281 |
* @access public |
| 282 |
* |
| 283 |
* @param array $settings |
| 284 |
* |
| 285 |
* @return array |
| 286 |
*/ |
| 287 |
public function add_setting_fields( $settings ) { |
| 288 |
if ( $this->config['id'] === give_get_current_setting_section() ) { |
| 289 |
$settings = $this->get_setting_fields(); |
| 290 |
} |
| 291 |
|
| 292 |
return $settings; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Get setting fields |
| 298 |
* |
| 299 |
* @since 2.0 |
| 300 |
* @access public |
| 301 |
* |
| 302 |
* @param int $form_id |
| 303 |
* |
| 304 |
* @return array |
| 305 |
*/ |
| 306 |
public function get_setting_fields( $form_id = null ) { |
| 307 |
return Give_Email_Setting_Field::get_setting_fields( $this, $form_id ); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
/** |
| 312 |
* Register email settings to form metabox. |
| 313 |
* |
| 314 |
* @since 2.0 |
| 315 |
* @access public |
| 316 |
* |
| 317 |
* @param array $settings |
| 318 |
* @param int $form_id |
| 319 |
* |
| 320 |
* @return array |
| 321 |
*/ |
| 322 |
public function add_metabox_setting_field( $settings, $form_id ) { |
| 323 |
|
| 324 |
if( Give_Email_Notification_Util::is_email_notification_active( $this ) ) { |
| 325 |
$settings[] = array( |
| 326 |
'id' => $this->config['id'], |
| 327 |
'title' => $this->config['label'], |
| 328 |
'fields' => $this->get_setting_fields( $form_id ), |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
return $settings; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
/** |
| 337 |
* Get extra setting field. |
| 338 |
* |
| 339 |
* @since 2.0 |
| 340 |
* @access public |
| 341 |
* |
| 342 |
* @param int $form_id |
| 343 |
* |
| 344 |
* @return array |
| 345 |
*/ |
| 346 |
public function get_extra_setting_fields( $form_id = null ) { |
| 347 |
return array(); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Get recipient(s). |
| 353 |
* |
| 354 |
* Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor. |
| 355 |
* |
| 356 |
* @since 2.0 |
| 357 |
* @access public |
| 358 |
* |
| 359 |
* @param int $form_id |
| 360 |
* |
| 361 |
* @return string|array |
| 362 |
*/ |
| 363 |
public function get_recipient( $form_id = null ) { |
| 364 |
if ( empty( $this->recipient_email ) && $this->config['has_recipient_field'] ) { |
| 365 |
$this->recipient_email = Give_Email_Notification_Util::get_value( |
| 366 |
$this, |
| 367 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'recipient', |
| 368 |
$form_id |
| 369 |
); |
| 370 |
|
| 371 |
|
| 372 |
/** |
| 373 |
* Filter the admin notice emails. |
| 374 |
* |
| 375 |
* @since 1.0 |
| 376 |
* @deprecated 2.0 |
| 377 |
*/ |
| 378 |
$this->recipient_email = apply_filters( 'give_admin_notice_emails', $this->recipient_email, $this, $form_id ); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Filter the recipients |
| 383 |
* |
| 384 |
* @since 2.0 |
| 385 |
*/ |
| 386 |
return apply_filters( |
| 387 |
"give_{$this->config['id']}_get_recipients", |
| 388 |
give_check_variable( |
| 389 |
$this->recipient_email, |
| 390 |
'empty', |
| 391 |
Give()->emails->get_from_address() |
| 392 |
), |
| 393 |
$this, |
| 394 |
$form_id |
| 395 |
); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Get notification status. |
| 400 |
* |
| 401 |
* @since 2.0 |
| 402 |
* @access public |
| 403 |
* |
| 404 |
* @param int $form_id |
| 405 |
* |
| 406 |
* @return bool |
| 407 |
*/ |
| 408 |
public function get_notification_status( $form_id = null ) { |
| 409 |
$notification_status = empty( $form_id ) |
| 410 |
? give_get_option( "{$this->config['id']}_notification", $this->config['notification_status'] ) |
| 411 |
: give_get_meta( $form_id, Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'notification', true, 'global' ); |
| 412 |
|
| 413 |
/** |
| 414 |
* Filter the notification status. |
| 415 |
* |
| 416 |
* @since 1.8 |
| 417 |
*/ |
| 418 |
return apply_filters( |
| 419 |
"give_{$this->config['id']}_get_notification_status", |
| 420 |
$notification_status, |
| 421 |
$this, |
| 422 |
$form_id |
| 423 |
); |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Get email subject. |
| 428 |
* |
| 429 |
* @since 2.0 |
| 430 |
* @access public |
| 431 |
* |
| 432 |
* @param int $form_id |
| 433 |
* |
| 434 |
* @return string |
| 435 |
*/ |
| 436 |
function get_email_subject( $form_id = null ) { |
| 437 |
$subject = wp_strip_all_tags( |
| 438 |
Give_Email_Notification_Util::get_value( |
| 439 |
$this, |
| 440 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject', |
| 441 |
$form_id, |
| 442 |
$this->config['default_email_subject'] |
| 443 |
) |
| 444 |
); |
| 445 |
|
| 446 |
/** |
| 447 |
* Filter the subject. |
| 448 |
* |
| 449 |
* @since 2.0 |
| 450 |
*/ |
| 451 |
return apply_filters( |
| 452 |
"give_{$this->config['id']}_get_email_subject", |
| 453 |
$subject, |
| 454 |
$this, |
| 455 |
$form_id |
| 456 |
); |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Get email message. |
| 461 |
* |
| 462 |
* @since 2.0 |
| 463 |
* @access public |
| 464 |
* |
| 465 |
* @param int $form_id |
| 466 |
* |
| 467 |
* @return string |
| 468 |
*/ |
| 469 |
public function get_email_message( $form_id = null ) { |
| 470 |
$message = Give_Email_Notification_Util::get_value( |
| 471 |
$this, |
| 472 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message', |
| 473 |
$form_id, |
| 474 |
$this->config['default_email_message'] |
| 475 |
); |
| 476 |
|
| 477 |
/** |
| 478 |
* Filter the message. |
| 479 |
* |
| 480 |
* @since 2.0 |
| 481 |
*/ |
| 482 |
return apply_filters( |
| 483 |
"give_{$this->config['id']}_get_email_message", |
| 484 |
$message, |
| 485 |
$this, |
| 486 |
$form_id |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Get email header. |
| 492 |
* |
| 493 |
* @param int $form_id The Form ID. |
| 494 |
* |
| 495 |
* @since 2.1.3 |
| 496 |
* |
| 497 |
* @return string |
| 498 |
*/ |
| 499 |
public function get_email_header( $form_id = null ) { |
| 500 |
$header = Give_Email_Notification_Util::get_value( |
| 501 |
$this, |
| 502 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_header', |
| 503 |
$form_id, |
| 504 |
$this->config['default_email_header'] |
| 505 |
); |
| 506 |
|
| 507 |
/** |
| 508 |
* Filter the header. |
| 509 |
* |
| 510 |
* @since 2.1.3 |
| 511 |
*/ |
| 512 |
return apply_filters( |
| 513 |
"give_{$this->config['id']}_get_email_header", |
| 514 |
$header, |
| 515 |
$this, |
| 516 |
$form_id |
| 517 |
); |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Get email content type. |
| 522 |
* |
| 523 |
* @since 2.0 |
| 524 |
* @access public |
| 525 |
* |
| 526 |
* @param $form_id |
| 527 |
* |
| 528 |
* @return string |
| 529 |
*/ |
| 530 |
public function get_email_content_type( $form_id ) { |
| 531 |
$content_type = Give_Email_Notification_Util::get_value( |
| 532 |
$this, |
| 533 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_content_type', |
| 534 |
$form_id, |
| 535 |
$this->config['content_type'] |
| 536 |
); |
| 537 |
|
| 538 |
/** |
| 539 |
* Filter the email content type. |
| 540 |
* |
| 541 |
* @since 2.0 |
| 542 |
*/ |
| 543 |
return apply_filters( |
| 544 |
"give_{$this->config['id']}_get_email_content_type", |
| 545 |
$content_type, |
| 546 |
$this, |
| 547 |
$form_id |
| 548 |
); |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Get email template. |
| 553 |
* |
| 554 |
* @since 2.0 |
| 555 |
* @access public |
| 556 |
* |
| 557 |
* @param $form_id |
| 558 |
* |
| 559 |
* @return string |
| 560 |
*/ |
| 561 |
public function get_email_template( $form_id ) { |
| 562 |
$email_template = give_get_meta( $form_id, '_give_email_template', true ); |
| 563 |
$email_template = Give_Email_Notification_Util::get_value( |
| 564 |
$this, |
| 565 |
Give_Email_Setting_Field::get_prefix( $this, $form_id ) .'email_template', |
| 566 |
$form_id, |
| 567 |
! empty( $email_template ) && Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ? |
| 568 |
$email_template : |
| 569 |
$this->config['email_template'] |
| 570 |
); |
| 571 |
|
| 572 |
/** |
| 573 |
* Filter the email template. |
| 574 |
* |
| 575 |
* @since 2.0 |
| 576 |
*/ |
| 577 |
return apply_filters( |
| 578 |
"give_{$this->config['id']}_get_email_template", |
| 579 |
$email_template, |
| 580 |
$this, |
| 581 |
$form_id |
| 582 |
); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
/** |
| 587 |
* Get allowed email tags for current email notification. |
| 588 |
* |
| 589 |
* @since 2.0 |
| 590 |
* @access private |
| 591 |
* |
| 592 |
* @param bool $formatted |
| 593 |
* |
| 594 |
* @return array |
| 595 |
*/ |
| 596 |
public function get_allowed_email_tags( $formatted = false ) { |
| 597 |
// Get all email tags. |
| 598 |
$email_tags = Give()->email_tags->get_tags(); |
| 599 |
|
| 600 |
// Skip if all email template tags context setup exit. |
| 601 |
if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) { |
| 602 |
$email_context = (array) $this->config['email_tag_context']; |
| 603 |
|
| 604 |
foreach ( $email_tags as $index => $email_tag ) { |
| 605 |
if ( in_array( $email_tag['context'], $email_context ) ) { |
| 606 |
continue; |
| 607 |
} |
| 608 |
|
| 609 |
unset( $email_tags[ $index ] ); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Disallow tags on Email Notifications which don't have a |
| 615 |
* recipient and if the tag's is_admin property is set to true. |
| 616 |
*/ |
| 617 |
if ( false === $this->config['has_recipient_field'] ) { |
| 618 |
foreach ( $email_tags as $index => $email_tag ) { |
| 619 |
if ( true === $email_tag['is_admin'] ) { |
| 620 |
unset( $email_tags[ $index ] ); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
if ( count( $email_tags ) && $formatted ) : ob_start() ?> |
| 626 |
<ul class="give-email-tags-wrap"> |
| 627 |
<?php foreach ( $email_tags as $email_tag ) : ?> |
| 628 |
<li class="give_<?php echo $email_tag['tag']; ?>_tag"> |
| 629 |
<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['desc']; ?> |
| 630 |
</li> |
| 631 |
<?php endforeach; ?> |
| 632 |
</ul> |
| 633 |
<?php |
| 634 |
$email_tags = ob_get_clean(); |
| 635 |
endif; |
| 636 |
|
| 637 |
return $email_tags; |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Get preview email recipients. |
| 642 |
* |
| 643 |
* @since 2.0 |
| 644 |
* @access public |
| 645 |
* |
| 646 |
* @param int $form_id |
| 647 |
* |
| 648 |
* @return array|string |
| 649 |
*/ |
| 650 |
public function get_preview_email_recipient( $form_id = null ) { |
| 651 |
$recipients = $this->get_recipient( $form_id ); |
| 652 |
|
| 653 |
/** |
| 654 |
* Filter the preview email recipients. |
| 655 |
* |
| 656 |
* @since 2.0 |
| 657 |
* |
| 658 |
* @param string|array $recipients List of recipients. |
| 659 |
* @param Give_Email_Notification $this |
| 660 |
*/ |
| 661 |
$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this, $form_id ); |
| 662 |
|
| 663 |
return $recipients; |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Get the recipient attachments. |
| 668 |
* |
| 669 |
* @since 2.0 |
| 670 |
* @access public |
| 671 |
* |
| 672 |
* @param int $form_id |
| 673 |
* |
| 674 |
* @return array |
| 675 |
*/ |
| 676 |
public function get_email_attachments( $form_id = null ) { |
| 677 |
/** |
| 678 |
* Filter the attachment. |
| 679 |
* |
| 680 |
* @since 2.0 |
| 681 |
*/ |
| 682 |
return apply_filters( "give_{$this->config['id']}_get_email_attachments", array(), $this, $form_id ); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
/** |
| 687 |
* Send preview email. |
| 688 |
* |
| 689 |
* @since 2.0 |
| 690 |
* @access public |
| 691 |
* |
| 692 |
* @param bool $send Flag to check if send email or not. |
| 693 |
* |
| 694 |
* @return bool |
| 695 |
*/ |
| 696 |
public function send_preview_email( $send = true ) { |
| 697 |
// Get form id |
| 698 |
$form_id = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null; |
| 699 |
|
| 700 |
// setup email data. |
| 701 |
$this->setup_email_data(); |
| 702 |
|
| 703 |
$attachments = $this->get_email_attachments(); |
| 704 |
$message = $this->preview_email_template_tags( $this->get_email_message( $form_id ) ); |
| 705 |
$subject = $this->preview_email_template_tags( $this->get_email_subject( $form_id ) ); |
| 706 |
$content_type = $this->get_email_content_type( $form_id ); |
| 707 |
|
| 708 |
// Setup email content type. |
| 709 |
Give()->emails->__set( 'content_type', $content_type ); |
| 710 |
Give()->emails->__set( 'html', true ); |
| 711 |
|
| 712 |
// Setup email template |
| 713 |
Give()->emails->__set( 'template', $this->get_email_template( $form_id ) ); |
| 714 |
|
| 715 |
// Set email header. |
| 716 |
Give()->emails->__set( 'heading', $this->preview_email_template_tags( $this->get_email_header( $form_id ) ) ); |
| 717 |
|
| 718 |
// Format plain content type email. |
| 719 |
if ( 'text/plain' === $content_type ) { |
| 720 |
Give()->emails->__set( 'html', false ); |
| 721 |
Give()->emails->__set( 'template', 'none' ); |
| 722 |
$message = strip_tags( $message ); |
| 723 |
} |
| 724 |
|
| 725 |
if ( Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ) { |
| 726 |
Give()->emails->form_id = $form_id; |
| 727 |
Give()->emails->from_name = give_get_meta( $form_id, '_give_from_name', true ); |
| 728 |
Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true ); |
| 729 |
} |
| 730 |
|
| 731 |
return $send |
| 732 |
? Give()->emails->send( $this->get_preview_email_recipient( $form_id ), $subject, $message, $attachments ) |
| 733 |
: false; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
/** |
| 738 |
* Send email notification. |
| 739 |
* |
| 740 |
* Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails): |
| 741 |
* 1. payment_id |
| 742 |
* 2. user_id |
| 743 |
* 3. form_id |
| 744 |
* 4. donor_id |
| 745 |
* 5. for third party email tags you can pass necessary param along above parameters other value replace by empty string. |
| 746 |
* |
| 747 |
* @since 2.0 |
| 748 |
* @access public |
| 749 |
* |
| 750 |
* @param array $email_tag_args Arguments which helps to decode email template tags. |
| 751 |
* |
| 752 |
* @return bool |
| 753 |
*/ |
| 754 |
public function send_email_notification( $email_tag_args = array() ) { |
| 755 |
/** |
| 756 |
* Fire the filter |
| 757 |
* |
| 758 |
* @since 2.2.3 |
| 759 |
*/ |
| 760 |
if ( apply_filters( 'give_is_stop_email_notification', false, $this ) ) { |
| 761 |
return false; |
| 762 |
} |
| 763 |
|
| 764 |
// Add email content type email tags. |
| 765 |
$email_tag_args['email_content_type'] = $this->config['content_type']; |
| 766 |
|
| 767 |
/** |
| 768 |
* Filter the email tag args |
| 769 |
* |
| 770 |
* @since 2.0 |
| 771 |
*/ |
| 772 |
$email_tag_args = apply_filters( "give_{$this->config['id']}_email_tag_args", $email_tag_args, $this ); |
| 773 |
|
| 774 |
// Get form id. |
| 775 |
$form_id = ! empty( $email_tag_args['form_id'] ) |
| 776 |
? absint( $email_tag_args['form_id'] ) |
| 777 |
: ( ! empty( $email_tag_args['payment_id'] ) ? give_get_payment_form_id( $email_tag_args['payment_id'] ) : null ); |
| 778 |
|
| 779 |
|
| 780 |
// Do not send email if notification is disable. |
| 781 |
if ( ! Give_Email_Notification_Util::is_email_notification_active( $this, $form_id ) ) { |
| 782 |
return false; |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* Fire action after before email send. |
| 787 |
* |
| 788 |
* @since 2.0 |
| 789 |
*/ |
| 790 |
do_action( "give_{$this->config['id']}_email_send_before", $this, $form_id ); |
| 791 |
|
| 792 |
$attachments = $this->get_email_attachments(); |
| 793 |
$message = give_do_email_tags( $this->get_email_message( $form_id ), $email_tag_args ); |
| 794 |
$subject = give_do_email_tags( $this->get_email_subject( $form_id ), $email_tag_args ); |
| 795 |
$content_type = $this->get_email_content_type( $form_id ); |
| 796 |
|
| 797 |
// Setup email content type. |
| 798 |
Give()->emails->__set( 'content_type', $content_type ); |
| 799 |
Give()->emails->__set( 'html', true ); |
| 800 |
|
| 801 |
// Set email template. |
| 802 |
Give()->emails->__set( 'template', $this->get_email_template( $form_id ) ); |
| 803 |
|
| 804 |
// Set email header. |
| 805 |
Give()->emails->__set( 'heading', give_do_email_tags( $this->get_email_header( $form_id ), $email_tag_args ) ); |
| 806 |
|
| 807 |
if ( 'text/plain' === $content_type ) { |
| 808 |
Give()->emails->__set( 'html', false ); |
| 809 |
Give()->emails->__set( 'template', 'none' ); |
| 810 |
$message = strip_tags( $message ); |
| 811 |
} |
| 812 |
|
| 813 |
if ( Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ) { |
| 814 |
Give()->emails->form_id = $form_id; |
| 815 |
Give()->emails->from_name = give_get_meta( $form_id, '_give_from_name', true ); |
| 816 |
Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true ); |
| 817 |
} |
| 818 |
|
| 819 |
// Send email. |
| 820 |
$email_status = Give()->emails->send( $this->get_recipient( $form_id ), $subject, $message, $attachments ); |
| 821 |
|
| 822 |
/** |
| 823 |
* Fire action after after email send. |
| 824 |
* |
| 825 |
* @since 2.0 |
| 826 |
*/ |
| 827 |
do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this, $form_id ); |
| 828 |
|
| 829 |
return $email_status; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
/** |
| 834 |
* Decode preview email template tags. |
| 835 |
* |
| 836 |
* @since 2.0 |
| 837 |
* |
| 838 |
* @param string $message |
| 839 |
* |
| 840 |
* @return string |
| 841 |
*/ |
| 842 |
public function preview_email_template_tags( $message ) { |
| 843 |
// Set Payment. |
| 844 |
$payment_id = give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'preview_id' ); |
| 845 |
$payment = $payment_id ? new Give_Payment( $payment_id ) : new stdClass(); |
| 846 |
|
| 847 |
// Set donor. |
| 848 |
$user_id = $payment_id |
| 849 |
? $payment->user_id |
| 850 |
: give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'user_id' ); |
| 851 |
$user_id = $user_id ? $user_id : wp_get_current_user()->ID; |
| 852 |
|
| 853 |
// Set receipt. |
| 854 |
$receipt_id = strtolower( md5( uniqid() ) ); |
| 855 |
|
| 856 |
$receipt_link_url = esc_url( add_query_arg( array( |
| 857 |
'payment_key' => $receipt_id, |
| 858 |
), give_get_history_page_uri() ) ); |
| 859 |
|
| 860 |
$receipt_link = sprintf( |
| 861 |
'<a href="%1$s">%2$s</a>', |
| 862 |
$receipt_link_url, |
| 863 |
esc_html__( 'View the receipt in your browser »', 'give' ) |
| 864 |
); |
| 865 |
|
| 866 |
// Set default values for tags. |
| 867 |
$this->config['preview_email_tags_values'] = wp_parse_args( |
| 868 |
$this->config['preview_email_tags_values'], |
| 869 |
array( |
| 870 |
'name' => give_email_tag_first_name( array( |
| 871 |
'payment_id' => $payment_id, |
| 872 |
'user_id' => $user_id, |
| 873 |
) ), |
| 874 |
'fullname' => give_email_tag_fullname( array( |
| 875 |
'payment_id' => $payment_id, |
| 876 |
'user_id' => $user_id, |
| 877 |
) ), |
| 878 |
'username' => give_email_tag_username( array( |
| 879 |
'payment_id' => $payment_id, |
| 880 |
'user_id' => $user_id, |
| 881 |
) ), |
| 882 |
'user_email' => give_email_tag_user_email( array( |
| 883 |
'payment_id' => $payment_id, |
| 884 |
'user_id' => $user_id, |
| 885 |
) ), |
| 886 |
'payment_total' => $payment_id ? give_email_tag_payment_total( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ), |
| 887 |
'amount' => $payment_id ? give_email_tag_amount( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ), |
| 888 |
'price' => $payment_id ? give_email_tag_price( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ), |
| 889 |
'payment_method' => $payment_id ? give_email_tag_payment_method( array( 'payment_id' => $payment_id ) ) : __( 'PayPal', 'give' ), |
| 890 |
'receipt_id' => $receipt_id, |
| 891 |
'payment_id' => $payment_id ? $payment_id : rand( 2000, 2050 ), |
| 892 |
'receipt_link_url' => $receipt_link_url, |
| 893 |
'receipt_link' => $receipt_link, |
| 894 |
'date' => $payment_id ? date( give_date_format(), strtotime( $payment->date ) ) : date( give_date_format(), current_time( 'timestamp' ) ), |
| 895 |
'donation' => $payment_id ? give_email_tag_donation( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title', 'give' ), |
| 896 |
'form_title' => $payment_id ? give_email_tag_form_title( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ), |
| 897 |
'sitename' => $payment_id ? give_email_tag_sitename( array( 'payment_id' => $payment_id ) ) : get_bloginfo( 'name' ), |
| 898 |
'billing_address' => $payment_id ? give_email_tag_billing_address( array( 'payment_id' => $payment_id ) ) : '', |
| 899 |
'email_access_link' => sprintf( |
| 900 |
'<a href="%1$s">%2$s</a>', |
| 901 |
add_query_arg( |
| 902 |
array( |
| 903 |
'give_nl' => uniqid(), |
| 904 |
), |
| 905 |
give_get_history_page_uri() |
| 906 |
), |
| 907 |
__( 'View your donation history »', 'give' ) |
| 908 |
), |
| 909 |
'reset_password_link' => $user_id ? give_email_tag_reset_password_link( array( 'user_id' => $user_id ), $payment_id ) : '', |
| 910 |
'site_url' => sprintf( |
| 911 |
'<a href="%1$s">%2$s</a>', |
| 912 |
get_bloginfo( 'url' ), |
| 913 |
get_bloginfo( 'url' ) |
| 914 |
), |
| 915 |
'admin_email' => give_email_admin_email(), |
| 916 |
'offline_mailing_address' => give_email_offline_mailing_address(), |
| 917 |
'donor_comment' => $payment_id ? give_email_donor_comment( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donor Comment', 'give' ), |
| 918 |
) |
| 919 |
); |
| 920 |
|
| 921 |
// Decode tags. |
| 922 |
foreach ( $this->config['preview_email_tags_values'] as $preview_tag => $value ) { |
| 923 |
if ( isset( $this->config['preview_email_tags_values'][ $preview_tag ] ) ) { |
| 924 |
$message = str_replace( "{{$preview_tag}}", $this->config['preview_email_tags_values'][ $preview_tag ], $message ); |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
return apply_filters( 'give_email_preview_template_tags', $message ); |
| 929 |
} |
| 930 |
|
| 931 |
/** |
| 932 |
* Setup email data |
| 933 |
* |
| 934 |
* @since 2.0 |
| 935 |
*/ |
| 936 |
public function setup_email_data() { |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
/** |
| 941 |
* Validate email form setting |
| 942 |
* |
| 943 |
* Note: internal use only |
| 944 |
* |
| 945 |
* @since 2.0 |
| 946 |
* @access public |
| 947 |
* |
| 948 |
* @param $form_meta_key |
| 949 |
* @param $form_meta_value |
| 950 |
* @param $post_id |
| 951 |
* |
| 952 |
*/ |
| 953 |
public function validate_form_recipient_field_value( $form_meta_key, $form_meta_value, $post_id ) { |
| 954 |
// Get valid emails. |
| 955 |
$new_form_meta_value = array_filter( $form_meta_value, function ( $value ) { |
| 956 |
return ! empty( $value['email'] ) && is_email( $value['email'] ); |
| 957 |
} ); |
| 958 |
|
| 959 |
// Remove duplicate emails from array. |
| 960 |
$email_arr = array(); |
| 961 |
foreach ( $new_form_meta_value as $index => $email ) { |
| 962 |
if( in_array( $email['email'], $email_arr ) ) { |
| 963 |
unset( $new_form_meta_value[$index] ); |
| 964 |
continue; |
| 965 |
} |
| 966 |
|
| 967 |
$email_arr[] = $email['email']; |
| 968 |
} |
| 969 |
|
| 970 |
$update = false; |
| 971 |
|
| 972 |
if ( empty( $new_form_meta_value ) ) { |
| 973 |
// Set default recipient. |
| 974 |
$form_meta_value = array( |
| 975 |
array( |
| 976 |
'email' => get_bloginfo( 'admin_email' ) |
| 977 |
), |
| 978 |
); |
| 979 |
|
| 980 |
$update = true; |
| 981 |
|
| 982 |
} elseif ( count( $new_form_meta_value ) !== count( $form_meta_value ) ) { |
| 983 |
// Filter recipient emails. |
| 984 |
$form_meta_value = $new_form_meta_value; |
| 985 |
|
| 986 |
$update = true; |
| 987 |
} |
| 988 |
|
| 989 |
if( $update ) { |
| 990 |
give_update_meta( $post_id, $form_meta_key, $form_meta_value ); |
| 991 |
} |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
endif; // End class_exists check |
| 996 |
|