1
|
|
|
<?php namespace EmailLog\Core\UI\Setting; |
2
|
|
|
|
3
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Email Log Setting. |
7
|
|
|
* Contains a setting section and a number of fields. |
8
|
|
|
* |
9
|
|
|
* @since 2.0.0 |
10
|
|
|
*/ |
11
|
|
|
abstract class Setting { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \EmailLog\Core\UI\Setting\SettingSection |
15
|
|
|
*/ |
16
|
|
|
protected $section; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Set default values for SettingSection. |
20
|
|
|
* Further customization can be done by the add-on in the `initialize` method. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() { |
23
|
|
|
$this->section = new SettingSection(); |
24
|
|
|
|
25
|
|
|
$this->initialize(); |
26
|
|
|
|
27
|
|
|
$this->section->fields = $this->get_fields(); |
28
|
|
|
$this->section->callback = array( $this, 'render' ); |
29
|
|
|
$this->section->sanitize_callback = array( $this, 'sanitize' ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Setup hooks and filters. |
34
|
|
|
*/ |
35
|
|
|
public function load() { |
36
|
|
|
add_filter( 'el_setting_sections', array( $this, 'register' ) ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Register the setting using the filter. |
41
|
|
|
* |
42
|
|
|
* @param SettingSection[] $sections List of existing SettingSections. |
43
|
|
|
* |
44
|
|
|
* @return SettingSection[] Modified list of SettingSections. |
45
|
|
|
*/ |
46
|
|
|
public function register( $sections ) { |
47
|
|
|
$sections[] = $this->section; |
48
|
|
|
|
49
|
|
|
return $sections; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the value stored in the option. |
54
|
|
|
* If no values are found then the default values are returned. |
55
|
|
|
* |
56
|
|
|
* @return array Stored value. |
57
|
|
|
*/ |
58
|
|
|
public function get_value() { |
59
|
|
|
$value = get_option( $this->section->option_name ); |
60
|
|
|
|
61
|
|
|
return wp_parse_args( $value, $this->section->default_value ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Customize the SettingSection. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
abstract protected function initialize(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the list of SettingFields. |
73
|
|
|
* |
74
|
|
|
* @return SettingField[] List of fields for the Setting. |
75
|
|
|
*/ |
76
|
|
|
protected function get_fields() { |
77
|
|
|
return $this->build_fields(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Render the Settings section. |
82
|
|
|
* |
83
|
|
|
* By default it does nothing. |
84
|
|
|
*/ |
85
|
|
|
public function render() { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sanitize the option values. |
91
|
|
|
* |
92
|
|
|
* @param mixed $values User entered values. |
93
|
|
|
* |
94
|
|
|
* @return mixed Sanitized values. |
95
|
|
|
*/ |
96
|
|
|
public function sanitize( $values ) { |
97
|
|
|
if ( ! is_array( $values ) ) { |
98
|
|
|
return array(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$values = wp_parse_args( $values, $this->section->default_value ); |
102
|
|
|
$sanitized_values = array(); |
103
|
|
|
|
104
|
|
|
foreach ( $this->section->field_labels as $field_id => $label ) { |
105
|
|
|
$callback = array( $this, 'sanitize_' . $field_id ); |
106
|
|
|
|
107
|
|
|
if ( is_callable( $callback ) ) { |
108
|
|
|
$sanitized_values[ $field_id ] = call_user_func( $callback, $values[ $field_id ] ); |
109
|
|
|
} else { |
110
|
|
|
$sanitized_values[ $field_id ] = $values[ $field_id ]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $sanitized_values; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Build SettingField objects from field id and labels. |
119
|
|
|
* |
120
|
|
|
* @since 2.1.0 |
121
|
|
|
* |
122
|
|
|
* @return \EmailLog\Core\UI\Setting\SettingField[] Built SettingFields. |
123
|
|
|
*/ |
124
|
|
|
protected function build_fields() { |
125
|
|
|
$fields = array(); |
126
|
|
|
|
127
|
|
|
foreach ( $this->section->field_labels as $field_id => $label ) { |
128
|
|
|
$field = new SettingField(); |
129
|
|
|
$field->id = $field_id; |
130
|
|
|
$field->title = $label; |
131
|
|
|
$field->args = array( 'id' => $field_id ); |
132
|
|
|
$field->callback = array( $this, 'render_' . $field_id . '_settings' ); |
133
|
|
|
|
134
|
|
|
$fields[] = $field; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $fields; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|