|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Helper\Helper; |
|
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Base class for fields with predefined options. |
|
10
|
|
|
* Mainly used to reduce the bloat on the base Field class. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Predefined_Options_Field extends Field { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Stores the raw, unprocessed field options |
|
16
|
|
|
* |
|
17
|
|
|
* @var array(array|callable) |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $option_collections = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Check if an array is indexed |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $array |
|
25
|
|
|
* @return boolean |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function is_indexed_array( $array ) { |
|
28
|
|
|
return array_keys( $array ) === range( 0, count( $array ) - 1 ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set the options of this field. |
|
33
|
|
|
* Accepts either array of data or a callback that returns the data. |
|
34
|
|
|
* |
|
35
|
|
|
* @param array|callable $options |
|
36
|
|
|
* @return self $this |
|
37
|
9 |
|
*/ |
|
38
|
9 |
View Code Duplication |
public function set_options( $options ) { |
|
39
|
4 |
|
if ( ! is_callable( $options ) && ! is_array( $options ) ) { |
|
40
|
|
|
Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>set_options()</code> method.' ); |
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
5 |
|
|
|
44
|
5 |
|
$this->option_collections = array(); |
|
45
|
|
|
return $this->add_options( $options ); |
|
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|callable $options |
|
53
|
|
|
* @return self $this |
|
54
|
13 |
|
*/ |
|
55
|
13 |
View Code Duplication |
public function add_options( $options ) { |
|
56
|
4 |
|
if ( ! is_callable( $options ) && ! is_array( $options ) ) { |
|
57
|
|
|
Incorrect_Syntax_Exception::raise( 'Only arrays and callbacks are allowed in the <code>add_options()</code> method.' ); |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
9 |
|
|
|
61
|
9 |
|
$this->option_collections[] = $options; |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get a populated array of options executing any callbacks in the process |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function load_options() { |
|
71
|
|
|
$options = array(); |
|
72
|
|
|
foreach ( $this->option_collections as $collection ) { |
|
73
|
|
|
$collection_items = array(); |
|
74
|
|
|
if ( is_callable( $collection ) ) { |
|
75
|
|
|
$collection_items = call_user_func( $collection ); |
|
76
|
|
|
if ( ! is_array( $collection_items ) ) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
$collection_items = $collection; |
|
81
|
|
|
} |
|
82
|
|
|
if ( $this->is_indexed_array( $options ) && $this->is_indexed_array( $collection_items ) ) { |
|
83
|
|
|
$options = array_merge( $options, $collection_items ); |
|
84
|
|
|
} else { |
|
85
|
|
|
$options = array_replace( $options, $collection_items ); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $options; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Retrieve the current options. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
13 |
|
*/ |
|
97
|
13 |
|
public function get_options() { |
|
98
|
|
|
return $this->load_options(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Retrieve the current options' values only. |
|
103
|
|
|
* |
|
104
|
|
|
* @return array $options |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function get_options_values() { |
|
107
|
|
|
$options = $this->parse_options( $this->get_options() ); |
|
108
|
|
|
return wp_list_pluck( $options, 'value' ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Changes the options array structure. This is needed to keep the array items order when it is JSON encoded. |
|
113
|
|
|
* Will also work with a callable that returns an array. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array|callable $options |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function parse_options( $options, $stringify_value = false ) { |
|
119
|
|
|
$parsed = array(); |
|
120
|
|
|
|
|
121
|
|
|
if ( is_callable( $options ) ) { |
|
122
|
|
|
$options = call_user_func( $options ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
foreach ( $options as $key => $value ) { |
|
126
|
|
|
$parsed[] = array( |
|
127
|
|
|
'value' => $stringify_value ? strval( $key ) : $key, |
|
128
|
|
|
'label' => strval( $value ), |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $parsed; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get an array of all values that are both in the passed array and the predefined list of options |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $values |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function get_values_from_options( $values ) { |
|
142
|
|
|
$options_values = $this->get_options_values(); |
|
143
|
|
|
$values = Helper::get_valid_options( $values, $options_values ); |
|
144
|
|
|
return $values; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|