Completed
Push — issues/611 ( f0b8b3...915615 )
by Ravinder
19:46
created

Give_Email_Notification::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
		/* @var     string $recipient_email Donor email.
64
		 * @access  protected
65
		 * @since   2.0
66
		 */
67
		protected $recipient_email = '';
68
69
70
		/**
71
		 * Setup email notification.
72
		 *
73
		 * @since 2.0
74
		 */
75
		public function init() {
76
77
		}
78
79
80
		/**
81
		 * Get instance.
82
		 *
83
		 * @since  2.0
84
		 * @access public
85
		 * @return Give_Email_Notification
86
		 */
87
		public static function get_instance() {
88
			$class = get_called_class();
89
			if ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) {
90
				self::$singleton[ $class ] = new $class();
91
			}
92
93
			return self::$singleton[ $class ];
94
		}
95
96
		/**
97
		 * Setup action and filters.
98
		 *
99
		 * @access  public
100
		 * @since   2.0
101
		 *
102
		 * @param array $config
103
		 */
104
		public function load( $config ) {
105
			// Set notification configuration.
106
			$this->config = wp_parse_args( $config, $this->config );
107
108
			// Set email preview header status.
109
			$this->config['has_preview_header'] = $this->config['has_preview'] && $this->config['has_preview_header'] ? true : false;
110
111
			// Set email content type
112
			$this->config['content_type'] = empty( $this->config['content_type'] ) || ! in_array( $this->config['content_type'], array(
113
				'text/html',
114
				'text/plain',
115
			) )
116
				? 'text/html' // @todo: use Give()->emails->get_content_Type() get email content type.
117
				: $this->config['content_type'];
118
			$this->config['content_type'] = give_get_option( "{$this->config['id']}_email_content_type", $this->config['content_type'] );
119
120
			/**
121
			 *  Filter the notification config.
122
			 *
123
			 * @since 2.0
124
			 *
125
			 * @param                         array                   Give_Email_Notification::config
126
			 * @param Give_Email_Notification $this
127
			 */
128
			$this->config = apply_filters( 'give_email_api_notification_config', $this->config, $this );
129
130
			// Setup filters.
131
			$this->setup_filters();
132
		}
133
134
135
		/**
136
		 * Setup filters.
137
		 *
138
		 * @since  2.0
139
		 * @access public
140
		 */
141
		private function setup_filters() {
142
			// Apply filter only for current email notification section.
143
			if ( give_get_current_setting_section() === $this->config['id'] ) {
144
				// Initialize email context for email notification.
145
				$this->config['email_tag_context'] = apply_filters(
146
					"give_{$this->config['id']}_email_tag_context",
147
					$this->config['email_tag_context'],
148
					$this
149
				);
150
			}
151
152
			// Setup setting fields.
153
			add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 );
154
155
			if ( $this->config['form_metabox_setting'] ) {
156
				add_filter(
157
					'give_email_notification_options_metabox_fields',
158
					array( $this, 'add_metabox_setting_field' ),
159
					10,
160
					2
161
				);
162
			}
163
164
165
			/**
166
			 * Filter the default email subject.
167
			 *
168
			 * @since 2.0
169
			 */
170
			$this->config['default_email_subject'] = apply_filters(
171
				"give_{$this->config['id']}_get_default_email_subject", '',
172
				$this->config['default_email_subject'],
173
				$this
174
			);
175
176
			/**
177
			 * Filter the default email message.
178
			 *
179
			 * @since 2.0
180
			 */
181
			$this->config['default_email_message'] = apply_filters(
182
				"give_{$this->config['id']}_get_default_email_message", '',
183
				$this->config['default_email_message'],
184
				$this
185
			);
186
		}
187
188
		/**
189
		 * Add sections.
190
		 *
191
		 * @since 2.0
192
		 *
193
		 * @param array $sections
194
		 *
195
		 * @return array
196
		 */
197
		public function add_section( $sections ) {
198
			$sections[ $this->config['id'] ] = $this->config['label'];
199
200
			return $sections;
201
		}
202
203
		/**
204
		 * Add sections.
205
		 *
206
		 * @since 2.0
207
		 *
208
		 * @param bool $hide_section
209
		 *
210
		 * @return bool
211
		 */
212
		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...
213
			$hide_section = true;
214
215
			return $hide_section;
216
		}
217
218
		/**
219
		 * Register email settings.
220
		 *
221
		 * @since  2.0
222
		 * @access public
223
		 *
224
		 * @param   array $settings
225
		 *
226
		 * @return  array
227
		 */
