Completed
Push — issues/611 ( 19900c...46c90d )
by Ravinder
21:01
created

add_metabox__setting_sields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 554.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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       1.9
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  1.9
22
	 * @access static
23
	 * @var
24
	 */
25
	static private $instance;
26
27
	/**
28
	 * Array of email notifications.
29
	 *
30
	 * @since  1.9
31
	 * @access private
32
	 * @var array
33
	 */
34
	private $emails = array();
35
36
	/**
37
	 * Singleton pattern.
38
	 *
39
	 * @since  1.9
40
	 * @access private
41
	 * Give_Payumoney_API constructor.
42
	 */
43
	private function __construct() {
44
	}
45
46
47
	/**
48
	 * Get instance.
49
	 *
50
	 * @since  1.9
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 ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
56
			self::$instance = new static();
57
		}
58
59
		return self::$instance;
60
	}
61
62
	/**
63
	 * Setup dependencies
64
	 *
65
	 * @since 1.9
66
	 */
67
	public function init() {
68
		// Load ajax handler.
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_sields' ), 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
			if ( ! $email->is_email_preview_has_header() ) {
83
				continue;
84
			}
85
86
			add_action( "give_{$email->get_id()}_email_preview", array( $this, 'email_preview_header' ) );
87
			add_filter( "give_{$email->get_id()}_email_preview_data", array( $this, 'email_preview_data' ) );
88
			add_filter( "give_{$email->get_id()}_email_preview_message", array( $this, 'email_preview_message', ), 1, 2 );
89
		}
90
	}
91
92
93
	/**
94
	 * Add setting to metabox.
95
	 *
96
	 * @since  1.9
97
	 * @access public
98
	 *
99
	 * @param array $settings
100
	 * @param int   $post_id
101
	 *
102
	 * @return array
103
	 */
104
	public function add_metabox__setting_sields( $settings, $post_id ) {
105
		$emails = $this->get_email_notifications();
106
107
		// Bailout.
108
		if ( empty( $emails ) ) {
109
			return $settings;
110
		}
111
112
		// Email notification setting.
113
		$settings['email_notification_options'] = array(
114
			'id'         => "email_notification_options",
115
			'title'      => __( 'Email Notification', 'give' ),
116
117
			/**
118
			 * Filter the email notification settings.
119
			 *
120
			 * @since 1.9
121
			 */
122
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
123
		);
124
125
		return $settings;
126
	}
127
128
	/**
129
	 * Add email notifications
130
	 *
131
	 * @since  1.9
132
	 * @access private
133
	 */
134
	private function add_emails_notifications() {
135
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
136
137
		$this->emails = array(
138
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
139
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
140
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
141
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
142
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
143
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
144
		);
145
146
		/**
147
		 * Filter the email notifications.
148
		 *
149
		 * @since 1.9
150
		 */
151
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
152
153
		// Bailout.
154
		if( empty( $this->emails ) ) {
155
			return;
156
		}
157
158
		// Initiate email notifications.
159
		foreach ( $this->emails as $email ) {
160
			$email->init();
161
		}
162
	}
163
164
165
	/**
166
	 * Get list of email notifications.
167
	 *
168
	 * @since  1.9
169
	 * @access public
170
	 * @return array
171
	 */
172
	public function get_email_notifications() {
173
		return $this->emails;
174
	}
175
176
177
	public function get_columns() {
178
		/**
179
		 * Filter the table columns
180
		 *
181
		 * @since 1.9
182
		 */
183
		return apply_filters( 'give_email_notification_setting_columns', array(
184
			'status'     => '',
185
			'name'       => __( 'Email', 'give' ),
186
			'email_type' => __( 'Content Type', 'give' ),
187
			'recipient'  => __( 'Recipient(s)', 'give' ),
188
			'setting'    => __( 'Edit Email', 'give' ),
189
		) );
190
	}
191
192
193
	/**
194
	 * Get name column.
195
	 *
196
	 * @since  1.9
197
	 * @access public
198
	 *
199
	 * @param Give_Email_Notification $email
200
	 */
201
	public function get_name_column( Give_Email_Notification $email ) {
202
		$edit_url = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) );
203
		?>
204
		<td class="give-email-notification-settings-table-name">
205
			<a class="row-title" href="<?php echo $edit_url; ?>"><?php echo $email->get_label(); ?></a>
206
			<?php if ( $desc = $email->get_description() ) : ?>
207
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo esc_attr( $desc ); ?>"></span>
208
			<?php endif; ?>
209
			<?php $this->print_row_actions( $email ); ?>
210
		</td>
211
		<?php
212
	}
213
214
215
	/**
216
	 * Print row actions.
217
	 *
218
	 * @since  1.9
219
	 * @access private
220
	 *
221
	 * @param Give_Email_Notification $email
222
	 */
