Test Failed
Pull Request — master (#2668)
by Devin
07:24 queued 30s
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 notification status from here.', '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
			if( $this->config['has_recipient_field'] ) {
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...
204
				add_action(
205
						"give_save__give_{$this->config['id']}_recipient",
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
206
						array( $this, 'validate_form_recipient_field_value' ),
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
207
						10,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
208
						3
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
209
				);
210
			}
211
212
			/**
213
			 * Filter the default email subject.
214
			 *
215
			 * @since 2.0
216
			 */
217
			$this->config['default_email_subject'] = apply_filters(
218
				"give_{$this->config['id']}_get_default_email_subject",
219
				$this->config['default_email_subject'],
220
				$this
221
			);
222
223
			/**
224
			 * Filter the default email message.
225
			 *
226
			 * @since 2.0
227
			 */
228
			$this->config['default_email_message'] = apply_filters(
229
				"give_{$this->config['id']}_get_default_email_message",
230
				$this->config['default_email_message'],
231
				$this
232
			);
233
		}
234
235
		/**
236
		 * Add sections.
237
		 *
238
		 * @since 2.0
239
		 *
240
		 * @param array $sections
241
		 *
242
		 * @return array
243
		 */
244
		public function add_section( $sections ) {
245
			$sections[ $this->config['id'] ] = $this->config['label'];
246
247
			return $sections;
248
		}
249
250
		/**
251
		 * Add sections.
252
		 *
253
		 * @since 2.0
254
		 *
255
		 * @param bool $hide_section
256
		 *
257
		 * @return bool
258
		 */
259
		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...
260
			$hide_section = true;
261
262
			return $hide_section;
263
		}
264
265
		/**
266
		 * Register email settings.
267
		 *
268
		 * @since  2.0
269
		 * @access public
270
		 *
271
		 * @param   array $settings
272
		 *
273
		 * @return  array
274
		 */
275
		public function add_setting_fields( $settings ) {
276
			if ( $this->config['id'] === give_get_current_setting_section() ) {
277
				$settings = $this->get_setting_fields();
278
			}
279
280
			return $settings;
281
		}
282
283
284
		/**
285
		 * Get setting fields
286
		 *
287
		 * @since  2.0
288
		 * @access public
289
		 *
290
		 * @param int $form_id
291
		 *
292
		 * @return array
293
		 */
294
		public function get_setting_fields( $form_id = null ) {
295
			return Give_Email_Setting_Field::get_setting_fields( $this, $form_id );
296
		}
297
298
299
		/**
300
		 * Register email settings to form metabox.
301
		 *
302
		 * @since  2.0
303
		 * @access public
304
		 *
305
		 * @param array $settings
306
		 * @param int   $form_id
307
		 *
308
		 * @return array
309
		 */
310
		public function add_metabox_setting_field( $settings, $form_id ) {
311
312
			if( Give_Email_Notification_Util::is_email_notification_active( $this ) ) {
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...
313
				$settings[] = array(
314
					'id'     => $this->config['id'],
315
					'title'  => $this->config['label'],
316
					'fields' => $this->get_setting_fields( $form_id ),
317
				);
318
			}
319
320
			return $settings;
321
		}
322
323
324
		/**
325
		 * Get extra setting field.
326
		 *
327
		 * @since  2.0
328
		 * @access public
329
		 *
330
		 * @param int $form_id
331
		 *
332
		 * @return array
333
		 */
334
		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...
335
			return array();
336
		}
337
338
339
		/**
340
		 * Get recipient(s).
341
		 *
342
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
343
		 *
344
		 * @since  2.0
345
		 * @access public
346
		 *
347
		 * @param int $form_id
348
		 *
349
		 * @return string|array
350
		 */
351
		public function get_recipient( $form_id = null ) {
352
			if ( empty( $this->recipient_email ) && $this->config['has_recipient_field'] ) {
353
				$this->recipient_email = Give_Email_Notification_Util::get_value(
354
						$this,
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
355
						Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'recipient',
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
356
						$form_id
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
357
				);
358
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
359
360
				/**
361
				 * Filter the admin notice emails.
362
				 *
363
				 * @since 1.0
364
				 * @deprecated 2.0
365
				 */
366
				$this->recipient_email = apply_filters( 'give_admin_notice_emails', $this->recipient_email, $this, $form_id );
367
			}
368
369
			/**
370
			 * Filter the recipients
371
			 *
372
			 * @since 2.0
373
			 */
374
			return apply_filters(
375
				"give_{$this->config['id']}_get_recipients",
376
				give_check_variable(
377
					$this->recipient_email,
378
					'empty',
379
					Give()->emails->get_from_address()
380
				),
381
				$this,
382
				$form_id
383
			);
384
		}