228
		public function add_setting_fields( $settings ) {
229
			if ( $this->config['id'] === give_get_current_setting_section() ) {
230
				$settings = $this->get_setting_fields();
231
			}
232
233
			return $settings;
234
		}
235
236
237
		/**
238
		 * Get setting fields
239
		 *
240
		 * @since  2.0
241
		 * @access public
242
		 *
243
		 * @param int $form_id
244
		 *
245
		 * @return array
246
		 */
247
		public function get_setting_fields( $form_id = 0 ) {
248
			return Give_Email_Setting_Field::get_setting_fields( $this, $form_id );
249
		}
250
251
252
		/**
253
		 * Register email settings to form metabox.
254
		 *
255
		 * @since  2.0
256
		 * @access public
257
		 *
258
		 * @param array $settings
259
		 * @param int   $post_id
260
		 *
261
		 * @return array
262
		 */
263
		public function add_metabox_setting_field( $settings, $post_id ) {
264
265
			$settings[] = array(
266
				'id'     => $this->config['id'],
267
				'title'  => $this->config['label'],
268
				'fields' => $this->get_setting_fields( $post_id ),
269
			);
270
271
			return $settings;
272
		}
273
274
275
		/**
276
		 * Get extra setting field.
277
		 *
278
		 * @since  2.0
279
		 * @access public
280
		 *
281
		 * @param int $form_id
282
		 *
283
		 * @return array
284
		 */
285
		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...
286
			return array();
287
		}
288
289
290
		/**
291
		 * Get recipient(s).
292
		 *
293
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
294
		 *
295
		 * @since  2.0
296
		 * @access public
297
		 *
298
		 * @param int $form_id
299
		 *
300
		 * @return string|array
301
		 */
302
		public function get_recipient( $form_id = null ) {
303
			if ( empty( $this->recipient_email ) && $this->config['has_recipient_field'] ) {
304
				$this->recipient_email = Give_Email_Notification_Util::get_value( $this, "{$this->config['id']}_recipient", $form_id );
305
			}
306
307
			/**
308
			 * Filter the recipients
309
			 *
310
			 * @since 2.0
311
			 */
312
			return apply_filters(
313
				"give_{$this->config['id']}_get_recipients",
314
				give_check_variable(
315
					$this->recipient_email,
316
					'empty',
317
					Give()->emails->get_from_address()
318
				),
319
				$this
320
			);
321
		}
322
323
		/**
324
		 * Get notification status.
325
		 *
326
		 * @since  2.0
327
		 * @access public
328
		 *
329
		 * @param int $form_id
330
		 *
331
		 * @return bool
332
		 */
333
		public function get_notification_status( $form_id = 0 ) {
334
335
			/**
336
			 * Filter the notification status.
337
			 *
338
			 * @since 1.8
339
			 */
340
			return apply_filters(
341
				"give_{$this->config['id']}_get_notification_status",
342
				Give_Email_Notification_Util::get_value(
343
					$this,
344
					"{$this->config['id']}_notification",
345
					$form_id,
346
					$this->config['notification_status']
347
				),
348
				$this
349
			);
350
		}
351
352
		/**
353
		 * Get email subject.
354
		 *
355
		 * @since  2.0
356
		 * @access public
357
		 *
358
		 * @param int $form_id
359
		 *
360
		 * @return string
361
		 */
362
		function get_email_subject( $form_id = 0 ) {
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...
363
			$subject = wp_strip_all_tags(
364
				Give_Email_Notification_Util::get_value(
365
					$this,
366
					"{$this->config['id']}_email_subject",
367
					$form_id,
368
					$this->config['default_email_subject']
369
				)
370
			);
371
372
			/**
373
			 * Filter the subject.
374
			 *
375
			 * @since 2.0
376
			 */
377
			return apply_filters(
378
				"give_{$this->config['id']}_get_email_subject",
379
				$subject,
380
				$this
381
			);
382
		}
383
384
		/**
385
		 * Get email message.
386
		 *
387
		 * @since  2.0
388
		 * @access public
389
		 *
390
		 * @param int $form_id
391
		 *
392
		 * @return string
393
		 */
394
		public function get_email_message( $form_id = 0 ) {
395
			$message = Give_Email_Notification_Util::get_value(
396
				$this,
397
				"{$this->config['id']}_email_message",
398
				$form_id,
399
				$this->config['default_email_message']
400
			);
401
402
			/**
403
			 * Filter the message.
404
			 *
405
			 * @since 2.0
406
			 */
407
			return apply_filters(
408
				"give_{$this->config['id']}_get_email_message",
409
				$message,
410
				$this
411
			);
412
		}
