Passed
Pull Request — dev/2.4.0 (#239)
by Sudar
06:10 queued 03:12
created

CoreSetting::sanitize_pause_emails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php namespace EmailLog\Core\UI\Setting;
2
3
use  \EmailLog\Core\UI\Page\SettingsPage;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * All Email Log Core settings.
9
 *
10
 * @since 2.1.0
11
 */
12
class CoreSetting extends Setting {
13
14
	protected function initialize() {
15
		$this->section->id          = 'email-log-core';
16
		$this->section->title       = __( 'Core Email Log Settings', 'email-log' );
17
		$this->section->option_name = 'email-log-core';
18
19
		$this->section->field_labels = array(
20
			'allowed_user_roles'    => __( 'Allowed User Roles', 'email-log' ),
21
			'remove_on_uninstall'   => __( 'Remove Data on Uninstall?', 'email-log' ),
22
			'hide_dashboard_widget' => __( 'Disable Dashboard Widget', 'email-log' ),
23
			'db_size_notification'  => __( 'Database Size Notification', 'email-log' ),
24
			'pause_emails'          => __( 'Pause Emails', 'email-log' ),
25
		);
26
27
		$this->section->default_value = array(
28
			'allowed_user_roles'    => array(),
29
			'remove_on_uninstall'   => '',
30
			'hide_dashboard_widget' => false,
31
			'db_size_notification'  => array(
32
				'notify'                    => false,
33
				'admin_email'               => '',
34
				'logs_threshold'            => '',
35
				'log_threshold_met'         => false,
36
				'threshold_email_last_sent' => false,
37
			),
38
			'pause_emails'          => 'false',
39
		);
40
41
		$this->load();
42
	}
43
44
	/**
45
	 * Override `load` method so that the core settings are displayed first.
46
	 *
47
	 * @inheritdoc
48
	 */
49
	public function load() {
50
		add_filter( 'el_setting_sections', array( $this, 'register' ), 9 );
51
52
		add_action( 'add_option_' . $this->section->option_name, array( $this, 'allowed_user_roles_added' ), 10, 2 );
53
		add_action( 'update_option_' . $this->section->option_name, array( $this, 'allowed_user_roles_changed' ), 10, 2 );
54
55
		add_action( 'el_email_log_inserted', array( $this, 'verify_email_log_threshold' ) );
56
		add_action( 'el_trigger_notify_email_when_log_threshold_met', array( $this, 'trigger_threshold_met_notification_email' ) );
57
	}
58
59
	/**
60
	 * Renders the Email Log `Allowed User Roles` settings.
61
	 *
62
	 * @param array $args Arguments.
63
	 */
64
	public function render_allowed_user_roles_settings( $args ) {
65
		$option         = $this->get_value();
66
		$selected_roles = $option[ $args['id'] ];
67
68
		$field_name = $this->section->option_name . '[' . $args['id'] . '][]';
69
70
		$available_roles = get_editable_roles();
71
		unset( $available_roles['administrator'] );
72
		?>
73
74
		<p>
75
			<input type="checkbox" checked disabled><?php _e( 'Administrator', 'email-log' ); ?>
76
		</p>
77
78
		<?php foreach ( $available_roles as $role_id => $role ) : ?>
79
			<p>
80
				<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $role_id ); ?>"
81
					<?php \EmailLog\Util\checked_array( $selected_roles, $role_id ); ?>>
82
83
				<?php echo $role['name']; ?>
84
			</p>
85
		<?php endforeach; ?>
86
87
		<p>
88
			<em>
89
				<?php _e( '<strong>Note:</strong> Users with the above User Roles can view Email Logs.', 'email-log' ); ?>
90
				<?php _e( 'Administrator role always has access and cannot be disabled.', 'email-log' ); ?>
91
			</em>
92
		</p>
93
94
		<?php
95
	}
96
97
	/**
98
	 * Sanitize allowed user roles setting.
99
	 *
100
	 * @param array $roles User selected user roles.
101
	 *
102
	 * @return array Sanitized user roles.
103
	 */
104
	public function sanitize_allowed_user_roles( $roles ) {
105
		if ( ! is_array( $roles ) ) {
0 ignored issues
show
introduced by
The condition is_array($roles) is always true.
Loading history...
106
			return array();
107
		}
108
109
		return array_map( 'sanitize_text_field', $roles );
110
	}
111
112
	/**
113
	 * Renders the Email Log `Remove Data on Uninstall?` settings.
114
	 *
115
	 * @param array $args
116
	 */
117
	public function render_remove_on_uninstall_settings( $args ) {
118
		$option      = $this->get_value();
119
		$remove_data = $option[ $args['id'] ];
120
121
		$field_name = $this->section->option_name . '[' . $args['id'] . ']';
122
		?>
123
124
		<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="true" <?php checked( 'true', $remove_data ); ?>>
125
		<?php _e( 'Check this box if you would like to completely remove all of its data when the plugin is deleted.', 'email-log' ) ?>
126
127
		<p>
128
			<em>
129
				<?php printf(
130
					__( '<strong>Note:</strong> You can also export the Email Logs using our <a href="%s" rel="noopener noreferrer" target="_blank">Export Logs</a> add-on.', 'email-log' ),
131
					'https://wpemaillog.com/addons/export-logs/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=settings&utm_content=el'
132
				); ?>
133
			</em>
134
		</p>
135
136
		<?php
137
	}
138
139
	/**
140
	 * Sanitize Remove on uninstall value.
141
	 *
142
	 * @param string $value User entered value.
143
	 *
144
	 * @return string Sanitized value.
145
	 */
146
	public function sanitize_remove_on_uninstall( $value ) {
147
		return sanitize_text_field( $value );
148
	}
149
150
	/**
151
	 * Allowed user role list option is added.
152
	 *
153
	 * @param string $option Option name.
154
	 * @param array  $value  Option value.
155
	 */
156
	public function allowed_user_roles_added( $option, $value ) {
157
		$this->allowed_user_roles_changed( array(), $value );
158
	}
159
160
	/**
161
	 * Allowed user role list option was update.
162
	 *
163
	 * Change user role capabilities when the allowed user role list is changed.
164
	 *
165
	 * @param array $old_value Old Value.
166
	 * @param array $new_value New Value.
167
	 */
168
	public function allowed_user_roles_changed( $old_value, $new_value ) {
169
		$old_roles = $this->get_user_roles( $old_value );
170
		$new_roles = $this->get_user_roles( $new_value );
171
172
		/**
173
		 * The user roles who can manage email log list is changed.
174
		 *
175
		 * @since 2.1.0
176
		 *
177
		 * @param array $old_roles Old user roles.
178
		 * @param array $new_roles New user roles.
179
		 */
180
		do_action( 'el-log-list-manage-user-roles-changed', $old_roles, $new_roles );
181
	}
182
183
	/**
184
	 * Get User roles from option value.
185
	 *
186
	 * @access protected
187
	 *
188
	 * @param array $option Option value
189
	 *
190
	 * @return array User roles.
191
	 */
192
	protected function get_user_roles( $option ) {
193
		if ( ! array_key_exists( 'allowed_user_roles', $option ) ) {
194
			return array();
195
		}
196
197
		$user_roles = $option['allowed_user_roles'];
198
		if ( ! is_array( $user_roles ) ) {
199
			$user_roles = array( $user_roles );
200
		}
201
202
		return $user_roles;
203
	}
204
205
	/**
206
	 * Renders the Email Log `Remove Data on Uninstall?` settings.
207
	 *
208
	 * @param array $args
209
	 */
210
	public function render_hide_dashboard_widget_settings( $args ) {
211
		$option                = $this->get_value();
212
		$hide_dashboard_widget = $option[ $args['id'] ];
213
214
		$field_name = $this->section->option_name . '[' . $args['id'] . ']';
215
		?>
216
217
		<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="true" <?php checked( 'true', $hide_dashboard_widget ); ?>>
218
		<?php _e( 'Check this box if you would like to disable dashboard widget.', 'email-log' ) ?>
219
220
		<p>
221
			<em>
222
				<?php printf(
223
					__( '<strong>Note:</strong> Each users can also disable dashboard widget using screen options', 'email-log' )
224
				); ?>
225
			</em>
226
		</p>
227
228
		<?php
229
	}
230
231
	/**
232
	 * Renders the Email Log `Database Size Notification` settings.
233
	 *
234
	 * @since 2.3.0
235
	 *
236
	 * @param array $args
237
	 */
238
	public function render_db_size_notification_settings( $args ) {
239
		$option                    = $this->get_value();
240
		$db_size_notification_data = $option[ $args['id'] ];
241
242
		$field_name = $this->section->option_name . '[' . $args['id'] . ']';
243
		// Since we store three different fields, give each field a unique name.
244
		$db_size_notification_field_name = $field_name . '[notify]';
245
		$admin_email_field_name          = $field_name . '[admin_email]';
246
		$logs_threshold_field_name       = $field_name . '[logs_threshold]';
247
248
		$email_log  = email_log();
249
		$logs_count = $email_log->table_manager->get_logs_count();
250
251
		$admin_email_input_field = sprintf(
252
			'<input type="email" name="%1$s" value="%2$s" size="35" />', esc_attr( $admin_email_field_name ), empty( $db_size_notification_data['admin_email'] ) ? get_option( 'admin_email', '' ) : esc_attr( $db_size_notification_data['admin_email'] ) );
0 ignored issues
show
Bug introduced by
It seems like empty($db_size_notificat...on_data['admin_email']) can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

252
			'<input type="email" name="%1$s" value="%2$s" size="35" />', esc_attr( $admin_email_field_name ), /** @scrutinizer ignore-type */ empty( $db_size_notification_data['admin_email'] ) ? get_option( 'admin_email', '' ) : esc_attr( $db_size_notification_data['admin_email'] ) );
Loading history...
253
254
		$logs_threshold_input_field = sprintf( '<input type="number" name="%1$s" placeholder="5000" value="%2$s" min="0" max="99999999" />',
255
			esc_attr( $logs_threshold_field_name ),
256
			empty( $db_size_notification_data['logs_threshold'] ) ? '' : esc_attr( $db_size_notification_data['logs_threshold'] )
257
		);
258
		?>
259
260
        <input type="checkbox" name="<?php echo esc_attr( $db_size_notification_field_name ); ?>" value="true" <?php
261
		checked( true, $db_size_notification_data['notify'] ); ?> />
262
		<?php
263
		// The values within each field are already escaped.
264
		printf( __( 'Notify %1$s if there are more than %2$s logs.', 'email-log' ),
265
			$admin_email_input_field,
266
			$logs_threshold_input_field
267
		);
268
		?>
269
        <p>
270
            <em>
271
				<?php printf(
272
					__( '%1$s There are %2$s email logs currently logged in the database.', 'email-log' ),
273
					'<strong>Note:</strong>',
274
					'<strong>' . esc_attr( $logs_count ) . '</strong>'
275
				); ?>
276
            </em>
277
        </p>
278
		<?php if ( ! empty( $db_size_notification_data['threshold_email_last_sent'] ) ) : ?>
279
            <p>
280
				<?php printf(
281
					__( 'Last notification email was sent on %1$s. Click %2$s button to reset sending the notification.', 'email-log' ),
282
					'<strong>' . get_date_from_gmt( date( 'Y-m-d H:i:s', $db_size_notification_data['threshold_email_last_sent'] ), \EmailLog\Util\get_user_defined_date_time_format() ) . '</strong>',
283
					'<b>Save</b>'
284
				); ?>
285
            </p>
286
		<?php endif; ?>
287
		<?php
288
	}
