Give_Email_Notification   F
last analyzed

Complexity

Total Complexity 97

Size/Duplication

Total Lines 984
Duplicated Lines 4.78 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 47
loc 984
rs 1.616
c 0
b 0
f 0
wmc 97
lcom 2
cbo 6

25 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
B get_instance() 0 21 8
C load() 0 46 9
A add_section() 0 5 1
A hide_section() 0 5 1
A add_setting_fields() 0 7 2
A get_setting_fields() 0 3 1
A add_metabox_setting_field() 0 12 2
A get_extra_setting_fields() 0 3 1
A get_recipient() 0 33 3
A get_notification_status() 0 17 2
A get_email_subject() 22 22 1
A get_email_message() 0 20 1
A get_email_header() 0 20 1
A get_email_content_type() 0 20 1
A get_email_template() 0 23 3
B get_allowed_email_tags() 5 44 11
A get_preview_email_recipient() 0 15 1
A get_email_attachments() 0 8 1
B send_preview_email() 10 39 5
B send_email_notification() 10 76 7
F preview_email_template_tags() 0 98 18
A setup_email_data() 0 2 1
B validate_form_recipient_field_value() 0 42 7
B setup_filters() 0 72 8

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Give_Email_Notification often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Give_Email_Notification, and based on these observations, apply Extract Interface, too.

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, GiveWP
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
		 * Array of notification settings.
39
		 *
40
		 * @since  2.0
41
		 * @access public
42
		 * @var array
43
		 */
44
		public $config = array(
45
			'id'                           => '',
46
			'label'                        => '',
47
			'description'                  => '',
48
			'has_recipient_field'          => false,
49
			'recipient_group_name'         => '',
50
			'notification_status'          => 'disabled',
51
			'notification_status_editable' => true,
52
			'notices'                      => array(),
53
			'content_type_editable'        => true,
54
			'has_preview'                  => true,
55
			'has_preview_header'           => true,
56
			'preview_email_tags_values'    => array(),
57
			'email_tag_context'            => 'all',
58
			'form_metabox_setting'         => true,
59
			'content_type'                 => '',
60
			'email_template'               => '',
61
			'default_email_subject'        => '',
62
			'default_email_message'        => '',
63
			'default_email_header'         => '',
64
			// This setting page will appear under core setting.
65
			'show_on_emails_setting_page'  => true,
66
			'form_metabox_id'              => 'give_email_notification_options_metabox_fields',
67
		);
68
69
		/**
70
		 * @var     string $recipient_email Donor email.
71
		 * @access  protected
72
		 * @since   2.0
73
		 */
74
		protected $recipient_email = '';
75
76
77
		/**
78
		 * Setup email notification.
79
		 *
80
		 * @since 2.0
81
		 */
82
		public function init() {
83
84
		}
85
86
87
		/**
88
		 * Get instance.
89
		 *
90
		 * @since  2.0
91
		 * @access public
92
		 *
93
		 * @param string $email_notification_id
94
		 *
95
		 * @return Give_Email_Notification
96
		 */
97
		public static function get_instance( $email_notification_id = '' ) {
98
			$class = '';
99
100
			if ( ! empty( $email_notification_id ) ) {
101
				/* @var Give_Email_Notification $class */
102
				foreach ( self::$singleton as $class ) {
103
					if ( $email_notification_id === $class->config['id'] ) {
104
						$class = get_class( $class );
105
						break;
106
					}
107
				}
108
			} else {
109
				$class = get_called_class();
110
			}
111
112
			if ( ! empty( $class ) && ( ! array_key_exists( $class, self::$singleton ) || is_null( self::$singleton[ $class ] ) ) ) {
113
				self::$singleton[ $class ] = new $class();
114
			}
115
116
			return ( isset( self::$singleton[ $class ] ) ? self::$singleton[ $class ] : null );
117
		}
118
119
		/**
120
		 * Setup action and filters.
121
		 *
122
		 * @access  public
123
		 * @since   2.0
124
		 *
125
		 * @param array $config
126
		 */