385
386
		/**
387
		 * Get notification status.
388
		 *
389
		 * @since  2.0
390
		 * @access public
391
		 *
392
		 * @param int $form_id
393
		 *
394
		 * @return bool
395
		 */
396
		public function get_notification_status( $form_id = null ) {
397
			$notification_status = empty( $form_id )
398
				? give_get_option( "{$this->config['id']}_notification", $this->config['notification_status'] )
399
				: 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...
400
401
			/**
402
			 * Filter the notification status.
403
			 *
404
			 * @since 1.8
405
			 */
406
			return apply_filters(
407
				"give_{$this->config['id']}_get_notification_status",
408
				$notification_status,
409
				$this,
410
				$form_id
411
			);
412
		}
413
414
		/**
415
		 * Get email subject.
416
		 *
417
		 * @since  2.0
418
		 * @access public
419
		 *
420
		 * @param int $form_id
421
		 *
422
		 * @return string
423
		 */
424 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...
425
			$subject = wp_strip_all_tags(
426
				Give_Email_Notification_Util::get_value(
427
					$this,
428
					Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject',
429
					$form_id,
430
					$this->config['default_email_subject']
431
				)
432
			);
433
434
			/**
435
			 * Filter the subject.
436
			 *
437
			 * @since 2.0
438
			 */
439
			return apply_filters(
440
				"give_{$this->config['id']}_get_email_subject",
441
				$subject,
442
				$this,
443
				$form_id
444
			);
445
		}
446
447
		/**
448
		 * Get email message.
449
		 *
450
		 * @since  2.0
451
		 * @access public
452
		 *
453
		 * @param int $form_id
454
		 *
455
		 * @return string
456
		 */
457
		public function get_email_message( $form_id = null ) {
458
			$message = Give_Email_Notification_Util::get_value(
459
				$this,
460
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
461
				$form_id,
462
				$this->config['default_email_message']
463
			);
464
465
			/**
466
			 * Filter the message.
467
			 *
468
			 * @since 2.0
469
			 */
470
			return apply_filters(
471
				"give_{$this->config['id']}_get_email_message",
472
				$message,
473
				$this,
474
				$form_id
475
			);
476
		}
477
478
479
		/**
480
		 * Get email content type.
481
		 *
482
		 * @since  2.0
483
		 * @access public
484
		 *
485
		 * @param $form_id
486
		 *
487
		 * @return string
488
		 */
489
		public function get_email_content_type( $form_id ) {
490
			$content_type = Give_Email_Notification_Util::get_value(
491
				$this,
492
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_content_type',
493
				$form_id,
494
				$this->config['content_type']
495
			);
496
497
			/**
498
			 * Filter the email content type.
499
			 *
500
			 * @since 2.0
501
			 */
502
			return apply_filters(
503
				"give_{$this->config['id']}_get_email_content_type",
504
				$content_type,
505
				$this,
506
				$form_id
507
			);
508
		}
509
510
		/**
511
		 * Get email template.
512
		 *
513
		 * @since  2.0
514
		 * @access public
515
		 *
516
		 * @param $form_id
517
		 *
518
		 * @return string
519
		 */
520
		public function get_email_template( $form_id ) {
521
			$email_template = give_get_meta( $form_id, '_give_email_template', true );
522
			$email_template = Give_Email_Notification_Util::get_value(
523
				$this,
524
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) .'email_template',
525
				$form_id,
526
				! empty( $email_template ) && Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ?
527
					$email_template :
528
					$this->config['email_template']
529
			);
530
531
			/**
532
			 * Filter the email template.
533
			 *
534
			 * @since 2.0
535
			 */
536
			return apply_filters(
537
				"give_{$this->config['id']}_get_email_template",
538
				$email_template,
539
				$this,
540
				$form_id
541
			);
542
		}