289
290
	/**
291
	 * Removes any additional keys set other than the ones in db_size_notification array.
292
	 *
293
	 * @since 2.3.0
294
	 *
295
	 * @param array $arr `db_size_notification` option array.
296
	 *
297
	 * @return array `db_size_notification` option array with keys removed other than the allowed.
298
	 */
299
	protected function restrict_array_to_db_size_notification_setting_keys( $arr ) {
300
		$allowed_keys = array_keys( $this->section->default_value['db_size_notification'] );
301
		$arr_keys     = array_keys( $arr );
302
303
		// Return the array when only the allowed keys exist.
304
		$intersecting_keys = array_intersect( $allowed_keys, $arr_keys );
305
		if ( count( $allowed_keys ) === count( $intersecting_keys ) ) {
306
			return $arr;
307
		}
308
309
		// Otherwise remove keys that aren't expected.
310
		$diff_keys = array_diff_key( $arr, $allowed_keys );
311
		foreach ( $diff_keys as $key ) {
312
			unset( $arr[ $key ] );
313
		}
314
315
		return $arr;
316
	}
317
318
	/**
319
	 * Sanitizes the db_size_notification option.
320
	 *
321
	 * @since 2.3.0
322
	 *
323
	 * @param array $db_size_notification_data
324
	 *
325
	 * @return array $db_size_notification_data
326
	 */
