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