Test Failed
Push — issues/370 ( 90279e )
by Ravinder
05:35
created

Give_Email_Notification::load()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 44
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 256
nop 1
dl 0
loc 44
rs 3
c 0
b 0
f 0
1
<?php
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
			'notices'                           => array(),
54
			'content_type_editable'             => true,
55
			'has_preview'                       => true,
56
			'has_preview_header'                => true,
57
			'preview_email_tags_values'         => array(),
58
			'email_tag_context'                 => 'all',
59
			'form_metabox_setting'              => true,
60
			'content_type'                      => '',
61
			'email_template'                    => '',
62
			'default_email_subject'             => '',
63
			'default_email_message'             => '',
64
			// This setting page will appear under core setting.
65
			'show_on_emails_setting_page'       => true,
66
		);
67
68
		/**
69
		 * @var     string $recipient_email Donor email.
70
		 * @access  protected
71
		 * @since   2.0
72
		 */
73
		protected $recipient_email = '';
74
75
76
		/**
77
		 * Setup email notification.
78
		 *
79
		 * @since 2.0
80
		 */
81
		public function init() {
82
83
		}
84
85
86
		/**
87
		 * Get instance.
88
		 *
89
		 * @since  2.0
90
		 * @access public
91
		 *
92
		 * @param string $email_notification_id
93
		 *
94
		 * @return Give_Email_Notification
95
		 */
96
		public static function get_instance( $email_notification_id = '' ) {
97
			$class = '';
98
99
			if ( ! empty( $email_notification_id ) ) {
100
				/* @var Give_Email_Notification $class */
101
				foreach ( self::$singleton as $class ) {
102
					if ( $email_notification_id === $class->config['id'] ) {
103
						$class = get_class( $class );
104
						break;
105
					}
106
				}
107
			} else {
108
				$class = get_called_class();
109
			}
110
111
			if ( ! empty( $class ) && ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) ) {
112
				self::$singleton[ $class ] = new $class();
113
			}
114
115
			return ( isset( self::$singleton[ $class ] ) ? self::$singleton[ $class ] : null );
116
		}
117
118
		/**
119
		 * Setup action and filters.
120
		 *
121
		 * @access  public
122
		 * @since   2.0
123
		 *
124
		 * @param array $config
125
		 */
126
		public function load( $config ) {
127
			// Set notification configuration.
128
			$this->config = wp_parse_args( $config, $this->config );
129
130
			// Set email preview header status.
131
			$this->config['has_preview_header'] = $this->config['has_preview'] && $this->config['has_preview_header'] ? true : false;
132
133
			// Set email content type
134
			$this->config['content_type'] = empty( $this->config['content_type'] ) || ! in_array( $this->config['content_type'], array(
135
				'text/html',
136
				'text/plain',
137
			) )
138
				? Give()->emails->get_content_type()
139
				: $this->config['content_type'];
140
			$this->config['content_type'] = give_get_option( Give_Email_Setting_Field::get_prefix( $this ) . 'email_content_type', $this->config['content_type'] );
141
142
			// Set email template type.
143
			$this->config['email_template'] = empty( $this->config['email_template'] )
144
				? give_get_option( 'email_template' )
145
				: $this->config['email_template'];
146
147
			// Set recipient group name.
148
			$this->config['recipient_group_name'] = empty( $this->config['recipient_group_name'] )
149
				? ( ! Give_Email_Notification_Util::has_recipient_field( $this ) ? __( 'Donor', 'give' ) : '' )
150
				: $this->config['recipient_group_name'];
151
152
			// Non notification status editable notice.
153
			$this->config['notices']['non-notification-status-editable'] = empty( $this->config['notices']['non-notification-status-editable'] )
154
				? __( 'You can not edit this notification directly. This will be enable or disable automatically on basis of plugin settings.', 'give' )
155
				: $this->config['notices']['non-notification-status-editable'];
156
157
			/**
158
			 *  Filter the notification config.
159
			 *
160
			 * @since 2.0
161
			 *
162
			 * @param                         array                   Give_Email_Notification::config
163
			 * @param Give_Email_Notification $this
164
			 */
165
			$this->config = apply_filters( 'give_email_api_notification_config', $this->config, $this );
166
167
			// Setup filters.
168
			$this->setup_filters();
169
		}