127
		public function load( $config ) {
128
			// Set notification configuration.
129
			$this->config = wp_parse_args( $config, $this->config );
130
131
			// Set email preview header status.
132
			$this->config['has_preview_header'] = $this->config['has_preview'] && $this->config['has_preview_header'] ? true : false;
133
134
			// Set email content type
135
			$this->config['content_type'] = empty( $this->config['content_type'] ) || ! in_array(
136
				$this->config['content_type'], array(
137
					'text/html',
138
					'text/plain',
139
				)
140
			)
141
				? Give()->emails->get_content_type()
142
				: $this->config['content_type'];
143
			$this->config['content_type'] = give_get_option( Give_Email_Setting_Field::get_prefix( $this ) . 'email_content_type', $this->config['content_type'] );
144
145
			// Set email template type.
146
			$this->config['email_template'] = empty( $this->config['email_template'] )
147
				? give_get_option( 'email_template' )
148
				: $this->config['email_template'];
149
150
			// Set recipient group name.
151
			$this->config['recipient_group_name'] = empty( $this->config['recipient_group_name'] )
152
				? ( ! Give_Email_Notification_Util::has_recipient_field( $this ) ? __( 'Donor', 'give' ) : '' )
153
				: $this->config['recipient_group_name'];
154
155
			// Non notification status editable notice.
156
			$this->config['notices']['non-notification-status-editable'] = empty( $this->config['notices']['non-notification-status-editable'] )
157
				? __( 'You can not edit notification status from here.', 'give' )
158
				: $this->config['notices']['non-notification-status-editable'];
159
160
			/**
161
			 *  Filter the notification config.
162
			 *
163
			 * @since 2.0
164
			 *
165
			 * @param                         array                   Give_Email_Notification::config
166
			 * @param Give_Email_Notification $this
167
			 */
168
			$this->config = apply_filters( 'give_email_api_notification_config', $this->config, $this );
169
170
			// Setup filters.
171
			$this->setup_filters();
172
		}
173
174
175
		/**
176
		 * Setup filters.
177
		 *
178
		 * @since  2.0
179
		 * @access public
180
		 */
181
		private function setup_filters() {
182
			// Do not setup filters if not admin.
183
			if ( ! is_admin() ) {
184
				return;
185
			}
186
187
			// Apply filter only for current email notification section.
188
			if ( isset( $_GET['section'] ) && give_get_current_setting_section() === $this->config['id'] ) {
189
				// Initialize email context for email notification.
190
				$this->config['email_tag_context'] = apply_filters(
191
					"give_{$this->config['id']}_email_tag_context",
192
					$this->config['email_tag_context'],
193
					$this
194
				);
195
			}
196
197
			// Setup setting fields.
198
			if ( $this->config['show_on_emails_setting_page'] ) {
199
				add_filter( 'give_get_settings_emails', array( $this, 'add_setting_fields' ), 10, 2 );
200
			}
201
202
			if ( $this->config['form_metabox_setting'] && ! empty( $this->config['form_metabox_id'] ) ) {
203
				add_filter(
204
					$this->config['form_metabox_id'],
205
					array( $this, 'add_metabox_setting_field' ),
206
					10,
207
					2
208
				);
209
			}
210
211
			if ( $this->config['has_recipient_field'] ) {
212
				add_action(
213
					"give_save__give_{$this->config['id']}_recipient",
214
					array( $this, 'validate_form_recipient_field_value' ),
215
					10,
216
					3
217
				);
218
			}
219
220
			/**
221
			 * Filter the default email subject.
222
			 *
223
			 * @since 2.0
224
			 */
225
			$this->config['default_email_subject'] = apply_filters(
226
				"give_{$this->config['id']}_get_default_email_subject",
227
				$this->config['default_email_subject'],
228
				$this
229
			);
230
231
			/**
232
			 * Filter the default email message.
233
			 *
234
			 * @since 2.0
235
			 */
236
			$this->config['default_email_message'] = apply_filters(
237
				"give_{$this->config['id']}_get_default_email_message",
238
				$this->config['default_email_message'],
239
				$this
240
			);
241
242
			/**
243
			 * Filter the default email header.
244
			 *
245
			 * @since 2.1.3
246
			 */
247
			$this->config['default_email_header'] = apply_filters(
248
				"give_{$this->config['id']}_get_default_email_header",
249
				$this->config['default_email_header'],
250
				$this
251
			);
252
		}
