Completed
Push — issues/611 ( 661115...758b1c )
by Ravinder
21:11
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 557.

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_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
			// Add section.
83
			add_filter( 'give_get_sections_emails', array( $email, 'add_section' ) );
84
85
			if ( ! $email->is_email_preview_has_header() ) {
86
				continue;
87
			}
88
89
			add_action( "give_{$email->get_id()}_email_preview", array( $this, 'email_preview_header' ) );
90
			add_filter( "give_{$email->get_id()}_email_preview_data", array( $this, 'email_preview_data' ) );
91
			add_filter( "give_{$email->get_id()}_email_preview_message", array( $this, 'email_preview_message', ), 1, 2 );
92
		}
93
	}
94
95
96
	/**
97
	 * Add setting to metabox.
98
	 *
99
	 * @since  1.9
100
	 * @access public
101
	 *
102
	 * @param array $settings
103
	 * @param int   $post_id
104
	 *
105
	 * @return array
106
	 */
107
	public function add_metabox_setting_fields( $settings, $post_id ) {
108
		$emails = $this->get_email_notifications();
109
110
		// Bailout.
111
		if ( empty( $emails ) ) {
112
			return $settings;
113
		}
114
115
		// Email notification setting.
116
		$settings['email_notification_options'] = array(
117
			'id'         => "email_notification_options",
118
			'title'      => __( 'Email Notification', 'give' ),
119
120
			/**
121
			 * Filter the email notification settings.
122
			 *
123
			 * @since 1.9
124
			 */
125
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
126
		);
127
128
		return $settings;
129
	}
130
131
	/**
132
	 * Add email notifications
133
	 *
134
	 * @since  1.9
135
	 * @access private
136
	 */
137
	private function add_emails_notifications() {
138
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
139
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
		);
148
149
		/**
150
		 * Filter the email notifications.
151
		 *
152
		 * @since 1.9
153
		 */
154
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
155
156
		// Bailout.
157
		if( empty( $this->emails ) ) {
158
			return;
159
		}
160
		
161
		// Initiate email notifications.
162
		foreach ( $this->emails as $email ) {
163
			$email->init();
164
		}
165
	}
166
167
168
	/**
169
	 * Get list of email notifications.
170
	 *
171
	 * @since  1.9
172
	 * @access public
173
	 * @return array
174
	 */
175
	public function get_email_notifications() {
176
		return $this->emails;
177
	}
178
179
180
	public function get_columns() {
181
		/**
182
		 * Filter the table columns
183
		 *
184
		 * @since 1.9
185
		 */
186
		return apply_filters( 'give_email_notification_setting_columns', array(
187
			'status'     => '',
188
			'name'       => __( 'Email', 'give' ),
189
			'email_type' => __( 'Content Type', 'give' ),
190
			'recipient'  => __( 'Recipient(s)', 'give' ),
191
			'setting'    => __( 'Edit Email', 'give' ),
192
		) );
193
	}
194
195
196
	/**
197
	 * Get name column.
198
	 *
199
	 * @since  1.9
200
	 * @access public
201
	 *
202
	 * @param Give_Email_Notification $email
203
	 */
204
	public function get_name_column( Give_Email_Notification $email ) {
205
		$edit_url = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) );
206
		?>
207
		<td class="give-email-notification-settings-table-name">
208
			<a class="row-title" href="<?php echo $edit_url; ?>"><?php echo $email->get_label(); ?></a>
209
			<?php if ( $desc = $email->get_description() ) : ?>
210
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo esc_attr( $desc ); ?>"></span>
211
			<?php endif; ?>
212
			<?php $this->print_row_actions( $email ); ?>
213
		</td>
214
		<?php
215
	}
216
217
218
	/**
219
	 * Print row actions.
220
	 *
221
	 * @since  1.9
222
	 * @access private
223
	 *
224
	 * @param Give_Email_Notification $email
225
	 */
226
	private function print_row_actions( $email ) {
227
		$edit_url    = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) );
228
		$row_actions = apply_filters(
229
			'give_email_notification_row_actions',
230
			array( 'edit' => "<a href=\"{$edit_url}\">" . __( 'Edit', 'give' ) . "</a>" ),
231
			$email
232
		);
233
		?>
234
		<?php if ( ! empty( $row_actions ) ) : $index = 0; ?>