223
	private function print_row_actions( $email ) {
224
		$edit_url    = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) );
225
		$row_actions = apply_filters(
226
			'give_email_notification_row_actions',
227
			array( 'edit' => "<a href=\"{$edit_url}\">" . __( 'Edit', 'give' ) . "</a>" ),
228
			$email
229
		);
230
		?>
231
		<?php if ( ! empty( $row_actions ) ) : $index = 0; ?>
232
			<div class="row-actions">
233
				<?php foreach ( $row_actions as $action => $link ) : ?>
234
					<?php $sep = 0 < $index ? '&nbsp;|&nbsp;' : ''; ?>
235
					<span class="<?php echo $action; ?>">
236
						<?php echo $sep . $link; ?>
237
					</span>
238
					<?php $index ++; endforeach; ?>
239
			</div>
240
			<?php
241
		endif;
242
	}
243
244
	/**
245
	 * Get recipient column.
246
	 *
247
	 * @since  1.9
248
	 * @access public
249
	 *
250
	 * @param Give_Email_Notification $email
251
	 */
252
	public function get_recipient_column( Give_Email_Notification $email ) {
253
		?>
254
		<td class="give-email-notification-settings-table-recipient">
255
			<?php
256
			if ( $email->get_recipient_group_name() ) {
257
				echo $email->get_recipient_group_name();
258
			} else {
259
				$recipients = $email->get_recipient();
260
				if ( is_array( $recipients ) ) {
261
					$recipients = implode( '<br>', $recipients );
262
				}
263
264
				echo $recipients;
265
			}
266
			?>
267
		</td>
268
		<?php
269
	}
270
271
	/**
272
	 * Get status column.
273
	 *
274
	 * @since  1.9
275
	 * @access public
276
	 *
277
	 * @param Give_Email_Notification $email
278
	 */
279
	public function get_status_column( Give_Email_Notification $email ) {
280
		?>
281
		<td class="give-email-notification-status">
282
			<?php
283
			$notification_status       = $email->get_notification_status();
284
			$notification_status_class = $email->is_email_notification_active()
285
				? 'dashicons-yes'
286
				: 'dashicons-no-alt';
287
			echo "<span class=\"give-email-notification-{$notification_status} dashicons {$notification_status_class}\" data-status=\"{$notification_status}\" data-id=\"{$email->get_id()}\"></span><span class=\"spinner\"></span>";
288
			?>
289
		</td>
290
		<?php
291
	}
292
293
	/**
294
	 * Get email_type column.
295
	 *
296
	 * @since  1.9
297
	 * @access public
298
	 *
299
	 * @param Give_Email_Notification $email
300
	 */
301
	public function get_email_type_column( Give_Email_Notification $email ) {
302
		?>
303
		<td class="give-email-notification-settings-table-email_type">
304
			<?php echo $email->get_email_type(); ?>
305
		</td>
306
		<?php
307
	}
308
309
	/**
310
	 * Get setting column.
311
	 *
312
	 * @since  1.9
313
	 * @access public
314
	 *
315
	 * @param Give_Email_Notification $email
316
	 */
317
	public function get_setting_column( Give_Email_Notification $email ) {
318
		?>
319
		<td class="give-email-notification-settings-table-actions">
320
			<a class="button button-small" data-tooltip="<?php echo __( 'Edit', 'give' ); ?> <?php echo $email->get_label(); ?>" href="<?php echo esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) ); ?>"><span class="dashicons dashicons-admin-generic"></span></a>
321
		</td>
322
		<?php
323
	}
324
325
	/**
326
	 * Render column.
327
	 *
328
	 * @since  1.9
329
	 * @access public
330
	 *
331
	 * @param Give_Email_Notification $email
332
	 * @param string                  $column_name
333
	 */
334
	public function render_column( Give_Email_Notification $email, $column_name ) {
335
		if ( method_exists( $this, "get_{$column_name}_column" ) ) {
336
			$this->{"get_{$column_name}_column"}( $email );
337
		} else {
338
			do_action( "give_email_notification_setting_column_$column_name", $email );
339
		}
340
	}
341
342
	/**
343
	 * Check if admin preview email or not
344
	 *
345
	 * @since  1.9
346
	 * @access public
347
	 * @return bool   $is_preview
348
	 */
349
	public function is_preview_email() {
350
		$is_preview = false;
351
352
		if (
353
			current_user_can( 'manage_give_settings' )
354
			&& ! empty( $_GET['give_action'] )
355
			&& 'preview_email' === $_GET['give_action']
356
		) {
357
			$is_preview = true;
358
		}
359
360
		return $is_preview;
361
	}