327
	public function sanitize_db_size_notification( $db_size_notification_data ) {
328
		$db_size_notification_data = $this->restrict_array_to_db_size_notification_setting_keys( $db_size_notification_data );
329
330
		foreach ( $db_size_notification_data as $setting => $value ) {
331
			if ( 'notify' === $setting ) {
332
				$db_size_notification_data[ $setting ] = \filter_var( $value, FILTER_VALIDATE_BOOLEAN );
333
			} elseif ( 'admin_email' === $setting ) {
334
				$db_size_notification_data[ $setting ] = \sanitize_email( $value );
335
			} elseif ( 'logs_threshold' === $setting ) {
336
				$db_size_notification_data[ $setting ] = absint( \sanitize_text_field( $value ) );
337
			}
338
		}
339
340
		// wp_parse_args won't merge nested array keys. So add the key here if it is not set.
341
		if ( ! array_key_exists( 'notify', $db_size_notification_data ) ) {
342
			$db_size_notification_data['notify'] = false;
343
		}
344
		if ( ! array_key_exists( 'log_threshold_met', $db_size_notification_data ) ) {
345
			$db_size_notification_data['log_threshold_met'] = false;
346
		}
347
		if ( ! array_key_exists( 'threshold_email_last_sent', $db_size_notification_data ) ) {
348
			$db_size_notification_data['threshold_email_last_sent'] = false;
349
		}
350
351
		return $db_size_notification_data;
352
	}
