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

Give_Email_Notifications   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 341
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 2
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 0 7 2
A get_email_notifications() 0 3 1
A add_metabox_setting_fields() 0 23 2
B init() 0 29 4
B add_emails_notifications() 0 28 3
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 20 5
A load() 0 3 1
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
80
		/* @var Give_Email_Notification $email */
81
		foreach ( $this->get_email_notifications() as $email ) {
82
			// Setup email section.
83
			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...
84
				add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) );
85
				add_filter( "give_hide_section_{$email->config['id']}_on_emails_page", array( $email, 'hide_section' ) );
86
			}
87
88
			// Setup email preview.
89
			if ( Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) {
90
				add_action( "give_{$email->config['id']}_email_preview", array( $this, 'email_preview_header' ) );
91
				add_filter( "give_{$email->config['id']}_email_preview_data", array( $this, 'email_preview_data' ) );
92
				add_filter( "give_{$email->config['id']}_email_preview_message", array( $this, 'email_preview_message' ), 1, 2 );
93
			}
94
		}
95
	}
96
97
98
	/**
99
	 * Add setting to metabox.
100
	 *
101
	 * @since  2.0
102
	 * @access public
103
	 *
104
	 * @param array $settings
105
	 * @param int   $post_id
106
	 *
107
	 * @return array
108
	 */
109
	public function add_metabox_setting_fields( $settings, $post_id ) {
110
		$emails = $this->get_email_notifications();
111
112
		// Bailout.
113
		if ( empty( $emails ) ) {
114
			return $settings;
115
		}
116
117
		// Email notification setting.
118
		$settings['email_notification_options'] = array(
119
			'id'         => 'email_notification_options',
120
			'title'      => __( 'Email Notification', 'give' ),
121
122
			/**
123
			 * Filter the email notification settings.
124
			 *
125
			 * @since 2.0
126
			 */
127
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
128
		);
129
130
		return $settings;
131
	}
132
133
	/**
134
	 * Add email notifications
135
	 *
136
	 * @since  2.0
137
	 * @access private
138
	 */
139
	private function add_emails_notifications() {
140
		$this->emails = array(
141
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
142
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
143
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
144
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
145
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
146
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
147
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php',
148
		);
149
150
		/**
151
		 * Filter the email notifications.
152
		 *
153
		 * @since 2.0
154
		 */
155
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
156
157
		// Bailout.
158
		if ( empty( $this->emails ) ) {
159
			return;
160
		}
161
162
		// Initiate email notifications.
163
		foreach ( $this->emails as $email ) {
164
			$email->init();
165
		}
166
	}
167
168
169
	/**
170
	 * Get list of email notifications.
171
	 *
172
	 * @since  2.0
173
	 * @access public
174
	 * @return array
175
	 */
176
	public function get_email_notifications() {
177
		return $this->emails;
178
	}
179
180
181
	/**
182
	 * Displays the email preview
183
	 *
184
	 * @since  2.0
185
	 * @access public
186
	 * @return bool|null
187
	 */
188
	public function preview_email() {
189
		// Bailout.
190
		if ( ! Give_Email_Notification_Util::can_preview_email() ) {
191
			return false;
192
		}
193
194
		// Security check.
195
		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...
196
197
		// Get email type.
198
		$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...
199
200
		/* @var Give_Email_Notification $email */
201
		foreach ( $this->get_email_notifications() as $email ) {
202
			if ( $email_type !== $email->config['id'] ) {
203
				continue;
204
			}
205
206
			// Set form id.
207
			$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...
208
209
			// Call setup email data to apply filter and other thing to email.
210
			$email->setup_email_data();
211
212
			// Decode message.
213
			$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) );
214
215
			// Set email template.
216
			Give()->emails->html    = true;
217
			Give()->emails->__set( 'template', $email->get_email_template( $form_id ) );
218
219
			if ( 'text/plain' === $email->config['content_type'] ) {
220
				// Give()->emails->__set( 'html', false );
221
				Give()->emails->__set( 'template', 'none' );
222
			}
223
224
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
225
226
				/**
227
				 * Filter the email preview data
228
				 *
229
				 * @since 2.0
230
				 *
231
				 * @param array
232
				 */
233
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
234
235
				/**
236
				 * Fire the give_{$email_type}_email_preview action
237
				 *
238
				 * @since 2.0
239
				 */
240
				do_action( "give_{$email_type}_email_preview", $email );
241
242
				/**
243
				 * Filter the email message
244
				 *
245
				 * @since 2.0
246
				 *
247
				 * @param string                  $email_message
248
				 * @param array                   $email_preview_data
249
				 * @param Give_Email_Notification $email
250
				 */
251
				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...
252
253
				exit();
254
			}
255
		}// End foreach().
256
	}
257
258
259
	/**
260
	 * Add header to donation receipt email preview
261
	 *
262
	 * @since   2.0
263
	 * @access  public
264
	 *
265
	 * @param Give_Email_Notification $email
266
	 */
267
	public function email_preview_header( $email ) {
268
		/**
269
		 * Filter the all email preview headers.
270
		 *
271
		 * @since 2.0
272
		 *
273
		 * @param Give_Email_Notification $email
274
		 */
275
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
276
277
		echo $email_preview_header;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$email_preview_header'
Loading history...
278
	}
279
280
	/**
281
	 * Add email preview data
282
	 *
283
	 * @since   2.0
284
	 * @access  public
285
	 *
286
	 * @param array $email_preview_data
287
	 *
288
	 * @return array
289
	 */
290
	public function email_preview_data( $email_preview_data ) {
291
		$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...
292
		$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...
293
294
		return $email_preview_data;
295
	}
296
297
	/**
298
	 * Replace email template tags.
299
	 *
300
	 * @since   2.0
301
	 * @access  public
302
	 *
303
	 * @param string $email_message
304
	 * @param array  $email_preview_data
305
	 *
306
	 * @return string
307
	 */
308
	public function email_preview_message( $email_message, $email_preview_data ) {
309
		if (
310
			! empty( $email_preview_data['payment_id'] )
311
			|| ! empty( $email_preview_data['user_id'] )
312
		) {
313
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
314
		}
315
316
		return $email_message;
317
	}
318
319
	/**
320
	 * Displays the email preview
321
	 *
322
	 * @since  2.0
323
	 * @access public
324
	 * @return bool|null
325
	 */
326
	public function send_preview_email() {
327
		// Bailout.
328
		if ( ! Give_Email_Notification_Util::can_send_preview_email() ) {
329
			return false;
330
		}
331
332
		// Security check.
333
		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...
334
335
		// Get email type.
336
		$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...
337
338
		/* @var Give_Email_Notification $email */
339
		foreach ( $this->get_email_notifications() as $email ) {
340
			if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) {
341
				$email->send_preview_email();
342
				break;
343
			}
344
		}
345
	}
346
347
348
	/**
349
	 * Load Give_Email_Notifications
350
	 *
351
	 * @since  2.0
352
	 * @access public
353
	 */
354
	public function load() {
355
		add_action( 'init', array( $this, 'init' ), -1 );
356
	}
357
}
358
359
// Helper class.
360
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
361
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php';
362
363
// Add backward compatibility.
364
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php';
365
366
/**
367
 * Initialize functionality.
368
 */
369
Give_Email_Notifications::get_instance()->load();
370