170
171
172
		/**
173
		 * Setup filters.
174
		 *
175
		 * @since  2.0
176
		 * @access public
177
		 */
178
		private function setup_filters() {
179
			// Apply filter only for current email notification section.
180
			if ( give_get_current_setting_section() === $this->config['id'] ) {
181
				// Initialize email context for email notification.
182
				$this->config['email_tag_context'] = apply_filters(
183
					"give_{$this->config['id']}_email_tag_context",
184
					$this->config['email_tag_context'],
185
					$this
186
				);
187
			}
188
189
			// Setup setting fields.
190
			if( $this->config['show_on_emails_setting_page'] ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
191
				add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 );
192
			}
193
194
			if ( $this->config['form_metabox_setting'] ) {
195
				add_filter(
196
					'give_email_notification_options_metabox_fields',
197
					array( $this, 'add_metabox_setting_field' ),
198
					10,
199
					2
200
				);
201
			}
202
203
			/**
204
			 * Filter the default email subject.
205
			 *
206
			 * @since 2.0
207
			 */
208
			$this->config['default_email_subject'] = apply_filters(
209
				"give_{$this->config['id']}_get_default_email_subject",
210
				$this->config['default_email_subject'],
211
				$this
212
			);
213
214
			/**
215
			 * Filter the default email message.
216
			 *
217
			 * @since 2.0
218
			 */
219
			$this->config['default_email_message'] = apply_filters(
220
				"give_{$this->config['id']}_get_default_email_message",
221
				$this->config['default_email_message'],
222
				$this
223
			);
224
		}
225
226
		/**
227
		 * Add sections.
228
		 *
229
		 * @since 2.0
230
		 *
231
		 * @param array $sections
232
		 *
233
		 * @return array
234
		 */
235
		public function add_section( $sections ) {
236
			$sections[ $this->config['id'] ] = $this->config['label'];
237
238
			return $sections;
239
		}
240
241
		/**
242
		 * Add sections.
243
		 *
244
		 * @since 2.0
245
		 *
246
		 * @param bool $hide_section
247
		 *
248
		 * @return bool
249
		 */
250
		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...
251
			$hide_section = true;
252
253
			return $hide_section;
254
		}
255
256
		/**
257
		 * Register email settings.
258
		 *
259
		 * @since  2.0
260
		 * @access public
261
		 *
262
		 * @param   array $settings
263
		 *
264
		 * @return  array
265
		 */
266
		public function add_setting_fields( $settings ) {
267
			if ( $this->config['id'] === give_get_current_setting_section() ) {
268
				$settings = $this->get_setting_fields();
269
			}
270
271
			return $settings;
272
		}
273
274
275
		/**
276
		 * Get setting fields
277
		 *
278
		 * @since  2.0
279
		 * @access public
280
		 *
281
		 * @param int $form_id
282
		 *
283
		 * @return array
284
		 */
285
		public function get_setting_fields( $form_id = null ) {
286
			return Give_Email_Setting_Field::get_setting_fields( $this, $form_id );
287
		}
288
289
290
		/**
291
		 * Register email settings to form metabox.
292
		 *
293
		 * @since  2.0
294
		 * @access public
295
		 *
296
		 * @param array $settings
297
		 * @param int   $form_id
298
		 *
299
		 * @return array
300
		 */
301
		public function add_metabox_setting_field( $settings, $form_id ) {
302
303
			$settings[] = array(
304
				'id'     => $this->config['id'],
305
				'title'  => $this->config['label'],
306
				'fields' => $this->get_setting_fields( $form_id ),
307
			);
308
309
			return $settings;
310
		}
311
312
313
		/**
314
		 * Get extra setting field.
315
		 *
316
		 * @since  2.0
317
		 * @access public
318
		 *
319
		 * @param int $form_id
320
		 *
321
		 * @return array
322
		 */
323
		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...
324
			return array();
325
		}
326
327
328
		/**
329
		 * Get recipient(s).
330
		 *
331
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
332
		 *
333
		 * @since  2.0
334
		 * @access public
335
		 *
336
		 * @param int $form_id
337
		 *
338
		 * @return string|array
339
		 */