253
254
		/**
255
		 * Add sections.
256
		 *
257
		 * @since 2.0
258
		 *
259
		 * @param array $sections
260
		 *
261
		 * @return array
262
		 */
263
		public function add_section( $sections ) {
264
			$sections[ $this->config['id'] ] = $this->config['label'];
265
266
			return $sections;
267
		}
268
269
		/**
270
		 * Add sections.
271
		 *
272
		 * @since 2.0
273
		 *
274
		 * @param bool $hide_section
275
		 *
276
		 * @return bool
277
		 */
278
		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...
279
			$hide_section = true;
280
281
			return $hide_section;
282
		}
283
284
		/**
285
		 * Register email settings.
286
		 *
287
		 * @since  2.0
288
		 * @access public
289
		 *
290
		 * @param   array $settings
291
		 *
292
		 * @return  array
293
		 */
294
		public function add_setting_fields( $settings ) {
295
			if ( $this->config['id'] === give_get_current_setting_section() ) {
296
				$settings = $this->get_setting_fields();
297
			}
298
299
			return $settings;
300
		}
301
302
303
		/**
304
		 * Get setting fields
305
		 *
306
		 * @since  2.0
307
		 * @access public
308
		 *
309
		 * @param int $form_id
310
		 *
311
		 * @return array
312
		 */
313
		public function get_setting_fields( $form_id = null ) {
314
			return Give_Email_Setting_Field::get_setting_fields( $this, $form_id );
315
		}
316
317
318
		/**
319
		 * Register email settings to form metabox.
320
		 *
321
		 * @since  2.0
322
		 * @access public
323
		 *
324
		 * @param array $settings
325
		 * @param int   $form_id
326
		 *
327
		 * @return array
328
		 */
329
		public function add_metabox_setting_field( $settings, $form_id ) {
330
331
			if ( Give_Email_Notification_Util::is_email_notification_active( $this ) ) {
332
				$settings[] = array(
333
					'id'     => $this->config['id'],
334
					'title'  => $this->config['label'],
335
					'fields' => $this->get_setting_fields( $form_id ),
336
				);
337
			}
338
339
			return $settings;
340
		}
341
342
343
		/**
344
		 * Get extra setting field.
345
		 *
346
		 * @since  2.0
347
		 * @access public
348
		 *
349
		 * @param int $form_id
350
		 *
351
		 * @return array
352
		 */
353
		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...
354
			return array();
355
		}
356
357
358
		/**
359
		 * Get recipient(s).
360
		 *
361
		 * Note: in case of admin notification this fx will return array of emails otherwise empty string or email of donor.
362
		 *
363
		 * @since  2.0
364
		 * @access public
365
		 *
366
		 * @param int $form_id
367
		 *
368
		 * @return string|array
369
		 */
370
		public function get_recipient( $form_id = null ) {
371
			if ( empty( $this->recipient_email ) && $this->config['has_recipient_field'] ) {
372
				$this->recipient_email = Give_Email_Notification_Util::get_value(
373
					$this,
374
					Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'recipient',
375
					$form_id
376
				);
377
378
				/**
379
				 * Filter the admin notice emails.
380
				 *
381
				 * @since 1.0
382
				 * @deprecated 2.0
383
				 */
384
				$this->recipient_email = apply_filters( 'give_admin_notice_emails', $this->recipient_email, $this, $form_id );
385
			}
386
387
			/**
388
			 * Filter the recipients
389
			 *
390
			 * @since 2.0
391
			 */
392
			return apply_filters(
393
				"give_{$this->config['id']}_get_recipients",
394
				give_check_variable(
395
					$this->recipient_email,
396
					'empty',
397
					Give()->emails->get_from_address()
398
				),
399
				$this,
400
				$form_id
401
			);
402
		}
