Completed
Pull Request — dev/2.1.0 (#102)
by Maria Daniel Deepak
06:54
created

EmailLogSetting::sanitize()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 30
rs 8.8571
c 2
b 0
f 0
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       = __( 'Email Log 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
			'remove_email_logs'  => __( 'Remove Data on Uninstall?', 'email-log' ),
29
		);
30
31
		foreach ( $email_log_fields as $field_id => $label ) {
32
			$field           = new SettingField();
33
			$field->id       = $field_id;
34
			$field->title    = $label;
35
			$field->args     = array( 'id' => $field_id );
36
			$field->callback = array( $this, 'render_email_log_settings' );
37
38
			$fields[] = $field;
39
		}
40
41
		return $fields;
42
	}
43
44
	/**
45
	 * Implement `render()` method.
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
		foreach ( $values as $key => $value ) {
65
			if ( $key === 'allowed_user_roles' ) {
66
				$values[ $key ] = array_map( 'sanitize_text_field', $values[ $key ] );
67
			} elseif ( $key === 'remove_email_logs' ) {
68
				$values[ $key ] = sanitize_text_field( $value );
69
			}
70
		}
71
		return $values;
72
	}
73
74
	/**
75
	 * Renders the Email Log settings fields.
76
	 *
77
	 * @param array $args
78
	 */
79
	public function render_email_log_settings( $args ) {
80
		$option          = $this->get_value();
81
		if ( 'allowed_user_roles' === $args['id'] ) {
82
			$available_roles = get_editable_roles();
83
			foreach( $available_roles as $role ) {
84
				if ( trim( $role['name'] ) === 'Administrator' ) {
85
					?>
86
					<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'] ); ?>
87
					</p>
88
					<?php
89
				} else {
90
					?>
91
					<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[ $args['id'] ], trim( $role['name'] ) ); ?> /> <?php echo trim( $role['name'] ); ?>
92
					</p>
93
					<?php
94
				}
95
			}
96
			?>
97
			<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' ); ?>
98
			<?php _e( '<small>Administrator role cannot be disabled.</small>', 'email-log' ); ?></p>
99
			<?php
100
		} elseif ( 'remove_email_logs' === $args['id'] ) {
101
?>
102
			<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' ) ?>
103
            <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>
104
<?php
105
		}
106
}
107
108
	/**
109
	 * Checks the Checkbox when values are present in a given array.
110
	 *
111
	 * Use this function in Checkbox fields.
112
	 *
113
	 * @param array $values   List of all possible values.
114
	 * @param string $current The current value to be checked.
115
	 */
116
	public function checked_array( $values, $current ) {
117
		if ( in_array( $current, $values ) ) {
118
			echo "checked='checked'";
119
		}
120
	}
121
}