Completed
Push — issues/611 ( 19900c...46c90d )
by Ravinder
21:01
created

Give_Email_Notification::get_email_attachments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 8
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
		 * Setup action and filters.
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
			add_filter( 'give_email_notification_options_metabox_fields', array( $this, 'add_metabox_setting_field' ), 10, 2 );
180
		}
181
182
		/**
183
		 * Register email settings.
184
		 *
185
		 * @since  1.9
186
		 * @access public
187
		 *
188
		 * @param   array $settings
189
		 *
190
		 * @return  array
191
		 */
192
		public function add_setting_fields( $settings ) {
193
			if ( $this->id === give_get_current_setting_section() ) {
194
				$settings = $this->get_setting_fields();
195
			}
196
197
			return $settings;
198
		}
199
200
201
		/**
202
		 * Get setting fields
203
		 *
204
		 * @since  1.9
205
		 * @access public
206
		 *
207
		 * @param int $form_id
208
		 *
209
		 * @return array
210
		 */
211
		public function get_setting_fields( $form_id = 0 ) {
212
			return Give_Email_Setting_Field::get_setting_fields( $this, $form_id );
213
		}
214
215
216
		/**
217
		 * Register email settings to form metabox.
218
		 *
219
		 * @since  1.9
220
		 * @access public
221
		 *
222
		 * @param array $settings
223
		 * @param int   $post_id
224
		 *
225
		 * @return array
226
		 */
227
		public function add_metabox_setting_field( $settings, $post_id ) {
228
229
			$settings[] = array(
230
				'id'     => $this->id,
231
				'title'  => $this->label,
232
				'fields' => $this->get_setting_fields( $post_id )
233
			);
234
235
			return $settings;
236
		}
237
238
239
		/**
240
		 * Get extra setting field.
241
		 *
242
		 * @since  1.9
243
		 * @access public
244
		 *
245
		 * @param int $form_id
246
		 *
247
		 * @return array
248
		 */
249
		public function get_extra_setting_fields( $form_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
250
			return array();
251
		}
252
253
		/**
254
		 * Get id.
255
		 *
256
		 * @since  1.9
257
		 * @access public
258
		 * @return string
259
		 */
260
		public function get_id() {
261
			return $this->id;
262
		}
263
264
		/**
265
		 * Get label.
266
		 *
267
		 * @since  1.9
268
		 * @access public
269
		 * @return string
270
		 */
271
		public function get_label() {
272
			return $this->label;
273
		}
274
275
		/**
276
		 * Get description.
277
		 *
278
		 * @since  1.9
279
		 * @access public
280
		 * @return string
281
		 */
282
		public function get_description() {
283
			return $this->description;
284
		}
285
286
		/**
287
		 * Get recipient(s).
288
		 *
289
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
290
		 *
291
		 * @since  1.9
292
		 * @access public
293
		 * @return string|array
294
		 */
295
		public function get_recipient() {
296
			$recipient = $this->recipient_email;
297
298
			if ( ! $recipient && $this->has_recipient_field ) {
299
				$recipient = give_get_option( "{$this->id}_recipient" );
300
			}
301
302
			/**
303
			 * Filter the recipients
304
			 *
305
			 * @since 1.9
306
			 */
307
			return apply_filters( "give_{$this->id}_get_recipients", give_check_variable( $recipient, 'empty', Give()->emails->get_from_address() ), $this );
308
		}
309
310
		/**
311
		 * Get recipient(s) group name.
312
		 **
313
		 * @since  1.9
314
		 * @access public
315
		 * @return string|array
316
		 */
317
		public function get_recipient_group_name() {
318
			return $this->recipient_group_name;
319
		}
320
321
		/**
322
		 * Get notification status.
323
		 *
324
		 * @since  1.9
325
		 * @access public
326
		 * @return bool
327
		 */
328
		public function get_notification_status() {
329
330
			/**
331
			 * Filter the notification status.
332
			 *
333
			 * @since 1.8
334
			 */
335
			return apply_filters( "give_{$this->id}_get_notification_status", give_get_option( "{$this->id}_notification", $this->notification_status ), $this );
336
		}
