PluginProbe ʕ •ᴥ•ʔ
Advanced Custom Fields: Extended / 0.9.2.7
Advanced Custom Fields: Extended v0.9.2.7
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 0.8.8.5 0.8.8.6 0.8.8.7 0.8.8.8 0.8.8.9 0.8.9 0.8.9.1 0.8.9.2 0.8.9.3 0.8.9.4 0.8.9.5 0.9 0.9.0.1 0.9.0.2 0.9.0.3 0.9.0.4 0.9.0.5 0.9.0.6 0.9.0.7 0.9.0.8 0.9.0.9 0.9.1 0.9.1.1 0.9.2 0.9.2.1 0.9.2.2 0.9.2.3 0.9.2.4 trunk 0.5 0.5.1 0.5.2 0.5.2.1 0.5.2.3 0.5.5 0.5.5.1 0.5.8 0.5.8.1 0.6 0.6.0.1 0.6.0.2 0.6.1 0.6.3 0.6.5 0.6.7 0.6.7.2 0.7 0.7.0.3 0.7.5 0.7.5.5 0.7.8 0.7.9 0.7.9.3 0.7.9.4 0.7.9.9.8 0.7.9.9.9 0.8 0.8.1 0.8.2 0.8.3 0.8.3.1 0.8.4 0.8.4.1 0.8.4.5 0.8.4.6 0.8.5 0.8.5.5
acf-extended / includes / fields-settings / choices-label.php
acf-extended / includes / fields-settings Last commit date
bidirectional.php 3 months ago choices-label.php 3 months ago data.php 3 months ago instructions.php 3 months ago permissions.php 3 months ago settings.php 3 months ago validation.php 3 months ago
choices-label.php
349 lines
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 if(!class_exists('acfe_field_choices_label')):
8
9 class acfe_field_choices_label{
10
11 /**
12 * construct
13 */
14 function __construct(){
15
16 // instructions
17 add_filter('acf/prepare_field/name=choices', array($this, 'prepare_instructions'), 20);
18
19 // filters
20 add_filter('acf/prepare_field/type=radio', array($this, 'prepare_choices'), 20);
21 add_filter('acf/prepare_field/type=checkbox', array($this, 'prepare_choices'), 20);
22 add_filter('acf/prepare_field/type=acfe_taxonomy_terms', array($this, 'prepare_choices'), 20);
23
24 add_filter('acf/prepare_field/type=radio', array($this, 'prepare_radio'), 20);
25 add_filter('acf/prepare_field/type=acfe_taxonomy_terms', array($this, 'prepare_radio'), 20);
26
27 }
28
29
30 /**
31 * prepare_instructions
32 *
33 * ACF Admin choices insctructions
34 */
35 function prepare_instructions($field){
36
37 // allowed types
38 $field_types = array('radio', 'checkbox', 'select');
39
40 // check field type
41 if(in_array(acfe_get($field['wrapper'], 'data-setting'), $field_types, true)){
42
43 $text = "<br/><br/>" . __('You may use "## Title" to create a group of options.', 'acfe');
44 $key = acfe_get($field, 'hint') ? 'hint' : 'instructions'; // handle hint / instructions
45
46 // add instructions
47 $field[ $key ] .= $text;
48
49 }
50
51 return $field;
52
53 }
54
55
56 /**
57 * prepare_choices
58 *
59 * @param $field
60 *
61 * @return mixed
62 */
63 function prepare_choices($field){
64
65 // bail early if no choices
66 if(empty($field['choices'])){
67 return $field;
68 }
69
70 // transform choices array
71 $field = $this->decode_choices_label($field);
72
73 // get labels
74 $labels = $this->get_labels($field['choices']);
75
76 // assign data attribute
77 if(!empty($labels)){
78 $field['wrapper']['data-acfe-labels'] = json_encode($labels);
79 }
80
81 return $field;
82
83 }
84
85
86 /**
87 * prepare_radio
88 *
89 * Radio fields need special treatment as they don't recognize nested arrays choices
90 * This function flattens the choices array. Then labels are added in javascript
91 *
92 * @param $field
93 *
94 * @return mixed
95 */
96 function prepare_radio($field){
97
98 if($field['type'] !== 'radio' && $field['field_type'] !== 'radio'){
99 return $field;
100 }
101
102 if(empty($field['choices'])){
103 return $field;
104 }
105
106 $choices = array();
107
108 // loop choices to flatten
109 foreach($field['choices'] as $key => $value){
110
111 if(is_array($value)){
112 $choices = $choices + $value;
113 }else{
114 $choices = $choices + array($key => $value);
115 }
116
117 }
118
119 // assign
120 $field['choices'] = $choices;
121
122 return $field;
123
124 }
125
126
127 /**
128 * decode_choices_label
129 *
130 * Transform choices with "##" into grouped choices
131 * Example:
132 *
133 * 'choices' => array(
134 * '## Fruits',
135 * 'apple' => 'Apple',
136 * 'banana' => 'Banana',
137 * )
138 *
139 * Becomes
140 *
141 * 'choices' => array(
142 * 'Fruits' => array(
143 * 'apple' => 'Apple',
144 * 'banana' => 'Banana',
145 * )
146 * )
147 *
148 * @param $field
149 *
150 * @return mixed
151 */
152 function decode_choices_label($field){
153
154 // bail early if no choices
155 if(empty($field['choices']) || !is_array($field['choices'])){
156 return $field;
157 }
158
159 // vars
160 $last_label = false;
161 $new_choices = array();
162
163 // loop choices
164 foreach($field['choices'] as $k => $choice){
165
166 // only strings
167 if(is_string($choice)){
168
169 // sanitize choice
170 $choice = trim($choice);
171
172 // ## Group Label
173 if(acfe_starts_with($choice, '##')){
174
175 // get label
176 $label = substr($choice, 2);
177 $label = trim($label);
178
179 // prepare new choices
180 $last_label = $label;
181 $new_choices[ $label ] = array();
182
183 continue; // new row
184 }
185
186 // assign sub choice to last label
187 if(!empty($last_label)){
188 $new_choices[ $last_label ][ $k ] = $choice;
189 }
190
191 }
192
193 }
194
195 // assign new choices
196 if(!empty($new_choices)){
197 $field['choices'] = $new_choices;
198 }
199
200 return $field;
201
202 }
203
204
205 /**
206 * strip_choices_label
207 *
208 * Removes choices starting with "##"
209 * This is mainly used on select2 ajax requests which doesn't handle grouped choices
210 *
211 * @param $field
212 *
213 * @return mixed
214 */
215 function strip_choices_label($field){
216
217 // bail early if no choices
218 if(empty($field['choices']) || !is_array($field['choices'])){
219 return $field;
220 }
221
222 // loop choices
223 foreach(array_keys($field['choices']) as $k){
224
225 // get choice
226 $choice = $field['choices'][ $k ];
227
228 // only string
229 if(is_string($choice) && acfe_starts_with($choice, '##')){
230 unset($field['choices'][ $k ]);
231 }
232
233 }
234
235 // return
236 return $field;
237
238 }
239
240
241 /**
242 * get_labels
243 *
244 * @param $choices
245 * @param $depth
246 * @param $labels
247 *
248 * @return array|mixed
249 */
250 function get_labels($choices = array(), $depth = 1, $labels = array()){
251
252 // bail early if no choices
253 if(empty($choices)){
254 return $labels;
255 }
256
257 // loop choices
258 foreach($choices as $key => $value){
259
260 // label must be an array
261 if(is_array($value)){
262
263 // get the first array key
264 reset($value);
265 $first_key = key($value);
266
267 if(!is_numeric($key)){
268 $labels = array_merge($labels, array($key => $first_key));
269 }
270
271 $labels = $this->get_labels($value, $depth+1, $labels);
272
273 }
274
275 }
276
277 // return
278 return $labels;
279
280 }
281
282 }
283
284 acf_new_instance('acfe_field_choices_label');
285
286 endif;
287
288
289 /**
290 * acfe_prepare_choices_label
291 *
292 * @param $field
293 *
294 * @return mixed
295 */
296 function acfe_prepare_choices_label($field){
297
298 // get instance
299 $instance = acf_get_instance('acfe_field_choices_label');
300
301 // perform actions
302 $field = $instance->prepare_choices($field);
303 $field = $instance->prepare_radio($field);
304
305 // return
306 return $field;
307
308 }
309
310
311 /**
312 * acfe_decode_choices_label
313 *
314 * @param $field
315 *
316 * @return mixed
317 */
318 function acfe_decode_choices_label($field){
319 $instance = acf_get_instance('acfe_field_choices_label');
320 return $instance->decode_choices_label($field);
321 }
322
323
324 /**
325 * acfe_strip_choices_label
326 *
327 * @param $field
328 *
329 * @return mixed
330 */
331 function acfe_strip_choices_label($field){
332 $instance = acf_get_instance('acfe_field_choices_label');
333 return $instance->strip_choices_label($field);
334 }
335
336
337 /**
338 * acfe_prepare_checkbox_labels
339 *
340 * @param $field
341 *
342 * @deprecated since 0.9.2.2 use acfe_prepare_choices_label instead
343 *
344 * @return mixed
345 */
346 function acfe_prepare_checkbox_labels($field){
347 acfe_deprecated_function(__FUNCTION__, '0.9.2.2', 'acfe_prepare_choices_label()');
348 return acfe_prepare_choices_label($field);
349 }