Completed
Push — issues/611 ( 758b1c...a976e9 )
by Ravinder
16:26
created

Give_Email_Notification::hide_section()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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