403
404
		/**
405
		 * Get notification status.
406
		 *
407
		 * @since  2.0
408
		 * @access public
409
		 *
410
		 * @param int $form_id
411
		 *
412
		 * @return bool
413
		 */
414
		public function get_notification_status( $form_id = null ) {
415
			$notification_status = empty( $form_id )
416
				? give_get_option( "{$this->config['id']}_notification", $this->config['notification_status'] )
417
				: 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...
418
419
			/**
420
			 * Filter the notification status.
421
			 *
422
			 * @since 1.8
423
			 */
424
			return apply_filters(
425
				"give_{$this->config['id']}_get_notification_status",
426
				$notification_status,
427
				$this,
428
				$form_id
429
			);
430
		}
431
432
		/**
433
		 * Get email subject.
434
		 *
435
		 * @since  2.0
436
		 * @access public
437
		 *
438
		 * @param int $form_id
439
		 *
440
		 * @return string
441
		 */
442 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...
443
			$subject = wp_strip_all_tags(
444
				Give_Email_Notification_Util::get_value(
445
					$this,
446
					Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_subject',
447
					$form_id,
448
					$this->config['default_email_subject']
449
				)
450
			);
451
452
			/**
453
			 * Filter the subject.
454
			 *
455
			 * @since 2.0
456
			 */
457
			return apply_filters(
458
				"give_{$this->config['id']}_get_email_subject",
459
				$subject,
460
				$this,
461
				$form_id
462
			);
463
		}
464
465
		/**
466
		 * Get email message.
467
		 *
468
		 * @since  2.0
469
		 * @access public
470
		 *
471
		 * @param int $form_id
472
		 *
473
		 * @return string
474
		 */
475
		public function get_email_message( $form_id = null ) {
476
			$message = Give_Email_Notification_Util::get_value(
477
				$this,
478
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_message',
479
				$form_id,
480
				$this->config['default_email_message']
481
			);
482
483
			/**
484
			 * Filter the message.
485
			 *
486
			 * @since 2.0
487
			 */
488
			return apply_filters(
489
				"give_{$this->config['id']}_get_email_message",
490
				$message,
491
				$this,
492
				$form_id
493
			);
494
		}
495
496
		/**
497
		 * Get email header.
498
		 *
499
		 * @param int $form_id The Form ID.
500
		 *
501
		 * @since  2.1.3
502
		 *
503
		 * @return string
504
		 */
505
		public function get_email_header( $form_id = null ) {
506
			$header = Give_Email_Notification_Util::get_value(
507
				$this,
508
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_header',
509
				$form_id,
510
				$this->config['default_email_header']
511
			);
512
513
			/**
514
			 * Filter the header.
515
			 *
516
			 * @since 2.1.3
517
			 */
518
			return apply_filters(
519
				"give_{$this->config['id']}_get_email_header",
520
				$header,
521
				$this,
522
				$form_id
523
			);
524
		}
525
526
		/**
527
		 * Get email content type.
528
		 *
529
		 * @since  2.0
530
		 * @access public
531
		 *
532
		 * @param $form_id
533
		 *
534
		 * @return string
535
		 */
536
		public function get_email_content_type( $form_id ) {
537
			$content_type = Give_Email_Notification_Util::get_value(
538
				$this,
539
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_content_type',
540
				$form_id,
541
				$this->config['content_type']
542
			);
543
544
			/**
545
			 * Filter the email content type.
546
			 *
547
			 * @since 2.0
548
			 */
549
			return apply_filters(
550
				"give_{$this->config['id']}_get_email_content_type",
551
				$content_type,
552
				$this,
553
				$form_id
554
			);
555
		}
556
557
		/**
558
		 * Get email template.
559
		 *
560
		 * @since  2.0
561
		 * @access public
562
		 *
563
		 * @param $form_id
564
		 *
565
		 * @return string
566
		 */
