1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base class for fields with predefined options. |
7
|
|
|
* Mainly used to reduce the bloat on the base Field class |
8
|
|
|
**/ |
9
|
|
|
abstract class Predefined_Options_Field extends Field { |
10
|
|
|
/** |
11
|
|
|
* Stores the field options (if any) |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
**/ |
15
|
|
|
protected $options = array(); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Set the field options |
19
|
|
|
* Callbacks are supported |
20
|
|
|
* |
21
|
|
|
* @param array|callback $options |
22
|
|
|
*/ |
23
|
|
|
protected function _set_options( $options ) { |
24
|
|
|
$this->options = (array) $options; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Add options to the field |
29
|
|
|
* Callbacks are supported |
30
|
|
|
* |
31
|
|
|
* @param array|callback $options |
32
|
|
|
*/ |
33
|
|
|
protected function _add_options( $options ) { |
34
|
|
|
$this->options[] = $options; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the options of this field. |
39
|
|
|
* Accepts either array of data or a callback that returns the data. |
40
|
|
|
* |
41
|
|
|
* @param array|callable $options |
42
|
|
|
*/ |
43
|
|
|
public function set_options( $options ) { |
44
|
|
|
$this->_set_options( $options ); |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add new options to this field. |
50
|
|
|
* Accepts either array of data or a callback that returns the data. |
51
|
|
|
* |
52
|
|
|
* @param array|callbacle $options |
53
|
|
|
*/ |
54
|
|
|
public function add_options( $options ) { |
55
|
|
|
$this->_add_options( $options ); |
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Check if there are callbacks and populate the options |
61
|
|
|
*/ |
62
|
|
|
protected function load_options() { |
63
|
|
|
if ( empty( $this->options ) ) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$options = array(); |
68
|
|
|
foreach ( $this->options as $key => $value ) { |
69
|
|
|
if ( is_callable( $value ) ) { |
70
|
|
|
$options = $options + (array) call_user_func( $value ); |
71
|
|
|
} else if ( is_array( $value ) ) { |
72
|
|
|
$options = $options + $value; |
73
|
|
|
} else { |
74
|
|
|
$options[ $key ] = $value; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->options = $options; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
83
|
|
|
* |
84
|
|
|
* @param array $options |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function parse_options( $options ) { |
88
|
|
|
$parsed = array(); |
89
|
|
|
|
90
|
|
|
foreach ( $options as $key => $value ) { |
91
|
|
|
$parsed[] = array( |
92
|
|
|
'name' => $value, |
93
|
|
|
'value' => $key, |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $parsed; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} // END Field |
101
|
|
|
|