Completed
Push — issues/611 ( 0c24f9...7d2e61 )
by Ravinder
20:50
created

Give_Email_Notification::get_email_message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 20
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       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
			'has_preview'                  => true,
54
			'has_preview_header'           => true,
55
			'preview_email_tags_values'    => array(),
56
			'email_tag_context'            => 'all',
57
			'form_metabox_setting'         => true,
58
			'content_type'                 => '',
59
			'default_email_subject'        => '',
60
			'default_email_message'        => '',
61
		);
62
63
		/**
64
		 * @var     string $recipient_email Donor email.
65
		 * @access  protected
66
		 * @since   2.0
67
		 */
68
		protected $recipient_email = '';
69
70
71
		/**
72
		 * Setup email notification.
73
		 *
74
		 * @since 2.0
75
		 */
76
		public function init() {
77
78
		}
79
80
81
		/**
82
		 * Get instance.
83
		 *
84
		 * @since  2.0
85
		 * @access public
86
		 * @return Give_Email_Notification
87
		 */
88
		public static function get_instance() {
89
			$class = get_called_class();
90
			if ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) {
91
				self::$singleton[ $class ] = new $class();
92
			}
93
94
			return self::$singleton[ $class ];
95
		}
96
97
		/**
98
		 * Setup action and filters.
99
		 *
100
		 * @access  public
101
		 * @since   2.0
102
		 *
103
		 * @param array $config
104
		 */
105
		public function load( $config ) {
106
			// Set notification configuration.
107
			$this->config = wp_parse_args( $config, $this->config );
108
109
			// Set email preview header status.
110
			$this->config['has_preview_header'] = $this->config['has_preview'] && $this->config['has_preview_header'] ? true : false;
111
112
			// Set email content type
113
			$this->config['content_type'] = empty( $this->config['content_type'] ) || ! in_array( $this->config['content_type'], array( 'text/html', 'text/plain', ) )
114
				? Give()->emails->get_content_type()
115
				: $this->config['content_type'];
116
			$this->config['content_type'] = give_get_option( "{$this->config['id']}_email_content_type", $this->config['content_type'] );
117
118
			/**
119
			 *  Filter the notification config.
120
			 *
121
			 * @since 2.0
122
			 *
123
			 * @param                         array                   Give_Email_Notification::config
124
			 * @param Give_Email_Notification $this
125
			 */
126
			$this->config = apply_filters( 'give_email_api_notification_config', $this->config, $this );
127
128
			// Setup filters.
129
			$this->setup_filters();
130
		}
131
132
133
		/**
134
		 * Setup filters.
135
		 *
136
		 * @since  2.0
137
		 * @access public
138
		 */
139
		private function setup_filters() {
140
			// Apply filter only for current email notification section.
141
			if ( give_get_current_setting_section() === $this->config['id'] ) {
142
				// Initialize email context for email notification.
143
				$this->config['email_tag_context'] = apply_filters(
144
					"give_{$this->config['id']}_email_tag_context",
145
					$this->config['email_tag_context'],
146
					$this
147
				);
148
			}
149
150
			// Setup setting fields.
151
			add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 );
152
153
			if ( $this->config['form_metabox_setting'] ) {
154
				add_filter(
155
					'give_email_notification_options_metabox_fields',
156
					array( $this, 'add_metabox_setting_field' ),
157
					10,
158
					2
159
				);
160
			}
161
162
			/**
163
			 * Filter the default email subject.
164
			 *
165
			 * @since 2.0
166
			 */
167
			$this->config['default_email_subject'] = apply_filters(
168
				"give_{$this->config['id']}_get_default_email_subject",
169
				$this->config['default_email_subject'],
170
				$this
171
			);
172
173
			/**
174
			 * Filter the default email message.
175
			 *
176
			 * @since 2.0
177
			 */
178
			$this->config['default_email_message'] = apply_filters(
179
				"give_{$this->config['id']}_get_default_email_message",
180
				$this->config['default_email_message'],
181
				$this
182
			);
183
		}