353
354
	/**
355
	 * Triggers the Cron to notify admin via email.
356
	 *
357
	 * Email is triggered only when the threshold setting is met.
358
	 *
359
	 * @since 2.3.0
360
	 */
361
	public function verify_email_log_threshold() {
362
		$cron_hook = 'el_trigger_notify_email_when_log_threshold_met';
363
		if ( ! wp_next_scheduled( $cron_hook ) ) {
364
			wp_schedule_event( time(), 'hourly', $cron_hook );
365
		}
366
	}
367
368
	/**
369
	 * Utility method to verify all the given keys exist in the given array.
370
	 *
371
	 * This method helps reduce the Cyclomatic Complexity in its parent method.
372
	 *
373
	 * @since 2.3.0
374
	 *
375
	 * @param array $arr  The array whose keys must be checked.
376
	 * @param array $keys All the required keys that the array must contain.
377
	 *
378
	 * @return bool
379
	 */
380
	protected function has_array_contains_required_keys( $arr, $keys ) {
381
		$has_keys = true;
382
383
		if ( ! is_array( $arr ) || ! is_array( $keys ) ) {
0 ignored issues
show
introduced by
The condition is_array($arr) is always true.
Loading history...
introduced by
The condition is_array($keys) is always true.
Loading history...
384
			return false;
385
		}
386
387
		foreach ( $arr as $key => $value ) {
388
			$has_keys = $has_keys && in_array( $key, $keys, true );
389
		}
390
391
		return $has_keys;
392
	}
393
394
	/**
395
	 * Send the Threshold notification email to the admin.
396
	 *
397
	 * @since 2.3.0
398
	 */