567
		public function get_email_template( $form_id ) {
568
			$email_template = give_get_meta( $form_id, '_give_email_template', true );
569
			$email_template = Give_Email_Notification_Util::get_value(
570
				$this,
571
				Give_Email_Setting_Field::get_prefix( $this, $form_id ) . 'email_template',
572
				$form_id,
573
				! empty( $email_template ) && Give_Email_Notification_Util::can_use_form_email_options( $this, $form_id ) ?
574
					$email_template :
575
					$this->config['email_template']
576
			);
577
578
			/**
579
			 * Filter the email template.
580
			 *
581
			 * @since 2.0
582
			 */
583
			return apply_filters(
584
				"give_{$this->config['id']}_get_email_template",
585
				$email_template,
586
				$this,
587
				$form_id
588
			);
589
		}
590
591
592
		/**
593
		 * Get allowed email tags for current email notification.
594
		 *
595
		 * @since  2.0
596
		 * @access private
597
		 *
598
		 * @param bool $formatted
599
		 *
600
		 * @return array
601
		 */
602
		public function get_allowed_email_tags( $formatted = false ) {
603
			// Get all email tags.
604
			$email_tags = Give()->email_tags->get_tags();
605
606
			// Skip if all email template tags context setup exit.
607
			if ( $this->config['email_tag_context'] && 'all' !== $this->config['email_tag_context'] ) {
608
				$email_context = (array) $this->config['email_tag_context'];
609
610
				foreach ( $email_tags as $index => $email_tag ) {
611
					if ( in_array( $email_tag['context'], $email_context ) ) {
612
						continue;
613
					}
614
615
					unset( $email_tags[ $index ] );
616
				}
617
			}
618
619
			/**
620
			 * Disallow tags on Email Notifications which don't have a
621
			 * recipient and if the tag's is_admin property is set to true.
622
			 */
623
			if ( false === $this->config['has_recipient_field'] ) {
624
				foreach ( $email_tags as $index => $email_tag ) {
625
					if ( true === $email_tag['is_admin'] ) {
626
						unset( $email_tags[ $index ] );
627
					}
628
				}
629
			}
630
631
			if ( count( $email_tags ) && $formatted ) :
632
				ob_start() ?>
633
				<ul class="give-email-tags-wrap">
634 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...
635
						<li class="give_<?php echo $email_tag['tag']; ?>_tag">
636
							<code>{<?php echo $email_tag['tag']; ?>}</code> - <?php echo $email_tag['desc']; ?>
637
						</li>
638
					<?php endforeach; ?>
639
				</ul>
640
				<?php
641
				$email_tags = ob_get_clean();
642
			endif;
643
644
			return $email_tags;
645
		}
646
647
		/**
648
		 * Get preview email recipients.
649
		 *
650
		 * @since  2.0
651
		 * @access public
652
		 *
653
		 * @param int $form_id
654
		 *
655
		 * @return array|string
656
		 */
657
		public function get_preview_email_recipient( $form_id = null ) {
658
			$recipients = $this->get_recipient( $form_id );
659
660
			/**
661
			 * Filter the preview email recipients.
662
			 *
663
			 * @since 2.0
664
			 *
665
			 * @param string|array            $recipients List of recipients.
666
			 * @param Give_Email_Notification $this
667
			 */
668
			$recipients = apply_filters( 'give_get_preview_email_recipient', $recipients, $this, $form_id );
669
670
			return $recipients;
671
		}
672
673
		/**
674
		 * Get the recipient attachments.
675
		 *
676
		 * @since  2.0
677
		 * @access public
678
		 *
679
		 * @param int $form_id
680
		 *
681
		 * @return array
682
		 */
683
		public function get_email_attachments( $form_id = null ) {
684
			/**
685
			 * Filter the attachment.
686
			 *
687
			 * @since 2.0
688
			 */
689
			return apply_filters( "give_{$this->config['id']}_get_email_attachments", array(), $this, $form_id );
690
		}