413
414
415
		/**
416
		 * Get allowed email tags for current email notification.
417
		 *
418
		 * @since  2.0
419
		 * @access private
420
		 *
421
		 * @param bool $formatted
422
		 *
423
		 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array<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...
424
		 */
425
		public function get_allowed_email_tags( $formatted = false ) {
426
			// Get all email tags.
427
			$email_tags = Give()->email_tags->get_tags();
428
429
			// Skip if all email template tags context setup exit.
430
			if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) {
431
				if ( is_array( $this->config['email_tag_context'] ) ) {
432
					foreach ( $email_tags as $index => $email_tag ) {
433
						if ( in_array( $email_tag['context'], $this->config['email_tag_context'] ) ) {
434
							continue;
435
						}
436
437
						unset( $email_tags[ $index ] );
438
					}
439
				} else {
440
					foreach ( $email_tags as $index => $email_tag ) {
441
						if ( $this->config['email_tag_context'] === $email_tag['context'] ) {
442
							continue;
443
						}
444
445
						unset( $email_tags[ $index ] );
446
					}
447
				}
448
			}
449
450
			if ( count( $email_tags ) && $formatted ) : ob_start() ?>
451
				<div class="give-email-tags-wrap">
452
					<?php foreach ( $email_tags as $email_tag ) : ?>
453
						<span class="give_<?php echo $email_tag['tag']; ?>_tag">
454
							<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['description']; ?>
455
						</span>
456
					<?php endforeach; ?>
457
				</div>
458
				<?php
459
				$email_tags = ob_get_clean();
460
			endif;
461
462
			return $email_tags;
463
		}
464
465
		/**
466
		 * Get preview email recipients.
467
		 *
468
		 * @since  2.0
469
		 * @access public
470
		 * @return array|string
471
		 */
472
		public function get_preview_email_recipient() {
473
			$recipients = $this->get_recipient();
474
475
			/**
476
			 * Filter the preview email recipients.
477
			 *
478
			 * @since 2.0
479
			 *
480
			 * @param string|array            $recipients List of recipients.
481
			 * @param Give_Email_Notification $this
482
			 */
483
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this );
484
485
			return $recipients;
486
		}
487
488
		/**
489
		 * Get the recipient attachments.
490
		 *
491
		 * @since  2.0
492
		 * @access public
493
		 * @return array
494
		 */
495
		public function get_email_attachments() {
496
			/**
497
			 * Filter the attachment.
498
			 *
499
			 * @since 2.0
500
			 */
501
			return apply_filters( "give_{$this->config['id']}_get_email_attachments", array(), $this );
502
		}
503
504
505
		/**
506
		 * Send preview email.
507
		 *
508
		 * @since  2.0
509
		 * @access public
510
		 */
511
		public function send_preview_email() {
512
			/**
513
			 * Fire action after before email send.
514
			 *
515
			 * @since 2.0
516
			 */
517
			do_action( "give_{$this->config['id']}_email_send_before", $this );
518
519
			// setup email data.
520
			$this->setup_email_data();
521
522
			$attachments = $this->get_email_attachments();
523
			$message     = $this->preview_email_template_tags( $this->get_email_message() );
524
			$subject     = $this->preview_email_template_tags( $this->get_email_subject() );
525
526
			// Setup email content type.
527
			Give()->emails->__set( 'content_type', $this->config['content_type'] );
528
529
			if ( 'text/plain' === $this->config['content_type'] ) {
530
				Give()->emails->__set( 'html', false );
531
				Give()->emails->__set( 'template', 'none' );
532
				$message = strip_tags( $message );
533
			}
534
535
			$email_status = Give()->emails->send( $this->get_preview_email_recipient(), $subject, $message, $attachments );
536
537
			/**
538
			 * Fire action after after email send.
539
			 *
540
			 * @since 2.0
541
			 */
542
			do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this );
543
		}
544
545
		/**
546
		 * Send email notification
547
		 *
548
		 * @since  2.0
549
		 * @access public
550
		 *
551
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
552
		 *
553
		 * @return bool
554
		 */
