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