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