555
		public function send_email_notification( $email_tag_args = array() ) {
556
			// Do not send email if notification is disable.
557
			if ( ! give_is_setting_enabled( $this->get_notification_status() ) ) {
558
				return false;
559
			}
560
561
			/**
562
			 * Fire action after before email send.
563
			 *
564
			 * @since 2.0
565
			 */
566
			do_action( "give_{$this->config['id']}_email_send_before", $this );
567
568
			$attachments = $this->get_email_attachments();
569
			$message     = give_do_email_tags( $this->get_email_message(), $email_tag_args );
570
			$subject     = give_do_email_tags( $this->get_email_subject(), $email_tag_args );
571
572
			// Setup email content type.
573
			Give()->emails->__set( 'content_type', $this->config['content_type'] );
574
575
			if ( 'text/plain' === $this->config['content_type'] ) {
576
				Give()->emails->__set( 'html', false );
577
				Give()->emails->__set( 'template', 'none' );
578
				$message = strip_tags( $message );
579
			}
580
581
			// Send email.
582
			$email_status = Give()->emails->send( $this->get_recipient(), $subject, $message, $attachments );
583
584
			/**
585
			 * Fire action after after email send.
586
			 *
587
			 * @since 2.0
588
			 */
589
			do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this );
590
591
			return $email_status;
592
		}
593
594
595
		/**
596
		 * Decode preview email template tags.
597
		 *
598
		 * @since 2.0
599
		 *
600
		 * @param string $message
601
		 *
602
		 * @return string
603
		 */
604
		public function preview_email_template_tags( $message ) {
605
			$user_id    = give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'user_id' );
606
			$user       = ! empty( $user_id ) ? get_user_by( 'id', $user_id ) : wp_get_current_user();
607
			$receipt_id = strtolower( md5( uniqid() ) );
608
609
			$receipt_link_url = esc_url( add_query_arg( array(
610
				'payment_key' => $receipt_id,
611
				'give_action' => 'view_receipt',
612
			), home_url() ) );
613
614
			$receipt_link = sprintf(
615
				'<a href="%1$s">%2$s</a>',
616
				$receipt_link_url,
617
				esc_html__( 'View the receipt in your browser &raquo;', 'give' )
618
			);
619
620
			// Set default values for tags.
621
			$this->config['preview_email_tags_values'] = wp_parse_args(
622
				$this->config['preview_email_tags_values'],
623
				array(
624
					'name'              => $user->display_name,
625
					'fullname'          => $user->display_name,
626
					'username'          => $user->user_login,
627
					'user_email'        => $user->user_email,
628
					'payment_total'     => give_currency_filter( give_format_amount( 10.50 ) ),
629
					'amount'            => give_currency_filter( give_format_amount( 10.50 ) ),
630
					'price'             => give_currency_filter( give_format_amount( 10.50 ) ),
631
					'payment_method'    => 'Paypal',
632
					'receipt_id'        => $receipt_id,
633
					'payment_id'        => give_check_variable( give_clean( $_GET ), 'isset_empty', rand( 2000, 2050 ), 'preview_id' ),
634
					'receipt_link_url'  => $receipt_link_url,
635
					'receipt_link'      => $receipt_link,
636
					'date'              => date( give_date_format(), current_time( 'timestamp' ) ),
637
					'donation'          => esc_html__( 'Sample Donation Form Title', 'give' ),
638
					'form_title'        => esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
639
					'sitename'          => get_bloginfo( 'name' ),
640
					'pdf_receipt'       => '<a href="#">Download Receipt</a>',
641
					'billing_address'   => '',
642
					'email_access_link' => sprintf(
643
						'<a href="%1$s">%2$s</a>',
644
						add_query_arg(
645
							array(
646
								'give_nl' => uniqid(),
647
							),
648
							get_permalink( give_get_option( 'history_page' ) )
649
						),
650
						__( 'Access Donation Details &raquo;', 'give' )
651
					),
652
				)
653
			);
654
655
			// Decode tags.
656
			foreach ( $this->config['preview_email_tags_values'] as $preview_tag => $value ) {
657
				if ( isset( $this->config['preview_email_tags_values'][ $preview_tag ] ) ) {
658
					$message = str_replace( "{{$preview_tag}}", $this->config['preview_email_tags_values'][ $preview_tag ], $message );
659
				}
660
			}
661
662
			return apply_filters( 'give_email_preview_template_tags', $message );
663
		}
664
665
		/**
666
		 * Setup email data
667
		 *
668
		 * @since 2.0
669
		 */
670
		public function setup_email_data() {
671
		}
672
	}
673
674
endif; // End class_exists check
675