Test Failed
Pull Request — master (#2668)
by Devin
07:24 queued 30s
created

Give_Email_Notifications   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 418
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 418
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 2
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 0 7 2
B init() 0 30 4
A add_metabox_setting_fields() 0 70 2
B add_emails_notifications() 0 28 3
A get_email_notifications() 0 3 1
C preview_email() 0 69 8
A email_preview_header() 0 12 1
A email_preview_data() 0 6 1
A email_preview_message() 0 10 3
B send_preview_email() 0 24 5
A load() 0 3 1
B validate_settings() 0 17 5
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
/**
15
 * Class Give_Email_Notifications
16
 */
17
class Give_Email_Notifications {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
18
	/**
19
	 * Instance.
20
	 *
21
	 * @since  2.0
22
	 * @access static
23
	 * @var
24
	 */
25
	static private $instance;
26
27
	/**
28
	 * Array of email notifications.
29
	 *
30
	 * @since  2.0
31
	 * @access private
32
	 * @var array
33
	 */
34
	private $emails = array();
35
36
	/**
37
	 * Singleton pattern.
38
	 *
39
	 * @since  2.0
40
	 * @access private
41
	 * Give_Payumoney_API constructor.
42
	 */
43
	private function __construct() {
44
	}
45
46
47
	/**
48
	 * Get instance.
49
	 *
50
	 * @since  2.0
51
	 * @access static
52
	 * @return static
53
	 */
54
	static function get_instance() {
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...
55
		if ( null === static::$instance ) {
56
			self::$instance = new static();
57
		}
58
59
		return self::$instance;
60
	}
61
62
	/**
63
	 * Setup dependencies
64
	 *
65
	 * @since 2.0
66
	 */
67
	public function init() {
68
		// Load files.
69
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/ajax-handler.php';
70
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-setting-field.php';
71
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/filters.php';
72
73
		// Load email notifications.
74
		$this->add_emails_notifications();
75
76
		add_filter( 'give_metabox_form_data_settings', array( $this, 'add_metabox_setting_fields' ), 10, 2 );
77
		add_action( 'init', array( $this, 'preview_email' ) );
78
		add_action( 'init', array( $this, 'send_preview_email' ) );
79
		add_action( 'init', array( $this, 'validate_settings' ) );
80
81
		/* @var Give_Email_Notification $email */
82
		foreach ( $this->get_email_notifications() as $email ) {
83
			// Setup email section.
84
			if( Give_Email_Notification_Util::is_show_on_emails_setting_page( $email ) ) {
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...
85
				add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) );
86
				add_filter( "give_hide_section_{$email->config['id']}_on_emails_page", array( $email, 'hide_section' ) );
87
			}
88
89
			// Setup email preview.
90
			if ( Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) {
91
				add_action( "give_{$email->config['id']}_email_preview", array( $this, 'email_preview_header' ) );
92
				add_filter( "give_{$email->config['id']}_email_preview_data", array( $this, 'email_preview_data' ) );
93
				add_filter( "give_{$email->config['id']}_email_preview_message", array( $this, 'email_preview_message' ), 1, 2 );
94
			}
95
		}
96
	}
97
98
99
	/**
100
	 * Add setting to metabox.
101
	 *
102
	 * @since  2.0
103
	 * @access public
104
	 *
105
	 * @param array $settings
106
	 * @param int   $post_id
107
	 *
108
	 * @return array
109
	 */
