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
|
|
|
'capability' => __( 'Capability', '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 capability can view Email Logs. The default capability is \'<strong>manage_options</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
|
|
|
$values[ $key ] = sanitize_text_field( $value ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $values; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Renders the Capability field to set Capability. |
73
|
|
|
* |
74
|
|
|
* @param array $args |
75
|
|
|
*/ |
76
|
|
|
public function render_email_log_capability_field( $args ) { |
77
|
|
|
$option = $this->get_value(); |
78
|
|
|
$admin_role_set = get_role( 'administrator' )->capabilities; |
79
|
|
|
$el_capability = ''; |
80
|
|
|
?> |
81
|
|
|
<select name="<?php echo esc_attr( $this->section->option_name . '[' . $args['id'] . ']' ); ?>"> |
82
|
|
|
<?php |
83
|
|
|
foreach( $admin_role_set as $capability => $grant ) { |
84
|
|
|
if ( ( false === $option && 'manage_options' === $capability ) || |
85
|
|
|
( $option[ $args['id'] ] === $capability ) ) { |
86
|
|
|
$selected = 'selected="selected"'; |
87
|
|
|
$el_capability = $capability; |
88
|
|
|
} |
89
|
|
|
?> |
90
|
|
|
<option value="<?php echo $capability; ?>" <?php echo isset( $selected ) ? $selected : ''; ?>><?php echo $capability; ?></option> |
91
|
|
|
<?php |
92
|
|
|
unset( $selected ); |
93
|
|
|
} |
94
|
|
|
?> |
95
|
|
|
</select> |
96
|
|
|
<?php |
97
|
|
|
if ( isset( $el_capability ) && ! empty( $el_capability ) ) { |
98
|
|
|
$this->modify_view_log_capability( $el_capability ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Sets the capability to view the Email Log content. |
104
|
|
|
* |
105
|
|
|
* Uses the Email Log API to set capability. Refer |
106
|
|
|
* @link https://wpemaillog.com/docs/developer-docs/el_view_email_log_capability/ |
107
|
|
|
* |
108
|
|
|
* @param string $capability |
109
|
|
|
*/ |
110
|
|
|
protected function modify_view_log_capability( $capability ) { |
111
|
|
|
apply_filters( 'el_view_email_log_capability', $capability ); |
112
|
|
|
} |
113
|
|
|
} |