337
338
		/**
339
		 * Get email subject.
340
		 *
341
		 * @since  1.9
342
		 * @access public
343
		 * @return string
344
		 */
345
		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...
346
			$subject = wp_strip_all_tags( give_get_option( "{$this->id}_email_subject", $this->get_default_email_subject() ) );
347
348
			/**
349
			 * Filter the subject.
350
			 *
351
			 * @since 1.9
352
			 */
353
			return apply_filters( "give_{$this->id}_get_email_subject", $subject, $this );
354
		}
355
356
		/**
357
		 * Get email message.
358
		 *
359
		 * @since  1.9
360
		 * @access public
361
		 * @return string
362
		 */
363
		public function get_email_message() {
364
			$message = give_get_option( "{$this->id}_email_message", $this->get_default_email_message() );
365
366
			/**
367
			 * Filter the message.
368
			 *
369
			 * @since 1.9
370
			 */
371
			return apply_filters( "give_{$this->id}_get_email_message", $message, $this );
372
		}
373
374
375
		/**
376
		 * Get email message field description
377
		 *
378
		 * @since 1.9
379
		 * @acess public
380
		 * @return string
381
		 */
382
		public function get_email_message_field_description() {
383
			$desc = esc_html__( 'Enter the email message.', 'give' );
384
385
			if ( $email_tag_list = $this->get_emails_tags_list_html() ) {
386
				$desc = sprintf(
387
					esc_html__( 'Enter the email that is sent to users after completing a successful donation. HTML is accepted. Available template tags: %s', 'give' ),
388
					$email_tag_list
389
				);
390
391
			}
392
393
			return $desc;
394
		}
395
396
		/**
397
		 * Get a formatted HTML list of all available email tags
398
		 *
399
		 * @since 1.0
400
		 *
401
		 * @return string
402
		 */
403
		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...
404
405
			// Get all email tags.
406
			$email_tags = $this->get_allowed_email_tags();
407
408
			ob_start();
409
			if ( count( $email_tags ) > 0 ) : ?>
410
				<div class="give-email-tags-wrap">
411
					<?php foreach ( $email_tags as $email_tag ) : ?>
412
						<span class="give_<?php echo $email_tag['tag']; ?>_tag">
413
					<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['description']; ?>
414
				</span>
415
					<?php endforeach; ?>
416
				</div>
417
			<?php endif;
418
419
			// Return the list.
420
			return ob_get_clean();
421
		}
422
423
424
		/**
425
		 * Get allowed email tags for current email notification.
426
		 *
427
		 * @since  1.9
428
		 * @access private
429
		 * @return array
430
		 */
431
		private function get_allowed_email_tags() {
432
			// Get all email tags.
433
			$email_tags = Give()->email_tags->get_tags();
434
435
			// Skip if all email template tags context setup exit.
436
			if ( $this->email_tag_context && 'all' !== $this->email_tag_context ) {
437
				if ( is_array( $this->email_tag_context ) ) {
438
					foreach ( $email_tags as $index => $email_tag ) {
439
						if ( in_array( $email_tag['context'], $this->email_tag_context ) ) {
440
							continue;
441
						}
442
443
						unset( $email_tags[ $index ] );
444
					}
445
446
				} else {
447
					foreach ( $email_tags as $index => $email_tag ) {
448
						if ( $this->email_tag_context === $email_tag['context'] ) {
449
							continue;
450
						}
451
452
						unset( $email_tags[ $index ] );
453
					}
454
				}
455
			}
456
457
			return $email_tags;
458
		}
459
460
		/**
461
		 * Get default email subject.
462
		 *
463
		 * @since  1.9
464
		 * @access public
465
		 * @return string
466
		 */
467
		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...
468
			return apply_filters( "give_{$this->id}give_get_default_email_subject", '', $this );
469
		}
470
471
		/**
472
		 * Get default email message.
473
		 *
474
		 * @since  1.9
475
		 * @access public
476
		 *
477
		 * @return string
478
		 */
479
		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...
480
			return apply_filters( "give_{$this->id}give_get_default_email_message", '', $this );
481
		}