340
		public function get_recipient( $form_id = null ) {
341
			if ( empty( $this->recipient_email ) && $this->config['has_recipient_field'] ) {
342
				$this->recipient_email = Give_Email_Notification_Util::get_value( $this, Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'recipient', $form_id );
343
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
344
345
				/**
346
				 * Filter the admin notice emails.
347
				 *
348
				 * @since 1.0
349
				 * @deprecated 2.0
350
				 */
351
				$this->recipient_email = apply_filters( 'give_admin_notice_emails', $this->recipient_email, $this, $form_id );
352
			}
353
354
			/**
355
			 * Filter the recipients
356
			 *
357
			 * @since 2.0
358
			 */
359
			return apply_filters(
360
				"give_{$this->config['id']}_get_recipients",
361
				give_check_variable(
362
					$this->recipient_email,
363
					'empty',
364
					Give()->emails->get_from_address()
365
				),
366
				$this,
367
				$form_id
368
			);
369
		}
370
371
		/**
372
		 * Get notification status.
373
		 *
374
		 * @since  2.0
375
		 * @access public
376
		 *
377
		 * @param int $form_id
378
		 *
379
		 * @return bool
380
		 */
381
		public function get_notification_status( $form_id = null ) {
382
			$notification_status = empty( $form_id )
383
				? give_get_option( "{$this->config['id']}_notification", $this->config['notification_status'] )
384
				: give_get_meta( $form_id, Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'notification', true, 'global' );
0 ignored issues
show
Documentation introduced by
'global' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
385
386
			/**
387
			 * Filter the notification status.
388
			 *
389
			 * @since 1.8
390
			 */
391
			return apply_filters(
392
				"give_{$this->config['id']}_get_notification_status",
393
				$notification_status,
394
				$this,
395
				$form_id
396
			);
397
		}
398
399
		/**
400
		 * Get email subject.
401
		 *
402
		 * @since  2.0
403
		 * @access public
404
		 *
405
		 * @param int $form_id
406
		 *
407
		 * @return string
408
		 */
409 View Code Duplication
		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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
410
			$subject = wp_strip_all_tags(
411
				Give_Email_Notification_Util::get_value(
412
					$this,
413
					Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject',
414
					$form_id,
415
					$this->config['default_email_subject']
416
				)
417
			);
418
419
			/**
420
			 * Filter the subject.
421
			 *
422
			 * @since 2.0
423
			 */
424
			return apply_filters(
425
				"give_{$this->config['id']}_get_email_subject",
426
				$subject,
427
				$this,
428
				$form_id
429
			);
430
		}
431
432
		/**
433
		 * Get email message.
434
		 *
435
		 * @since  2.0
436
		 * @access public
437
		 *
438
		 * @param int $form_id
439
		 *
440
		 * @return string
441
		 */
442
		public function get_email_message( $form_id = null ) {
443
			$message = Give_Email_Notification_Util::get_value(
444
				$this,
445
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
446
				$form_id,
447
				$this->config['default_email_message']
448
			);
449
450
			/**
451
			 * Filter the message.
452
			 *
453
			 * @since 2.0
454
			 */
455
			return apply_filters(
456
				"give_{$this->config['id']}_get_email_message",
457
				$message,
458
				$this,
459
				$form_id
460
			);
461
		}
462
463
464
		/**
465
		 * Get email content type.
466
		 *
467
		 * @since  2.0
468
		 * @access public
469
		 *
470
		 * @param $form_id
471
		 *
472
		 * @return string
473
		 */
474
		public function get_email_content_type( $form_id ) {
475
			$content_type = Give_Email_Notification_Util::get_value(
476
				$this,
477
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_content_type',
478
				$form_id,
479
				$this->config['content_type']
480
			);
481
482
			/**
483
			 * Filter the email content type.
484
			 *
485
			 * @since 2.0
486
			 */
487
			return apply_filters(
488
				"give_{$this->config['id']}_get_email_content_type",
489
				$content_type,
490
				$this,
491
				$form_id
492
			);
493
		}