691
692
693
		/**
694
		 * Send preview email.
695
		 *
696
		 * @since  2.0
697
		 * @access public
698
		 *
699
		 * @param bool $send Flag to check if send email or not.
700
		 *
701
		 * @return bool
702
		 */
703
		public function send_preview_email( $send = true ) {
704
			// Get form id
705
			$form_id = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null;
706
707
			// setup email data.
708
			$this->setup_email_data();
709
710
			$attachments  = $this->get_email_attachments();
711
			$message      = $this->preview_email_template_tags( $this->get_email_message( $form_id ) );
712
			$subject      = $this->preview_email_template_tags( $this->get_email_subject( $form_id ) );
713
			$content_type = $this->get_email_content_type( $form_id );
714
715
			// Setup email content type.
716
			Give()->emails->__set( 'content_type', $content_type );
717
			Give()->emails->__set( 'html', true );
718
719
			// Setup email template
720
			Give()->emails->__set( 'template', $this->get_email_template( $form_id ) );
721
722
			// Set email header.
723
			Give()->emails->__set( 'heading', $this->preview_email_template_tags( $this->get_email_header( $form_id ) ) );
724
725
			// Format plain content type email.
726 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...
727
				Give()->emails->__set( 'html', false );
728
				Give()->emails->__set( 'template', 'none' );
729
				$message = strip_tags( $message );
730
			}
731
732 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...
733
				Give()->emails->form_id      = $form_id;
734
				Give()->emails->from_name    = give_get_meta( $form_id, '_give_from_name', true );
735
				Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true );
736
			}
737
738
			return $send
739
				? 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...
740
				: false;
741
		}
742
743
744
		/**
745
		 * Send email notification.
746
		 *
747
		 * Note: To render email tags in all context certain parameters are necessary for core (includes/emails/class-give-emails):
748
		 *    1. payment_id
749
		 *    2. user_id
750
		 *    3. form_id
751
		 *    4. donor_id
752
		 *    5. for third party email tags you can pass necessary param along above parameters other value replace by empty string.
753
		 *
754
		 * @since  2.0
755
		 * @access public
756
		 *
757
		 * @param array $email_tag_args Arguments which helps to decode email template tags.
758
		 *
759
		 * @return bool
760
		 */
761
		public function send_email_notification( $email_tag_args = array() ) {
762
			/**
763
			 * Fire the filter
764
			 *
765
			 * @since 2.2.3
766
			 */
767
			if ( apply_filters( 'give_is_stop_email_notification', false, $this ) ) {
768
				return false;
769
			}
770
771
			// Add email content type email tags.
772
			$email_tag_args['email_content_type'] = $this->config['content_type'];
773
774
			/**
775
			 * Filter the email tag args
776
			 *
777
			 * @since 2.0
778
			 */
779
			$email_tag_args = apply_filters( "give_{$this->config['id']}_email_tag_args", $email_tag_args, $this );
780
781
			// Get form id.
782
			$form_id = ! empty( $email_tag_args['form_id'] )
783
				? absint( $email_tag_args['form_id'] )
784
				: ( ! empty( $email_tag_args['payment_id'] ) ? give_get_payment_form_id( $email_tag_args['payment_id'] ) : null );
785
786
			// Do not send email if notification is disabled.
787
			if ( ! Give_Email_Notification_Util::is_email_notification_active( $this, $form_id ) ) {
788
				return false;
789
			}
790
791
			/**
792
			 * Fire action after before email send.
793
			 *
794
			 * @since 2.0
795
			 */
796
			do_action( "give_{$this->config['id']}_email_send_before", $this, $form_id );
797
798
			$attachments  = $this->get_email_attachments();
799
			$message      = give_do_email_tags( $this->get_email_message( $form_id ), $email_tag_args );
800
			$subject      = give_do_email_tags( $this->get_email_subject( $form_id ), $email_tag_args );
801
			$content_type = $this->get_email_content_type( $form_id );
802
803
			// Setup email content type.
804
			Give()->emails->__set( 'content_type', $content_type );
805
			Give()->emails->__set( 'html', true );
806
807
			// Set email template.
808
			Give()->emails->__set( 'template', $this->get_email_template( $form_id ) );
809
810
			// Set email header.
811
			Give()->emails->__set( 'heading', give_do_email_tags( $this->get_email_header( $form_id ), $email_tag_args ) );
812
813 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...
814
				Give()->emails->__set( 'html', false );
815
				Give()->emails->__set( 'template', 'none' );
816
				$message = strip_tags( $message );
817
			}
