Completed
Pull Request — dev/2.1.0 (#100)
by Maria Daniel Deepak
02:01
created

EmailLogSetting::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
namespace EmailLog\Core\UI\Setting;
3
use EmailLog\Core\UI\Setting\SettingField;
4
5
class EmailLogSetting extends Setting {
6
7
	/**
8
	 * Implement `initialize()` method.
9
	 */
10
	protected function initialize() {
11
		$this->section->id          = 'email-log';
12
		$this->section->title       = __( 'Plugin Settings', 'email-log' );
13
		$this->section->option_name = 'el_email_log';
14
15
		$this->load();
16
	}
17
18
	/**
19
	 * Implement `get_fields()` method.
20
	 *
21
	 * @return array
22
	 */
23
	public function get_fields() {
24
		$fields = array();
25
26
		$email_log_fields = array(
27
			'allowed_user_roles'  => __( 'Allowed User Roles', 'email-log' ),
28
		);
29
30
		foreach ( $email_log_fields as $field_id => $label ) {
31
			$field           = new SettingField();
32
			$field->id       = $field_id;
33
			$field->title    = $label;
34
			$field->args     = array( 'id' => $field_id );
35
			$field->callback = array( $this, 'render_email_log_capability_field' );
36
37
			$fields[] = $field;
38
		}
39
40
		return $fields;
41
	}
42
43
	/**
44
	 * Implement `render()` method.
45
	 */
46
	public function render() {
47
	?>
48
		<p><?php _e( 'Users with the following <strong>User Roles</strong> can view Email Logs. The default User Role is \'<strong>administrator</strong>\'.', 'email-log' ); ?></p>
49
	<?php
50
	}
51
52
	/**
53
	 * Implement `sanitize()` method.
54
	 *
55
	 * @param mixed $values {@inheritDoc}
56
	 *
57
	 * @return mixed $values {@inheritDoc}
58
	 */
59
	public function sanitize( $values ) {
60
		if ( ! is_array( $values ) ) {
61
			return array();
62
		}
63
64
		foreach ( $values as $key => $value ) {
65
			if ( $key === 'allowed_user_roles' ) {
66
				$values[ $key ] = array_map( 'sanitize_text_field', $values[ $key ] );
67
			}
68
		}
69
70
		return $values;
71
	}
72
73
	/**
74
	 * Renders the Capability field to set Capability.
75
	 *
76
	 * @param array $args
77
	 */
78
	public function render_email_log_capability_field( $args ) {
79
		$option          = $this->get_value();
80
		$option          = $option[ $args['id'] ];
81
		$available_roles = get_editable_roles();
82
		foreach( $available_roles as $role ) {
83
			if ( trim( $role['name'] ) === 'Administrator' ) {
84
	?>
85
			<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'] ); ?>
86
			</p>
87
	<?php
88
			} else {
89
	?>
90
			<p><input type="checkbox" name="<?php echo esc_attr( $this->section->option_name . '[' . $args['id'] . '][]' ); ?>" value="<?php echo trim( $role['name'] ); ?>" <?php $this->checked_array( $option, trim( $role['name'] ) ); ?> /> <?php echo trim( $role['name'] ); ?>
91
			</p>
92
	<?php
93
			}
94
		}
95
	?>
96
		<p><?php _e( '<em><strong>Note:</strong> Administrator role cannot be disabled.</em>', 'email-log' ); ?></p>
97
	<?php
98
}
99
100
	/**
101
	 * Checks the Checkbox when values are present in a given array.
102
	 *
103
	 * Use this function in Checkbox fields.
104
	 *
105
	 * @param array $values   List of all possible values.
106
	 * @param string $current The current value to be checked.
107
	 */
108
	public function checked_array( $values, $current ) {
109
		if ( in_array( $current, $values ) ) {
110
			echo "checked='checked'";
111
		}
112
	}
113
}