494
495
		/**
496
		 * Get email template.
497
		 *
498
		 * @since  2.0
499
		 * @access public
500
		 *
501
		 * @param $form_id
502
		 *
503
		 * @return string
504
		 */
505
		public function get_email_template( $form_id ) {
506
			$content_type = Give_Email_Notification_Util::get_value(
507
				$this,
508
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) .'email_template',
509
				$form_id,
510
				$this->config['email_template']
511
			);
512
513
			/**
514
			 * Filter the email template.
515
			 *
516
			 * @since 2.0
517
			 */
518
			return apply_filters(
519
				"give_{$this->config['id']}_get_email_template",
520
				$content_type,
521
				$this,
522
				$form_id
523
			);
524
		}
525
526
527
		/**
528
		 * Get allowed email tags for current email notification.
529
		 *
530
		 * @since  2.0
531
		 * @access private
532
		 *
533
		 * @param bool $formatted
534
		 *
535
		 * @return array
536
		 */
537
		public function get_allowed_email_tags( $formatted = false ) {
538
			// Get all email tags.
539
			$email_tags = Give()->email_tags->get_tags();
540
541
			// Skip if all email template tags context setup exit.
542
			if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) {
543
				if ( is_array( $this->config['email_tag_context'] ) ) {
544 View Code Duplication
					foreach ( $email_tags as $index => $email_tag ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
545
						if ( in_array( $email_tag['context'], $this->config['email_tag_context'] ) ) {
546
							continue;
547
						}
548
549
						unset( $email_tags[ $index ] );
550
					}
551
				} else {
552 View Code Duplication
					foreach ( $email_tags as $index => $email_tag ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
553
						if ( $this->config['email_tag_context'] === $email_tag['context'] ) {
554
							continue;
555
						}
556
557
						unset( $email_tags[ $index ] );
558
					}
559
				}
560
			}
561
562
			if ( count( $email_tags ) && $formatted ) : ob_start() ?>
563
				<div class="give-email-tags-wrap">
564 View Code Duplication
					<?php foreach ( $email_tags as $email_tag ) : ?>
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
565
						<span class="give_<?php echo $email_tag['tag']; ?>_tag">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$email_tag'
Loading history...
566
							<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['description']; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$email_tag'
Loading history...
567
						</span>
568
					<?php endforeach; ?>
569
				</div>
570
				<?php
571
				$email_tags = ob_get_clean();
572
			endif;
573
574
			return $email_tags;
575
		}
576
577
		/**
578
		 * Get preview email recipients.
579
		 *
580
		 * @since  2.0
581
		 * @access public
582
		 *
583
		 * @param int $form_id
584
		 *
585
		 * @return array|string
586
		 */
587
		public function get_preview_email_recipient( $form_id = null ) {
588
			$recipients = $this->get_recipient( $form_id );
589
590
			/**
591
			 * Filter the preview email recipients.
592
			 *
593
			 * @since 2.0
594
			 *
595
			 * @param string|array            $recipients List of recipients.
596
			 * @param Give_Email_Notification $this
597
			 */
598
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this, $form_id );
599
600
			return $recipients;
601
		}
602
603
		/**
604
		 * Get the recipient attachments.
605
		 *
606
		 * @since  2.0
607
		 * @access public
608
		 *
609
		 * @param int $form_id
610
		 *
611
		 * @return array
612
		 */
613
		public function get_email_attachments( $form_id = null ) {
614
			/**
615
			 * Filter the attachment.
616
			 *
617
			 * @since 2.0
618
			 */
619
			return apply_filters( "give_{$this->config['id']}_get_email_attachments", array(), $this, $form_id );
620
		}
621
622
623
		/**
624
		 * Send preview email.
625
		 *
626
		 * @since  2.0
627
		 * @access public
628
		 */
