Completed
Push — dev/2.1.0 ( 0893b9...39cfde )
by Sudar
01:42
created

EmailLogSetting::get_fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 20
ccs 0
cts 14
cp 0
crap 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
namespace EmailLog\Core\UI\Setting;
3
use EmailLog\Core\UI\Setting\SettingField;
4
5
/**
6
 * Class EmailLogSetting
7
 * @package EmailLog\Core\UI\Setting
8
 *
9
 * All Email Log Core settings are handled.
10
 *
11
 * @since 2.1.0
12
 */
13
class EmailLogSetting extends Setting {
14
15
	/**
16
	 * Implement `initialize()` method.
17
	 */
18
	protected function initialize() {
19
		$this->section->id          = 'email-log';
20
		$this->section->title       = __( 'Email Log Settings', 'email-log' );
21
		$this->section->option_name = 'el_email_log_core';
22
23
		$this->load();
24
	}
25
26
	public function get_fields() {
27
		$fields = array();
28
29
		$email_log_fields = array(
30
			'allowed_user_roles'  => __( 'Allowed User Roles', 'email-log' ),
31
			'remove_on_uninstall' => __( 'Remove Data on Uninstall?', 'email-log' ),
32
		);
33
34
		foreach ( $email_log_fields as $field_id => $label ) {
35
			$field           = new SettingField();
36
			$field->id       = $field_id;
37
			$field->title    = $label;
38
			$field->args     = array( 'id' => $field_id );
39
			$field->callback = array( $this,'render_' . $field_id . '_settings' );
40
41
			$fields[] = $field;
42
		}
43
44
		return $fields;
45
	}
46
47
	public function render() {
48
	?>
49
		<p><?php _e( 'Email Log Settings lets you control who can view Email Logs and lets you keep the Email Logs when you delete the plugin.', 'email-log' ); ?></p>
50
	<?php
51
	}
52
53
	/**
54
	 * Implement `sanitize()` method.
55
	 *
56
	 * @param mixed $values {@inheritDoc}
57
	 *
58
	 * @return mixed $values {@inheritDoc}
59
	 */
60
	public function sanitize( $values ) {
61
		if ( ! is_array( $values ) ) {
62
			return array();
63
		}
64
65
		// TODO: Dissect sanitization methods to have separate methods for each field.
66
		foreach ( $values as $key => $value ) {
67
			if ( $key === 'allowed_user_roles' ) {
68
				$values[ $key ] = array_map( 'sanitize_text_field', $values[ $key ] );
69
			} elseif ( $key === 'remove_on_uninstall' ) {
70
				$values[ $key ] = sanitize_text_field( $value );
71
			}
72
		}
73
		return $values;
74
	}
75
76
	/**
77
	 * Renders the Email Log `Allowed User Roles` settings.
78
	 *
79
	 * @param array $args
80
	 */
81
	public function render_allowed_user_roles_settings( $args ) {
82
		$option          = $this->get_value();
83
		$available_roles = get_editable_roles();
84
		foreach( $available_roles as $role ) {
85
			if ( trim( $role['name'] ) === 'Administrator' ) {
86
				?>
87
				<p><input type="checkbox" name="<?php echo esc_attr( $this->section->option_name . '[' . $args['id'] . '][]' ); ?>" value="<?php echo trim( $role['name'] ); ?>" checked="checked" /> <?php echo trim( $role['name'] ); ?>
88
				</p>
89
				<?php
90
			} else {
91
				?>
92
				<p><input type="checkbox" name="<?php echo esc_attr( $this->section->option_name . '[' . $args['id'] . '][]' ); ?>" value="<?php echo trim( $role['name'] ); ?>" <?php \EmailLog\Util\checked_array( $option[ $args['id'] ], trim( $role['name'] ) ); ?> /> <?php echo trim( $role['name'] ); ?>
93
				</p>
94
				<?php
95
			}
96
		}
97
		?>
98
		<p><?php _e( '<small><strong>Note:</strong> Users with the above <strong>User Roles</strong> can view Email Logs. The default User Role is \'<strong>administrator</strong>\'.</small>', 'email-log' ); ?>
99
		<?php _e( '<small>Administrator role cannot be disabled.</small>', 'email-log' ); ?></p>
100
		<?php
101
	}
102
103
	/**
104
	 * Renders the Email Log `Remove Data on Uninstall?` settings.
105
	 *
106
	 * @param array $args
107
	 */
108
	public function render_remove_on_uninstall_settings( $args ) {
109
		$option          = $this->get_value();
110
?>
111
		<input type="checkbox" name="<?php echo esc_attr( $this->section->option_name . '[' . $args['id'] . ']' ); ?>" value="true" <?php checked( 'true', $option[ $args['id'] ] ); ?> /> <?php _e( 'Check this box if you would like to completely remove all of its data when the plugin is deleted.', 'email-log' ) ?>
112
		<p><?php _e( '<small><strong>Note:</strong> You can also export the Email Logs using our <a href="https://wpemaillog.com/addons/export-logs/" rel="noopener noreferrer" target="_blank">Export Logs</a> add-on.</small>', 'email-log' ); ?></p>
113
<?php
114
	}
115
}