Column.php
7.19 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
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
<?php
namespace AC\Settings;
use AC;
use AC\Form\Element;
use AC\View;
abstract class Column {
/**
* A (short) reference to this setting
* @var string
*/
protected $name;
/**
* The options this field manages (optionally with default values)
* @var array
*/
protected $options = [];
/**
* @var AC\Column
*/
protected $column;
/**
* Options that are set by the user and should not be overwritten with defaults
* @var array
*/
private $user_set = [];
/**
* @param AC\Column $column
*/
public function __construct( AC\Column $column ) {
$this->column = $column;
$this->set_options();
$this->set_name();
}
/**
* @return array
* @see AC\Settings_Column::$options
*/
protected abstract function define_options();
/**
* Create a string representation of this setting
* @return View|false
*/
public abstract function create_view();
/**
* Get settings that depend on this setting
* @return Column[]
*/
public function get_dependent_settings() {
return [];
}
private function set_options() {
foreach ( $this->define_options() as $option => $value ) {
if ( is_numeric( $option ) ) {
$option = $value;
$value = null;
}
$this->set_option( $option, $value );
$this->set_default( $value, $option );
}
}
/**
* Set an option and set value afterwards
*
* @param string $option
* @param mixed $value
*/
private function set_option( $option, $value = null ) {
$this->options[ $option ] = $value;
}
public function has_option( $option ) {
return array_key_exists( $option, $this->options );
}
/**
* Get a managed option
* @return false|string
*/
protected function get_default_option() {
reset( $this->options );
return key( $this->options );
}
/**
* Return the value of all options
* @return array
*/
public function get_values() {
$values = [];
foreach ( array_keys( $this->options ) as $option ) {
$values[ $option ] = $this->get_value( $option );
}
return $values;
}
/**
* Get value of this setting, optionally specified with a key
* Will return the value of the default option
*
* @param string|null $option
*
* @return string|array|int|bool
*/
public function get_value( $option = null ) {
if ( null === $option ) {
$option = $this->get_default_option();
}
if ( ! $this->has_option( $option ) ) {
return null;
}
$method = 'get_' . $option;
if ( ! method_exists( $this, $method ) ) {
return null;
}
return $this->$method();
}
/**
* Set the values of this setting
*
* @param array $values
*/
public function set_values( array $values ) {
foreach ( $values as $option => $value ) {
$this->set_value( $value, $option );
}
}
/**
* Invoke the setter of the setting
*
* @param string|array|int|bool $value
* @param string $option
*
* @return bool
*/
private function invoke_option_setter( $option, $value ) {
$method = 'set_' . $option;
if ( ! method_exists( $this, $method ) ) {
return false;
}
return $this->$method( $value );
}
/**
* Set value of an option
*
* @param string|array|int|bool $value
* @param string|null $option
*
* @return bool
*/
public function set_value( $value, $option = null ) {
if ( null === $option ) {
$option = $this->get_default_option();
}
if ( ! $this->has_option( $option ) ) {
return false;
}
$result = $this->invoke_option_setter( $option, $value );
if ( $result ) {
$this->user_set[] = $option;
}
return $result;
}
/**
* Set a default value unless option is loaded from settings
*
* @param string|array|int|bool $value
* @param string|null $option
*
* @return bool
*/
public function set_default( $value, $option = null ) {
if ( null === $option ) {
$option = $this->get_default_option();
}
if ( ! $this->has_option( $option ) ) {
return false;
}
$this->set_option( $option, $value );
// check if value is user set
if ( ! in_array( $option, $this->user_set ) ) {
$this->invoke_option_setter( $option, $value );
}
return true;
}
/**
* Get the default value of an option if set
*
* @param string|null $option
*
* @return mixed
*/
public function get_default( $option = null ) {
if ( null === $option ) {
$option = $this->get_default_option();
}
return $this->has_option( $option ) ? $this->options[ $option ] : null;
}
/**
* @return string
*/
public function get_name() {
return $this->name;
}
/**
* Default to self::get_default_option()
*/
protected function set_name() {
$this->name = $this->get_default_option();
}
/**
* Add an element to this setting
*
* @param string $type
* @param string|null $name
*
* @return Element\Select|Element\Input|Element\Radio
*/
protected function create_element( $type, $name = null ) {
if ( null === $name ) {
$name = $this->get_default_option();
}
switch ( $type ) {
case 'checkbox' :
$element = new Element\Checkbox( $name );
break;
case 'radio' :
$element = new Element\Radio( $name );
break;
case 'select' :
$element = new AC\Settings\Form\Element\Select( $name );
break;
case 'multi-select' :
$element = new Element\MultiSelect( $name );
break;
default:
$element = new Element\Input( $name );
$element->set_type( $type );
}
$element->set_name( $name );
$element->set_id( sprintf( 'ac-%s-%s', $this->column->get_name(), $name ) );
$element->add_class( 'ac-setting-input_' . $name );
// try to set current value
$value = $this->get_value( $name );
if ( null !== $value ) {
$element->set_value( $value );
}
return $element;
}
/**
* Render the output of self::create_header()
* @return false|string
*/
public function render_header() {
if ( ! ( $this instanceof Header ) ) {
return false;
}
/* @var Header $this */
$view = $this->create_header_view();
if ( ! ( $view instanceof View ) ) {
return false;
}
if ( null == $view->get_template() ) {
$view->set_template( 'settings/header' );
}
if ( null == $view->setting ) {
$view->set( 'setting', $this->get_name() );
}
return $view->render();
}
/**
* Render the output of self::create_view()
* @return false|string
*/
public function render() {
$view = $this->create_view();
if ( ! ( $view instanceof View ) ) {
return false;
}
$template = 'settings/section';
// set default template
if ( null === $view->get_template() ) {
$view->set_template( $template );
}
// set default name
if ( null === $view->get( 'name' ) ) {
$view->set( 'name', $this->name );
}
// set default for
if ( null === $view->get( 'for' ) ) {
$setting = $view->get( 'setting' );
if ( $setting instanceof AC\Form\Element ) {
$view->set( 'for', $setting->get_id() );
}
}
// set default template for nested sections
foreach ( (array) $view->sections as $section ) {
if ( $section instanceof View && null === $section->get_template() ) {
$section->set_template( $template );
}
}
return $view->render();
}
public function __toString() {
$rendered = $this->render();
if ( ! is_string( $rendered ) ) {
return '';
}
return $rendered;
}
public function get_column() {
return $this->column;
}
}