184
185
		/**
186
		 * Add sections.
187
		 *
188
		 * @since 2.0
189
		 *
190
		 * @param array $sections
191
		 *
192
		 * @return array
193
		 */
194
		public function add_section( $sections ) {
195
			$sections[ $this->config['id'] ] = $this->config['label'];
196
197
			return $sections;
198
		}
199
200
		/**
201
		 * Add sections.
202
		 *
203
		 * @since 2.0
204
		 *
205
		 * @param bool $hide_section
206
		 *
207
		 * @return bool
208
		 */
209
		public function hide_section( $hide_section ) {
0 ignored issues
show
Unused Code introduced by
The parameter $hide_section 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...
210
			$hide_section = true;
211
212
			return $hide_section;
213
		}
214
215
		/**
216
		 * Register email settings.
217
		 *
218
		 * @since  2.0
219
		 * @access public
220
		 *
221
		 * @param   array $settings
222
		 *
223
		 * @return  array
224
		 */
225
		public function add_setting_fields( $settings ) {
226
			if ( $this->config['id'] === give_get_current_setting_section() ) {
227
				$settings = $this->get_setting_fields();
228
			}
229
230
			return $settings;
231
		}
232
233
234
		/**
235
		 * Get setting fields
236
		 *
237
		 * @since  2.0
238
		 * @access public
239
		 *
240
		 * @param int $form_id
241
		 *
242
		 * @return array
243
		 */
244
		public function get_setting_fields( $form_id = null ) {
245
			return Give_Email_Setting_Field::get_setting_fields( $this, $form_id );
246
		}
247
248
249
		/**
250
		 * Register email settings to form metabox.
251
		 *
252
		 * @since  2.0
253
		 * @access public
254
		 *
255
		 * @param array $settings
256
		 * @param int   $form_id
257
		 *
258
		 * @return array
259
		 */
260
		public function add_metabox_setting_field( $settings, $form_id ) {
261
262
			$settings[] = array(
263
				'id'     => $this->config['id'],
264
				'title'  => $this->config['label'],
265
				'fields' => $this->get_setting_fields( $form_id ),
266
			);
267
268
			return $settings;
269
		}
270
271
272
		/**
273
		 * Get extra setting field.
274
		 *
275
		 * @since  2.0
276
		 * @access public
277
		 *
278
		 * @param int $form_id
279
		 *
280
		 * @return array
281
		 */
282
		public function get_extra_setting_fields( $form_id = null ) {
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...
283
			return array();
284
		}
285
286
287
		/**
288
		 * Get recipient(s).
289
		 *
290
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
291
		 *
292
		 * @since  2.0
293
		 * @access public
294
		 *
295
		 * @param int $form_id
296
		 *
297
		 * @return string|array
298
		 */
299
		public function get_recipient( $form_id = null ) {
300
			if ( empty( $this->recipient_email ) && $this->config['has_recipient_field'] ) {
301
				$this->recipient_email = Give_Email_Notification_Util::get_value( $this, "{$this->config['id']}_recipient", $form_id );
302
			}
303
304
			/**
305
			 * Filter the recipients
306
			 *
307
			 * @since 2.0
308
			 */
309
			return apply_filters(
310
				"give_{$this->config['id']}_get_recipients",
311
				give_check_variable(
312
					$this->recipient_email,
313
					'empty',
314
					Give()->emails->get_from_address()
315
				),
316
				$this,
317
				$form_id
318
			);
319
		}
320
321
		/**
322
		 * Get notification status.
323
		 *
324
		 * @since  2.0
325
		 * @access public
326
		 *
327
		 * @param int $form_id
328
		 *
329
		 * @return bool
330
		 */
331
		public function get_notification_status( $form_id = null ) {
332
			$notification_status = empty( $form_id )
333
				? give_get_option( "{$this->config['id']}_notification", $this->config['notification_status'] )
334
				: get_post_meta( $form_id,"{$this->config['id']}_notification", true  );
335
336
			/**
337
			 * Filter the notification status.
338
			 *
339
			 * @since 1.8
340
			 */
341
			return apply_filters(
342
				"give_{$this->config['id']}_get_notification_status",
343
				$notification_status,
344
				$this,
345
				$form_id
346
			);
347
		}