482
483
484
		/**
485
		 * Get preview email recipients.
486
		 *
487
		 * @since  1.9
488
		 * @access public
489
		 * @return array|string
490
		 */
491
		public function get_preview_email_recipient() {
492
			$recipients = $this->get_recipient();
493
494
			/**
495
			 * Filter the preview email recipients.
496
			 *
497
			 * @since 1.9
498
			 *
499
			 * @param string|array            $recipients List of recipients.
500
			 * @param Give_Email_Notification $this
501
			 */
502
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this );
503
504
			return $recipients;
505
		}
506
507
		/**
508
		 * Get the recipient attachments.
509
		 *
510
		 * @since  1.9
511
		 * @access public
512
		 * @return array
513
		 */
514
		public function get_email_attachments() {
515
			/**
516
			 * Filter the attachment.
517
			 *
518
			 * @since 1.9
519
			 */
520
			return apply_filters( "give_{$this->id}_get_email_attachments", array(), $this );
521
		}
522
523
524
		/**
525
		 * Get email content type
526
		 *
527
		 * @since  1.9
528
		 * @access public
529
		 * @return string
530
		 */
531
		public function get_email_type() {
532
			return Give()->emails->get_content_type();
533
		}
534
535
		/**
536
		 * Check email active or not.
537
		 *
538
		 * @since  1.9
539
		 * @access public
540
		 * @return string
541
		 */
542
		public function is_email_notification_active() {
543
			return give_is_setting_enabled( $this->get_notification_status() );
544
		}
545
546
		/**
547
		 * Check email preview header active or not.
548
		 *
549
		 * @since  1.9
550
		 * @access public
551
		 * @return bool
552
		 */
553
		public function is_email_preview() {
554
			return $this->has_preview;
555
		}
556
557
		/**
558
		 * Check email preview header active or not.
559
		 *
560
		 * @since  1.9
561
		 * @access public
562
		 * @return bool
563
		 */
564
		public function is_email_preview_has_header() {
565
			return $this->has_preview_header;
566
		}
567
568
		/**
569
		 * Check if notification has recipient field or not.
570
		 *
571
		 * @since  1.9
572
		 * @access public
573
		 * @return bool
574
		 */
575
		public function has_recipient_field() {
576
			return $this->has_recipient_field;
577
		}
578
579
		/**
580
		 * Check if notification has preview field or not.
581
		 *
582
		 * @since  1.9
583
		 * @access public
584
		 * @return bool
585
		 */
586
		public function has_preview() {
587
			return $this->has_preview;
588
		}
589
590
		/**
591
		 * Send preview email.
592
		 *
593
		 * @since  1.9
594
		 * @access public
595
		 */
596
		public function send_preview_email() {
597
			// setup email data.
598
			$this->setup_email_data();
599
600
			$attachments = $this->get_email_attachments();
601
			$message     = $this->preview_email_template_tags( $this->get_email_message() );
602
			$subject     = $this->preview_email_template_tags( $this->get_email_subject() );
603
604
			Give()->emails->send( $this->get_preview_email_recipient(), $subject, $message, $attachments );
605
		}
606
607
		/**
608
		 * Send email notification
609
		 *
610
		 * @since  1.9
611
		 * @access public
612
		 *
613
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
614
		 * 
615
		 * @return bool
616
		 */
617
		public function send_email_notification( $email_tag_args = array() ) {
618
			// Do not send email if notification is disable.
619
			if ( ! give_is_setting_enabled( $this->get_notification_status() ) ) {
620
				return false;
621
			}
622
623
			/**
624
			 * Fire action after before email send.
625
			 *
626
			 * @since 1.9
627
			 */
628
			do_action( "give_{$this->id}_email_send_before", $this );
629
630
631
			$attachments = $this->get_email_attachments();
632
			$message     = give_do_email_tags( $this->get_email_message(), $email_tag_args );
633
			$subject     = give_do_email_tags( $this->get_email_subject(), $email_tag_args );
634
635
			// Send email.
636
			$email_status = Give()->emails->send( $this->get_recipient(), $subject, $message, $attachments );
637
638
			/**
639
			 * Fire action after after email send.
640
			 *
641
			 * @since 1.9
642
			 */
643
			do_action( "give_{$this->id}_email_send_after", $email_status, $this );
644
645
			return $email_status;
646
		}
