Completed
Push — issues/611 ( d123e1...0564a6 )
by Ravinder
20:29
created

Give_Email_Notification::load()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 27 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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       1.9
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       1.9
26
	 */
27
	abstract class Give_Email_Notification {
28
		static private $singleton = array();
29
30
		/**
31
		 * @var     string $id The email's unique identifier.
32
		 */
33
		protected $id = '';
34
35
		/**
36
		 * @var     string $label Name of the email.
37
		 * @access  protected
38
		 * @since   1.9
39
		 */
40
		protected $label = '';
41
42
		/**
43
		 * @var     string $label Name of the email.
44
		 * @access  protected
45
		 * @since   1.9
46
		 */
47
		protected $description = '';
48
49
		/**
50
		 * @var     bool $has_preview Flag to check if email notification has preview setting field.
51
		 * @access  protected
52
		 * @since   1.9
53
		 */
54
		protected $has_preview = true;
55
56
		/**
57
		 * @var     bool $has_preview Flag to check if email notification has preview header.
58
		 * @access  protected
59
		 * @since   1.9
60
		 */
61
		protected $has_preview_header = true;
62
63
		/**
64
		 * @var     bool $preview_email_tags_values Default value to replace email template tags in preview email.
65
		 * @access  protected
66
		 * @since   1.9
67
		 */
68
		protected $preview_email_tags_values = true;
69
70
		/**
71
		 * @var     bool $has_recipient_field Flag to check if email notification has recipient setting field.
72
		 * @access  protected
73
		 * @since   1.9
74
		 */
75
		protected $has_recipient_field = false;
76
77
		/**
78
		 * @var     string $notification_status Flag to check if email notification enabled or not.
79
		 * @access  protected
80
		 * @since   1.9
81
		 */
82
		protected $notification_status = 'disabled';
83
84
		/**
85
		 * @var     string|array $email_tag_context List of template tags which we can add to email notification.
86
		 * @access  protected
87
		 * @since   1.9
88
		 */
89
		protected $email_tag_context = 'all';
90
91
		/**
92
		 * @var     string $recipient_email Donor email.
93
		 * @access  protected
94
		 * @since   1.9
95
		 */
96
		protected $recipient_email = '';
97
98
		/**
99
		 * @var     string $recipient_group_name Categories single or group of recipient.
100
		 * @access  protected
101
		 * @since   1.9
102
		 */
103
		protected $recipient_group_name = '';
104
105
		/**
106
		 * Setup email notification.
107
		 *
108
		 * @since 1.9
109
		 */
110
		public function init(){
111
112
		}
113
114
115
		/**
116
		 * Get instance.
117
		 *
118
		 * @since 1.9
119
		 * @access public
120
		 * @return Give_Email_Notification
121
		 */
122
		public static function get_instance() {
123
			$class = get_called_class();
124
			if ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) {
125
				self::$singleton[ $class ] = new $class();
126
			}
127
128
			return self::$singleton[ $class ];
129
		}
130
131
		/**
132
		 * Create a class instance.
133
		 *
134
		 * @access  public
135
		 * @since   1.9
136
		 */
137
		public function load() {
138
			// Set email preview header status.
139
			$this->has_preview_header = $this->has_preview && $this->has_preview_header ? true : false;
140
141
			// setup filters.
142
			$this->setup_filters();
143
		}
144
145
146
		/**
147
		 * Set key value.
148
		 *
149
		 * @since  1.9
150
		 * @access public
151
		 *
152
		 * @param $key
153
		 * @param $value
154
		 */
155
		public function __set( $key, $value ) {
156
			$this->$key = $value;
157
		}
158
159
160
		/**
161
		 * Setup filters.
162
		 *
163
		 * @since  1.9
164
		 * @access public
165
		 */
166
		private function setup_filters() {
167
			// Apply filter only for current email notification section.
168
			if ( give_get_current_setting_section() === $this->id ) {
169
				// Initialize email context for email notification.
170
				$this->email_tag_context = apply_filters(
171
					"give_{$this->id}_email_tag_context",
172
					$this->email_tag_context,
173
					$this
174
				);
175
			}
176
177
			// Setup setting fields.
178
			add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 );
179
		}
180
181
		/**
182
		 * Register email settings.
183
		 *
184
		 * @since  1.9
185
		 * @access public
186
		 *
187
		 * @param   array $settings
188
		 *
189
		 * @return  array
190
		 */
191
		public function add_setting_fields( $settings ) {
192
			if ( $this->id === give_get_current_setting_section() ) {
193
				$settings = Give_Email_Setting_Field::get_setting_fields( $this );
194
			}
195
196
			return $settings;
197
		}
198
199
200
		/**
201
		 * Get extra setting field.
202
		 *
203
		 * @since  1.9
204
		 * @access public
205
		 * @return array
206
		 */