235
			<div class="row-actions">
236
				<?php foreach ( $row_actions as $action => $link ) : ?>
237
					<?php $sep = 0 < $index ? '&nbsp;|&nbsp;' : ''; ?>
238
					<span class="<?php echo $action; ?>">
239
						<?php echo $sep . $link; ?>
240
					</span>
241
					<?php $index ++; endforeach; ?>
242
			</div>
243
			<?php
244
		endif;
245
	}
246
247
	/**
248
	 * Get recipient column.
249
	 *
250
	 * @since  1.9
251
	 * @access public
252
	 *
253
	 * @param Give_Email_Notification $email
254
	 */
255
	public function get_recipient_column( Give_Email_Notification $email ) {
256
		?>
257
		<td class="give-email-notification-settings-table-recipient">
258
			<?php
259
			if ( $email->get_recipient_group_name() ) {
260
				echo $email->get_recipient_group_name();
261
			} else {
262
				$recipients = $email->get_recipient();
263
				if ( is_array( $recipients ) ) {
264
					$recipients = implode( '<br>', $recipients );
265
				}
266
267
				echo $recipients;
268
			}
269
			?>
270
		</td>
271
		<?php
272
	}
273
274
	/**
275
	 * Get status column.
276
	 *
277
	 * @since  1.9
278
	 * @access public
279
	 *
280
	 * @param Give_Email_Notification $email
281
	 */
282
	public function get_status_column( Give_Email_Notification $email ) {
283
		?>
284
		<td class="give-email-notification-status">
285
			<?php
286
			$notification_status       = $email->get_notification_status();
287
			$notification_status_class = $email->is_email_notification_active()
288
				? 'dashicons-yes'
289
				: 'dashicons-no-alt';
290
			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>";
291
			?>
292
		</td>
293
		<?php
294
	}
295
296
	/**
297
	 * Get email_type column.
298
	 *
299
	 * @since  1.9
300
	 * @access public
301
	 *
302
	 * @param Give_Email_Notification $email
303
	 */
304
	public function get_email_type_column( Give_Email_Notification $email ) {
305
		?>
306
		<td class="give-email-notification-settings-table-email_type">
307
			<?php echo $email->get_email_type(); ?>
308
		</td>
309
		<?php
310
	}
311
312
	/**
313
	 * Get setting column.
314
	 *
315
	 * @since  1.9
316
	 * @access public
317
	 *
318
	 * @param Give_Email_Notification $email
319
	 */
320
	public function get_setting_column( Give_Email_Notification $email ) {
321
		?>
322
		<td class="give-email-notification-settings-table-actions">
323
			<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>
324
		</td>
325
		<?php
326
	}
327
328
	/**
329
	 * Render column.
330
	 *
331
	 * @since  1.9
332
	 * @access public
333
	 *
334
	 * @param Give_Email_Notification $email
335
	 * @param string                  $column_name
336
	 */
337
	public function render_column( Give_Email_Notification $email, $column_name ) {
338
		if ( method_exists( $this, "get_{$column_name}_column" ) ) {
339
			$this->{"get_{$column_name}_column"}( $email );
340
		} else {
341
			do_action( "give_email_notification_setting_column_$column_name", $email );
342
		}
343
	}
344
345
	/**
346
	 * Check if admin preview email or not
347
	 *
348
	 * @since  1.9
349
	 * @access public
350
	 * @return bool   $is_preview
351
	 */
352
	public function is_preview_email() {
353
		$is_preview = false;
354
355
		if (
356
			current_user_can( 'manage_give_settings' )
357
			&& ! empty( $_GET['give_action'] )
358
			&& 'preview_email' === $_GET['give_action']
359
		) {
360
			$is_preview = true;
361
		}
362
363
		return $is_preview;
364
	}
365
366
	/**
367
	 * Check if admin preview email or not
368
	 *
369
	 * @since  1.9
370
	 * @access public
371
	 * @return bool   $is_preview
372
	 */
373
	public function is_send_preview_email() {
374
		$is_preview = false;
375
376
		if (
377
			current_user_can( 'manage_give_settings' )
378
			&& ! empty( $_GET['give_action'] )
379
			&& 'send_preview_email' === $_GET['give_action']
380
		) {
381
			$is_preview = true;
382
		}
383
384
		return $is_preview;
385
	}
