Completed
Push — master ( fec9e0...63ebd4 )
by Sudar
01:40
created

CoreSetting::get_user_roles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
1
<?php namespace EmailLog\Core\UI\Setting;
2
3
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
4
5
/**
6
 * All Email Log Core settings.
7
 *
8
 * @since 2.1.0
9
 */
10
class CoreSetting extends Setting {
11
12
	protected function initialize() {
13
		$this->section->id          = 'email-log-core';
14
		$this->section->title       = __( 'Core Email Log Settings', 'email-log' );
15
		$this->section->option_name = 'email-log-core';
16
17
		$this->section->field_labels = array(
18
			'allowed_user_roles'  => __( 'Allowed User Roles', 'email-log' ),
19
			'remove_on_uninstall' => __( 'Remove Data on Uninstall?', 'email-log' ),
20
		);
21
22
		$this->section->default_value = array(
23
			'allowed_user_roles'  => array(),
24
			'remove_on_uninstall' => '',
25
		);
26
27
		$this->load();
28
	}
29
30
	/**
31
	 * Override `load` method so that the core settings are displayed first.
32
	 *
33
	 * @inheritdoc
34
	 */
35
	public function load() {
36
		add_filter( 'el_setting_sections', array( $this, 'register' ), 9 );
37
38
		add_action( 'add_option_' . $this->section->option_name, array( $this, 'allowed_user_roles_added' ), 10, 2 );
39
		add_action( 'update_option_' . $this->section->option_name, array( $this, 'allowed_user_roles_changed' ), 10, 2 );
40
	}
41
42
	/**
43
	 * Renders the Email Log `Allowed User Roles` settings.
44
	 *
45
	 * @param array $args Arguments.
46
	 */
47
	public function render_allowed_user_roles_settings( $args ) {
48
		$option         = $this->get_value();
49
		$selected_roles = $option[ $args['id'] ];
50
51
		$field_name = $this->section->option_name . '[' . $args['id'] . '][]';
52
53
		$available_roles = get_editable_roles();
54
		unset( $available_roles['administrator'] );
55
		?>
56
57
		<p>
58
			<input type="checkbox" checked disabled><?php _e( 'Administrator', 'email-log' ); ?>
59
		</p>
60
61
		<?php foreach ( $available_roles as $role_id => $role ) : ?>
62
			<p>
63
				<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $role_id ); ?>"
64
					<?php \EmailLog\Util\checked_array( $selected_roles, $role_id ); ?>>
65
66
				<?php echo $role['name']; ?>
67
			</p>
68
		<?php endforeach; ?>
69
70
		<p>
71
			<em>
72
				<?php _e( '<strong>Note:</strong> Users with the above User Roles can view Email Logs.', 'email-log' ); ?>
73
				<?php _e( 'Administrator role always has access and cannot be disabled.', 'email-log' ); ?>
74
			</em>
75
		</p>
76
77
		<?php
78
	}
79
80
	/**
81
	 * Sanitize allowed user roles setting.
82
	 *
83
	 * @param array $roles User selected user roles.
84
	 *
85
	 * @return array Sanitized user roles.
86
	 */
87
	public function sanitize_allowed_user_roles( $roles ) {
88
		if ( ! is_array( $roles ) ) {
89
			return array();
90
		}
91
92
		return array_map( 'sanitize_text_field', $roles );
93
	}
94
95
	/**
96
	 * Renders the Email Log `Remove Data on Uninstall?` settings.
97
	 *
98
	 * @param array $args
99
	 */
100
	public function render_remove_on_uninstall_settings( $args ) {
101
		$option      = $this->get_value();
102
		$remove_data = $option[ $args['id'] ];
103
104
		$field_name = $this->section->option_name . '[' . $args['id'] . ']';
105
		?>
106
107
		<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="true" <?php checked( 'true', $remove_data ); ?>>
108
		<?php _e( 'Check this box if you would like to completely remove all of its data when the plugin is deleted.', 'email-log' ) ?>
109
110
		<p>
111
			<em>
112
				<?php printf(
113
					__( '<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' ),
114
					'https://wpemaillog.com/addons/export-logs/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=settings&utm_content=el'
115
				); ?>
116
			</em>
117
		</p>
118
119
		<?php
120
	}
121
122
	/**
123
	 * Sanitize Remove on uninstall value.
124
	 *
125
	 * @param string $value User entered value.
126
	 *
127
	 * @return string Sanitized value.
128
	 */
129
	public function sanitize_remove_on_uninstall( $value ) {
130
		return sanitize_text_field( $value );
131
	}
132
133
	/**
134
	 * Allowed user role list option is added.
135
	 *
136
	 * @param string $option Option name.
137
	 * @param array  $value  Option value.
138
	 */
139
	public function allowed_user_roles_added( $option, $value ) {
140
		$this->allowed_user_roles_changed( array(), $value );
141
	}
142
143
	/**
144
	 * Allowed user role list option was update.
145
	 *
146
	 * Change user role capabilities when the allowed user role list is changed.
147
	 *
148
	 * @param array $old_value Old Value.
149
	 * @param array $new_value New Value.
150
	 */
151
	public function allowed_user_roles_changed( $old_value, $new_value ) {
152
		$old_roles = $this->get_user_roles( $old_value );
153
		$new_roles = $this->get_user_roles( $new_value );
154
155
		/**
156
		 * The user roles who can manage email log list is changed.
157
		 *
158
		 * @since 2.1.0
159
		 *
160
		 * @param array $old_roles Old user roles.
161
		 * @param array $new_roles New user roles.
162
		 */
163
		do_action( 'el-log-list-manage-user-roles-changed', $old_roles, $new_roles );
164
	}
165
166
	/**
167
	 * Get User roles from option value.
168
	 *
169
	 * @access protected
170
	 *
171
	 * @param array $option Option value
172
	 *
173
	 * @return array User roles.
174
	 */
175
	protected function get_user_roles( $option ) {
176
		if ( ! array_key_exists( 'allowed_user_roles', $option ) ) {
177
			return array();
178
		}
179
180
		$user_roles = $option['allowed_user_roles'];
181
		if ( ! is_array( $user_roles ) ) {
182
			$user_roles = array( $user_roles );
183
		}
184
185
		return $user_roles;
186
	}
187
}
188