-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettings.php
388 lines (318 loc) · 11.7 KB
/
Settings.php
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace StatCollector;
class Settings {
/** @var Number The default priority for this plugin's actions/hooks */
public $priority = 10;
/** @var String The prefix for all option IDs for this plugin */
public $prefix = 'statc';
/** @var String The title of the plugin */
public $plugin = 'Stat Collector';
/** @var String ID and slug of the settings page */
public $page = 'collector_config';
/** @var String The title of the settings page */
public $admin_title = 'Stat Collector Settings';
/** @var String The capibility required to be able to see the settings */
public $capability = 'manage_options';
/** @var String Title of the section */
public $section_title = 'Database';
/** @var String Section description */
public $section_instructions = '<p>Enter connection information for the MySQL
database. By default, Stat Collector will use existing environment variables
for these settings but the values in these fields will override them.</p>';
/** @var String Post fix for the option group */
public $option_group = 'settings';
/** @var String Postfix for host option ID */
public $host = 'host';
/** @var String Label for host option */
public $host_label = 'Host (including port)';
/** @var String Postfix for database option ID */
public $database = 'database';
/** @var String Label for database option */
public $database_label = 'Name';
/** @var String Postfix for database user option ID */
public $user = 'user';
/** @var String Label for database user option */
public $user_label = 'Username';
/** @var String Postfix for database password option ID */
public $password = 'password';
/** @var String Label for database password option */
public $password_label = 'Password';
/** @var String Postfix for bootstrapped flag option ID */
public $bootstrapped = 'bootstrapped';
/** @var String Label for bootstrapped flag option */
public $bootstrapped_label = 'Tables Created';
/** @var String Expected setting for bootstrapped flag option */
public $bootstrapped_value = '5';
/** @var String Checkbox label for bootstrapped flag option */
public $bootstrapped_field = 'Checked if tables have been created. Tables are
automatically created on the initial database query.';
/** @var Boolean Disabled state for the bootstrapped flag option checkbox */
private $bootstrapped_disabled = true;
/** @var String Postfix for the ssl flag option ID */
public $ssl = 'ssl';
/** @var String Label for the ssl flag option */
public $ssl_label = 'Certificate Authority';
/** @var String Expected setting for the ssl flag option */
public $ssl_value = '5';
/** @var String Checkbox label for the ssl flag option */
public $ssl_field = 'Checked if a certificate authority
<code>ca.pem</code> is found in the file directory of this plugin. MySQL will
be set to use a Secure Sockets Layer (SSL).';
/** @var String Disabled state for the checkbox flag option */
private $ssl_disabled = true;
/** @var String Default filename for the ssl certificate authority */
public $ssl_ca = 'ca.pem';
/** @var String Postfix for the connection flag option ID */
public $connection = 'connection';
/** @var String Label for the connection flag option */
public $connection_label = 'Connection';
/** @var String Expected setting for the connection flag option */
public $connection_value = '5';
/** @var String Checkbox label for the connection flag option */
public $connection_field = 'Checked if a connection can be made to the database
using the credentials above. SSL is not required to make a connection.';
/** @var String Disabled state for the checkbox flag option */
private $connection_disabled = true;
/** @var String Postfix for the section id for options */
public $section_id = 'database';
/** @var String Postfix for the section id for options */
public $notify = 'notify';
/** @var String Label for the notify flag option */
public $notify_label = 'Send Notifications';
/** @var String Expected setting for notify flag option */
public $notify_value = '5';
/** @var String Checkbox label for the notify flag option */
public $notify_field = 'Check to nofity the admin if there is an
error. This will be disabled on the first instance of an error, however, all
errors are logged.';
/** @var String Disabled state for the checkbox flag option */
private $notify_disabled = false;
/** @var String Amount of errors to tolerate before sending another error notification. */
public $notify_error_count = 20;
/**
* Uses add_options_page to create page for the Stat Collector settings.
* @link https://developer.wordpress.org/reference/functions/add_options_page/
*/
public function addOptions() {
add_options_page(
$this->admin_title,
$this->plugin,
$this->capability,
$this->page,
function() {
$statc_option_group = implode('_', [
$this->prefix,
$this->option_group
]);
echo '<div class="wrap">';
echo ' <h1>' . $this->admin_title . '</h1>';
echo ' <form method="post" action="options.php">';
do_settings_sections($this->page);
settings_fields($statc_option_group);
submit_button();
echo ' </form>';
echo '</div>';
}
);
return $this;
}
/**
* Create the settings sections within the WP Admin.
*/
public function addSettings() {
/**
* Page Setting Keys
*/
$statc_section_id = $this->prefix . '_' . $this->section_id;
$statc_option_group = $this->prefix . '_' . $this->option_group;
/**
* Option Keys
*/
$statc_host = $this->prefix . '_' . $this->host;
$statc_database = $this->prefix . '_' . $this->database;
$statc_user = $this->prefix . '_' . $this->user;
$statc_password = $this->prefix . '_' . $this->password;
$statc_bootstrapped = $this->prefix . '_' . $this->bootstrapped;
$statc_ssl = $this->prefix . '_' . $this->ssl;
$statc_connection = $this->prefix . '_' . $this->connection;
$statc_notify = $this->prefix . '_' . $this->notify;
/**
* Add Settings Section
*/
add_settings_section($statc_section_id, $this->section_title, function() {
echo $this->section_instructions;
}, $this->page);
/**
* Add Settings Fields
*/
add_settings_field(
$statc_host, // Field name
$this->host_label, // Label
[$this, 'settingsFieldInput'], // HTML content
$this->page, // Page
$statc_section_id, // Section ID
array( // Args passed to callback
'id' => $statc_host,
'placeholder' => '',
'private' => false
)
);
add_settings_field(
$statc_database,
$this->database_label,
[$this, 'settingsFieldInput'],
$this->page,
$statc_section_id,
array(
'id' => $statc_database,
'placeholder' => '',
'private' => false
)
);
add_settings_field(
$statc_user,
$this->user_label,
[$this, 'settingsFieldInput'],
$this->page,
$statc_section_id,
array(
'id' => $statc_user,
'placeholder' => '',
'private' => false
)
);
add_settings_field(
$statc_password,
$this->password_label,
[$this, 'settingsFieldInput'],
$this->page,
$statc_section_id,
array(
'id' => $statc_password,
'placeholder' => '',
'private' => true
)
);
add_settings_field(
$statc_bootstrapped,
$this->bootstrapped_label,
[$this, 'settingsFieldCheckbox'],
$this->page,
$statc_section_id,
array(
'id' => $statc_bootstrapped,
'value' => $this->bootstrapped_value,
'label' => $this->bootstrapped_field,
'disabled' => $this->bootstrapped_disabled
)
);
add_settings_field(
$statc_ssl,
$this->ssl_label,
[$this, 'settingsFieldCheckbox'],
$this->page,
$statc_section_id,
array(
'id' => $statc_ssl,
'value' => $this->ssl_value,
'label' => $this->ssl_field,
'disabled' => $this->ssl_disabled
)
);
add_settings_field(
$statc_connection,
$this->connection_label,
[$this, 'settingsFieldCheckbox'],
$this->page,
$statc_section_id,
array(
'id' => $statc_connection,
'value' => $this->connection_value,
'label' => $this->connection_field,
'disabled' => $this->connection_disabled
)
);
add_settings_field(
$statc_notify,
$this->notify_label,
[$this, 'settingsFieldCheckbox'],
$this->page,
$statc_section_id,
array(
'id' => $statc_notify,
'value' => $this->notify_value,
'label' => $this->notify_field,
'disabled' => $this->notify_disabled
)
);
/**
* Register settings with WordPress so they are saved
*/
register_setting($statc_option_group, $statc_host);
register_setting($statc_option_group, $statc_database);
register_setting($statc_option_group, $statc_user);
register_setting($statc_option_group, $statc_password);
register_setting($statc_option_group, $statc_notify);
/** If these are not disabled, they will need to be registered */
// register_setting($statc_option_group, $statc_bootstrapped);
// register_setting($statc_option_group, $statc_ssl);
// register_setting($statc_option_group, $statc_connection);
}
/**
* Callback for add_settings_field. Function that fills the field with the
* desired form inputs. The function should echo its output.
* @link https://developer.wordpress.org/reference/functions/add_settings_field/
*
* @param Array $args Field arguments, [ID, Placeholder] of the text input
*/
public function settingsFieldInput($args) {
echo implode('', [
'<input ',
($args['private']) ? 'type="password" ' : 'type="text" ',
'size="40" ',
'name="' . $args['id'] . '" ',
'id="' . $args['id'] . '" ',
'value="' . get_option($args['id'], '') . '" ',
'placeholder="' . __($args['placeholder']) . '" ',
'/>'
]);
if (defined(strtoupper($args['id']))) {
$constant = constant(strtoupper($args['id']));
$html = $constant;
$html = ($args['private']) ? str_repeat('•', strlen($constant)) : $constant;
echo implode('', [
'<p class="description">',
__('Environment currently set to '),
'<code>' . $html . '</code>',
'<p>'
]);
}
}
/**
* Callback for add_settings_field. Creates the checkbox markup for a given admin option.
* @link https://developer.wordpress.org/reference/functions/add_settings_field/
*
* @param [type] $args Field arguments [ID, Value, Label, Disabled], of the text input
*/
public function settingsFieldCheckbox($args) {
echo implode('', [
'<fieldset>',
' <legend class="screen-reader-text"><span>' . __($args['label']) . '</span></legend>',
' <label for="' . $args['id'] . '">',
' <input type="checkbox" value="' . $args['value'] . '" name="' . $args['id'] . '" id="' . $args['id'] . '" ',
' ' . checked($args['value'], get_option($args['id']), false) . ' ',
' ' . disabled($args['disabled'], true, false) . ' >',
' ' . __($args['label']) . '',
' </label>',
'</fieldset>',
]);
if (defined(strtoupper($args['id']))) {
echo implode([
'<p class="description">',
__('Environment currently set to '),
'<code>' . constant(strtoupper($args['id'])) . '</code>',
'<p>'
], '');
}
}
}