Completed
Push — issues/611 ( e1f393...0587e7 )
by Ravinder
21:03
created

Give_Email_Notifications::print_row_actions()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 4
nop 1
dl 0
loc 23
rs 8.7972
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 576.

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       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 ) {
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 2.0
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
			add_filter( "give_hide_section_{$email->get_id()}_on_emails_page", array( $email, 'hide_section' ) );
85
86
			if ( ! Give_Email_Notification_Util::is_email_preview_has_header( $email ) ) {
87
				continue;
88
			}
89
90
			add_action( "give_{$email->get_id()}_email_preview", array( $this, 'email_preview_header' ) );
91
			add_filter( "give_{$email->get_id()}_email_preview_data", array( $this, 'email_preview_data' ) );
92
			add_filter( "give_{$email->get_id()}_email_preview_message", array( $this, 'email_preview_message' ), 1, 2 );
93
		}
94
	}
95
96
97
	/**
98
	 * Add setting to metabox.
99
	 *
100
	 * @since  2.0
101
	 * @access public
102
	 *
103
	 * @param array $settings
104
	 * @param int   $post_id
105
	 *
106
	 * @return array
107
	 */
108
	public function add_metabox_setting_fields( $settings, $post_id ) {
109
		$emails = $this->get_email_notifications();
110
111
		// Bailout.
112
		if ( empty( $emails ) ) {
113
			return $settings;
114
		}
115
116
		// Email notification setting.
117
		$settings['email_notification_options'] = array(
118
			'id'         => 'email_notification_options',
119
			'title'      => __( 'Email Notification', 'give' ),
120
121
			/**
122
			 * Filter the email notification settings.
123
			 *
124
			 * @since 2.0
125
			 */
126
			'sub-fields' => apply_filters( 'give_email_notification_options_metabox_fields', array(), $post_id ),
127
		);
128
129
		return $settings;
130
	}
131
132
	/**
133
	 * Add email notifications
134
	 *
135
	 * @since  2.0
136
	 * @access private
137
	 */
138
	private function add_emails_notifications() {
139
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/abstract-email-notification.php';
140
		require_once GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-notification-util.php';
141
142
		$this->emails = array(
143
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donation-email.php',
144
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donation-receipt-email.php',
145
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-offline-donation-email.php',
146
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-offline-donation-instruction-email.php',
147
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-new-donor-register-email.php',
148
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-donor-register-email.php',
149
			include GIVE_PLUGIN_DIR . 'includes/admin/emails/class-email-access-email.php',
150
		);
151
152
		/**
153
		 * Filter the email notifications.
154
		 *
155
		 * @since 2.0
156
		 */
157
		$this->emails = apply_filters( 'give_email_notifications', $this->emails, $this );
158
159
		// Bailout.
160
		if ( empty( $this->emails ) ) {
161
			return;
162
		}
163
164
		// Initiate email notifications.
165
		foreach ( $this->emails as $email ) {
166
			$email->init();
167
		}
168
	}
169
170
171
	/**
172
	 * Get list of email notifications.
173
	 *
174
	 * @since  2.0
175
	 * @access public
176
	 * @return array
177
	 */
178
	public function get_email_notifications() {
179
		return $this->emails;
180
	}
181
182
183
	public function get_columns() {
184
		/**
185
		 * Filter the table columns
186
		 *
187
		 * @since 2.0
188
		 */
189
		return apply_filters( 'give_email_notification_setting_columns', array(
190
			'status'     => '',
191
			'name'       => __( 'Email', 'give' ),
192
			'email_type' => __( 'Content Type', 'give' ),
193
			'recipient'  => __( 'Recipient(s)', 'give' ),
194
			'setting'    => __( 'Edit Email', 'give' ),
195
		) );
196
	}
197
198
199
	/**
200
	 * Get name column.
201
	 *
202
	 * @since  2.0
203
	 * @access public
204
	 *
205
	 * @param Give_Email_Notification $email
206
	 */