207
		public function get_extra_setting_fields() {
208
			return array();
209
		}
210
211
		/**
212
		 * Get id.
213
		 *
214
		 * @since  1.9
215
		 * @access public
216
		 * @return string
217
		 */
218
		public function get_id() {
219
			return $this->id;
220
		}
221
222
		/**
223
		 * Get label.
224
		 *
225
		 * @since  1.9
226
		 * @access public
227
		 * @return string
228
		 */
229
		public function get_label() {
230
			return $this->label;
231
		}
232
233
		/**
234
		 * Get description.
235
		 *
236
		 * @since  1.9
237
		 * @access public
238
		 * @return string
239
		 */
240
		public function get_description() {
241
			return $this->description;
242
		}
243
244
		/**
245
		 * Get recipient(s).
246
		 *
247
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
248
		 *
249
		 * @since  1.9
250
		 * @access public
251
		 * @return string|array
252
		 */
253
		public function get_recipient() {
254
			$recipient = give_check_variable( $this->recipient_email, 'empty', Give()->emails->get_from_address() );
255
256
			if ( ! $recipient && $this->has_recipient_field ) {
257
				$recipient = give_get_option( "{$this->id}_recipient" );
258
			}
259
260
			/**
261
			 * Filter the recipients
262
			 *
263
			 * @since 1.9
264
			 */
265
			return apply_filters( "give_{$this->id}_get_recipients", $recipient, $this );
266
		}
267
268
		/**
269
		 * Get recipient(s) group name.
270
		 **
271
		 * @since  1.9
272
		 * @access public
273
		 * @return string|array
274
		 */
275
		public function get_recipient_group_name() {
276
			return $this->recipient_group_name;
277
		}
278
279
		/**
280
		 * Get notification status.
281
		 *
282
		 * @since  1.9
283
		 * @access public
284
		 * @return bool
285
		 */
286
		public function get_notification_status() {
287
288
			/**
289
			 * Filter the notification status.
290
			 *
291
			 * @since 1.8
292
			 */
293
			return apply_filters( "give_{$this->id}_get_notification_status", give_get_option( "{$this->id}_notification", $this->notification_status ), $this );
294
		}
295
296
		/**
297
		 * Get email subject.
298
		 *
299
		 * @since  1.9
300
		 * @access public
301
		 * @return string
302
		 */
303
		function get_email_subject() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
304
			$subject = wp_strip_all_tags( give_get_option( "{$this->id}_email_subject", $this->get_default_email_subject() ) );
305
306
			/**
307
			 * Filter the subject.
308
			 *
309
			 * @since 1.9
310
			 */
311
			return apply_filters( "give_{$this->id}_get_email_subject", $subject, $this );
312
		}
313
314
		/**
315
		 * Get email message.
316
		 *
317
		 * @since  1.9
318
		 * @access public
319
		 * @return string
320
		 */
321
		public function get_email_message() {
322
			$message = give_get_option( "{$this->id}_email_message", $this->get_default_email_message() );
323
324
			/**
325
			 * Filter the message.
326
			 *
327
			 * @since 1.9
328
			 */
329
			return apply_filters( "give_{$this->id}_get_email_message", $message, $this );
330
		}
331
332
333
		/**
334
		 * Get email message field description
335
		 *
336
		 * @since 1.9
337
		 * @acess public
338
		 * @return string
339
		 */
340
		public function get_email_message_field_description() {
341
			$desc = esc_html__( 'Enter the email message.', 'give' );
342
343
			if ( $email_tag_list = $this->get_emails_tags_list_html() ) {
344
				$desc = sprintf(
345
					esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
346
					$email_tag_list
347
				);
348
349
			}
350
351
			return $desc;
352
		}
353
354
		/**
355
		 * Get a formatted HTML list of all available email tags
356
		 *
357
		 * @since 1.0
358
		 *
359
		 * @return string
360
		 */
361
		function get_emails_tags_list_html() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
362
363
			// Get all email tags.
364
			$email_tags = $this->get_allowed_email_tags();
365
366
			ob_start();
367
			if ( count( $email_tags ) > 0 ) : ?>
368
				<div class="give-email-tags-wrap">
369
					<?php foreach ( $email_tags as $email_tag ) : ?>
370
						<span class="give_<?php echo $email_tag['tag']; ?>_tag">
371
					<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['description']; ?>
372
				</span>
373
					<?php endforeach; ?>
374
				</div>
375
			<?php endif;
376
377
			// Return the list.
378
			return ob_get_clean();
379
		}
380
381
382
		/**
383
		 * Get allowed email tags for current email notification.
384
		 *
385
		 * @since  1.9
386
		 * @access private
387
		 * @return array
388
		 */
