Give_Email_Notification::get_email_message()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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