Completed
Push — issues/611 ( d123e1...0564a6 )
by Ravinder
20:29
created

Give_Email_Notifications::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
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 514.

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