PluginProbe
Advanced Custom Fields: Extended / 0.9.2.3
Advanced Custom Fields: Extended v0.9.2.3
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 All 92 releases
acf-extended / includes / modules / form / module-form-format.php
module-form-format.php
463 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 if(!class_exists('acfe_module_form_format')):
8
9 class acfe_module_form_format{
10
11 /**
12 * __construct
13 */
14 function __construct(){
15
16 add_filter('acfe/form/format_value/type=post_object', array($this, 'format_value_post_object'), 5, 4);
17 add_filter('acfe/form/format_value/type=relationship', array($this, 'format_value_post_object'), 5, 4);
18 add_filter('acfe/form/format_value/type=user', array($this, 'format_value_user'), 5, 4);
19 add_filter('acfe/form/format_value/type=taxonomy', array($this, 'format_value_taxonomy'), 5, 4);
20 add_filter('acfe/form/format_value/type=acfe_taxonomy_terms', array($this, 'format_value_taxonomy'), 5, 4);
21 add_filter('acfe/form/format_value/type=image', array($this, 'format_value_file'), 5, 4);
22 add_filter('acfe/form/format_value/type=file', array($this, 'format_value_file'), 5, 4);
23 add_filter('acfe/form/format_value/type=gallery', array($this, 'format_value_file'), 5, 4);
24 add_filter('acfe/form/format_value/type=select', array($this, 'format_value_select'), 5, 4);
25 add_filter('acfe/form/format_value/type=checkbox', array($this, 'format_value_select'), 5, 4);
26 add_filter('acfe/form/format_value/type=radio', array($this, 'format_value_select'), 5, 4);
27 add_filter('acfe/form/format_value/type=google_map', array($this, 'format_value_google_map'), 5, 4);
28 add_filter('acfe/form/format_value/type=repeater', array($this, 'format_value_repeater'), 5, 4);
29 add_filter('acfe/form/format_value/type=flexible_content', array($this, 'format_value_repeater'), 5, 4);
30 add_filter('acfe/form/format_value/type=group', array($this, 'format_value_group'), 5, 4);
31 add_filter('acfe/form/format_value/type=acfe_date_range_picker', array($this, 'format_value_date_range_picker'), 5, 4);
32 add_filter('acfe/form/format_value/type=acfe_address', array($this, 'format_value_address'), 5, 4);
33
34 }
35
36
37 /**
38 * format_value_post_object
39 *
40 * @param $formatted
41 * @param $unformatted
42 * @param $post_id
43 * @param $field
44 *
45 * @return string
46 */
47 function format_value_post_object($formatted, $unformatted, $post_id, $field){
48
49 // vars
50 $value = acf_get_array($unformatted);
51 $array = array();
52
53 // loop values
54 foreach($value as $p_id){
55 $array[] = get_the_title($p_id);
56 }
57
58 // merge
59 return implode(', ', $array);
60
61 }
62
63
64 /**
65 * format_value_user
66 *
67 * @param $formatted
68 * @param $unformatted
69 * @param $post_id
70 * @param $field
71 *
72 * @return string
73 */
74 function format_value_user($formatted, $unformatted, $post_id, $field){
75
76 // vars
77 $value = acf_get_array($unformatted);
78 $array = array();
79
80 // loop values
81 foreach($value as $user_id){
82
83 // get user data
84 $user_data = get_userdata($user_id);
85
86 // validate
87 if($user_data){
88 $array[] = $user_data->user_nicename;
89 }
90
91 }
92
93 // merge
94 return implode(', ', $array);
95
96 }
97
98
99 /**
100 * format_value_taxonomy
101 *
102 * @param $formatted
103 * @param $unformatted
104 * @param $post_id
105 * @param $field
106 *
107 * @return string
108 */
109 function format_value_taxonomy($formatted, $unformatted, $post_id, $field){
110
111 // vars
112 $value = acf_get_array($unformatted);
113 $array = array();
114
115 // loop values
116 foreach($value as $term_id){
117
118 // get term
119 $term = get_term($term_id);
120
121 // validate
122 if($term && !is_wp_error($term)){
123 $array[] = $term->name;
124 }
125
126 }
127
128 // merge
129 return implode(', ', $array);
130
131 }
132
133
134 /**
135 * format_value_file
136 *
137 * @param $formatted
138 * @param $unformatted
139 * @param $post_id
140 * @param $field
141 *
142 * @return string
143 */
144 function format_value_file($formatted, $unformatted, $post_id, $field){
145
146 $value = acf_get_array($unformatted);
147 $array = array();
148
149 foreach($value as $v){
150 $array[] = get_the_title($v);
151 }
152
153 return implode(', ', $array);
154
155 }
156
157
158 /**
159 * format_value_select
160 *
161 * @param $formatted
162 * @param $unformatted
163 * @param $post_id
164 * @param $field
165 *
166 * @return string
167 */
168 function format_value_select($formatted, $unformatted, $post_id, $field){
169
170 // vars
171 $value = acf_get_array($unformatted);
172 $array = array();
173
174 // loop values
175 foreach($value as $v){
176 $array[] = acf_maybe_get($field['choices'], $v, $v);
177 }
178
179 // merge
180 return implode(', ', $array);
181
182 }
183
184
185 /**
186 * format_value_google_map
187 *
188 * @param $formatted
189 * @param $unformatted
190 * @param $post_id
191 * @param $field
192 *
193 * @return mixed|null
194 */
195 function format_value_google_map($formatted, $unformatted, $post_id, $field){
196
197 if(is_string($formatted)){
198 $formatted = json_decode(wp_unslash($formatted), true);
199 }
200
201 $formatted = acf_get_array($formatted);
202
203 return acf_maybe_get($formatted, 'address');
204
205 }
206
207
208 /**
209 * format_value_repeater
210 *
211 * @param $formatted
212 * @param $unformatted
213 * @param $post_id
214 * @param $field
215 *
216 * @return string
217 */
218 function format_value_repeater($formatted, $unformatted, $post_id, $field){
219
220 // vars
221 $value = acf_get_array($unformatted);
222 $return = '';
223
224 // loop values
225 foreach($value as $i => $sub_fields){
226
227 $array = array();
228 $return .= "<br/>\n- ";
229
230 // loop subfields keys
231 foreach($sub_fields as $key => $val){
232
233 // get subfield
234 $sub_field = acf_get_field($key);
235
236 // validate
237 if($sub_field){
238
239 // label
240 $label = !empty($sub_field['label']) ? $sub_field['label'] : $sub_field['name'];
241
242 // value
243 $sub_field['name'] = $field['name'] . '_' . $i . '_' . $sub_field['name'];
244 $val = $this->format_value($val, $sub_field);
245
246 // append
247 $array[] = "{$label}: {$val}";
248
249 }
250
251 }
252
253 // merge
254 $return .= implode(', ', $array);
255
256 }
257
258 // return
259 return $return;
260
261 }
262
263
264 /**
265 * format_value_group
266 *
267 * @param $formatted
268 * @param $unformatted
269 * @param $post_id
270 * @param $field
271 *
272 * @return string
273 */
274 function format_value_group($formatted, $unformatted, $post_id, $field){
275
276 // vars
277 $value = acf_get_array($unformatted);
278 $array = array();
279 $return = '';
280
281 // loop subfields keys
282 foreach($value as $key => $val){
283
284 // get sub field
285 $sub_field = acf_get_field($key);
286
287 // validate
288 if($sub_field){
289
290 // label
291 $label = !empty($sub_field['label']) ? $sub_field['label'] : $sub_field['name'];
292
293 // format value
294 $sub_field['name'] = $field['name'] . '_' . $sub_field['name'];
295 $val = $this->format_value($val, $sub_field);
296
297 // append
298 $array[] = "{$label}: {$val}";
299
300 }
301
302 }
303
304 // merge
305 $return .= implode(', ', $array);
306
307 // return
308 return $return;
309
310 }
311
312
313 /**
314 * format_value_date_range_picker
315 *
316 * @param $formatted
317 * @param $unformatted
318 * @param $post_id
319 * @param $field
320 *
321 * @return mixed|string
322 */
323 function format_value_date_range_picker($formatted, $unformatted, $post_id, $field){
324
325 if(!empty($formatted) && is_array($formatted)){
326
327 $start = acf_maybe_get($formatted, 'start');
328 $end = acf_maybe_get($formatted, 'end');
329
330 return "{$start} - {$end}";
331
332 }
333
334 return $formatted;
335
336 }
337
338
339 /**
340 * format_value_address
341 *
342 * @param $formatted
343 * @param $unformatted
344 * @param $post_id
345 * @param $field
346 *
347 * @return mixed|string
348 */
349 function format_value_address($formatted, $unformatted, $post_id, $field){
350
351 if(!empty($formatted) && is_array($formatted)){
352 return acf_maybe_get($formatted, 'address');
353 }
354
355 return $formatted;
356
357 }
358
359
360 /**
361 * format_value_array
362 *
363 * @param $value
364 *
365 * @return mixed|string
366 */
367 function format_value_array($value){
368
369 // bail early
370 if(!is_array($value)){
371 return $value;
372 }
373
374 $return = array();
375
376 foreach($value as $i => $v){
377
378 $key = !is_numeric($i) ? "$i: " : '';
379
380 if(is_object($v)){
381 $v = (array) $v;
382 }
383
384 $return[] = $key . $this->format_value_array($v);
385
386 }
387
388 return implode(', ', $return);
389
390 }
391
392
393 /**
394 * format_value
395 *
396 * @param $unformatted
397 * @param $field
398 *
399 * @return mixed|string|null
400 */
401 function format_value($unformatted, $field){
402
403 // vars
404 $post_id = acf_get_valid_post_id();
405 $field_name = $field['name'];
406
407 // check & delete store
408 // this fix an issue where different group subfields with same name will output same value
409 // this is because group subfields have singular name. ie: 'textarea' instead of 'group_textarea'
410 $store = acf_get_store('values');
411 if($store->has("$post_id:$field_name:formatted")){
412 $store->remove("$post_id:$field_name:formatted");
413 }
414
415 // format value
416 $formatted = acf_format_value($unformatted, $post_id, $field);
417
418 // pass thru filters
419 $formatted = apply_filters("acfe/form/format_value", $formatted, $unformatted, $post_id, $field);
420 $formatted = apply_filters("acfe/form/format_value/type={$field['type']}", $formatted, $unformatted, $post_id, $field);
421 $formatted = apply_filters("acfe/form/format_value/key={$field['key']}", $formatted, $unformatted, $post_id, $field);
422 $formatted = apply_filters("acfe/form/format_value/name={$field['name']}", $formatted, $unformatted, $post_id, $field);
423
424 if(is_object($formatted)){
425 $formatted = (array) $formatted;
426 }
427
428 // format array value
429 if(is_array($formatted)){
430 $formatted = $this->format_value_array($formatted);
431 }
432
433 return $formatted;
434
435 }
436
437 }
438
439 acf_new_instance('acfe_module_form_format');
440
441 endif;
442
443
444 /**
445 * acfe_form_format_value
446 *
447 * @param $value
448 * @param $field
449 * @param $deprecated
450 *
451 * @return mixed
452 */
453 function acfe_form_format_value($value, $field, $deprecated = null){
454
455 // deprecated
456 if($deprecated !== null){
457 _deprecated_function('ACF Extended: acfe_form_format_value($value, $field, $deprecated) 3rd argument', '0.8.8', 'pass field array as 2nd argument');
458 $field = $deprecated; // second argument was $post_id
459 }
460
461 return acf_get_instance('acfe_module_form_format')->format_value($value, $field);
462
463 }