Completed
Push — dev/2.1.0 ( c5edab...60662d )
by Sudar
01:44
created

CoreSetting   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 153
rs 10
c 1
b 0
f 0
wmc 15
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 17 1
A load() 0 5 1
B render_allowed_user_roles_settings() 0 32 2
A sanitize_allowed_user_roles() 0 7 2
A render_remove_on_uninstall_settings() 0 18 1
A sanitize_remove_on_uninstall() 0 3 1
C allowed_user_roles_changed() 0 27 7
1
<?php namespace EmailLog\Core\UI\Setting;
2
3
use EmailLog\Core\UI\Page\LogListPage;
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
		);
23
24
		$this->section->default_value = array(
25
			'allowed_user_roles'  => array(),
26
			'remove_on_uninstall' => '',
27
		);
28
29
		$this->load();
30
	}
31
32
	/**
33
	 * Override `load` method so that the core settings are displayed first.
34
	 *
35
	 * @inheritdoc
36
	 */
37
	public function load() {
38
		add_filter( 'el_setting_sections', array( $this, 'register' ), 9 );
39
40
		add_action( 'update_option_' . $this->section->option_name, array( $this, 'allowed_user_roles_changed'), 10, 2 );
41
	}
42
43
	/**
44
	 * Renders the Email Log `Allowed User Roles` settings.
45
	 *
46
	 * @param array $args Arguments.
47
	 */
48
	public function render_allowed_user_roles_settings( $args ) {
49
		$option         = $this->get_value();
50
		$selected_roles = $option[ $args['id'] ];
51
52
		$field_name = $this->section->option_name . '[' . $args['id'] . '][]';
53
54
		$available_roles = get_editable_roles();
55
		unset( $available_roles['administrator'] );
56
		?>
57
58
		<p>
59
			<input type="checkbox" checked disabled><?php _e( 'Administrator', 'email-log' ); ?>
60
		</p>
61
62
		<?php foreach ( $available_roles as $role_id => $role ) : ?>
63
			<p>
64
				<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $role_id ); ?>"
65
					<?php \EmailLog\Util\checked_array( $selected_roles, $role_id ); ?>>
66
67
				<?php echo $role['name']; ?>
68
			</p>
69
		<?php endforeach; ?>
70
71
		<p>
72
			<em>
73
				<?php _e( '<strong>Note:</strong> Users with the above User Roles can view Email Logs.', 'email-log' ); ?>
74
				<?php _e( 'Administrator role always has access and cannot be disabled.', 'email-log' ); ?>
75
			</em>
76
		</p>
77
78
		<?php
79
	}
80
81
	/**
82
	 * Sanitize allowed user roles setting.
83
	 *
84
	 * @param array $roles User selected user roles.
85
	 *
86
	 * @return array Sanitized user roles.
87
	 */
88
	public function sanitize_allowed_user_roles( $roles ) {
89
		if ( ! is_array( $roles ) ) {
90
			return array();
91
		}
92
93
		return array_map( 'sanitize_text_field', $roles );
94
	}
95
96
	/**
97
	 * Renders the Email Log `Remove Data on Uninstall?` settings.
98
	 *
99
	 * @param array $args
100
	 */
101
	public function render_remove_on_uninstall_settings( $args ) {
102
		$option      = $this->get_value();
103
		$remove_data = $option[ $args['id'] ];
104
105
		$field_name = $this->section->option_name . '[' . $args['id'] . ']';
106
		?>
107
108
		<input type="checkbox" name="<?php echo esc_attr( $field_name ); ?>" value="true" <?php checked( 'true', $remove_data ); ?>>
109
		<?php _e( 'Check this box if you would like to completely remove all of its data when the plugin is deleted.', 'email-log' ) ?>
110
111
		<p>
112
			<em>
113
				<?php _e( '<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.', 'email-log' ); ?>
114
			</em>
115
		</p>
116
117
		<?php
118
	}
119
120
	/**
121
	 * Sanitize Remove on uninstall value.
122
	 *
123
	 * @param string $value User entered value.
124
	 *
125
	 * @return string Sanitized value.
126
	 */
127
	public function sanitize_remove_on_uninstall( $value ) {
128
		return sanitize_text_field( $value );
129
	}
130
131
	/**
132
	 * Change user role capabilities when the allowed user role list is changed.
133
	 *
134
	 * @param array $old_value Old Value.
135
	 * @param array $new_value New Value.
136
	 */
137
	public function allowed_user_roles_changed( $old_value, $new_value ) {
138
		$old_roles = $old_value['allowed_user_roles'];
139
		if ( ! is_array( $old_roles ) ) {
140
			$old_roles = array( $old_roles );
141
		}
142
143
		$new_roles = $new_value['allowed_user_roles'];
144
		if ( ! is_array( $new_roles ) ) {
145
			$new_roles = array( $new_roles );
146
		}
147
148
		foreach ( $old_roles as $old_role ) {
149
			$role = get_role( $old_role );
150
151
			if ( ! is_null( $role ) ) {
152
				$role->remove_cap( LogListPage::CAPABILITY );
153
			}
154
		}
155
156
		foreach ( $new_roles as $new_role ) {
157
			$role = get_role( $new_role );
158
159
			if ( ! is_null( $role ) ) {
160
				$role->add_cap( LogListPage::CAPABILITY );
161
			}
162
		}
163
	}
164
}
165