Test Failed
Push — issues/2619 ( cfea6c )
by Ravinder
04:46
created

Give_Email_Notifications::email_preview_header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Email Notification
4
 *
5
 * This class handles all email notification settings.
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Emails
9
 * @copyright   Copyright (c) 2016, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.0
12
 */
13
14
/**
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
123
			/**
124
			 * Filter the email notification settings.
125
			 *
126
			 * @since 2.0
127
			 */
128
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
129
		);
130
131
		return $settings;
132
	}
133
134
	/**
135
	 * Add email notifications
136
	 *
137
	 * @since  2.0
138
	 * @access private
139
	 */
140
	private function add_emails_notifications() {
141
		$this->emails = array(
142
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
143
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
144
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
145
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
146
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
147
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
148
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php',
149
		);
150
151
		/**
152
		 * Filter the email notifications.
153
		 *
154
		 * @since 2.0
155
		 */
156
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
157
158
		// Bailout.
159
		if ( empty( $this->emails ) ) {
160
			return;
161
		}
162
163
		// Initiate email notifications.
164
		foreach ( $this->emails as $email ) {
165
			$email->init();
166
		}
167
	}
168
169
170
	/**
171
	 * Get list of email notifications.
172
	 *
173
	 * @since  2.0
174
	 * @access public
175
	 * @return array
176
	 */
177
	public function get_email_notifications() {
178
		return $this->emails;
179
	}
180
181
182
	/**
183
	 * Displays the email preview
184
	 *
185
	 * @since  2.0
186
	 * @access public
187
	 * @return bool|null
188
	 */
189
	public function preview_email() {
190
		// Bailout.
191
		if ( ! Give_Email_Notification_Util::can_preview_email() ) {
192
			return false;
193
		}
194
195
		// Security check.
196
		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...
197
198
		// Get email type.
199
		$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...
200
201
		/* @var Give_Email_Notification $email */
202
		foreach ( $this->get_email_notifications() as $email ) {
203
			if ( $email_type !== $email->config['id'] ) {
204
				continue;
205
			}
206
207
			// Set form id.
208
			$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...
209
210
			// Call setup email data to apply filter and other thing to email.
211
			$email->setup_email_data();
212
213
			// Decode message.
214
			$email_message = $email->preview_email_template_tags( $email->get_email_message( $form_id ) );
215
216
			// Set email template.
217
			Give()->emails->html    = true;
218
			Give()->emails->__set( 'template', $email->get_email_template( $form_id ) );
219
220
			if ( 'text/plain' === $email->config['content_type'] ) {
221
				// Give()->emails->__set( 'html', false );
222
				Give()->emails->__set( 'template', 'none' );
223
			}
224
225
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
226
227
				/**
228
				 * Filter the email preview data
229
				 *
230
				 * @since 2.0
231
				 *
232
				 * @param array
233
				 */
234
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
235
236
				/**
237
				 * Fire the give_{$email_type}_email_preview action
238
				 *
239
				 * @since 2.0
240
				 */
241
				do_action( "give_{$email_type}_email_preview", $email );
242
243
				/**
244
				 * Filter the email message
245
				 *
246
				 * @since 2.0
247
				 *
248
				 * @param string                  $email_message
249
				 * @param array                   $email_preview_data
250
				 * @param Give_Email_Notification $email
251
				 */
252
				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...
253
254
				exit();
255
			}
256
		}// End foreach().
257
	}
258
259
260
	/**
261
	 * Add header to donation receipt email preview
262
	 *
263
	 * @since   2.0
264
	 * @access  public
265
	 *
266
	 * @param Give_Email_Notification $email
267
	 */
268
	public function email_preview_header( $email ) {
269
		/**
270
		 * Filter the all email preview headers.
271
		 *
272
		 * @since 2.0
273
		 *
274
		 * @param Give_Email_Notification $email
275
		 */
276
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
277
278
		echo $email_preview_header;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$email_preview_header'
Loading history...
279
	}
280
281
	/**
282
	 * Add email preview data
283
	 *
284
	 * @since   2.0
285
	 * @access  public
286
	 *
287
	 * @param array $email_preview_data
288
	 *
289
	 * @return array
290
	 */
291
	public function email_preview_data( $email_preview_data ) {
292
		$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...
293
		$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...
294
295
		return $email_preview_data;
296
	}
297
298
	/**
299
	 * Replace email template tags.
300
	 *
301
	 * @since   2.0
302
	 * @access  public
303
	 *
304
	 * @param string $email_message
305
	 * @param array  $email_preview_data
306
	 *
307
	 * @return string
308
	 */
309
	public function email_preview_message( $email_message, $email_preview_data ) {
310
		if (
311
			! empty( $email_preview_data['payment_id'] )
312
			|| ! empty( $email_preview_data['user_id'] )
313
		) {
314
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
315
		}
316
317
		return $email_message;
318
	}
319
320
	/**
321
	 * Displays the email preview
322
	 *
323
	 * @since  2.0
324
	 * @access public
325
	 * @return bool|null
326
	 */
327
	public function send_preview_email() {
328
		// Bailout.
329
		if ( ! Give_Email_Notification_Util::can_send_preview_email() ) {
330
			return false;
331
		}
332
333
		// Security check.
334
		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...
335
336
		// Get email type.
337
		$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...
338
339
		/* @var Give_Email_Notification $email */
340
		foreach ( $this->get_email_notifications() as $email ) {
341
			if ( $email_type === $email->config['id'] && Give_Email_Notification_Util::is_email_preview( $email ) ) {
342
				$email->send_preview_email();
343
				break;
344
			}
345
		}
346
347
		// Remove the test email query arg.
348
		wp_redirect( remove_query_arg( 'give_action' ) );
349
		exit;
350
	}
351
352
353
	/**
354
	 * Load Give_Email_Notifications
355
	 *
356
	 * @since  2.0
357
	 * @access public
358
	 */
359
	public function load() {
360
		add_action( 'init', array( $this, 'init' ), -1 );
361
	}
362
363
364
	/**
365
	 * Verify email setting before saving
366
	 *
367
	 * @since  2.0
368
	 * @access public
369
	 */
370
	public function validate_settings() {
371
		// Bailout.
372
		if (
373
			! Give_Admin_Settings::is_saving_settings() ||
374
			'emails' !== give_get_current_setting_tab() ||
375
			! isset( $_GET['section'] )
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
376
		) {
377
			return;
378
		}
379
380
		// Get email type.
381
		$email_type = give_get_current_setting_section();
382
383
		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...
384
			$_POST["{$email_type}_recipient"] = 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...
385
		}
386
	}
387
}
388
389
// Helper class.
390
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
391
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php';
392
393
// Add backward compatibility.
394
require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/backward-compatibility.php';
395
396
/**
397
 * Initialize functionality.
398
 */
399
Give_Email_Notifications::get_instance()->load();
400