348
349
		/**
350
		 * Get email subject.
351
		 *
352
		 * @since  2.0
353
		 * @access public
354
		 *
355
		 * @param int $form_id
356
		 *
357
		 * @return string
358
		 */
359
		function get_email_subject( $form_id = null ) {
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...
360
			$subject = wp_strip_all_tags(
361
				Give_Email_Notification_Util::get_value(
362
					$this,
363
					"{$this->config['id']}_email_subject",
364
					$form_id,
365
					$this->config['default_email_subject']
366
				)
367
			);
368
369
			/**
370
			 * Filter the subject.
371
			 *
372
			 * @since 2.0
373
			 */
374
			return apply_filters(
375
				"give_{$this->config['id']}_get_email_subject",
376
				$subject,
377
				$this,
378
				$form_id
379
			);
380
		}
381
382
		/**
383
		 * Get email message.
384
		 *
385
		 * @since  2.0
386
		 * @access public
387
		 *
388
		 * @param int $form_id
389
		 *
390
		 * @return string
391
		 */
392
		public function get_email_message( $form_id = null ) {
393
			$message = Give_Email_Notification_Util::get_value(
394
				$this,
395
				"{$this->config['id']}_email_message",
396
				$form_id,
397
				$this->config['default_email_message']
398
			);
399
400
			/**
401
			 * Filter the message.
402
			 *
403
			 * @since 2.0
404
			 */
405
			return apply_filters(
406
				"give_{$this->config['id']}_get_email_message",
407
				$message,
408
				$this,
409
				$form_id
410
			);
411
		}
412
413
414
		/**
415
		 * Get meial content type.
416
		 *
417
		 * @since 2.0
418
		 * @access public
419
		 *
420
		 * @param $form_id
421
		 *
422
		 * @return string
423
		 */
424
		public function get_email_content_type( $form_id ) {
425
			$content_type = Give_Email_Notification_Util::get_value(
426
				$this,
427
				"{$this->config['id']}_email_content_type",
428
				$form_id,
429
				$this->config['content_type']
430
			);
431
432
			/**
433
			 * Filter the email content type.
434
			 *
435
			 * @since 2.0
436
			 */
437
			return apply_filters(
438
				"give_{$this->config['id']}_get_email_message",
439
				$content_type,
440
				$this,
441
				$form_id
442
			);
443
		}
444
445
446
		/**
447
		 * Get allowed email tags for current email notification.
448
		 *
449
		 * @since  2.0
450
		 * @access private
451
		 *
452
		 * @param bool $formatted
453
		 *
454
		 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
455
		 */
456
		public function get_allowed_email_tags( $formatted = false ) {
457
			// Get all email tags.
458
			$email_tags = Give()->email_tags->get_tags();
459
460
			// Skip if all email template tags context setup exit.
461
			if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) {
462
				if ( is_array( $this->config['email_tag_context'] ) ) {
463
					foreach ( $email_tags as $index => $email_tag ) {
464
						if ( in_array( $email_tag['context'], $this->config['email_tag_context'] ) ) {
465
							continue;
466
						}
467
468
						unset( $email_tags[ $index ] );
469
					}
470
				} else {
471
					foreach ( $email_tags as $index => $email_tag ) {
472
						if ( $this->config['email_tag_context'] === $email_tag['context'] ) {
473
							continue;
474
						}
475
476
						unset( $email_tags[ $index ] );
477
					}
478
				}
479
			}
480
481
			if ( count( $email_tags ) && $formatted ) : ob_start() ?>
482
				<div class="give-email-tags-wrap">
483
					<?php foreach ( $email_tags as $email_tag ) : ?>
484
						<span class="give_<?php echo $email_tag['tag']; ?>_tag">
485
							<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['description']; ?>
486
						</span>
487
					<?php endforeach; ?>
488
				</div>
489
				<?php
490
				$email_tags = ob_get_clean();
491
			endif;
492
493
			return $email_tags;
