-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettingsPage.php
More file actions
354 lines (322 loc) · 8.96 KB
/
Copy pathSettingsPage.php
File metadata and controls
354 lines (322 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
namespace Amarkal\Settings;
/**
* Implements a settings page.
*/
class SettingsPage
{
/**
* Configuration array
*
* @var array
*/
private $config;
/**
* The UI form instance
*
* @var Amarkal\UI\Form
*/
private $form;
/**
* The list of arguments for each section
*
* @var array
*/
private $sections;
/**
* The list of fields for the entire settings page
*
* @var Amarkal\UI\ComponentList
*/
private $fields;
/**
* An instance of the current field values (either the from the database, or the defaults)
*
* @var array
*/
private $values;
/**
* Set the config, create a form instance and add actions.
*
* @param array $config
*/
public function __construct( array $config = array() )
{
$this->config = array_merge($this->default_args(), $config);
$this->fields = new \Amarkal\UI\ComponentList();
$this->sections = array();
$this->form = new \Amarkal\UI\Form($this->fields);
\add_action('admin_menu', array($this,'add_submenu_page'));
\add_action('admin_enqueue_scripts', array($this,'enqueue_scripts'));
}
/**
* Add a section to this settings page
*
* @param array $args
* @return void
*/
public function add_section( array $args )
{
$args = \array_merge($this->default_section_args(), $args);
$slug = $args['slug'];
if(array_key_exists($slug, $this->sections))
{
throw new \RuntimeException("A section with '$slug' has already been created for this page.");
}
$this->sections[$slug] = $args;
}
/**
* Add a settings field to this settings page
*
* @param array $args
* @return void
*/
public function add_field( array $args )
{
$this->fields->add_component($args);
}
/**
* Get the current values for all fields from the database, or the default values if none exists
*
* @return array
*/
public function get_field_values()
{
if(!isset($this->values))
{
$this->values = array_merge($this->form->reset(), $this->get_old_instance());
}
return $this->values;
}
/**
* Get the value of the given field from the database, or the default value if none exists
*
* @param string $name
* @return any
*/
public function get_field_value( $name )
{
$values = $this->get_field_values();
return $values[$name];
}
/**
* Internally used to add a submenu page for this settings page
*/
public function add_submenu_page()
{
\add_submenu_page(
$this->config['parent_slug'],
$this->config['title'],
$this->config['menu_title'],
$this->config['capability'],
$this->config['slug'],
array($this, 'render')
);
}
/**
* Conditionally enqueue settings scripts and styles if the calling page is
* a settings page.
*/
public function enqueue_scripts()
{
// Only enqueue styles & scripts if this is a settings page
if($this->config['slug'] === filter_input(INPUT_GET, 'page'))
{
\wp_enqueue_style('amarkal-settings');
\wp_enqueue_script('amarkal-settings');
}
}
/**
* Render the settings page
*/
public function render()
{
$this->form->update($this->get_old_instance());
include __DIR__.'/SettingsPage.phtml';
\add_filter('admin_footer_text', array($this, 'footer_credits'));
}
/**
* Ajax callback internally used to update options values for the given
* settings page.
*
* @param array $new_instance
* @return array
*/
public function update( $new_instance )
{
if($this->can_update())
{
$old_instance = $this->get_old_instance();
$this->values = $this->form->update($new_instance, $old_instance);
\update_option($this->config['slug'], $this->values);
return $this->results_array(
$this->get_errors(),
$this->values
);
}
return $this->results_array(
array("You don't have permission to manage options on this site")
);
}
/**
* Ajax callback internally used to reset all component values to their
* defaults for the given settings page.
*
* @return type
*/
public function reset()
{
if($this->can_update())
{
\delete_option($this->config['slug']);
$this->values = $this->form->reset();
return $this->results_array(
array(),
$this->values
);
}
return $this->results_array(
array("You don't have permission to manage options on this site")
);
}
/**
* Ajax callback internally used to reset all component values to their
* defaults for the given settings section.
*
* @param string $slug
* @return void
*/
public function reset_section( $slug )
{
if($this->can_update())
{
$old_instance = $this->get_old_instance();
$final_instance = $this->form->reset_components($this->get_section_fields($slug));
// Array merge is needed in order not to delete fields from other sections
// since the $final_instance only contains the fields that were reset
$this->values = array_merge($old_instance, $final_instance);
\update_option($this->config['slug'], $this->values);
return $this->results_array(
array(),
$this->values
);
}
return $this->results_array(
array("You don't have permission to manage options on this site")
);
}
/**
* Renders Amarkal's credits on the page's footer.
*/
public function footer_credits()
{
echo '<span id="footer-thankyou">Created with <a href="https://github.com/amarkal/amarkal-settings">amarkal-settings</a>, a module within the <a href="https://github.com/amarkal/">Amarkal Framework</a></span>';
}
/**
* Get the component corresponding to the given name
*
* @param [string] $name
* @throws RuntimeException when the component cannot be found
* @return void
*/
public function get_component($name)
{
return $this->form->get_component_list()->get_by_name($name);
}
/**
* Get all the fields for the given section
*
* @param string $slug
* @return array
*/
private function get_section_fields($slug)
{
$fields = array();
foreach($this->fields->get_all() as $c)
{
if($c->section === $slug)
{
$fields[] = $c;
}
}
return $fields;
}
/**
* Get all errors from the form instance.
*
* @return array
*/
private function get_errors()
{
$errors = array();
foreach($this->form->get_errors() as $name => $error)
{
$errors[$name] = $error;
}
return $errors;
}
/**
* Generates a results array to be returned when an Ajax request is made.
*
* @param array $errors The list of errors
* @param array $values The list of values
* @return array
*/
private function results_array( $errors = array(), $values = '' )
{
return array(
'values' => $values,
'errors' => $errors
);
}
/**
* Check if the current user has the required privileges to update the
* settings values.
*
* @return boolean
*/
private function can_update()
{
return \current_user_can($this->config['capability']);
}
/**
* Get the old instance from the database.
*
* @return array
*/
private function get_old_instance()
{
return \get_option($this->config['slug'], array());
}
/**
* The default config arguments array for a page.
*
* @return array
*/
private function default_args()
{
return array(
'parent_slug' => '',
'slug' => '',
'title' => '',
'subtitle' => '',
'menu_title' => '',
'capability' => 'manage_options',
'footer_html' => '',
'subfooter_html' => ''
);
}
/**
* The default config arguments array for a section.
*
* @return void
*/
private function default_section_args()
{
return array(
'slug' => '',
'title' => '',
'subtitle' => ''
);
}
}