389
		private function get_allowed_email_tags() {
390
			// Get all email tags.
391
			$email_tags = Give()->email_tags->get_tags();
392
393
			// Skip if all email template tags context setup exit.
394
			if ( $this->email_tag_context && 'all' !== $this->email_tag_context ) {
395
				if ( is_array( $this->email_tag_context ) ) {
396
					foreach ( $email_tags as $index => $email_tag ) {
397
						if ( in_array( $email_tag['context'], $this->email_tag_context ) ) {
398
							continue;
399
						}
400
401
						unset( $email_tags[ $index ] );
402
					}
403
404
				} else {
405
					foreach ( $email_tags as $index => $email_tag ) {
406
						if ( $this->email_tag_context === $email_tag['context'] ) {
407
							continue;
408
						}
409
410
						unset( $email_tags[ $index ] );
411
					}
412
				}
413
			}
414
415
			return $email_tags;
416
		}
417
418
		/**
419
		 * Get default email subject.
420
		 *
421
		 * @since  1.9
422
		 * @access public
423
		 * @return string
424
		 */
425
		function get_default_email_subject() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
426
			return apply_filters( "give_{$this->id}give_get_default_email_subject", '', $this );
427
		}
428
429
		/**
430
		 * Get default email message.
431
		 *
432
		 * @since  1.9
433
		 * @access public
434
		 *
435
		 * @return string
436
		 */
437
		function get_default_email_message() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
438
			return apply_filters( "give_{$this->id}give_get_default_email_message", '', $this );
439
		}
440
441
442
		/**
443
		 * Get preview email recipients.
444
		 *
445
		 * @since  1.9
446
		 * @access public
447
		 * @return array|string
448
		 */
449
		public function get_preview_email_recipient() {
450
			$recipients = $this->get_recipient();
451
452
			/**
453
			 * Filter the preview email recipients.
454
			 *
455
			 * @since 1.9
456
			 *
457
			 * @param string|array            $recipients List of recipients.
458
			 * @param Give_Email_Notification $this
459
			 */
460
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this );
461
462
			return $recipients;
463
		}
464
465
		/**
466
		 * Get the recipient attachments.
467
		 *
468
		 * @since  1.9
469
		 * @access public
470
		 * @return array
471
		 */
472
		public function get_email_attachments() {
473
			/**
474
			 * Filter the attachment.
475
			 *
476
			 * @since 1.9
477
			 */
478
			return apply_filters( "give_{$this->id}_get_email_attachments", array(), $this );
479
		}
480
481
482
		/**
483
		 * Get email content type
484
		 *
485
		 * @since  1.9
486
		 * @access public
487
		 * @return string
488
		 */
489
		public function get_email_type() {
490
			return Give()->emails->get_content_type();
491
		}
492
493
		/**
494
		 * Check email active or not.
495
		 *
496
		 * @since  1.9
497
		 * @access public
498
		 * @return string
499
		 */
500
		public function is_email_notification_active() {
501
			return give_is_setting_enabled( $this->get_notification_status() );
502
		}
503
504
		/**
505
		 * Check email preview header active or not.
506
		 *
507
		 * @since  1.9
508
		 * @access public
509
		 * @return bool
510
		 */
511
		public function is_email_preview() {
512
			return $this->has_preview;
513
		}
514
515
		/**
516
		 * Check email preview header active or not.
517
		 *
518
		 * @since  1.9
519
		 * @access public
520
		 * @return bool
521
		 */
522
		public function is_email_preview_has_header() {
523
			return $this->has_preview_header;
524
		}
525
526
		/**
527
		 * Check if notification has recipient field or not.
528
		 *
529
		 * @since  1.9
530
		 * @access public
531
		 * @return bool
532
		 */
533
		public function has_recipient_field() {
534
			return $this->has_recipient_field;
535
		}
536
537
		/**
538
		 * Check if notification has preview field or not.
539
		 *
540
		 * @since  1.9
541
		 * @access public
542
		 * @return bool
543
		 */
544
		public function has_preview() {
545
			return $this->has_preview;
546
		}
547
548
		/**
549
		 * Send preview email.
550
		 *
551
		 * @since  1.9
552
		 * @access public
553
		 */
554
		public function send_preview_email() {
555
			// setup email data.
556
			$this->setup_email_data();
557
558
			$attachments = $this->get_email_attachments();
559
			$message     = $this->preview_email_template_tags( $this->get_email_message() );
560
			$subject     = $this->preview_email_template_tags( $this->get_email_subject() );
561
562
			Give()->emails->send( $this->get_preview_email_recipient(), $subject, $message, $attachments );
563
		}
564
565
		/**
566
		 * Send email notification
567
		 *
568
		 * @since  1.9
569
		 * @access public
570
		 *
571
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
572
		 */