399
	public function trigger_threshold_met_notification_email() {
400
		$email_log  = email_log();
401
		$logs_count = absint( $email_log->table_manager->get_logs_count() );
402
403
		$setting_data = $this->get_value();
404
405
		// Return early.
406
		if ( ! array_key_exists( 'db_size_notification', $setting_data ) ) {
407
			return;
408
		}
409
410
		$db_size_notification_key  = 'db_size_notification';
411
		$db_size_notification_data = $setting_data[ $db_size_notification_key ];
412
413
		// Return early.
414
		$keys = array_keys( $this->section->default_value['db_size_notification'] );
415
		if ( ! $this->has_array_contains_required_keys( $db_size_notification_data, $keys ) ) {
416
			return;
417
		}
418
419
		$is_notification_enabled = $db_size_notification_data['notify'];
420
		$admin_email             = $db_size_notification_data['admin_email'];
421
		$logs_threshold          = absint( $db_size_notification_data['logs_threshold'] );
422
		$logs_threshold_met      = $db_size_notification_data['log_threshold_met'];
423
424
		// Ideally threshold cannot be 0. Also, skip sending email if it is already sent.
425
		if ( 0 === $logs_threshold || true === $logs_threshold_met ) {
426
			return;
427
		}
428
429
		if ( $logs_count < $logs_threshold ) {
430
			return;
431
		}
432
433
		$this->register_threshold_met_admin_notice();
434
435
		if ( $is_notification_enabled && is_email( $admin_email ) ) {
436
			$subject = sprintf( __( 'Email Log Plugin: Your log threshold of %s has been met', 'email-log' ), $logs_threshold );
437
			$message = <<<EOT
438
<p>This email is generated by the Email Log plugin.</p>
439
<p>Your log threshold of $logs_threshold has been met. You may manually delete the logs to keep your database table in size.</p>
440
<p>Also, consider using our <a href="https://wpemaillog.com/addons/auto-delete-logs/">Auto Delete Logs</a> plugin to delete the logs automatically.</p>
441
EOT;
442
			$headers = array( 'Content-Type: text/html; charset=UTF-8' );
443
444
			/**
445
			 * Filters Log threshold notification email subject.
446
			 *
447
			 * @since 2.3.0
448
			 *
449
			 * @param string $subject The email subject.
450
			 */
451
			$subject = apply_filters( 'el_log_threshold_met_notification_email_subject', $subject );
452
453
			/**
454
			 * Filters Log threshold notification email body.
455
			 *
456
			 * @since 2.3.0
457
			 *
458
			 * @param string $message        The email body.
459
			 * @param int    $logs_threshold The log threshold value set by the user.
460
			 */
461
			$message = apply_filters( 'el_log_threshold_met_notification_email_body', $message, $logs_threshold );
462
463
			wp_mail( $admin_email, $subject, $message, $headers );
464
465
			$setting_data[ $db_size_notification_key ]['log_threshold_met']         = true;
466
			$setting_data[ $db_size_notification_key ]['threshold_email_last_sent'] = time();
467
			\update_option( $this->section->option_name, $setting_data );
468
		}
469
	}
470
471
	/**
472
	 * Registers the Email Log threshold met admin notice.
473
	 *
474
	 * @since 2.3.0
475
	 */
476
	public function register_threshold_met_admin_notice() {
477
		add_action( 'admin_notices', array( $this, 'render_log_threshold_met_notice' ) );
478
	}
479
480
	/**
481
	 * Displays the Email Log threshold met admin notice.
482
	 *
483
	 * @since 2.3.0
484
	 */
485
	public function render_log_threshold_met_notice() {
486
		$email_log      = email_log();
487
		$logs_count     = absint( $email_log->table_manager->get_logs_count() );
488
		$notice_message = sprintf( __( 'Currently there are %1$s logged, which is more than the threshold that is set in the %2$s screen. You can delete some logs or increase the threshold. You can also use our %3$s add-on to automatically delete logs', 'email-log' ),
489
			$logs_count . _n( ' email log', ' email logs', $logs_count, 'email-log' ),
490
			'<a href="' . esc_url( admin_url( 'admin.php?page=' . SettingsPage::PAGE_SLUG ) ) . '">settings</a> screen',
491
			'<a href="' . esc_url( 'https://wpemaillog.com/addons/auto-delete-logs/' ) . '">Auto Delete Logs</a>'
492
			 );
493
		?>
494
        <div class="notice notice-warning is-dismissible">
495
            <p><?php echo $notice_message; ?></p>
496
        </div>
497
		<?php
498
	}
499
500
	/**
501
	 * Sanitize Pause Emails value.
502
	 *
503
	 * @param string $value User selected value. Either 'true' or 'false'.
504
	 *
505
	 * @return string Sanitized value.
506
	 */
507
	public function sanitize_pause_emails( $value ) {
508
		return sanitize_text_field( $value );
509
	}
510
511
	public function render_pause_emails_settings( $args ) {
512
		$option      = $this->get_value();
513
		$remove_data = $option[ $args['id'] ];
514
515
		$field_name = $this->section->option_name . '[' . $args['id'] . ']';
516
		?>
517
518
		<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="true" <?php checked( 'true', $remove_data ); ?>>
519
		<?php _e( 'Check this box if you would like to pause the emails that are sent.', 'email-log' ) ?>
520
521
		<p>
522
			<em>
523
				<?php echo wp_kses(
524
					__( '<strong>Note</strong>: Emails will only be logged and won\'t be sent.', 'email-log' ),
525
					array(
526
						'strong' => array(),
527
						'em'     => array(),
528
					)
529
				);
530
				?>
531
			</em>
532
		</p>
533
534
		<?php
535
	}
536
}
537