818
819 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...
820
				Give()->emails->form_id      = $form_id;
821
				Give()->emails->from_name    = give_get_meta( $form_id, '_give_from_name', true );
822
				Give()->emails->from_address = give_get_meta( $form_id, '_give_from_email', true );
823
			}
824
825
			// Send email.
826
			$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...
827
828
			/**
829
			 * Fire action after after email send.
830
			 *
831
			 * @since 2.0
832
			 */
833
			do_action( "give_{$this->config['id']}_email_send_after", $email_status, $this, $form_id );
834
835
			return $email_status;
836
		}
837
838
839
		/**
840
		 * Decode preview email template tags.
841
		 *
842
		 * @since 2.0
843
		 *
844
		 * @param string $message Email Template Message.
845
		 *
846
		 * @return string
847
		 */
848
		public function preview_email_template_tags( $message ) {
849
850
			$get_data = give_clean( filter_input_array( INPUT_GET ) );
851
852
			// Set Payment.
853
			$payment_id = give_check_variable( $get_data, '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...
854
			$payment    = $payment_id ? new Give_Payment( $payment_id ) : new stdClass();
855
856
			// Set donor.
857
			$user_id = $payment_id
858
				? $payment->user_id
859
				: give_check_variable( $get_data, '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...
860
			$user_id = $user_id ? $user_id : wp_get_current_user()->ID;
861
862
			$receipt_link_url = give_get_receipt_url( $payment_id );
863
			$receipt_link     = give_get_receipt_link( $payment_id );
864
865
			// Set default values for tags.
866
			$this->config['preview_email_tags_values'] = wp_parse_args(
867
				$this->config['preview_email_tags_values'],
868
				array(
869
					'name'                    => give_email_tag_first_name(
870
						array(
871
							'payment_id' => $payment_id,
872
							'user_id'    => $user_id,
873
						)
874
					),
875
					'fullname'                => give_email_tag_fullname(
876
						array(
877
							'payment_id' => $payment_id,
878
							'user_id'    => $user_id,
879
						)
880
					),
881
					'username'                => give_email_tag_username(
882
						array(
883
							'payment_id' => $payment_id,
884
							'user_id'    => $user_id,
885
						)
886
					),
887
					'user_email'              => give_email_tag_user_email(
888
						array(
889
							'payment_id' => $payment_id,
890
							'user_id'    => $user_id,
891
						)
892
					),
893
					'payment_total'           => $payment_id ? give_email_tag_payment_total( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
894
					'amount'                  => $payment_id ? give_email_tag_amount( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
895
					'price'                   => $payment_id ? give_email_tag_price( array( 'payment_id' => $payment_id ) ) : give_currency_filter( '10.50' ),
896
					'payment_method'          => $payment_id ? give_email_tag_payment_method( array( 'payment_id' => $payment_id ) ) : __( 'PayPal', 'give' ),
897
					'payment_id'              => $payment_id ? $payment_id : rand( 2000, 2050 ),
898
					'receipt_link_url'        => $receipt_link_url,
899
					'receipt_link'            => $receipt_link,
900
					'date'                    => $payment_id ? date( give_date_format(), strtotime( $payment->date ) ) : date( give_date_format(), current_time( 'timestamp' ) ),
901
					'donation'                => $payment_id ? give_email_tag_donation( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title', 'give' ),
902
					'form_title'              => $payment_id ? give_email_tag_form_title( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donation Form Title - Sample Donation Level', 'give' ),
903
					'sitename'                => $payment_id ? give_email_tag_sitename( array( 'payment_id' => $payment_id ) ) : get_bloginfo( 'name' ),
904
					'billing_address'         => $payment_id ? give_email_tag_billing_address( array( 'payment_id' => $payment_id ) ) : '',
905
					'email_access_link'       => sprintf(
906
						'<a href="%1$s">%2$s</a>',
907
						add_query_arg(
908
							array(
909
								'give_nl' => uniqid(),
910
							),
911
							give_get_history_page_uri()
912
						),
913
						__( 'View your donation history &raquo;', 'give' )
914
					),
915
					'donation_history_link'   => sprintf(
916
						'<a href="%1$s">%2$s</a>',
917
						add_query_arg(
918
							array(
919
								'give_nl' => uniqid(),
920
							),
921
							give_get_history_page_uri()
922
						),
923
						__( 'View your donation history &raquo;', 'give' )
924
					),
925
					'reset_password_link'     => $user_id ? give_email_tag_reset_password_link( array( 'user_id' => $user_id ), $payment_id ) : '',
926
					'site_url'                => sprintf(
927
						'<a href="%1$s">%2$s</a>',
928
						get_bloginfo( 'url' ),
929
						get_bloginfo( 'url' )
930
					),
931
					'admin_email'             => give_email_admin_email(),
932
					'offline_mailing_address' => give_email_offline_mailing_address(),
933
					'donor_comment'           => $payment_id ? give_email_donor_comment( array( 'payment_id' => $payment_id ) ) : esc_html__( 'Sample Donor Comment', 'give' ),
934
				)
935
			);
936
937
			// Decode tags.
938
			foreach ( $this->config['preview_email_tags_values'] as $preview_tag => $value ) {
939
				if ( isset( $this->config['preview_email_tags_values'][ $preview_tag ] ) ) {
940
					$message = str_replace( "{{$preview_tag}}", $this->config['preview_email_tags_values'][ $preview_tag ], $message );
941
				}
942
			}
943
944
			return apply_filters( 'give_email_preview_template_tags', $message );
945
		}
946
947
		/**
948
		 * Setup email data
949
		 *
950
		 * @since 2.0
951
		 */
952
		public function setup_email_data() {
953
		}
954
955
956
		/**
957
		 * Validate email form setting
958
		 *
959
		 * Note: internal use only
960
		 *
961
		 * @since  2.0
962
		 * @access public
963
		 *
964
		 * @param $form_meta_key
965
		 * @param $form_meta_value
966
		 * @param $post_id
967
		 */
968
		public function validate_form_recipient_field_value( $form_meta_key, $form_meta_value, $post_id ) {
969
			// Get valid emails.
970
			$new_form_meta_value = array_filter(
971
				$form_meta_value, function ( $value ) {
972
					return ! empty( $value['email'] ) && is_email( $value['email'] );
973
				}
974
			);
975
976
			// Remove duplicate emails from array.
977
			$email_arr = array();
978
			foreach ( $new_form_meta_value as $index => $email ) {
979
				if ( in_array( $email['email'], $email_arr ) ) {
980
					unset( $new_form_meta_value[ $index ] );
981
					continue;
982
				}
983
984
				$email_arr[] = $email['email'];
985
			}
986
987
			$update = false;
988
989
			if ( empty( $new_form_meta_value ) ) {
990
				// Set default recipient.
991
				$form_meta_value = array(
992
					array(
993
						'email' => get_bloginfo( 'admin_email' ),
994
					),
995
				);
996
997
				$update = true;
998
999
			} elseif ( count( $new_form_meta_value ) !== count( $form_meta_value ) ) {
1000
				// Filter recipient emails.
1001
				$form_meta_value = $new_form_meta_value;
1002
1003
				$update = true;
1004
			}
1005
1006
			if ( $update ) {
1007
				give_update_meta( $post_id, $form_meta_key, $form_meta_value );
1008
			}
1009
		}
1010
	}
1011
1012
endif; // End class_exists check
1013