110
	public function add_metabox_setting_fields( $settings, $post_id ) {
111
		$emails = $this->get_email_notifications();
112
113
		// Bailout.
114
		if ( empty( $emails ) ) {
115
			return $settings;
116
		}
117
118
		// Email notification setting.
119
		$settings['email_notification_options'] = array(
120
			'id'         => 'email_notification_options',
121
			'title'      => __( 'Email Notification', 'give' ),
122
			'fields'     => array(
123
				array(
124
					'name'        => __( 'Email Options', 'give' ),
125
					'description' => __( '', 'give' ),
126
					'id'          => '_give_email_options',
127
					'type'        => 'radio_inline',
128
					'default'     => 'global',
129
					'options'     => array(
130
						'global'   => __( 'Global Options' ),
131
						'enabled'  => __( 'Customize', 'give' ),
132
					),
133
				),
134
				array(
135
					'id'      => '_give_email_template',
136
					'name'    => esc_html__( 'Email Template', 'give' ),
137
					'desc'    => esc_html__( 'Choose your template from the available registered template types.', 'give' ),
138
					'type'    => 'select',
139
					'default' => 'default',
140
					'options' => give_get_email_templates(),
141
				),
142
				array(
143
					'id'   => '_give_email_logo',
144
					'name' => esc_html__( 'Logo', 'give' ),
145
					'desc' => esc_html__( 'Upload or choose a logo to be displayed at the top of the donation receipt emails. Displayed on HTML emails only.', 'give' ),
146
					'type' => 'file',
147
				),
148
				array(
149
					'id'      => '_give_from_name',
150
					'name'    => esc_html__( 'From Name', 'give' ),
151
					'desc'    => esc_html__( 'The name which appears in the "From" field in all Give donation emails.', 'give' ),
152
					'default' => get_bloginfo( 'name' ),
153
					'type'    => 'text',
154
				),
155
				array(
156
					'id'      => '_give_from_email',
157
					'name'    => esc_html__( 'From Email', 'give' ),
158
					'desc'    => esc_html__( 'Email address from which all Give emails are sent from. This will act as the "from" and "reply-to" email address.', 'give' ),
159
					'default' => get_bloginfo( 'admin_email' ),
160
					'type'    => 'text',
161
				),
162
				array(
163
					'name'  => 'email_notification_docs',
164
					'type'  => 'docs_link',
165
					'url'   => 'http://docs.givewp.com/email-notification',
166
					'title' => __( 'Email Notification', 'give' ),
167
				),
168
			),
169
170
			/**
171
			 * Filter the email notification settings.
172
			 *
173
			 * @since 2.0
174
			 */
175
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
176
		);
177
178
		return $settings;
179
	}
180
181
	/**
182
	 * Add email notifications
183
	 *
184
	 * @since  2.0
185
	 * @access private
186
	 */
187
	private function add_emails_notifications() {
188
		$this->emails = array(
189
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
190
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
191
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
192
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
193
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
194
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
195
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php',
196
		);
197
198
		/**
199
		 * Filter the email notifications.
200
		 *
201
		 * @since 2.0
202
		 */
203
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
204
205
		// Bailout.
206
		if ( empty( $this->emails ) ) {
207
			return;
208
		}
209
210
		// Initiate email notifications.
211
		foreach ( $this->emails as $email ) {
212
			$email->init();
213
		}
214
	}
215
216
217
	/**
218
	 * Get list of email notifications.
219
	 *
220
	 * @since  2.0
221
	 * @access public
222
	 * @return array
223
	 */
224
	public function get_email_notifications() {
225
		return $this->emails;
226
	}
227
228
229
	/**
230
	 * Displays the email preview
231
	 *
232
	 * @since  2.0
233
	 * @access public
234
	 * @return bool|null
235
	 */
236
	public function preview_email() {
237
		// Bailout.
238
		if ( ! Give_Email_Notification_Util::can_preview_email() ) {
239
			return false;
240
		}
241
242
		// Security check.
243
		give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_GET
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
244
245
		// Get email type.
246
		$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : '';
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
247
248
		/* @var Give_Email_Notification $email */
249
		foreach ( $this->get_email_notifications() as $email ) {
250
			if ( $email_type !== $email->config['id'] ) {
251
				continue;
252
			}
253
254
			// Set form id.
255
			$form_id = empty( $_GET['form_id']  ) ? null : absint( $_GET['form_id'] );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 2 found
Loading history...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
256
257
			// Call setup email data to apply filter and other thing to email.
258
			$email->setup_email_data();
259
260
			// Decode message.
261
			$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) );
262
263
			// Set email template.
264
			Give()->emails->html    = true;
265
			Give()->emails->__set( 'template', $email->get_email_template( $form_id ) );
266
267
			if ( 'text/plain' === $email->config['content_type'] ) {
268
				// Give()->emails->__set( 'html', false );
269
				Give()->emails->__set( 'template', 'none' );
270
			}
271
272
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
273
274
				/**
275
				 * Filter the email preview data
276
				 *
277
				 * @since 2.0
278
				 *
279
				 * @param array
280
				 */