207
	public function get_name_column( Give_Email_Notification $email ) {
208
		$edit_url = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) );
209
		?>
210
		<td class="give-email-notification-settings-table-name">
211
			<a class="row-title" href="<?php echo $edit_url; ?>"><?php echo $email->get_label(); ?></a>
212
			<?php if ( $desc = $email->get_description() ) : ?>
213
				<span class="give-tooltip give-icon give-icon-question" data-tooltip="<?php echo esc_attr( $desc ); ?>"></span>
214
			<?php endif; ?>
215
			<?php $this->print_row_actions( $email ); ?>
216
		</td>
217
		<?php
218
	}
219
220
221
	/**
222
	 * Print row actions.
223
	 *
224
	 * @since  2.0
225
	 * @access private
226
	 *
227
	 * @param Give_Email_Notification $email
228
	 */
229
	private function print_row_actions( $email ) {
230
		$edit_url    = esc_url( admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=emails&section=' . $email->get_id() ) );
231
		$row_actions = apply_filters(
232
			'give_email_notification_row_actions',
233
			array(
234
				'edit' => "<a href=\"{$edit_url}\">" . __( 'Edit', 'give' ) . '</a>',
235
			),
236
			$email
237
		);
238
		?>
239
		<?php if ( ! empty( $row_actions ) ) : $index = 0; ?>
240
			<div class="row-actions">
241
				<?php foreach ( $row_actions as $action => $link ) : ?>
242
					<?php $sep = 0 < $index ? '&nbsp;|&nbsp;' : ''; ?>
243
					<span class="<?php echo $action; ?>">
244
						<?php echo $sep . $link; ?>
245
					</span>
246
					<?php $index ++;
247
endforeach; ?>
248
			</div>
249
			<?php
250
		endif;
251
	}
252
253
	/**
254
	 * Get recipient column.
255
	 *
256
	 * @since  2.0
257
	 * @access public
258
	 *
259
	 * @param Give_Email_Notification $email
260
	 */
261
	public function get_recipient_column( Give_Email_Notification $email ) {
262
		?>
263
		<td class="give-email-notification-settings-table-recipient">
264
			<?php
265
			if ( $email->get_recipient_group_name() ) {
266
				echo $email->get_recipient_group_name();
267
			} else {
268
				$recipients = $email->get_recipient();
269
				if ( is_array( $recipients ) ) {
270
					$recipients = implode( '<br>', $recipients );
271
				}
272
273
				echo $recipients;
274
			}
275
			?>
276
		</td>
277
		<?php
278
	}
279
280
	/**
281
	 * Get status column.
282
	 *
283
	 * @since  2.0
284
	 * @access public
285
	 *
286
	 * @param Give_Email_Notification $email
287
	 */
288
	public function get_status_column( Give_Email_Notification $email ) {
289
		?>
290
		<td class="give-email-notification-status">
291
			<?php
292
			$notification_status       = $email->get_notification_status();
293
			$default_class = "give-email-notification-{$notification_status} dashicons";
294
			$attributes['class'] = Give_Email_Notification_Util::is_email_notification_active( $email )
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attributes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attributes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
295
				? "{$default_class} dashicons-yes"
296
				: "{$default_class} dashicons-no-alt";
297
298
			$attributes['data-status'] = "{$notification_status}";
299
			$attributes['data-id'] = "{$email->get_id()}";
300
301
			$attributes['data-edit'] = (int) Give_Email_Notification_Util::is_notification_status_editable( $email );
302
303
			if ( ! $attributes['data-edit'] ) {
304
				$attributes['data-tooltip'] = __( 'You can not edit this notification directly. This will be enable or disable automatically on basis of plugin settings.', 'give' );
305
			}
306
307
			$attribute_str = '';
308
			foreach ( $attributes as $tag => $value ) {
309
				$attribute_str .= " {$tag}=\"{$value}\"";
310
			}
311
			?>
312
			<span<?php echo $attribute_str;?>></span><span class=\"spinner\"></span>
313
		</td>
314
		<?php
315
	}
316
317
	/**
318
	 * Get email_type column.
319
	 *
320
	 * @since  2.0
321
	 * @access public
322
	 *
323
	 * @param Give_Email_Notification $email
324
	 */
325
	public function get_email_type_column( Give_Email_Notification $email ) {
326
		?>
327
		<td class="give-email-notification-settings-table-email_type">
328
			<?php echo $email->get_email_type(); ?>
329
		</td>
330
		<?php
331
	}
332
333
	/**
334
	 * Get setting column.
335
	 *
336
	 * @since  2.0
337
	 * @access public
338
	 *
339
	 * @param Give_Email_Notification $email
340
	 */
341
	public function get_setting_column( Give_Email_Notification $email ) {
342
		?>
343
		<td class="give-email-notification-settings-table-actions">
344
			<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>
345
		</td>
346
		<?php
347
	}
348
349
	/**
350
	 * Render column.
351
	 *
352
	 * @since  2.0
353
	 * @access public
354
	 *
355
	 * @param Give_Email_Notification $email
356
	 * @param string                  $column_name
357
	 */
358
	public function render_column( Give_Email_Notification $email, $column_name ) {
359
		if ( method_exists( $this, "get_{$column_name}_column" ) ) {
360
			$this->{"get_{$column_name}_column"}( $email );
361
		} else {
362
			do_action( "give_email_notification_setting_column_$column_name", $email );
363
		}
364
	}
365
366
	/**
367
	 * Check if admin preview email or not
368
	 *
369
	 * @since  2.0
370
	 * @access public
371
	 * @return bool   $is_preview
372
	 */
373
	public function is_preview_email() {
374
		$is_preview = false;
375
376
		if (
377
			current_user_can( 'manage_give_settings' )
378
			&& ! empty( $_GET['give_action'] )
379
			&& 'preview_email' === $_GET['give_action']
380
		) {
381
			$is_preview = true;
382
		}
383
384
		return $is_preview;
385
	}
386
387
	/**
388
	 * Check if admin preview email or not
389
	 *
390
	 * @since  2.0
391
	 * @access public
392
	 * @return bool   $is_preview
393
	 */
394
	public function is_send_preview_email() {
395
		$is_preview = false;
396
397
		if (
398
			current_user_can( 'manage_give_settings' )
399
			&& ! empty( $_GET['give_action'] )
400
			&& 'send_preview_email' === $_GET['give_action']
401
		) {
402
			$is_preview = true;
403
		}
404
405
		return $is_preview;
406
	}
407
408
	/**
409
	 * Displays the email preview
410
	 *
411
	 * @since  2.0
412
	 * @access public
413
	 * @return bool|null
414
	 */
415
	public function preview_email() {
416
		// Bailout.
417
		if ( ! $this->is_preview_email() ) {
418
			return false;
419
		}
420
421
		// Security check.
422
		give_validate_nonce( $_GET['_wpnonce'], 'give-preview-email' );
423
424
		// Get email type.
425
		$email_type = isset( $_GET['email_type'] ) ? esc_attr( $_GET['email_type'] ) : '';
426
427
		/* @var Give_Email_Notification $email */
428
		foreach ( $this->get_email_notifications() as $email ) {
429
			if ( $email_type !== $email->get_id() ) {
430
				continue;
431
			}
432
433
			// Call setup email data to apply filter and other thing to email.
434
			$email->setup_email_data();
435
436
			// Decode message.
437
			$email_message = $email->preview_email_template_tags( $email->get_email_message() );
438
439
			if ( $email_message = Give()->emails->build_email( $email_message ) ) {
440
441
				/**
442
				 * Filter the email preview data
443
				 *
444
				 * @since 2.0
445
				 *
446
				 * @param array
447
				 */
448
				$email_preview_data = apply_filters( "give_{$email_type}_email_preview_data", array() );
449
450
				/**
451
				 * Fire the give_{$email_type}_email_preview action
452
				 *
453
				 * @since 2.0
454
				 */
455
				do_action( "give_{$email_type}_email_preview", $email );
456
457
				/**
458
				 * Filter the email message
459
				 *
460
				 * @since 2.0
461
				 *
462
				 * @param string                  $email_message
463
				 * @param array                   $email_preview_data
464
				 * @param Give_Email_Notification $email
465
				 */
466
				echo apply_filters( "give_{$email_type}_email_preview_message", $email_message, $email_preview_data, $email );
467
468
				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...
469
			}
470
		}// 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...
471
	}
472
473
474
	/**
475
	 * Add header to donation receipt email preview
476
	 *
477
	 * @since   2.0
478
	 * @access  public
479
	 *
480
	 * @param Give_Email_Notification $email
481
	 */
482
	public function email_preview_header( $email ) {
483
		/**
484
		 * Filter the all email preview headers.
485
		 *
486
		 * @since 2.0
487
		 *
488
		 * @param Give_Email_Notification $email
489
		 */
490
		$email_preview_header = apply_filters( 'give_email_preview_header', give_get_preview_email_header(), $email );
491
492
		/**
493
		 * Filter the specific email preview header.
494
		 *
495
		 * @since 2.0
496
		 *
497
		 * @param Give_Email_Notification $email
498
		 */
499
		$email_preview_header = apply_filters( "give_email_preview_{$email->get_id()}_header", $email_preview_header, $email );
500
501
		echo $email_preview_header;
502
	}
503
504
	/**
505
	 * Add email preview data
506
	 *
507
	 * @since   2.0
508
	 * @access  public
509
	 *
510
	 * @param array $email_preview_data
511
	 *
512
	 * @return array
513
	 */
514
	public function email_preview_data( $email_preview_data ) {
515
		$email_preview_data['payment_id'] = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'preview_id' ) );