647
648
649
		/**
650
		 * Decode preview email template tags.
651
		 *
652
		 * @since 1.9
653
		 *
654
		 * @param string $message
655
		 *
656
		 * @return string
657
		 */
658
		public function preview_email_template_tags( $message ) {
659
			$user_id    = give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'user_id' );
660
			$user       = ! empty( $user_id ) ? get_user_by( 'id', $user_id ) : wp_get_current_user();
661
			$receipt_id = strtolower( md5( uniqid() ) );
662
663
			$receipt_link_url = esc_url( add_query_arg( array(
664
				'payment_key' => $receipt_id,
665
				'give_action' => 'view_receipt',
666
			), home_url() ) );
667
668
			$receipt_link = sprintf(
669
				'<a href="%1$s">%2$s</a>',
670
				$receipt_link_url,
671
				esc_html__( 'View the receipt in your browser &raquo;', 'give' )
672
			);
673
674
675
			$this->preview_email_tags_values = wp_parse_args(
676
				$this->preview_email_tags_values,
677
				array(
678
					'payment_total'    => give_currency_filter( give_format_amount( 10.50 ) ),
679
					'payment_method'   => 'Paypal',
680
					'receipt_id'       => $receipt_id,
681
					'payment_id' => give_check_variable( give_clean( $_GET ), 'isset_empty', rand( 2000, 2050 ), 'preview_id' ),
682
					'receipt_link_url' => $receipt_link_url,
683
					'receipt_link'     => $receipt_link,
684
					'user'             => $user,
685
					'date'             => date( give_date_format(), current_time( 'timestamp' ) ),
686
					'donation'         => esc_html__( 'Sample Donation Form Title', 'give' ),
687
					'form_title'       => esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
688
					'sitename'         => get_bloginfo( 'name' ),
689
					'pdf_receipt'      => '<a href="#">Download Receipt</a>',
690
					'billing_address'  => '',
691
				)
692
			);
693
694
695
			$message = str_replace( '{name}', $this->preview_email_tags_values['user']->display_name, $message );
696
			$message = str_replace( '{fullname}', $this->preview_email_tags_values['user']->display_name, $message );
697
			$message = str_replace( '{username}', $this->preview_email_tags_values['user']->user_login, $message );
698
			$message = str_replace( '{user_email}', $this->preview_email_tags_values['user']->user_email, $message );
699
			$message = str_replace( '{date}', $this->preview_email_tags_values['date'], $message );
700
			$message = str_replace( '{amount}', $this->preview_email_tags_values['payment_total'], $message );
701
			$message = str_replace( '{price}', $this->preview_email_tags_values['payment_total'], $message );
702
			$message = str_replace( '{payment_total}', $this->preview_email_tags_values['payment_total'], $message );
703
			$message = str_replace( '{donation}', $this->preview_email_tags_values['donation'], $message );
704
			$message = str_replace( '{form_title}', $this->preview_email_tags_values['form_title'], $message );
705
			$message = str_replace( '{receipt_id}', $this->preview_email_tags_values['receipt_id'], $message );
706
			$message = str_replace( '{payment_method}', $this->preview_email_tags_values['payment_method'], $message );
707
			$message = str_replace( '{sitename}', $this->preview_email_tags_values['sitename'], $message );
708
			$message = str_replace( '{payment_id}', $this->preview_email_tags_values['payment_id'], $message );
709
			$message = str_replace( '{receipt_link}', $this->preview_email_tags_values['receipt_link'], $message );
710
			$message = str_replace( '{receipt_link_url}', $this->preview_email_tags_values['receipt_link_url'], $message );
711
			$message = str_replace( '{pdf_receipt}', $this->preview_email_tags_values['pdf_receipt'], $message );
712
713
			return apply_filters( 'give_email_preview_template_tags', $message );
714
		}
715
716
		/**
717
		 * Setup email data
718
		 *
719
		 * @since 1.9
720
		 */
721
		public function setup_email_data() {
722
		}
723
	}
724
725
endif; // End class_exists check
726