281
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
282
283
				/**
284
				 * Fire the give_{$email_type}_email_preview action
285
				 *
286
				 * @since 2.0
287
				 */
288
				do_action( "give_{$email_type}_email_preview", $email );
289
290
				/**
291
				 * Filter the email message
292
				 *
293
				 * @since 2.0
294
				 *
295
				 * @param string                  $email_message
296
				 * @param array                   $email_preview_data
297
				 * @param Give_Email_Notification $email
298
				 */
299
				echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email );
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'apply_filters'
Loading history...
300
301
				exit();
302
			}
303
		}// End foreach().
304
	}
305
306
307
	/**
308
	 * Add header to donation receipt email preview
309
	 *
310
	 * @since   2.0
311
	 * @access  public
312
	 *
313
	 * @param Give_Email_Notification $email
314
	 */
315
	public function email_preview_header( $email ) {
316
		/**
317
		 * Filter the all email preview headers.
318
		 *
319
		 * @since 2.0
320
		 *
321
		 * @param Give_Email_Notification $email
322
		 */
323
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
324
325
		echo $email_preview_header;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$email_preview_header'
Loading history...
326
	}
327
328
	/**
329
	 * Add email preview data
330
	 *
331
	 * @since   2.0
332
	 * @access  public
333
	 *
334
	 * @param array $email_preview_data
335
	 *
336
	 * @return array
337
	 */
338
	public function email_preview_data( $email_preview_data ) {
339
		$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 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...
340
		$email_preview_data['user_id']    = absint( give_check_variable( give_clean( $_GET ), 'isset', 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...
341
342
		return $email_preview_data;
343
	}
344
345
	/**
346
	 * Replace email template tags.
347
	 *
348
	 * @since   2.0
349
	 * @access  public
350
	 *
351
	 * @param string $email_message
352
	 * @param array  $email_preview_data
353
	 *
354
	 * @return string
355
	 */
356
	public function email_preview_message( $email_message, $email_preview_data ) {
357
		if (
358
			! empty( $email_preview_data['payment_id'] )
359
			|| ! empty( $email_preview_data['user_id'] )
360
		) {
361
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
362
		}
363
364
		return $email_message;
365
	}
366
367
	/**
368
	 * Displays the email preview
369
	 *
370
	 * @since  2.0
371
	 * @access public
372
	 * @return bool|null
373
	 */
374
	public function send_preview_email() {
375
		// Bailout.
376
		if ( ! Give_Email_Notification_Util::can_send_preview_email() ) {
377
			return false;
378
		}
379
380
		// Security check.
381
		give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-validated input variable: $_GET
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
382
383
		// Get email type.
384
		$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' );
0 ignored issues
show
Documentation introduced by
'' 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...
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
385
386
		/* @var Give_Email_Notification $email */
387
		foreach ( $this->get_email_notifications() as $email ) {
388
			if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) {
389
				$email->send_preview_email();
390
				break;
391
			}
392
		}
393
394
		// Remove the test email query arg.
395
		wp_redirect( remove_query_arg( 'give_action' ) );
396
		exit;
397
	}
398
399
400
	/**
401
	 * Load Give_Email_Notifications
402
	 *
403
	 * @since  2.0
404
	 * @access public
405
	 */
406
	public function load() {
407
		add_action( 'init', array( $this, 'init' ), -1 );
408
	}
409
410
411
	/**
412
	 * Verify email setting before saving
413
	 *
414
	 * @since  2.0
415
	 * @access public
416
	 */
417
	public function validate_settings() {
418
		// Bailout.
419
		if (
420
			! Give_Admin_Settings::is_saving_settings() ||
421
			'emails' !== give_get_current_setting_tab() ||
422
			! isset( $_GET['section'] )
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
423
		) {
424
			return;
425
		}
426
427
		// Get email type.
428
		$email_type = give_get_current_setting_section();
429
430
		if ( ! empty( $_POST["{$email_type}_recipient"] ) ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
431
			$_POST["{$email_type}_recipient"] = array_unique( array_filter( $_POST["{$email_type}_recipient"] ) );
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
432
		}
433
	}
434
}
435
436
// Helper class.
437
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
438
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php';
439
440
// Add backward compatibility.
441
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php';
442
443
/**
444
 * Initialize functionality.
445
 */
446
Give_Email_Notifications::get_instance()->load();
447