516
		$email_preview_data['user_id']   = absint( give_check_variable( give_clean( $_GET ), 'isset', 0, 'user_id' ) );
517
518
		return $email_preview_data;
519
	}
520
521
	/**
522
	 * Replace email template tags.
523
	 *
524
	 * @since   2.0
525
	 * @access  public
526
	 *
527
	 * @param string $email_message
528
	 * @param array  $email_preview_data
529
	 *
530
	 * @return string
531
	 */
532
	public function email_preview_message( $email_message, $email_preview_data ) {
533
		if (
534
			! empty( $email_preview_data['payment_id'] )
535
			|| ! empty( $email_preview_data['user_id'] )
536
		) {
537
			$email_message = give_do_email_tags( $email_message, $email_preview_data );
538
		}
539
540
		return $email_message;
541
	}
542
543
	/**
544
	 * Displays the email preview
545
	 *
546
	 * @since  2.0
547
	 * @access public
548
	 * @return bool|null
549
	 */
550
	public function send_preview_email() {
551
		// Bailout.
552
		if ( ! $this->is_send_preview_email() ) {
553
			return false;
554
		}
555
556
		// Security check.
557
		give_validate_nonce( $_GET['_wpnonce'], 'give-send-preview-email' );
558
559
		// Get email type.
560
		$email_type = give_check_variable( give_clean( $_GET ), 'isset', '', 'email_type' );
561
562
		/* @var Give_Email_Notification $email */
563
		foreach ( $this->get_email_notifications() as $email ) {
564
			if ( $email_type === $email->get_id() && Give_Email_Notification_Util::is_email_preview( $email ) ) {
565
				$email->send_preview_email();
566
				break;
567
			}
568
		}
569
	}
570
}
571
572
573
/**
574
 * Initialize functionality.
575
 */
576
Give_Email_Notifications::get_instance()->init();
577