543
544
545
		/**
546
		 * Get allowed email tags for current email notification.
547
		 *
548
		 * @since  2.0
549
		 * @access private
550
		 *
551
		 * @param bool $formatted
552
		 *
553
		 * @return array
554
		 */
555
		public function get_allowed_email_tags( $formatted = false ) {
556
			// Get all email tags.
557
			$email_tags = Give()->email_tags->get_tags();
558
559
			// Skip if all email template tags context setup exit.
560
			if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) {
561
				if ( is_array( $this->config['email_tag_context'] ) ) {
562 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...
563
						if ( in_array( $email_tag['context'], $this->config['email_tag_context'] ) ) {
564
							continue;
565
						}
566
567
						unset( $email_tags[ $index ] );
568
					}
569
				} else {
570 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...
571
						if ( $this->config['email_tag_context'] === $email_tag['context'] ) {
572
							continue;
573
						}
574
575
						unset( $email_tags[ $index ] );
576
					}
577
				}
578
			}
579
580
			if ( count( $email_tags ) && $formatted ) : ob_start() ?>
581
				<div class="give-email-tags-wrap">
582 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...
583
						<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...
584
							<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...
585
						</span>
586
					<?php endforeach; ?>
587
				</div>
588
				<?php
589
				$email_tags = ob_get_clean();
590
			endif;
591
592
			return $email_tags;
593
		}
594
595
		/**
596
		 * Get preview email recipients.
597
		 *
598
		 * @since  2.0
599
		 * @access public
600
		 *
601
		 * @param int $form_id
602
		 *
603
		 * @return array|string
604
		 */
605
		public function get_preview_email_recipient( $form_id = null ) {
606
			$recipients = $this->get_recipient( $form_id );
607
608
			/**
609
			 * Filter the preview email recipients.
610
			 *
611
			 * @since 2.0
612
			 *
613
			 * @param string|array            $recipients List of recipients.
614
			 * @param Give_Email_Notification $this
615
			 */
616
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this, $form_id );
617
618
			return $recipients;
619
		}
620
621
		/**
622
		 * Get the recipient attachments.
623
		 *
624
		 * @since  2.0
625
		 * @access public
626
		 *
627
		 * @param int $form_id
628
		 *
629
		 * @return array
630
		 */
631
		public function get_email_attachments( $form_id = null ) {
632
			/**
633
			 * Filter the attachment.
634
			 *
635
			 * @since 2.0
636
			 */
637
			return apply_filters( "give_{$this->config['id']}_get_email_attachments", array(), $this, $form_id );
638
		}
639
640
641
		/**
642
		 * Send preview email.
643
		 *
644
		 * @since  2.0
645
		 * @access public
646
		 */
647
		public function send_preview_email() {
648
			// Get form id
649
			$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...
650
651
			// setup email data.
652
			$this->setup_email_data();
653
654
			$attachments  = $this->get_email_attachments();
655
			$message      = $this->preview_email_template_tags( $this->get_email_message( $form_id ) );
656
			$subject      = $this->preview_email_template_tags( $this->get_email_subject( $form_id ) );
657
			$content_type = $this->get_email_content_type( $form_id );
658
659
			// Setup email content type.
660
			Give()->emails->__set( 'content_type', $content_type );
661
			Give()->emails->__set( 'html', true );
662
663
			// Setup email template
664
			Give()->emails->__set( 'template', $this->get_email_template( $form_id ) );
665
666
			// Format plain content type email.
667 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...
668
				Give()->emails->__set( 'html', false );
669
				Give()->emails->__set( 'template', 'none' );
670
				$message = strip_tags( $message );
671
			}
672
673 View Code Duplication
			if ( Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ) {
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...
674
				Give()->emails->form_id      = $form_id;
675
				Give()->emails->from_name    = give_get_meta( $form_id, '_give_from_name', true );
676
				Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true );
677
			}
678
679
			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...
680
		}
681
682
683
		/**
684
		 * Send email notification.
685
		 *
686
		 * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails):
687
		 *    1. payment_id
688
		 *    2. user_id
689
		 *    3. form_id
690
		 *    4. donor_id
691
		 *    5. for third party email tags you can pass necessary param along above parameters other value replace by empty string.
692
		 *
693
		 * @since  2.0
694
		 * @access public
695
		 *
696
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
697
		 *
698
		 * @return bool
699
		 */