494
		}
495
496
		/**
497
		 * Get preview email recipients.
498
		 *
499
		 * @since  2.0
500
		 * @access public
501
		 *
502
		 * @param int $form_id
503
		 * @return array|string
504
		 */
505
		public function get_preview_email_recipient( $form_id = null ) {
506
			$recipients = $this->get_recipient( $form_id );
507
508
			/**
509
			 * Filter the preview email recipients.
510
			 *
511
			 * @since 2.0
512
			 *
513
			 * @param string|array            $recipients List of recipients.
514
			 * @param Give_Email_Notification $this
515
			 */
516
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this, $form_id );
517
518
			return $recipients;
519
		}
520
521
		/**
522
		 * Get the recipient attachments.
523
		 *
524
		 * @since  2.0
525
		 * @access public
526
		 *
527
		 * @param int $form_id
528
		 * @return array
529
		 */
530
		public function get_email_attachments( $form_id = null ) {
531
			/**
532
			 * Filter the attachment.
533
			 *
534
			 * @since 2.0
535
			 */
536
			return apply_filters( "give_{$this->config['id']}_get_email_attachments", array(), $this, $form_id );
537
		}
538
539
540
		/**
541
		 * Send preview email.
542
		 *
543
		 * @since  2.0
544
		 * @access public
545
		 */
546
		public function send_preview_email() {
547
			// Get form id
548
			$form_id = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
549
550
			// setup email data.
551
			$this->setup_email_data();
552
553
			$attachments = $this->get_email_attachments();
554
			$message     = $this->preview_email_template_tags( $this->get_email_message( $form_id ) );
555
			$subject     = $this->preview_email_template_tags( $this->get_email_subject( $form_id ) );
556
557
			// Setup email content type.
558
			Give()->emails->__set( 'content_type', $this->config['content_type'] );
559
560
			// Format plain content type email.
561
			if ( 'text/plain' === $this->config['content_type'] ) {
562
				Give()->emails->__set( 'html', false );
563
				Give()->emails->__set( 'template', 'none' );
564
				$message = strip_tags( $message );
565
			}
566
567
			return Give()->emails->send( $this->get_preview_email_recipient( $form_id ), $subject, $message, $attachments );
568
		}
569
570
571
		/**
572
		 * Send email notification.
573
		 *
574
		 * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails):
575
		 * 	1. payment_id
576
		 * 	2. user_id
577
		 * 	3. form_id
578
		 * 	4. donor_id
579
		 * 	5. for third party email tags you can pass necessary param along above parameters other value replace by empty string.
580
		 *
581
		 * @since  2.0
582
		 * @access public
583
		 *
584
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
585
		 *
586
		 * @return bool
587
		 */
588
		public function send_email_notification( $email_tag_args = array() ) {
589
			// Add email content type email tags.
590
			$email_tag_args['email_content_type'] = $this->config['content_type'];
591
592
			/**
593
			 * Filter the email tag args
594
			 *
595
			 * @since 2.0
596
			 */
597
			$email_tag_args = apply_filters( "give_{$this->config['id']}_email_tag_args", $email_tag_args, $this );
598
599
			// Get form id.
600
			$form_id = ! empty( $email_tag_args['form_id'] )
601
				? absint( $email_tag_args['form_id'] )
602
				: ( ! empty( $email_tag_args['payment_id'] ) ? give_get_payment_form_id( $email_tag_args['payment_id'] ) : null );
603
604
605
			// Do not send email if notification is disable.
606
			if ( ! Give_Email_Notification_Util::is_email_notification_active( $this, $form_id ) ) {
607
				return false;
608
			}
609
610
			/**
611
			 * Fire action after before email send.
612
			 *
613
			 * @since 2.0
614
			 */
615
			do_action( "give_{$this->config['id']}_email_send_before", $this, $form_id );
616
617
			$attachments  = $this->get_email_attachments();
618
			$message      = give_do_email_tags( $this->get_email_message( $form_id ), $email_tag_args );
619
			$subject      = give_do_email_tags( $this->get_email_subject( $form_id ), $email_tag_args );
620
			$content_type = $this->get_email_content_type( $form_id );
621
622
			// Setup email content type.
623
			Give()->emails->__set( 'content_type', $content_type );
624
625
			if ( 'text/plain' === $content_type ) {
626
				Give()->emails->__set( 'html', false );
627
				Give()->emails->__set( 'template', 'none' );
628
				$message = strip_tags( $message );
629
			}
630
631
			// Send email.
632
			$email_status = Give()->emails->send( $this->get_recipient( $form_id ), $subject, $message, $attachments );
633
634
			/**
635
			 * Fire action after after email send.
636
			 *
637
			 * @since 2.0
638
			 */
639
			do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this, $form_id );