629
		public function send_preview_email() {
630
			// Get form id
631
			$form_id = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
632
633
			// setup email data.
634
			$this->setup_email_data();
635
636
			$attachments  = $this->get_email_attachments();
637
			$message      = $this->preview_email_template_tags( $this->get_email_message( $form_id ) );
638
			$subject      = $this->preview_email_template_tags( $this->get_email_subject( $form_id ) );
639
			$content_type = $this->get_email_content_type( $form_id );
640
641
			// Setup email content type.
642
			Give()->emails->__set( 'content_type', $content_type );
643
			Give()->emails->__set( 'html', true );
644
645
			// Setup email template
646
			Give()->emails->__set( 'template', $this->get_email_template( $form_id ) );
647
648
			// Format plain content type email.
649 View Code Duplication
			if ( 'text/plain' === $content_type ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
650
				Give()->emails->__set( 'html', false );
651
				Give()->emails->__set( 'template', 'none' );
652
				$message = strip_tags( $message );
653
			}
654
655
			return Give()->emails->send( $this->get_preview_email_recipient( $form_id ), $subject, $message, $attachments );
0 ignored issues
show
Bug introduced by
It seems like $this->get_preview_email_recipient($form_id) targeting Give_Email_Notification:...eview_email_recipient() can also be of type array; however, Give_Emails::send() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
656
		}
657
658
659
		/**
660
		 * Send email notification.
661
		 *
662
		 * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails):
663
		 *    1. payment_id
664
		 *    2. user_id
665
		 *    3. form_id
666
		 *    4. donor_id
667
		 *    5. for third party email tags you can pass necessary param along above parameters other value replace by empty string.
668
		 *
669
		 * @since  2.0
670
		 * @access public
671
		 *
672
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
673
		 *
674
		 * @return bool
675
		 */
676
		public function send_email_notification( $email_tag_args = array() ) {
677
			// Add email content type email tags.
678
			$email_tag_args['email_content_type'] = $this->config['content_type'];
679
680
			/**
681
			 * Filter the email tag args
682
			 *
683
			 * @since 2.0
684
			 */
685
			$email_tag_args = apply_filters( "give_{$this->config['id']}_email_tag_args", $email_tag_args, $this );
686
687
			// Get form id.
688
			$form_id = ! empty( $email_tag_args['form_id'] )
689
				? absint( $email_tag_args['form_id'] )
690
				: ( ! empty( $email_tag_args['payment_id'] ) ? give_get_payment_form_id( $email_tag_args['payment_id'] ) : null );
691
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
692
693
			// Do not send email if notification is disable.
694
			if ( ! Give_Email_Notification_Util::is_email_notification_active( $this, $form_id ) ) {
695
				return false;
696
			}
697
698
			/**
699
			 * Fire action after before email send.
700
			 *
701
			 * @since 2.0
702
			 */
703
			do_action( "give_{$this->config['id']}_email_send_before", $this, $form_id );
704
705
			$attachments  = $this->get_email_attachments();
706
			$message      = give_do_email_tags( $this->get_email_message( $form_id ), $email_tag_args );
707
			$subject      = give_do_email_tags( $this->get_email_subject( $form_id ), $email_tag_args );
708
			$content_type = $this->get_email_content_type( $form_id );
709
710
			// Setup email content type.
711
			Give()->emails->__set( 'content_type', $content_type );
712
			Give()->emails->__set( 'html', true );
713
714
			// Set email template.
715
			Give()->emails->__set( 'template', $this->get_email_template( $form_id ) );
716
717 View Code Duplication
			if ( 'text/plain' === $content_type ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
718
				Give()->emails->__set( 'html', false );
719
				Give()->emails->__set( 'template', 'none' );
720
				$message = strip_tags( $message );
721
			}
722
723
			// Send email.
724
			$email_status = Give()->emails->send( $this->get_recipient( $form_id ), $subject, $message, $attachments );
0 ignored issues
show
Bug introduced by
It seems like $this->get_recipient($form_id) targeting Give_Email_Notification::get_recipient() can also be of type array; however, Give_Emails::send() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
725
726
			/**
727
			 * Fire action after after email send.
728
			 *
729
			 * @since 2.0
730
			 */
731
			do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this, $form_id );
732
733
			return $email_status;
734
		}
735
736
737
		/**
738
		 * Decode preview email template tags.
739
		 *
740
		 * @since 2.0
741
		 *
742
		 * @param string $message
743
		 *
744
		 * @return string
745
		 */
746
		public function preview_email_template_tags( $message ) {
747
			// Set Payment.
748
			$payment_id = give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'preview_id' );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
749
			$payment    = $payment_id ? new Give_Payment( $payment_id ) : new stdClass();