362
363
	/**
364
	 * Check if admin preview email or not
365
	 *
366
	 * @since  1.9
367
	 * @access public
368
	 * @return bool   $is_preview
369
	 */
370
	public function is_send_preview_email() {
371
		$is_preview = false;
372
373
		if (
374
			current_user_can( 'manage_give_settings' )
375
			&& ! empty( $_GET['give_action'] )
376
			&& 'send_preview_email' === $_GET['give_action']
377
		) {
378
			$is_preview = true;
379
		}
380
381
		return $is_preview;
382
	}
383
384
	/**
385
	 * Displays the email preview
386
	 *
387
	 * @since  1.9
388
	 * @access public
389
	 * @return bool|null
390
	 */
391
	public function preview_email() {
392
		// Bailout.
393
		if ( ! $this->is_preview_email() ) {
394
			return false;
395
		}
396
397
		// Security check.
398
		give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' );
399
400
401
		// Get email type.
402
		$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : '';
403
404
		/* @var Give_Email_Notification $email */
405
		foreach ( $this->get_email_notifications() as $email ) {
406
			if ( $email_type !== $email->get_id() ) {
407
				continue;
408
			}
409
410
			// Call setup email data to apply filter and other thing to email.
411
			$email->setup_email_data();
412
413
			// Decode message.
414
			$email_message = $email->preview_email_template_tags( $email->get_email_message() );
415
416
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
417
418
				/**
419
				 * Filter the email preview data
420
				 *
421
				 * @since 1.9
422
				 *
423
				 * @param array
424
				 */
425
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
426
427
				/**
428
				 * Fire the give_{$email_type}_email_preview action
429
				 *
430
				 * @since 1.9
431
				 */
432
				do_action( "give_{$email_type}_email_preview", $email );
433
434
				/**
435
				 * Filter the email message
436
				 *
437
				 * @since 1.9
438
				 *
439
				 * @param string                  $email_message
440
				 * @param array                   $email_preview_data
441
				 * @param Give_Email_Notification $email
442
				 */
443
				echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email );
444
445
				exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method preview_email() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
446
			}
447
		}// End foreach().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
448
	}
449
450
451
	/**
452
	 * Add header to donation receipt email preview
453
	 *
454
	 * @since   1.9
455
	 * @access  public
456
	 *
457
	 * @param Give_Email_Notification $email
458
	 */
459
	public function email_preview_header( $email ) {
460
		/**
461
		 * Filter the all email preview headers.
462
		 *
463
		 * @since 1.9
464
		 *
465
		 * @param Give_Email_Notification $email
466
		 */
467
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
468
469
		/**
470
		 * Filter the specific email preview header.
471
		 *
472
		 * @since 1.9
473
		 *
474
		 * @param Give_Email_Notification $email
475
		 */
476
		$email_preview_header = apply_filters( "give_email_preview_{$email->get_id()}_header", $email_preview_header, $email );
477
478
		echo $email_preview_header;
479
	}
480
481
	/**
482
	 * Add email preview data
483
	 *
484
	 * @since   1.9
485
	 * @access  public
486
	 *
487
	 * @param array $email_preview_data
488
	 *
489
	 * @return array
490
	 */
491
	public function email_preview_data( $email_preview_data ) {
492
		$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) );
493
		$email_preview_data['user_id']   = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) );
494
495
		return $email_preview_data;
496
	}
497
498
	/**
499
	 * Replace email template tags.
500
	 *
501
	 * @since   1.9
502
	 * @access  public
503
	 *
504
	 * @param string $email_message
505
	 * @param array  $email_preview_data
506
	 *
507
	 * @return string
508
	 */
509
	public function email_preview_message( $email_message, $email_preview_data ) {
510
		if(
511
			! empty( $email_preview_data['payment_id'] )
512
			|| ! empty( $email_preview_data['user_id'] )
513
		) {
514
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
515
		}
516
517
		return $email_message;
518
	}
519
520
	/**
521
	 * Displays the email preview
522
	 *
523
	 * @since  1.9
524
	 * @access public
525
	 * @return bool|null
526
	 */
527
	public function send_preview_email() {
528
		// Bailout.
529
		if ( ! $this->is_send_preview_email() ) {
530
			return false;
531
		}
532
533
		// Security check.
534
		give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' );
535
536
537
		// Get email type.
538
		$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' );
539
540
		/* @var Give_Email_Notification $email */
541
		foreach ( $this->get_email_notifications() as $email ) {
542
			if ( $email_type === $email->get_id() && $email->is_email_preview() ) {
543
				$email->send_preview_email();
544
				break;
545
			}
546
		}
547
	}
548
}
549
550
551
/**
552
 * Initialize functionality.
553
 */
554
Give_Email_Notifications::get_instance()->init();
555