573
		public function send_email_notification( $email_tag_args = array() ) {
574
			// Do not send email if notification is disable.
575
			if ( ! give_is_setting_enabled( $this->get_notification_status() ) ) {
576
				return;
577
			}
578
579
			/**
580
			 * Fire action after before email send.
581
			 *
582
			 * @since 1.9
583
			 */
584
			do_action( "give_{$this->id}_email_send_before", $this );
585
586
587
			$attachments = $this->get_email_attachments();
588
			$message     = give_do_email_tags( $this->get_email_message(), $email_tag_args );
589
			$subject     = give_do_email_tags( $this->get_email_subject(), $email_tag_args );
590
591
			// Send email.
592
			$email_status = Give()->emails->send( $this->get_recipient(), $subject, $message, $attachments );
593
594
			/**
595
			 * Fire action after after email send.
596
			 *
597
			 * @since 1.9
598
			 */
599
			do_action( "give_{$this->id}_email_send_after", $email_status, $this );
600
		}
601
602
603
		/**
604
		 * Decode preview email template tags.
605
		 *
606
		 * @since 1.9
607
		 *
608
		 * @param string $message
609
		 *
610
		 * @return string
611
		 */
612
		public function preview_email_template_tags( $message ) {
613
			$user       = wp_get_current_user();
614
			$receipt_id = strtolower( md5( uniqid() ) );
615
616
			$receipt_link_url = esc_url( add_query_arg( array(
617
				'payment_key' => $receipt_id,
618
				'give_action' => 'view_receipt',
619
			), home_url() ) );
620
621
			$receipt_link = sprintf(
622
				'<a href="%1$s">%2$s</a>',
623
				$receipt_link_url,
624
				esc_html__( 'View the receipt in your browser &raquo;', 'give' )
625
			);
626
627
628
			$this->preview_email_tags_values = wp_parse_args(
629
				$this->preview_email_tags_values,
630
				array(
631
					'payment_total'    => give_currency_filter( give_format_amount( 10.50 ) ),
632
					'payment_method'   => 'Paypal',
633
					'receipt_id'       => $receipt_id,
634
					'payment_id'       => rand( 2000, 2050 ),
635
					'receipt_link_url' => $receipt_link_url,
636
					'receipt_link'     => $receipt_link,
637
					'user'             => $user,
638
					'date'             => date( give_date_format(), current_time( 'timestamp' ) ),
639
					'donation'         => esc_html__( 'Sample Donation Form Title', 'give' ),
640
					'form_title'       => esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
641
					'sitename'         => get_bloginfo( 'name' ),
642
					'pdf_receipt'      => '<a href="#">Download Receipt</a>',
643
					'billing_address'  => '',
644
				)
645
			);
646
647
648
			$message = str_replace( '{name}', $this->preview_email_tags_values['user']->display_name, $message );
649
			$message = str_replace( '{fullname}', $this->preview_email_tags_values['user']->display_name, $message );
650
			$message = str_replace( '{username}', $this->preview_email_tags_values['user']->user_login, $message );
651
			$message = str_replace( '{user_email}', $this->preview_email_tags_values['user']->user_email, $message );
652
			$message = str_replace( '{date}', $this->preview_email_tags_values['date'], $message );
653
			$message = str_replace( '{amount}', $this->preview_email_tags_values['payment_total'], $message );
654
			$message = str_replace( '{price}', $this->preview_email_tags_values['payment_total'], $message );
655
			$message = str_replace( '{payment_total}', $this->preview_email_tags_values['payment_total'], $message );
656
			$message = str_replace( '{donation}', $this->preview_email_tags_values['donation'], $message );
657
			$message = str_replace( '{form_title}', $this->preview_email_tags_values['form_title'], $message );
658
			$message = str_replace( '{receipt_id}', $this->preview_email_tags_values['receipt_id'], $message );
659
			$message = str_replace( '{payment_method}', $this->preview_email_tags_values['payment_method'], $message );
660
			$message = str_replace( '{sitename}', $this->preview_email_tags_values['sitename'], $message );
661
			$message = str_replace( '{payment_id}', $this->preview_email_tags_values['payment_id'], $message );
662
			$message = str_replace( '{receipt_link}', $this->preview_email_tags_values['receipt_link'], $message );
663
			$message = str_replace( '{receipt_link_url}', $this->preview_email_tags_values['receipt_link_url'], $message );
664
			$message = str_replace( '{pdf_receipt}', $this->preview_email_tags_values['pdf_receipt'], $message );
665
666
			return apply_filters( 'give_email_preview_template_tags', $message );
667
		}
668
669
		/**
670
		 * Setup email data
671
		 *
672
		 * @since 1.9
673
		 */
674
		public function setup_email_data() {
675
		}
676
	}
677
678
endif; // End class_exists check
679