750
751
			// Set donor.
752
			$user_id = $payment_id
753
				? $payment->user_id
754
				: give_check_variable( give_clean( $_GET ), 'isset_empty', 0, 'user_id' );
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
755
			$user_id = $user_id ? $user_id : wp_get_current_user()->ID;
756
757
			// Set receipt.
758
			$receipt_id = strtolower( md5( uniqid() ) );
759
760
			$receipt_link_url = esc_url( add_query_arg( array(
761
				'payment_key' => $receipt_id,
762
				'give_action' => 'view_receipt',
763
			), home_url() ) );
764
765
			$receipt_link = sprintf(
766
				'<a href="%1$s">%2$s</a>',
767
				$receipt_link_url,
768
				esc_html__( 'View the receipt in your browser &raquo;', 'give' )
769
			);
770
771
			// Set default values for tags.
772
			$this->config['preview_email_tags_values'] = wp_parse_args(
773
				$this->config['preview_email_tags_values'],
774
				array(
775
					'name'              => give_email_tag_first_name( array(
776
						'payment_id' => $payment_id,
777
						'user_id'    => $user_id,
778
					) ),
779
					'fullname'          => give_email_tag_fullname( array(
780
						'payment_id' => $payment_id,
781
						'user_id'    => $user_id,
782
					) ),
783
					'username'          => give_email_tag_username( array(
784
						'payment_id' => $payment_id,
785
						'user_id'    => $user_id,
786
					) ),
787
					'user_email'        => give_email_tag_user_email( array(
788
						'payment_id' => $payment_id,
789
						'user_id'    => $user_id,
790
					) ),
791
					'payment_total'     => $payment_id ? give_email_tag_payment_total( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
792
					'amount'            => $payment_id ? give_email_tag_amount( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
793
					'price'             => $payment_id ? give_email_tag_price( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
794
					'payment_method'    => $payment_id ? give_email_tag_payment_method( array( 'payment_id' => $payment_id ) ) : __( 'PayPal', 'give' ),
795
					'receipt_id'        => $receipt_id,
796
					'payment_id'        => $payment_id ? $payment_id : rand( 2000, 2050 ),
797
					'receipt_link_url'  => $receipt_link_url,
798
					'receipt_link'      => $receipt_link,
799
					'date'              => $payment_id ? date( give_date_format(), strtotime( $payment->date ) ) : date( give_date_format(), current_time( 'timestamp' ) ),
800
					'donation'          => $payment_id ? give_email_tag_donation( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title', 'give' ),
801
					'form_title'        => $payment_id ? give_email_tag_form_title( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
802
					'sitename'          => $payment_id ? give_email_tag_sitename( array( 'payment_id' => $payment_id ) ) : get_bloginfo( 'name' ),
803
					'pdf_receipt'       => '<a href="#">Download Receipt</a>',
804
					'billing_address'   => $payment_id ? give_email_tag_billing_address( array( 'payment_id' => $payment_id ) ) : '',
805
					'email_access_link' => sprintf(
806
						'<a href="%1$s">%2$s</a>',
807
						add_query_arg(
808
							array(
809
								'give_nl' => uniqid(),
810
							),
811
							get_permalink( give_get_option( 'history_page' ) )
812
						),
813
						__( 'Access Donation Details &raquo;', 'give' )
814
					),
815
					'reset_password_link' => $user_id ? give_email_tag_reset_password_link( array( 'user_id' => $user_id ), $payment_id ) : '',
816
				)
817
			);
818
819
			// Decode tags.
820
			foreach ( $this->config['preview_email_tags_values'] as $preview_tag => $value ) {
821
				if ( isset( $this->config['preview_email_tags_values'][ $preview_tag ] ) ) {
822
					$message = str_replace( "{{$preview_tag}}", $this->config['preview_email_tags_values'][ $preview_tag ], $message );
823
				}
824
			}
825
826
			return apply_filters( 'give_email_preview_template_tags', $message );
827
		}
828
829
		/**
830
		 * Setup email data
831
		 *
832
		 * @since 2.0
833
		 */
834
		public function setup_email_data() {
835
		}
836
	}
837
838
endif; // End class_exists check
839