640
641
			return $email_status;
642
		}
643
644
645
		/**
646
		 * Decode preview email template tags.
647
		 *
648
		 * @since 2.0
649
		 *
650
		 * @param string $message
651
		 *
652
		 * @return string
653
		 */
654
		public function preview_email_template_tags( $message ) {
655
			$user_id    = give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'user_id' );
656
			$user       = ! empty( $user_id ) ? get_user_by( 'id', $user_id ) : wp_get_current_user();
657
			$receipt_id = strtolower( md5( uniqid() ) );
658
659
			$receipt_link_url = esc_url( add_query_arg( array(
660
				'payment_key' => $receipt_id,
661
				'give_action' => 'view_receipt',
662
			), home_url() ) );
663
664
			$receipt_link = sprintf(
665
				'<a href="%1$s">%2$s</a>',
666
				$receipt_link_url,
667
				esc_html__( 'View the receipt in your browser &raquo;', 'give' )
668
			);
669
670
			// Set default values for tags.
671
			$this->config['preview_email_tags_values'] = wp_parse_args(
672
				$this->config['preview_email_tags_values'],
673
				array(
674
					'name'              => $user->display_name,
675
					'fullname'          => $user->display_name,
676
					'username'          => $user->user_login,
677
					'user_email'        => $user->user_email,
678
					'payment_total'     => give_currency_filter( give_format_amount( 10.50 ) ),
679
					'amount'            => give_currency_filter( give_format_amount( 10.50 ) ),
680
					'price'             => give_currency_filter( give_format_amount( 10.50 ) ),
681
					'payment_method'    => 'Paypal',
682
					'receipt_id'        => $receipt_id,
683
					'payment_id'        => give_check_variable( give_clean( $_GET ), 'isset_empty', rand( 2000, 2050 ), 'preview_id' ),
684
					'receipt_link_url'  => $receipt_link_url,
685
					'receipt_link'      => $receipt_link,
686
					'date'              => date( give_date_format(), current_time( 'timestamp' ) ),
687
					'donation'          => esc_html__( 'Sample Donation Form Title', 'give' ),
688
					'form_title'        => esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
689
					'sitename'          => get_bloginfo( 'name' ),
690
					'pdf_receipt'       => '<a href="#">Download Receipt</a>',
691
					'billing_address'   => '',
692
					'email_access_link' => sprintf(
693
						'<a href="%1$s">%2$s</a>',
694
						add_query_arg(
695
							array(
696
								'give_nl' => uniqid(),
697
							),
698
							get_permalink( give_get_option( 'history_page' ) )
699
						),
700
						__( 'Access Donation Details &raquo;', 'give' )
701
					),
702
				)
703
			);
704
705
			// Decode tags.
706
			foreach ( $this->config['preview_email_tags_values'] as $preview_tag => $value ) {
707
				if ( isset( $this->config['preview_email_tags_values'][ $preview_tag ] ) ) {
708
					$message = str_replace( "{{$preview_tag}}", $this->config['preview_email_tags_values'][ $preview_tag ], $message );
709
				}
710
			}
711
712
			return apply_filters( 'give_email_preview_template_tags', $message );
713
		}
714
715
		/**
716
		 * Setup email data
717
		 *
718
		 * @since 2.0
719
		 */
720
		public function setup_email_data() {
721
		}
722
	}
723
724
endif; // End class_exists check
725