PluginProbe
Advanced Custom Fields (ACF®) / 3.3.1
Advanced Custom Fields (ACF®) v3.3.1
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / core / api.php

api.php in Advanced Custom Fields (ACF®) 3.3.1, at core/api.php

732 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // set some globals
4 reset_the_repeater_field();
5
6
7 /*--------------------------------------------------------------------------------------
8 *
9 * get_fields
10 *
11 * @author Elliot Condon
12 * @since 1.0.3
13 *
14 *-------------------------------------------------------------------------------------*/
15
16 function get_fields($post_id = false)
17 {
18 // vars
19 global $post;
20
21 if(!$post_id)
22 {
23 $post_id = $post->ID;
24 }
25
26
27 // default
28 $value = array();
29
30 $keys = get_post_custom_keys($post_id);
31
32 if($keys)
33 {
34 foreach($keys as $key)
35 {
36 if(substr($key, 0, 1) != "_")
37 {
38 $value[$key] = get_field($key, $post_id);
39 }
40 }
41 }
42
43 // no value
44 if(empty($value))
45 {
46 return false;
47 }
48
49 return $value;
50
51 }
52
53
54 /*--------------------------------------------------------------------------------------
55 *
56 * get_field
57 *
58 * @author Elliot Condon
59 * @since 1.0.3
60 *
61 *-------------------------------------------------------------------------------------*/
62
63 function get_field($field_name, $post_id = false)
64 {
65 global $post, $acf;
66
67 if(!$post_id)
68 {
69 $post_id = $post->ID;
70 }
71
72
73 // allow for option == options
74 if( $post_id == "option" )
75 {
76 $post_id = "options";
77 }
78
79
80 // return cache
81 $cache = wp_cache_get('acf_get_field_' . $post_id . '_' . $field_name);
82 if($cache)
83 {
84 return $cache;
85 }
86
87 // default
88 $value = "";
89
90
91 // get value
92 $field_key = "";
93 if( is_numeric($post_id) )
94 {
95 $field_key = get_post_meta($post_id, '_' . $field_name, true);
96 }
97 else
98 {
99 $field_key = get_option('_' . $post_id . '_' . $field_name);
100 }
101
102
103 if($field_key != "")
104 {
105 // we can load the field properly!
106 $field = $acf->get_acf_field($field_key);
107 $value = $acf->get_value_for_api($post_id, $field);
108 }
109 else
110 {
111 // just load the text version
112 if( is_numeric($post_id) )
113 {
114 $value = get_post_meta($post_id, $field_name, true);
115 }
116 else
117 {
118 $value = get_option($post_id . '_' . $field_name);
119 }
120
121 }
122
123 // no value?
124 if($value == "") $value = false;
125
126 // update cache
127 wp_cache_set('acf_get_field_' . $post_id . '_' . $field_name, $value);
128
129 return $value;
130
131 }
132
133
134 /*--------------------------------------------------------------------------------------
135 *
136 * the_field
137 *
138 * @author Elliot Condon
139 * @since 1.0.3
140 *
141 *-------------------------------------------------------------------------------------*/
142
143 function the_field($field_name, $post_id = false)
144 {
145 $value = get_field($field_name, $post_id);
146
147 if(is_array($value))
148 {
149 $value = @implode(', ',$value);
150 }
151
152 echo $value;
153 }
154
155
156 /*--------------------------------------------------------------------------------------
157 *
158 * the_repeater_field
159 *
160 * @author Elliot Condon
161 * @since 1.0.3
162 *
163 *-------------------------------------------------------------------------------------*/
164
165 function the_repeater_field($field_name, $post_id = false)
166 {
167
168 // if no field, create field + reset count
169 if(!$GLOBALS['acf_field'])
170 {
171 reset_the_repeater_field();
172 $GLOBALS['acf_field'] = get_field($field_name, $post_id);
173 }
174
175 // increase order_no
176 $GLOBALS['acf_count']++;
177
178 // vars
179 $field = $GLOBALS['acf_field'];
180 $i = $GLOBALS['acf_count'];
181
182 if(isset($field[$i]))
183 {
184 return true;
185 }
186
187 // no row, reset the global values
188 reset_the_repeater_field();
189 return false;
190
191 }
192
193 function the_flexible_field($field_name, $post_id = false)
194 {
195 return the_repeater_field($field_name, $post_id);
196 }
197
198
199 /*--------------------------------------------------------------------------------------
200 *
201 * reset_the_repeater_field
202 *
203 * @author Elliot Condon
204 * @since 1.0.3
205 *
206 *-------------------------------------------------------------------------------------*/
207
208 function reset_the_repeater_field()
209 {
210 $GLOBALS['acf_field'] = false;
211 $GLOBALS['acf_count'] = -1;
212 }
213
214
215 /*--------------------------------------------------------------------------------------
216 *
217 * get_sub_field
218 *
219 * @author Elliot Condon
220 * @since 1.0.3
221 *
222 *-------------------------------------------------------------------------------------*/
223
224 function get_sub_field($field_name)
225 {
226
227 // vars
228 $field = $GLOBALS['acf_field'];
229 $i = $GLOBALS['acf_count'];
230
231 // no value
232 if(!$field) return false;
233
234 if(!isset($field[$i][$field_name])) return false;
235
236 return $field[$i][$field_name];
237 }
238
239
240 /*--------------------------------------------------------------------------------------
241 *
242 * the_sub_field
243 *
244 * @author Elliot Condon
245 * @since 1.0.3
246 *
247 *-------------------------------------------------------------------------------------*/
248
249 function the_sub_field($field_name, $field = false)
250 {
251 $value = get_sub_field($field_name, $field);
252
253 if(is_array($value))
254 {
255 $value = implode(', ',$value);
256 }
257
258 echo $value;
259 }
260
261
262 /*--------------------------------------------------------------------------------------
263 *
264 * register_field
265 *
266 * @author Elliot Condon
267 * @since 3.0.0
268 *
269 *-------------------------------------------------------------------------------------*/
270
271 $GLOBALS['acf_register_field'] = array();
272
273 function register_field($class = "", $url = "")
274 {
275 $GLOBALS['acf_register_field'][] = array(
276 'url' => $url,
277 'class' => $class,
278 );
279 }
280
281 function acf_register_field($array)
282 {
283 $array = array_merge($array, $GLOBALS['acf_register_field']);
284
285 return $array;
286 }
287 add_filter('acf_register_field', 'acf_register_field');
288
289
290 /*--------------------------------------------------------------------------------------
291 *
292 * register_field_group
293 *
294 * @author Elliot Condon
295 * @since 3.0.6
296 *
297 *-------------------------------------------------------------------------------------*/
298
299 $GLOBALS['acf_register_field_group'] = array();
300
301 function register_field_group($array)
302 {
303 // add id
304 if(!isset($array['id']))
305 {
306 $array['id'] = uniqid();
307 }
308
309
310 // 3.2.5 - changed show_on_page option
311 if( !isset($array['options']['hide_on_screen']) && isset($array['options']['show_on_page']) )
312 {
313 $show_all = array('the_content', 'discussion', 'custom_fields', 'comments', 'slug', 'author');
314 $array['options']['hide_on_screen'] = array_diff($show_all, $array['options']['show_on_page']);
315 unset( $array['options']['show_on_page'] );
316 }
317
318
319 $GLOBALS['acf_register_field_group'][] = $array;
320 }
321
322 function acf_register_field_group($array)
323 {
324 $array = array_merge($array, $GLOBALS['acf_register_field_group']);
325
326 // order field groups based on menu_order
327 // Obtain a list of columns
328 foreach ($array as $key => $row) {
329 $menu_order[$key] = $row['menu_order'];
330 }
331
332 // Sort the array with menu_order ascending
333 // Add $array as the last parameter, to sort by the common key
334 if(isset($menu_order))
335 {
336 array_multisort($menu_order, SORT_ASC, $array);
337 }
338
339
340 return $array;
341 }
342 add_filter('acf_register_field_group', 'acf_register_field_group');
343
344
345 /*--------------------------------------------------------------------------------------
346 *
347 * register_options_page
348 *
349 * @author Elliot Condon
350 * @since 3.0.0
351 *
352 *-------------------------------------------------------------------------------------*/
353
354 $GLOBALS['acf_register_options_page'] = array();
355
356 function register_options_page($title = "")
357 {
358 $GLOBALS['acf_register_options_page'][] = array(
359 'title' => $title,
360 'slug' => 'options-' . sanitize_title_with_dashes( $title ),
361 );
362 }
363
364 function acf_register_options_page($array)
365 {
366 $array = array_merge($array, $GLOBALS['acf_register_options_page']);
367
368 return $array;
369 }
370 add_filter('acf_register_options_page', 'acf_register_options_page');
371
372
373
374 /*--------------------------------------------------------------------------------------
375 *
376 * get_row_layout
377 *
378 * @author Elliot Condon
379 * @since 1.0.3
380 *
381 *-------------------------------------------------------------------------------------*/
382
383 function get_row_layout()
384 {
385
386 // vars
387 $field = $GLOBALS['acf_field'];
388 $i = $GLOBALS['acf_count'];
389
390 // no value
391 if(!$field) return false;
392
393 if(!isset($field[$i]['acf_fc_layout'])) return false;
394
395 return $field[$i]['acf_fc_layout'];
396 }
397
398
399 /*--------------------------------------------------------------------------------------
400 *
401 * shorcode support
402 *
403 * @author Elliot Condon
404 * @since 1.1.1
405 *
406 *-------------------------------------------------------------------------------------*/
407
408 function acf_shortcode( $atts )
409 {
410 // extract attributs
411 extract( shortcode_atts( array(
412 'field' => ""
413 ), $atts ) );
414
415 // $field is requird
416 if(!$field || $field == "")
417 {
418 return "";
419 }
420
421 // get value and return it
422 $value = get_field($field);
423
424 if(is_array($value))
425 {
426 $value = @implode(', ',$value);
427 }
428
429 return $value;
430 }
431 add_shortcode( 'acf', 'acf_shortcode' );
432
433
434 /*--------------------------------------------------------------------------------------
435 *
436 * Front end form Head
437 *
438 * @author Elliot Condon
439 * @since 1.1.4
440 *
441 *-------------------------------------------------------------------------------------*/
442
443 function acf_form_head()
444 {
445 // global vars
446 global $acf;
447
448
449
450 // run database save first
451 if(isset($_POST) && isset($_POST['acf_save']))
452 {
453 $post_id = $_POST['post_id'];
454
455 // save fields
456 $fields = $_POST['fields'];
457
458 if($fields)
459 {
460 foreach($fields as $key => $value)
461 {
462 // get field
463 $field = $acf->get_acf_field($key);
464
465 $acf->update_value($post_id, $field, $value);
466 }
467 }
468
469
470 // redirect
471 if(isset($_POST['return']))
472 {
473 wp_redirect($_POST['return']);
474 exit;
475 }
476
477 }
478
479
480 // register css / javascript
481 do_action('acf_print_scripts-input');
482 do_action('acf_print_styles-input');
483
484 // need wp styling
485 wp_enqueue_style(array(
486 'colors-fresh'
487 ));
488
489
490 // form was not posted, load js head stuff
491 add_action('wp_head', 'acf_form_wp_head');
492
493 }
494
495 function acf_form_wp_head()
496 {
497 // global vars
498 global $post, $acf;
499
500
501 // Style
502 echo '<link rel="stylesheet" type="text/css" href="'.$acf->dir.'/css/global.css?ver=' . $acf->version . '" />';
503 echo '<link rel="stylesheet" type="text/css" href="'.$acf->dir.'/css/input.css?ver=' . $acf->version . '" />';
504
505
506 // Javascript
507 echo '<script type="text/javascript" src="'.$acf->dir.'/js/input-actions.js?ver=' . $acf->version . '" ></script>';
508 echo '<script type="text/javascript">acf.post_id = ' . $post->ID . ';</script>';
509
510
511 // add user js + css
512 do_action('acf_head-input');
513 }
514
515
516 /*--------------------------------------------------------------------------------------
517 *
518 * Front end form
519 *
520 * @author Elliot Condon
521 * @since 1.1.4
522 *
523 *-------------------------------------------------------------------------------------*/
524
525 function acf_form($options = null)
526 {
527 global $post, $acf;
528
529
530 // defaults
531 $defaults = array(
532 'post_id' => $post->ID, // post id to get field groups from and save data to
533 'field_groups' => array(), // this will find the field groups for this post
534 'form_attributes' => array( // attributes will be added to the form element
535 'class' => ''
536 ),
537 'return' => add_query_arg( 'updated', 'true', get_permalink() ), // return url
538 'html_field_open' => '<div class="field">', // field wrapper open
539 'html_field_close' => '</div>', // field wrapper close
540 'html_before_fields' => '', // html inside form before fields
541 'html_after_fields' => '', // html inside form after fields
542 'submit_value' => 'Update', // vale for submit field
543 'updated_message' => 'Post updated.', // default updated message. Can be false
544 );
545
546
547 // merge defaults with options
548 if($options && is_array($options))
549 {
550 $options = array_merge($defaults, $options);
551 }
552 else
553 {
554 $options = $defaults;
555 }
556
557
558 // register post box
559 if(!$options['field_groups'])
560 {
561 $options['field_groups'] = $acf->get_input_metabox_ids(array('post_id' => $options['post_id']), false);
562 }
563
564
565 // updated message
566 if(isset($_GET['updated']) && $_GET['updated'] == 'true' && $options['updated_message'])
567 {
568 echo '<div id="message" class="updated"><p>' . $options['updated_message'] . '</p></div>';
569 }
570
571 // display form
572 ?>
573 <form action="" id="post" method="post" <?php if($options['form_attributes']){foreach($options['form_attributes'] as $k => $v){echo $k . '="' . $v .'" '; }} ?>>
574 <div style="display:none">
575 <input type="hidden" name="acf_save" value="true" />
576 <input type="hidden" name="post_id" value="<?php echo $options['post_id']; ?>" />
577 <input type="hidden" name="return" value="<?php echo $options['return']; ?>" />
578 <?php wp_editor('', 'acf_settings'); ?>
579 </div>
580
581 <div id="poststuff">
582 <?php
583
584 // html before fields
585 echo $defaults['html_before_fields'];
586
587 $field_groups = $acf->get_field_groups();
588 if($field_groups):
589 foreach($field_groups as $field_group):
590
591 if(!in_array($field_group['id'], $options['field_groups'])) continue;
592
593
594 // defaults
595 if(!$field_group['options'])
596 {
597 $field_group['options'] = array(
598 'layout' => 'default'
599 );
600 }
601
602
603 if($field_group['fields'])
604 {
605 echo '<div id="acf_' . $field_group['id'] . '" class="postbox acf_postbox"><div class="inside">';
606 echo '<div class="options" data-layout="' . $field_group['options']['layout'] . '" data-show="true"></div>';
607 $acf->render_fields_for_input($field_group['fields'], $options['post_id']);
608 echo '</div></div>';
609 }
610
611 endforeach;
612 endif;
613
614 // html after fields
615 echo $defaults['html_after_fields'];
616
617 ?>
618 <!-- Submit -->
619 <div class="field">
620 <input type="submit" value="<?php echo $options['submit_value']; ?>" />
621 </div>
622 <!-- / Submit -->
623
624 </div><!-- <div id="poststuff"> -->
625 </form>
626 <?php
627
628 }
629
630
631 /*--------------------------------------------------------------------------------------
632 *
633 * update_field
634 *
635 * @author Elliot Condon
636 * @since 3.1.9
637 *
638 *-------------------------------------------------------------------------------------*/
639
640 function update_field($field_name, $value, $post_id = false)
641 {
642 global $post, $acf;
643
644 if(!$post_id)
645 {
646 $post_id = $post->ID;
647 }
648
649
650 // allow for option == options
651 if( $post_id == "option" )
652 {
653 $post_id = "options";
654 }
655
656
657 // get value
658 $field_key = "";
659 if( is_numeric($post_id) )
660 {
661 $field_key = get_post_meta($post_id, '_' . $field_name, true);
662 }
663 else
664 {
665 $field_key = get_option('_' . $post_id . '_' . $field_name);
666 }
667
668
669 // create default field to save the data as plain text
670 $field = array(
671 'type' => 'text',
672 'name' => $field_name,
673 'key' => ''
674 );
675
676 if($field_key != "")
677 {
678 // we can load the field properly!
679 $field = $acf->get_acf_field($field_key);
680 }
681
682
683 // sub fields? They need formatted data
684 if( isset($field['sub_fields']) )
685 {
686 // define sub field keys
687 $sub_field_keys = array();
688 if( $field['sub_fields'] )
689 {
690 foreach( $field['sub_fields'] as $sub_field )
691 {
692 $sub_field_keys[ $sub_field['name'] ] = $sub_field['key'];
693 }
694 }
695
696
697 // loop through the values and format the array to use sub field keys
698 if( $value )
699 {
700 foreach( $value as $row_i => $row)
701 {
702 if( $row )
703 {
704 foreach( $row as $sub_field_name => $sub_field_value )
705 {
706
707 if( isset($sub_field_keys[$sub_field_name]) )
708 {
709 // change the array key from "sub_field_name" to "sub_field_key"
710 $value[$row_i][ $sub_field_keys[$sub_field_name] ] = $sub_field_value;
711
712 unset( $value[$row_i][$sub_field_name] );
713 }
714
715 }
716 // foreach( $row as $sub_field_name => $sub_field_value )
717 }
718 // if( $row )
719 }
720 // foreach( $value as $row_i => $row)
721 }
722 // if( $value )
723
724 }
725
726
727
728
729 $acf->update_value($post_id, $field, $value);
730 }
731
732 ?>