700
		public function send_email_notification( $email_tag_args = array() ) {
701
			// Add email content type email tags.
702
			$email_tag_args['email_content_type'] = $this->config['content_type'];
703
704
			/**
705
			 * Filter the email tag args
706
			 *
707
			 * @since 2.0
708
			 */
709
			$email_tag_args = apply_filters( "give_{$this->config['id']}_email_tag_args", $email_tag_args, $this );
710
711
			// Get form id.
712
			$form_id = ! empty( $email_tag_args['form_id'] )
713
				? absint( $email_tag_args['form_id'] )
714
				: ( ! empty( $email_tag_args['payment_id'] ) ? give_get_payment_form_id( $email_tag_args['payment_id'] ) : null );
715
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
716
717
			// Do not send email if notification is disable.
718
			if ( ! Give_Email_Notification_Util::is_email_notification_active( $this, $form_id ) ) {
719
				return false;
720
			}
721
722
			/**
723
			 * Fire action after before email send.
724
			 *
725
			 * @since 2.0
726
			 */
727
			do_action( "give_{$this->config['id']}_email_send_before", $this, $form_id );
728
729
			$attachments  = $this->get_email_attachments();
730
			$message      = give_do_email_tags( $this->get_email_message( $form_id ), $email_tag_args );
731
			$subject      = give_do_email_tags( $this->get_email_subject( $form_id ), $email_tag_args );
732
			$content_type = $this->get_email_content_type( $form_id );
733
734
			// Setup email content type.
735
			Give()->emails->__set( 'content_type', $content_type );
736
			Give()->emails->__set( 'html', true );
737
738
			// Set email template.
739
			Give()->emails->__set( 'template', $this->get_email_template( $form_id ) );
740
741 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...
742
				Give()->emails->__set( 'html', false );
743
				Give()->emails->__set( 'template', 'none' );
744
				$message = strip_tags( $message );
745
			}
746
747 View Code Duplication
			if ( Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ) {
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...
748
				Give()->emails->form_id      = $form_id;
749
				Give()->emails->from_name    = give_get_meta( $form_id, '_give_from_name', true );
750
				Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true );
751
			}
752
753
			// Send email.
754
			$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...
755
756
			/**
757
			 * Fire action after after email send.
758
			 *
759
			 * @since 2.0
760
			 */
761
			do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this, $form_id );
762
763
			return $email_status;
764
		}
765
766
767
		/**
768
		 * Decode preview email template tags.
769
		 *
770
		 * @since 2.0
771
		 *
772
		 * @param string $message
773
		 *
774
		 * @return string
775
		 */
