Completed
Push — issues/611 ( e1f393...0587e7 )
by Ravinder
21:03
created

n_active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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