386
387
	/**
388
	 * Displays the email preview
389
	 *
390
	 * @since  1.9
391
	 * @access public
392
	 * @return bool|null
393
	 */
394
	public function preview_email() {
395
		// Bailout.
396
		if ( ! $this->is_preview_email() ) {
397
			return false;
398
		}
399
400
		// Security check.
401
		give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' );
402
403
404
		// Get email type.
405
		$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : '';
406
407
		/* @var Give_Email_Notification $email */
408
		foreach ( $this->get_email_notifications() as $email ) {
409
			if ( $email_type !== $email->get_id() ) {
410
				continue;
411
			}
412
413
			// Call setup email data to apply filter and other thing to email.
414
			$email->setup_email_data();
415
416
			// Decode message.
417
			$email_message = $email->preview_email_template_tags( $email->get_email_message() );
418
419
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
420
421
				/**
422
				 * Filter the email preview data
423
				 *
424
				 * @since 1.9
425
				 *
426
				 * @param array
427
				 */
428
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
429
430
				/**
431
				 * Fire the give_{$email_type}_email_preview action
432
				 *
433
				 * @since 1.9
434
				 */
435
				do_action( "give_{$email_type}_email_preview", $email );
436
437
				/**
438
				 * Filter the email message
439
				 *
440
				 * @since 1.9
441
				 *
442
				 * @param string                  $email_message
443
				 * @param array                   $email_preview_data
444
				 * @param Give_Email_Notification $email
445
				 */
446
				echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email );
447
448
				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...
449
			}
450
		}// 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...
451
	}
452
453
454
	/**
455
	 * Add header to donation receipt email preview
456
	 *
457
	 * @since   1.9
458
	 * @access  public
459
	 *
460
	 * @param Give_Email_Notification $email
461
	 */
462
	public function email_preview_header( $email ) {
463
		/**
464
		 * Filter the all email preview headers.
465
		 *
466
		 * @since 1.9
467
		 *
468
		 * @param Give_Email_Notification $email
469
		 */
470
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
471
472
		/**
473
		 * Filter the specific email preview header.
474
		 *
475
		 * @since 1.9
476
		 *
477
		 * @param Give_Email_Notification $email
478
		 */
479
		$email_preview_header = apply_filters( "give_email_preview_{$email->get_id()}_header", $email_preview_header, $email );
480
481
		echo $email_preview_header;
482
	}
483
484
	/**
485
	 * Add email preview data
486
	 *
487
	 * @since   1.9
488
	 * @access  public
489
	 *
490
	 * @param array $email_preview_data
491
	 *
492
	 * @return array
493
	 */
494
	public function email_preview_data( $email_preview_data ) {
495
		$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) );
496
		$email_preview_data['user_id']   = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) );
497
498
		return $email_preview_data;
499
	}
500
501
	/**
502
	 * Replace email template tags.
503
	 *
504
	 * @since   1.9
505
	 * @access  public
506
	 *
507
	 * @param string $email_message
508
	 * @param array  $email_preview_data
509
	 *
510
	 * @return string
511
	 */
512
	public function email_preview_message( $email_message, $email_preview_data ) {
513
		if(
514
			! empty( $email_preview_data['payment_id'] )
515
			|| ! empty( $email_preview_data['user_id'] )
516
		) {
517
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
518
		}
519
520
		return $email_message;
521
	}
522
523
	/**
524
	 * Displays the email preview
525
	 *
526
	 * @since  1.9
527
	 * @access public
528
	 * @return bool|null
529
	 */
530
	public function send_preview_email() {
531
		// Bailout.
532
		if ( ! $this->is_send_preview_email() ) {
533
			return false;
534
		}
535
536
		// Security check.
537
		give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' );
538
539
540
		// Get email type.
541
		$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' );
542
543
		/* @var Give_Email_Notification $email */
544
		foreach ( $this->get_email_notifications() as $email ) {
545
			if ( $email_type === $email->get_id() && $email->is_email_preview() ) {
546
				$email->send_preview_email();
547
				break;
548
			}
549
		}
550
	}
551
}
552
553
554
/**
555
 * Initialize functionality.
556
 */
557
Give_Email_Notifications::get_instance()->init();
558