776
		public function preview_email_template_tags( $message ) {
777
			// Set Payment.
778
			$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...
779
			$payment    = $payment_id ? new Give_Payment( $payment_id ) : new stdClass();
780
781
			// Set donor.
782
			$user_id = $payment_id
783
				? $payment->user_id
784
				: 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...
785
			$user_id = $user_id ? $user_id : wp_get_current_user()->ID;
786
787
			// Set receipt.
788
			$receipt_id = strtolower( md5( uniqid() ) );
789
790
			$receipt_link_url = esc_url( add_query_arg( array(
791
				'payment_key' => $receipt_id,
792
			), give_get_history_page_uri() ) );
793
794
			$receipt_link = sprintf(
795
				'<a href="%1$s">%2$s</a>',
796
				$receipt_link_url,
797
				esc_html__( 'View the receipt in your browser &raquo;', 'give' )
798
			);
799
800
			// Set default values for tags.
801
			$this->config['preview_email_tags_values'] = wp_parse_args(
802
				$this->config['preview_email_tags_values'],
803
				array(
804
					'name'              => give_email_tag_first_name( array(
805
						'payment_id' => $payment_id,
806
						'user_id'    => $user_id,
807
					) ),
808
					'fullname'          => give_email_tag_fullname( array(
809
						'payment_id' => $payment_id,
810
						'user_id'    => $user_id,
811
					) ),
812
					'username'          => give_email_tag_username( array(
813
						'payment_id' => $payment_id,
814
						'user_id'    => $user_id,
815
					) ),
816
					'user_email'        => give_email_tag_user_email( array(
817
						'payment_id' => $payment_id,
818
						'user_id'    => $user_id,
819
					) ),
820
					'payment_total'     => $payment_id ? give_email_tag_payment_total( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
821
					'amount'            => $payment_id ? give_email_tag_amount( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
822
					'price'             => $payment_id ? give_email_tag_price( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
823
					'payment_method'    => $payment_id ? give_email_tag_payment_method( array( 'payment_id' => $payment_id ) ) : __( 'PayPal', 'give' ),
824
					'receipt_id'        => $receipt_id,
825
					'payment_id'        => $payment_id ? $payment_id : rand( 2000, 2050 ),
826
					'receipt_link_url'  => $receipt_link_url,
827
					'receipt_link'      => $receipt_link,
828
					'date'              => $payment_id ? date( give_date_format(), strtotime( $payment->date ) ) : date( give_date_format(), current_time( 'timestamp' ) ),
829
					'donation'          => $payment_id ? give_email_tag_donation( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title', 'give' ),
830
					'form_title'        => $payment_id ? give_email_tag_form_title( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
831
					'sitename'          => $payment_id ? give_email_tag_sitename( array( 'payment_id' => $payment_id ) ) : get_bloginfo( 'name' ),
832
					'pdf_receipt'       => sprintf(
833
						'<a href="#">%s</a>',
834
						__( 'Download Receipt', 'give' )
835
					),
836
					'billing_address'   => $payment_id ? give_email_tag_billing_address( array( 'payment_id' => $payment_id ) ) : '',
837
					'email_access_link' => sprintf(
838
						'<a href="%1$s">%2$s</a>',
839
						add_query_arg(
840
							array(
841
								'give_nl' => uniqid(),
842
							),
843
							give_get_history_page_uri()
844
						),
845
						__( 'View your donation history &raquo;', 'give' )
846
					),
847
					'reset_password_link' => $user_id ? give_email_tag_reset_password_link( array( 'user_id' => $user_id ), $payment_id ) : '',
848
				)
849
			);
850
851
			// Decode tags.
852
			foreach ( $this->config['preview_email_tags_values'] as $preview_tag => $value ) {
853
				if ( isset( $this->config['preview_email_tags_values'][ $preview_tag ] ) ) {
854
					$message = str_replace( "{{$preview_tag}}", $this->config['preview_email_tags_values'][ $preview_tag ], $message );
855
				}
856
			}
857
858
			return apply_filters( 'give_email_preview_template_tags', $message );
859
		}
860
861
		/**
862
		 * Setup email data
863
		 *
864
		 * @since 2.0
865
		 */
866
		public function setup_email_data() {
867
		}
868
869
870
		/**
871
		 * Validate email form setting
872
		 *
873
		 * Note: internal use only
874
		 *
875
		 * @since  2.0
876
		 * @access public
877
		 *
878
		 * @param $form_meta_key
879
		 * @param $form_meta_value
880
		 * @param $post_id
881
		 *
882
		 */
883
		public function validate_form_recipient_field_value( $form_meta_key, $form_meta_value, $post_id ) {
884
			// Get valid emails.
885
			$new_form_meta_value = array_filter( $form_meta_value, function ( $value ) {
886
				return ! empty( $value['email'] ) && is_email( $value['email'] );
887
			} );
888
889
			// Remove duplicate emails from array.
890
			$email_arr = array();
891
			foreach ( $new_form_meta_value as $index => $email ) {
892
				if( in_array( $email['email'], $email_arr  ) ) {
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...
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
893
					unset( $new_form_meta_value[$index] );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
894
					continue;
895
				}
896
897
				$email_arr[] = $email['email'];
898
			}
899
900
			$update = false;
901
902
			if ( empty( $new_form_meta_value ) ) {
903
				// Set default recipient.
904
				$form_meta_value = array(
905
					array(
906
						'email' => get_bloginfo( 'admin_email' )
907
					),
908
				);
909
910
				$update = true;
911
912
			} elseif ( count( $new_form_meta_value ) !== count( $form_meta_value ) ) {
913
				// Filter recipient emails.
914
				$form_meta_value = $new_form_meta_value;
915
916
				$update = true;
917
			}
918
919
			if( $update ) {
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...
920
				give_update_meta( $post_id, $form_meta_key, $form_meta_value );
921
			}
922
		}
923
	}
924
925
endif; // End class_exists check
926