PluginProbe ʕ •ᴥ•ʔ
Advanced Custom Fields (ACF®) / 3.3.8
Advanced Custom Fields (ACF®) v3.3.8
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 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.1.6 6.1.7 6.1.8 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.6.1 6.2.7 6.2.8 6.2.9 6.3.0 6.3.1 6.3.10.2 6.3.11 6.3.12 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.6.1 6.4.0 6.4.0.1 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.6.0 6.6.1 6.6.2 6.7.0 6.7.1 6.7.2 6.8.0 trunk 1.0.0 1.0.2 1.0.3 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.1 2.1.3 2.1.4 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.6 3.0.7 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.8 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.4.10 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 5.10 5.10.1 5.10.2 5.11 5.11.1 5.11.2 5.11.3 5.11.4 5.12 5.12.1 5.12.2 5.12.3 5.12.4 5.12.5 5.12.6 5.6.10 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 5.6.7 5.6.8 5.6.9 5.7.0 5.7.1 5.7.10 5.7.12 5.7.13 5.7.2 5.7.3 5.7.4 5.7.5 5.7.6 5.7.7 5.7.8 5.7.9 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.2 5.8.3 5.8.4
advanced-custom-fields / acf.php
advanced-custom-fields Last commit date
core 13 years ago css 13 years ago images 13 years ago js 13 years ago lang 13 years ago acf.php 13 years ago readme.txt 13 years ago screenshot-1.png 13 years ago screenshot-2.png 13 years ago screenshot-3.png 13 years ago screenshot-4.png 13 years ago
acf.php
1625 lines
1 <?php
2 /*
3 Plugin Name: Advanced Custom Fields
4 Plugin URI: http://www.advancedcustomfields.com/
5 Description: Fully customise WordPress edit screens with powerful fields. Boasting a professional interface and a powerfull API, it’s a must have for any web developer working with WordPress. Field types include: Wysiwyg, text, textarea, image, file, select, checkbox, page link, post object, date picker, color picker, repeater, flexible content, gallery and more!
6 Version: 3.3.8
7 Author: Elliot Condon
8 Author URI: http://www.elliotcondon.com/
9 License: GPL
10 Copyright: Elliot Condon
11 */
12
13 include('core/api.php');
14
15 $acf = new Acf();
16
17 class Acf
18 {
19 var $dir,
20 $path,
21 $version,
22 $upgrade_version,
23 $fields,
24 $cache,
25
26 // controllers
27 $upgrade,
28 $settings,
29 $field_groups,
30 $field_group,
31 $input,
32 $options_page,
33 $everything_fields;
34
35
36 /*
37 * Constructor
38 *
39 * @description:
40 * @since 1.0.0
41 * @created: 23/06/12
42 */
43
44 function Acf()
45 {
46
47 // vars
48 $this->path = plugin_dir_path(__FILE__);
49 $this->dir = plugins_url('',__FILE__);
50 $this->version = '3.3.8';
51 $this->upgrade_version = '3.3.3'; // this is the latest version which requires an upgrade
52 $this->cache = array(); // basic array cache to hold data throughout the page load
53
54
55 // set text domain
56 load_plugin_textdomain('acf', false, basename(dirname(__FILE__)).'/lang' );
57
58
59 // controllers
60 $this->setup_controllers();
61
62
63 // actions
64 add_action('init', array($this, 'init'));
65 add_filter('post_updated_messages', array($this, 'post_updated_messages'));
66 add_filter('manage_edit-acf_columns', array($this, 'acf_columns_filter'));
67
68 add_action('admin_menu', array($this,'admin_menu'));
69 add_action('admin_head', array($this,'admin_head'));
70
71 add_action('wp_ajax_get_input_metabox_ids', array($this, 'get_input_metabox_ids'));
72
73
74 return true;
75 }
76
77
78 /*
79 * get_cache
80 *
81 * @description: Simple ACF (once per page) cache
82 * @since 3.1.9
83 * @created: 23/06/12
84 */
85
86 function get_cache($key = false)
87 {
88 // key is required
89 if( !$key )
90 return false;
91
92
93 // does cache at key exist?
94 if( !isset($this->cache[$key]) )
95 return false;
96
97
98 // return cahced item
99 return $this->cache[$key];
100 }
101
102
103 /*
104 * set_cache
105 *
106 * @description: Simple ACF (once per page) cache
107 * @since 3.1.9
108 * @created: 23/06/12
109 */
110
111 function set_cache($key = false, $value = null)
112 {
113 // key is required
114 if( !$key )
115 return false;
116
117
118 // update the cache array
119 $this->cache[$key] = $value;
120
121
122 // return true. Probably not needed
123 return true;
124 }
125
126
127 /*
128 * setup_fields
129 *
130 * @description: Create an array of field objects, including custom registered field types
131 * @since 1.0.0
132 * @created: 23/06/12
133 */
134
135 function setup_fields()
136 {
137 // vars
138 $return = array();
139
140
141 // include parent field
142 include_once('core/fields/acf_field.php');
143
144
145 // include child fields
146 include_once('core/fields/acf_field.php');
147 include_once('core/fields/text.php');
148 include_once('core/fields/textarea.php');
149 include_once('core/fields/wysiwyg.php');
150 include_once('core/fields/image.php');
151 include_once('core/fields/file.php');
152 include_once('core/fields/number.php');
153 include_once('core/fields/select.php');
154 include_once('core/fields/checkbox.php');
155 include_once('core/fields/radio.php');
156 include_once('core/fields/true_false.php');
157 include_once('core/fields/page_link.php');
158 include_once('core/fields/post_object.php');
159 include_once('core/fields/relationship.php');
160 include_once('core/fields/date_picker/date_picker.php');
161 include_once('core/fields/color_picker.php');
162
163
164 // add child fields
165 $return['text'] = new acf_Text($this);
166 $return['textarea'] = new acf_Textarea($this);
167 $return['wysiwyg'] = new acf_Wysiwyg($this);
168 $return['image'] = new acf_Image($this);
169 $return['file'] = new acf_File($this);
170 $return['number'] = new acf_Number($this);
171 $return['select'] = new acf_Select($this);
172 $return['checkbox'] = new acf_Checkbox($this);
173 $return['radio'] = new acf_Radio($this);
174 $return['true_false'] = new acf_True_false($this);
175 $return['page_link'] = new acf_Page_link($this);
176 $return['post_object'] = new acf_Post_object($this);
177 $return['relationship'] = new acf_Relationship($this);
178 $return['date_picker'] = new acf_Date_picker($this);
179 $return['color_picker'] = new acf_Color_picker($this);
180
181
182 // add repeater
183 if($this->is_field_unlocked('repeater'))
184 {
185 include_once('core/fields/repeater.php');
186 $return['repeater'] = new acf_Repeater($this);
187 }
188
189
190 // add flexible content
191 if($this->is_field_unlocked('flexible_content'))
192 {
193 include_once('core/fields/flexible_content.php');
194 $return['flexible_content'] = new acf_Flexible_content($this);
195 }
196
197
198 // add gallery
199 if($this->is_field_unlocked('gallery'))
200 {
201 include_once('core/fields/gallery.php');
202 $return['gallery'] = new acf_Gallery($this);
203 }
204
205
206 // hook to load in third party fields
207 $custom = apply_filters('acf_register_field',array());
208 if(!empty($custom))
209 {
210 foreach($custom as $v)
211 {
212 //var_dump($v['url']);
213 include($v['url']);
214 $name = $v['class'];
215 $custom_field = new $name($this);
216 $return[$custom_field->name] = $custom_field;
217 }
218 }
219
220
221 // set all the fields
222 $this->fields = $return;
223 }
224
225
226 /*
227 * setup_fields
228 *
229 * @description:
230 * @since 3.2.6
231 * @created: 23/06/12
232 */
233
234 function setup_controllers()
235 {
236 // Settings
237 include_once('core/controllers/settings.php');
238 $this->settings = new acf_settings($this);
239
240
241 // upgrade
242 include_once('core/controllers/upgrade.php');
243 $this->upgrade = new acf_upgrade($this);
244
245
246 // field_groups
247 include_once('core/controllers/field_groups.php');
248 $this->field_groups = new acf_field_groups($this);
249
250
251 // field_group
252 include_once('core/controllers/field_group.php');
253 $this->field_group = new acf_field_group($this);
254
255
256 // input
257 include_once('core/controllers/input.php');
258 $this->input = new acf_input($this);
259
260
261 // options page
262 include_once('core/controllers/options_page.php');
263 $this->options_page = new acf_options_page($this);
264
265
266 // everthing fields
267 include_once('core/controllers/everything_fields.php');
268 $this->everything_fields = new acf_everything_fields($this);
269 }
270
271
272 /*
273 * admin_menu
274 *
275 * @description:
276 * @since 1.0.0
277 * @created: 23/06/12
278 */
279
280 function admin_menu() {
281
282 // add acf page to options menu
283 add_utility_page(__("Custom Fields",'acf'), __("Custom Fields",'acf'), 'manage_options', 'edit.php?post_type=acf');
284
285 }
286
287
288 /*
289 * Init
290 *
291 * @description:
292 * @since 1.0.0
293 * @created: 23/06/12
294 */
295
296 function init()
297 {
298 // setup fields
299 $this->setup_fields();
300
301
302 // Create ACF post type
303 $labels = array(
304 'name' => __( 'Field&nbsp;Groups', 'acf' ),
305 'singular_name' => __( 'Advanced Custom Fields', 'acf' ),
306 'add_new' => __( 'Add New' , 'acf' ),
307 'add_new_item' => __( 'Add New Field Group' , 'acf' ),
308 'edit_item' => __( 'Edit Field Group' , 'acf' ),
309 'new_item' => __( 'New Field Group' , 'acf' ),
310 'view_item' => __('View Field Group', 'acf'),
311 'search_items' => __('Search Field Groups', 'acf'),
312 'not_found' => __('No Field Groups found', 'acf'),
313 'not_found_in_trash' => __('No Field Groups found in Trash', 'acf'),
314 );
315
316 register_post_type('acf', array(
317 'labels' => $labels,
318 'public' => false,
319 'show_ui' => true,
320 '_builtin' => false,
321 'capability_type' => 'page',
322 'hierarchical' => true,
323 'rewrite' => false,
324 'query_var' => "acf",
325 'supports' => array(
326 'title',
327 ),
328 'show_in_menu' => false,
329 ));
330
331 }
332
333
334 /*
335 * post_updated_messages
336 *
337 * @description: messages for saving a field group
338 * @since 1.0.0
339 * @created: 23/06/12
340 */
341
342 function post_updated_messages( $messages )
343 {
344 global $post, $post_ID;
345
346 $messages['acf'] = array(
347 0 => '', // Unused. Messages start at index 1.
348 1 => __('Field group updated.', 'acf'),
349 2 => __('Custom field updated.', 'acf'),
350 3 => __('Custom field deleted.', 'acf'),
351 4 => __('Field group updated.', 'acf'),
352 /* translators: %s: date and time of the revision */
353 5 => isset($_GET['revision']) ? sprintf( __('Field group restored to revision from %s', 'acf'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
354 6 => __('Field group published.', 'acf'),
355 7 => __('Field group saved.', 'acf'),
356 8 => __('Field group submitted.', 'acf'),
357 9 => __('Field group scheduled for.', 'acf'),
358 10 => __('Field group draft updated.', 'acf'),
359 );
360
361 return $messages;
362 }
363
364
365 /*
366 * acf_columns_filter
367 *
368 * @description: Custom Columns for ACF
369 * @since 1.0.0
370 * @created: 23/06/12
371 */
372
373 function acf_columns_filter($columns)
374 {
375 $columns = array(
376 'cb' => '<input type="checkbox" />',
377 'title' => __("Title"),
378 );
379 return $columns;
380 }
381
382
383
384 /*--------------------------------------------------------------------------------------
385 *
386 * admin_head
387 *
388 * @author Elliot Condon
389 * @since 1.0.0
390 *
391 *-------------------------------------------------------------------------------------*/
392
393 function admin_head()
394 {
395 // vars
396 global $post, $pagenow;
397
398
399 // hide upgrade page from nav
400 echo '<style type="text/css">
401 #toplevel_page_edit-post_type-acf a[href="edit.php?post_type=acf&page=acf-upgrade"]{ display:none; }
402 #toplevel_page_edit-post_type-acf .wp-menu-image { background: url("../wp-admin/images/menu.png") no-repeat scroll 0 -33px transparent; }
403 #toplevel_page_edit-post_type-acf:hover .wp-menu-image { background-position: 0 -1px; }
404 #toplevel_page_edit-post_type-acf .wp-menu-image img { display:none; }
405 </style>';
406
407 }
408
409
410 /*--------------------------------------------------------------------------------------
411 *
412 * get_field_groups
413 *
414 * This function returns an array of post objects found in the get_pages and the
415 * register_field_group calls.
416 *
417 * @author Elliot Condon
418 * @since 3.0.6
419 *
420 *-------------------------------------------------------------------------------------*/
421
422 function get_field_groups()
423 {
424 // return cache
425 $cache = $this->get_cache('acf_field_groups');
426 if($cache != false)
427 {
428 return $cache;
429 }
430
431 // vars
432 $acfs = array();
433
434 // get acf's
435 $result = get_pages(array(
436 'numberposts' => -1,
437 'post_type' => 'acf',
438 'sort_column' => 'menu_order',
439 'order' => 'ASC',
440 ));
441
442 // populate acfs
443 if($result)
444 {
445 foreach($result as $acf)
446 {
447 $acfs[] = array(
448 'id' => $acf->ID,
449 'title' => get_the_title($acf->ID),
450 'fields' => $this->get_acf_fields($acf->ID),
451 'location' => $this->get_acf_location($acf->ID),
452 'options' => $this->get_acf_options($acf->ID),
453 'menu_order' => $acf->menu_order,
454 );
455 }
456 }
457
458 // hook to load in registered field groups
459 $acfs = apply_filters('acf_register_field_group', $acfs);
460
461 // update cache
462 $this->set_cache('acf_field_groups', $acfs);
463
464 // return
465 if(empty($acfs))
466 {
467 return false;
468 }
469 return $acfs;
470 }
471
472
473 /*--------------------------------------------------------------------------------------
474 *
475 * get_acf_fields
476 * - returns an array of fields for a acf object
477 *
478 * @author Elliot Condon
479 * @since 1.0.0
480 *
481 *-------------------------------------------------------------------------------------*/
482
483 function get_acf_fields($post_id)
484 {
485 // vars
486 $return = array();
487 $keys = get_post_custom_keys($post_id);
488
489 if($keys)
490 {
491 foreach($keys as $key)
492 {
493 if(strpos($key, 'field_') !== false)
494 {
495 $field = $this->get_acf_field($key, $post_id);
496
497 $return[$field['order_no']] = $field;
498 }
499 }
500
501 ksort($return);
502 }
503 // return fields
504 return $return;
505
506 }
507
508
509 /*--------------------------------------------------------------------------------------
510 *
511 * get_acf_field
512 * - returns a field
513 *
514 * @author Elliot Condon
515 * @since 1.0.0
516 *
517 *-------------------------------------------------------------------------------------*/
518
519 function get_acf_field($field_name, $post_id = false)
520 {
521 // vars
522 $post_id = $post_id ? $post_id : $this->get_post_meta_post_id($field_name);
523 $field = false;
524
525 // if this acf ($post_id) is trashed don't use it's fields
526 if(get_post_status($post_id) != "trash")
527 {
528 $field = get_post_meta($post_id, $field_name, true);
529 }
530
531 // field could be registered via php, and not in db at all!
532 if(!$field)
533 {
534 // hook to load in registered field groups
535 $acfs = apply_filters('acf_register_field_group', array());
536 if($acfs)
537 {
538 // loop through acfs
539 foreach($acfs as $acf)
540 {
541 // loop through fields
542 if($acf['fields'])
543 {
544 foreach($acf['fields'] as $field)
545 {
546 if($field['key'] == $field_name)
547 {
548 return $field;
549 }
550 }
551 }
552 // if($acf['fields'])
553 }
554 // foreach($acfs as $acf)
555 }
556 // if($acfs)
557 }
558 // if(!$field)
559
560 return $field;
561
562 }
563
564
565 /*--------------------------------------------------------------------------------------
566 *
567 * get_post_meta_post_id
568 * - returns the post_id for a meta_key
569 *
570 * @author Elliot Condon
571 * @since 1.0.0
572 *
573 *-------------------------------------------------------------------------------------*/
574
575 function get_post_meta_post_id($field_name)
576 {
577 global $wpdb;
578 $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $field_name) );
579
580 if($post_id) return (int)$post_id;
581
582 return false;
583 }
584
585
586 /*--------------------------------------------------------------------------------------
587 *
588 * create_field
589 *
590 * @author Elliot Condon
591 * @since 1.0.0
592 *
593 *-------------------------------------------------------------------------------------*/
594
595 function create_field($field)
596 {
597 if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']]))
598 {
599 _e('Error: Field Type does not exist!','acf');
600 return false;
601 }
602
603 // defaults
604 if(!isset($field['class'])) $field['class'] = $field['type'];
605
606 $this->fields[$field['type']]->create_field($field);
607 }
608
609
610 /*--------------------------------------------------------------------------------------
611 *
612 * get_acf_location
613 *
614 * @author Elliot Condon
615 * @since 1.0.0
616 *
617 *-------------------------------------------------------------------------------------*/
618
619 function get_acf_location($post_id)
620 {
621 // vars
622 $return = array(
623 'rules' => array(),
624 'allorany' => get_post_meta($post_id, 'allorany', true) ? get_post_meta($post_id, 'allorany', true) : 'all',
625 );
626
627 // get all fields
628 $rules = get_post_meta($post_id, 'rule', false);
629
630 if($rules)
631 {
632 foreach($rules as $rule)
633 {
634 $return['rules'][$rule['order_no']] = $rule;
635 }
636 }
637
638 ksort($return['rules']);
639
640 // return fields
641 return $return;
642
643 }
644
645
646 /*--------------------------------------------------------------------------------------
647 *
648 * get_acf_options
649 *
650 * @author Elliot Condon
651 * @since 1.0.0
652 *
653 *-------------------------------------------------------------------------------------*/
654
655 function get_acf_options($post_id)
656 {
657
658 // defaults
659 $options = array(
660 'position' => get_post_meta($post_id, 'position', true) ? get_post_meta($post_id, 'position', true) : 'normal',
661 'layout' => get_post_meta($post_id, 'layout', true) ? get_post_meta($post_id, 'layout', true) : 'default',
662 'hide_on_screen' => get_post_meta($post_id, 'hide_on_screen', true) ? get_post_meta($post_id, 'hide_on_screen', true) : array(),
663 );
664
665
666 // return
667 return $options;
668 }
669
670
671
672 /*--------------------------------------------------------------------------------------
673 *
674 * get_value
675 *
676 * @author Elliot Condon
677 * @since 3.0.0
678 *
679 *-------------------------------------------------------------------------------------*/
680
681 function get_value($post_id, $field)
682 {
683 if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']]))
684 {
685 return false;
686 }
687
688 return $this->fields[$field['type']]->get_value($post_id, $field);
689 }
690
691
692 /*--------------------------------------------------------------------------------------
693 *
694 * get_value_for_api
695 *
696 * @author Elliot Condon
697 * @since 3.0.0
698 *
699 *-------------------------------------------------------------------------------------*/
700
701 function get_value_for_api($post_id, $field)
702 {
703 if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']]))
704 {
705 return '';
706 }
707
708 return $this->fields[$field['type']]->get_value_for_api($post_id, $field);
709 }
710
711
712 /*--------------------------------------------------------------------------------------
713 *
714 * update_value
715 *
716 * @author Elliot Condon
717 * @since 3.0.0
718 *
719 *-------------------------------------------------------------------------------------*/
720
721 function update_value($post_id, $field, $value)
722 {
723 $this->fields[$field['type']]->update_value($post_id, $field, $value);
724 }
725
726
727 /*--------------------------------------------------------------------------------------
728 *
729 * update_field
730 *
731 * @author Elliot Condon
732 * @since 3.0.0
733 *
734 *-------------------------------------------------------------------------------------*/
735
736 function update_field($post_id, $field)
737 {
738 // format the field (select, repeater, etc)
739 $field = $this->pre_save_field($field);
740
741 // save it!
742 update_post_meta($post_id, $field['key'], $field);
743 }
744
745
746 /*--------------------------------------------------------------------------------------
747 *
748 * pre_save_field
749 *
750 * @author Elliot Condon
751 * @since 3.0.0
752 *
753 *-------------------------------------------------------------------------------------*/
754
755 function pre_save_field($field)
756 {
757 // format the field (select, repeater, etc)
758 return $this->fields[$field['type']]->pre_save_field($field);
759 }
760
761
762 /*--------------------------------------------------------------------------------------
763 *
764 * format_value_for_api
765 *
766 * @author Elliot Condon
767 * @since 3.0.0
768 *
769 *-------------------------------------------------------------------------------------*/
770
771 function format_value_for_api($value, $field)
772 {
773 return $this->fields[$field['type']]->format_value_for_api($value, $field);
774 }
775
776
777 /*--------------------------------------------------------------------------------------
778 *
779 * create_format_data
780 *
781 * @author Elliot Condon
782 * @since 3.0.0
783 *
784 *-------------------------------------------------------------------------------------*/
785
786 function create_format_data($field)
787 {
788 return $this->fields[$field['type']]->create_format_data($field);
789 }
790
791
792 /*
793 * render_fields_for_input
794 *
795 * @description:
796 * @since 3.1.6
797 * @created: 23/06/12
798 */
799
800 function render_fields_for_input($fields, $post_id)
801 {
802
803 // create fields
804 if($fields)
805 {
806 foreach($fields as $field)
807 {
808 // if they didn't select a type, skip this field
809 if($field['type'] == 'null') continue;
810
811 // set value
812 $field['value'] = $this->get_value($post_id, $field);
813
814 // required
815 if(!isset($field['required']))
816 {
817 $field['required'] = "0";
818 }
819
820 $required_class = "";
821 $required_label = "";
822
823 if($field['required'] == "1")
824 {
825 $required_class = ' required';
826 $required_label = ' <span class="required">*</span>';
827 }
828
829 echo '<div id="acf-' . $field['name'] . '" class="field field-' . $field['type'] . $required_class . '">';
830
831 echo '<p class="label">';
832 echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
833 echo $field['instructions'];
834 echo '</p>';
835
836 $field['name'] = 'fields[' . $field['key'] . ']';
837 $this->create_field($field);
838
839 echo '</div>';
840
841 }
842 // foreach($fields as $field)
843 }
844 // if($fields)
845
846 }
847
848
849 /*--------------------------------------------------------------------------------------
850 *
851 * get_input_metabox_ids
852 * - called by function.fields to hide / show metaboxes
853 *
854 * @author Elliot Condon
855 * @since 2.0.5
856 *
857 *-------------------------------------------------------------------------------------*/
858
859 function get_input_metabox_ids($overrides = array(), $json = true)
860 {
861 // overrides
862 if(isset($_POST))
863 {
864 if(isset($_POST['post_id']) && $_POST['post_id'] != 'false') $overrides['post_id'] = $_POST['post_id'];
865 if(isset($_POST['page_template']) && $_POST['page_template'] != 'false') $overrides['page_template'] = $_POST['page_template'];
866 if(isset($_POST['page_parent']) && $_POST['page_parent'] != 'false') $overrides['page_parent'] = $_POST['page_parent'];
867 if(isset($_POST['page_type']) && $_POST['page_type'] != 'false') $overrides['page_type'] = $_POST['page_type'];
868 if(isset($_POST['page']) && $_POST['page'] != 'false') $overrides['page'] = $_POST['page'];
869 if(isset($_POST['post']) && $_POST['post'] != 'false') $overrides['post'] = $_POST['post'];
870 if(isset($_POST['post_category']) && $_POST['post_category'] != 'false') $overrides['post_category'] = $_POST['post_category'];
871 if(isset($_POST['post_format']) && $_POST['post_format'] != 'false') $overrides['post_format'] = $_POST['post_format'];
872 if(isset($_POST['taxonomy']) && $_POST['taxonomy'] != 'false') $overrides['taxonomy'] = $_POST['taxonomy'];
873 }
874
875 // create post object to match against
876 $post = isset($overrides['post_id']) ? get_post($_POST['post_id']) : false;
877
878 // find all acf objects
879 $acfs = $this->get_field_groups();
880
881 // blank array to hold acfs
882 $return = array();
883
884 if($acfs)
885 {
886 foreach($acfs as $acf)
887 {
888 $add_box = false;
889
890 if($acf['location']['allorany'] == 'all')
891 {
892 // ALL
893 $add_box = true;
894
895 if($acf['location']['rules'])
896 {
897 foreach($acf['location']['rules'] as $rule)
898 {
899
900 // if any rules dont return true, dont add this acf
901 if(!$this->match_location_rule($post, $rule, $overrides))
902 {
903 $add_box = false;
904 }
905 }
906 }
907
908 }
909 elseif($acf['location']['allorany'] == 'any')
910 {
911 // ANY
912
913 $add_box = false;
914
915 if($acf['location']['rules'])
916 {
917 foreach($acf['location']['rules'] as $rule)
918 {
919 // if any rules return true, add this acf
920 if($this->match_location_rule($post, $rule, $overrides))
921 {
922 $add_box = true;
923 }
924 }
925 }
926 }
927
928 if($add_box == true)
929 {
930 $return[] = $acf['id'];
931 }
932
933 }
934 }
935
936 if($json)
937 {
938 echo json_encode($return);
939 die;
940 }
941 else
942 {
943 return $return;
944 }
945
946
947 }
948
949
950
951
952
953 /*--------------------------------------------------------------------------------------
954 *
955 * match_location_rule
956 *
957 * @author Elliot Condon
958 * @since 2.0.0
959 *
960 *-------------------------------------------------------------------------------------*/
961
962 function match_location_rule($post = null, $rule = array(), $overrides = array())
963 {
964
965 // no post? Thats okay if you are one of the bellow exceptions. Otherwise, return false
966 if(!$post)
967 {
968 $exceptions = array(
969 'user_type',
970 'options_page',
971 'ef_taxonomy',
972 'ef_user',
973 'ef_media',
974 );
975
976 if( !in_array($rule['param'], $exceptions) )
977 {
978 return false;
979 }
980 }
981
982
983 if(!isset($rule['value']))
984 {
985 return false;
986 }
987
988
989 switch ($rule['param']) {
990
991 // POST TYPE
992 case "post_type":
993
994 $post_type = isset($overrides['post_type']) ? $overrides['post_type'] : get_post_type($post);
995
996 if($rule['operator'] == "==")
997 {
998 if($post_type == $rule['value'])
999 {
1000 return true;
1001 }
1002
1003 return false;
1004 }
1005 elseif($rule['operator'] == "!=")
1006 {
1007 if($post_type != $rule['value'])
1008 {
1009 return true;
1010 }
1011
1012 return false;
1013 }
1014
1015 break;
1016
1017 // PAGE
1018 case "page":
1019
1020 $page = isset($overrides['page']) ? $overrides['page'] : $post->ID;
1021
1022 if($rule['operator'] == "==")
1023 {
1024 if($page == $rule['value'])
1025 {
1026 return true;
1027 }
1028
1029 return false;
1030 }
1031 elseif($rule['operator'] == "!=")
1032 {
1033 if($page != $rule['value'])
1034 {
1035 return true;
1036 }
1037
1038 return false;
1039 }
1040
1041 break;
1042
1043 // PAGE
1044 case "page_type":
1045
1046 $page_type = isset($overrides['page_type']) ? $overrides['page_type'] : $post->post_parent;
1047
1048 if($rule['operator'] == "==")
1049 {
1050 if($rule['value'] == "parent" && $page_type == "0")
1051 {
1052 return true;
1053 }
1054
1055 if($rule['value'] == "child" && $page_type != "0")
1056 {
1057 return true;
1058 }
1059
1060 return false;
1061 }
1062 elseif($rule['operator'] == "!=")
1063 {
1064 if($rule['value'] == "parent" && $page_type != "0")
1065 {
1066 return true;
1067 }
1068
1069 if($rule['value'] == "child" && $page_type == "0")
1070 {
1071 return true;
1072 }
1073
1074 return false;
1075 }
1076
1077 break;
1078
1079 // PAGE PARENT
1080 case "page_parent":
1081
1082 $page_parent = isset($overrides['page_parent']) ? $overrides['page_parent'] : $post->post_parent;
1083
1084 if($rule['operator'] == "==")
1085 {
1086 if($page_parent == $rule['value'])
1087 {
1088 return true;
1089 }
1090
1091 return false;
1092
1093 }
1094 elseif($rule['operator'] == "!=")
1095 {
1096 if($page_parent != $rule['value'])
1097 {
1098 return true;
1099 }
1100
1101 return false;
1102 }
1103
1104 break;
1105
1106 // PAGE
1107 case "page_template":
1108
1109 $page_template = isset($overrides['page_template']) ? $overrides['page_template'] : get_post_meta($post->ID,'_wp_page_template',true);
1110
1111 if($rule['operator'] == "==")
1112 {
1113 if($page_template == $rule['value'])
1114 {
1115 return true;
1116 }
1117
1118 if($rule['value'] == "default" && !$page_template)
1119 {
1120 return true;
1121 }
1122
1123 return false;
1124 }
1125 elseif($rule['operator'] == "!=")
1126 {
1127 if($page_template != $rule['value'])
1128 {
1129 return true;
1130 }
1131
1132 return false;
1133 }
1134
1135 break;
1136
1137 // POST
1138 case "post":
1139
1140 $post_id = isset($overrides['post']) ? $overrides['post'] : $post->ID;
1141
1142 if($rule['operator'] == "==")
1143 {
1144 if($post_id == $rule['value'])
1145 {
1146 return true;
1147 }
1148
1149 return false;
1150 }
1151 elseif($rule['operator'] == "!=")
1152 {
1153 if($post_id != $rule['value'])
1154 {
1155 return true;
1156 }
1157
1158 return false;
1159 }
1160
1161 break;
1162
1163 // POST CATEGORY
1164 case "post_category":
1165
1166 $cats = array();
1167
1168 if(isset($overrides['post_category']))
1169 {
1170 $cats = $overrides['post_category'];
1171 }
1172 else
1173 {
1174 $all_cats = get_the_category($post->ID);
1175 foreach($all_cats as $cat)
1176 {
1177 $cats[] = $cat->term_id;
1178 }
1179 }
1180 if($rule['operator'] == "==")
1181 {
1182 if($cats)
1183 {
1184 if(in_array($rule['value'], $cats))
1185 {
1186 return true;
1187 }
1188 }
1189
1190 return false;
1191 }
1192 elseif($rule['operator'] == "!=")
1193 {
1194 if($cats)
1195 {
1196 if(!in_array($rule['value'], $cats))
1197 {
1198 return true;
1199 }
1200 }
1201
1202 return false;
1203 }
1204
1205 break;
1206
1207
1208 // USER TYPE
1209 case "user_type":
1210
1211 if($rule['operator'] == "==")
1212 {
1213 if(current_user_can($rule['value']))
1214 {
1215 return true;
1216 }
1217
1218 return false;
1219 }
1220 elseif($rule['operator'] == "!=")
1221 {
1222 if(!current_user_can($rule['value']))
1223 {
1224 return true;
1225 }
1226
1227 return false;
1228 }
1229
1230 break;
1231
1232 // Options Page
1233 case "options_page":
1234
1235 if(!function_exists('get_admin_page_title'))
1236 {
1237 return false;
1238 }
1239
1240 // value has changed in 3.2.6 to a sanitized string
1241 if( strpos( $rule['value'] ,'options-') === false )
1242 {
1243 $rule['value'] = 'options-' . sanitize_title_with_dashes( $rule['value'] );
1244 }
1245
1246 // generate the page title to match against
1247 $page_title = 'options-' . sanitize_title_with_dashes( get_admin_page_title() );
1248
1249 if($rule['operator'] == "==")
1250 {
1251 if( $page_title == $rule['value'] )
1252 {
1253 return true;
1254 }
1255
1256 return false;
1257 }
1258 elseif($rule['operator'] == "!=")
1259 {
1260 if( $page_title == $rule['value'] )
1261 {
1262 return true;
1263 }
1264
1265 return false;
1266 }
1267
1268 break;
1269
1270
1271 // Post Format
1272 case "post_format":
1273
1274
1275 $post_format = isset($overrides['post_format']) ? $overrides['post_format'] : get_post_format( $post->ID );
1276 if($post_format == "0") $post_format = "standard";
1277
1278 if($rule['operator'] == "==")
1279 {
1280 if($post_format == $rule['value'])
1281 {
1282 return true;
1283 }
1284
1285 return false;
1286 }
1287 elseif($rule['operator'] == "!=")
1288 {
1289 if($post_format != $rule['value'])
1290 {
1291 return true;
1292 }
1293
1294 return false;
1295 }
1296
1297
1298 break;
1299
1300 // Taxonomy
1301 case "taxonomy":
1302
1303 $terms = array();
1304
1305 if(isset($overrides['taxonomy']))
1306 {
1307 $terms = $overrides['taxonomy'];
1308 }
1309 else
1310 {
1311 $taxonomies = get_object_taxonomies($post->post_type);
1312 if($taxonomies)
1313 {
1314 foreach($taxonomies as $tax)
1315 {
1316 $all_terms = get_the_terms($post->ID, $tax);
1317 if($all_terms)
1318 {
1319 foreach($all_terms as $all_term)
1320 {
1321 $terms[] = $all_term->term_id;
1322 }
1323 }
1324 }
1325 }
1326 }
1327
1328 if($rule['operator'] == "==")
1329 {
1330 if($terms)
1331 {
1332 if(in_array($rule['value'], $terms))
1333 {
1334 return true;
1335 }
1336 }
1337
1338 return false;
1339 }
1340 elseif($rule['operator'] == "!=")
1341 {
1342 if($terms)
1343 {
1344 if(!in_array($rule['value'], $terms))
1345 {
1346 return true;
1347 }
1348 }
1349
1350 return false;
1351 }
1352
1353
1354 break;
1355
1356 // Everything Fields: Taxonomy
1357 case "ef_taxonomy":
1358
1359 if( !isset($overrides['ef_taxonomy']) )
1360 {
1361 return false;
1362 }
1363
1364 $ef_taxonomy = $overrides['ef_taxonomy'];
1365
1366 if($rule['operator'] == "==")
1367 {
1368 if( $ef_taxonomy == $rule['value'] || $rule['value'] == "all" )
1369 {
1370 return true;
1371 }
1372
1373 return false;
1374 }
1375 elseif($rule['operator'] == "!=")
1376 {
1377 if( $ef_taxonomy != $rule['value'] || $rule['value'] == "all" )
1378 {
1379 return true;
1380 }
1381
1382 return false;
1383 }
1384
1385
1386 break;
1387
1388 // Everything Fields: User
1389 case "ef_user":
1390
1391 if( !isset($overrides['ef_user']) )
1392 {
1393 return false;
1394 }
1395
1396 $ef_user = $overrides['ef_user'];
1397
1398 if($rule['operator'] == "==")
1399 {
1400 if( user_can($ef_user, $rule['value']) || $rule['value'] == "all" )
1401 {
1402 return true;
1403 }
1404
1405 return false;
1406 }
1407 elseif($rule['operator'] == "!=")
1408 {
1409 if( user_can($ef_user, $rule['value']) || $rule['value'] == "all" )
1410 {
1411 return true;
1412 }
1413
1414 return false;
1415 }
1416
1417
1418 break;
1419
1420 // Everything Fields: Media
1421 case "ef_media":
1422
1423 if( !isset($overrides['ef_media']) )
1424 {
1425 return false;
1426 }
1427
1428 $ef_media = $overrides['ef_media'];
1429
1430 if($rule['operator'] == "==")
1431 {
1432 if( $rule['value'] == "all" )
1433 {
1434 return true;
1435 }
1436
1437 return false;
1438 }
1439 elseif($rule['operator'] == "!=")
1440 {
1441 if( $rule['value'] == "all" )
1442 {
1443 return true;
1444 }
1445
1446 return false;
1447 }
1448
1449
1450 break;
1451 }
1452
1453 }
1454
1455
1456 /*--------------------------------------------------------------------------------------
1457 *
1458 * is_field_unlocked
1459 *
1460 * @author Elliot Condon
1461 * @since 3.0.0
1462 *
1463 *-------------------------------------------------------------------------------------*/
1464
1465 function is_field_unlocked($field_name)
1466 {
1467 $hashes = array(
1468 'repeater' => 'bbefed143f1ec106ff3a11437bd73432',
1469 'options_page' => '1fc8b993548891dc2b9a63ac057935d8',
1470 'flexible_content' => 'd067e06c2b4b32b1c1f5b6f00e0d61d6',
1471 'gallery' => '69f4adc9883195bd206a868ffa954b49',
1472 );
1473
1474 $hash = md5( $this->get_license_key($field_name) );
1475
1476 if( $hashes[$field_name] == $hash )
1477 {
1478 return true;
1479 }
1480
1481 return false;
1482
1483 }
1484
1485 /*--------------------------------------------------------------------------------------
1486 *
1487 * is_field_unlocked
1488 *
1489 * @author Elliot Condon
1490 * @since 3.0.0
1491 *
1492 *-------------------------------------------------------------------------------------*/
1493
1494 function get_license_key($field_name)
1495 {
1496 return get_option('acf_' . $field_name . '_ac');
1497 }
1498
1499
1500 /*--------------------------------------------------------------------------------------
1501 *
1502 * admin_message
1503 *
1504 * @author Elliot Condon
1505 * @since 2.0.5
1506 *
1507 *-------------------------------------------------------------------------------------*/
1508
1509 function admin_message($message = "", $type = 'updated')
1510 {
1511 $GLOBALS['acf_mesage'] = $message;
1512 $GLOBALS['acf_mesage_type'] = $type;
1513
1514 add_action('admin_notices', array($this, 'acf_admin_notice'));
1515 }
1516
1517 function acf_admin_notice()
1518 {
1519 echo '<div class="' . $GLOBALS['acf_mesage_type'] . '" id="message">'.$GLOBALS['acf_mesage'].'</div>';
1520 }
1521
1522
1523
1524 /*--------------------------------------------------------------------------------------
1525 *
1526 * get_taxonomies_for_select
1527 *
1528 *---------------------------------------------------------------------------------------
1529 *
1530 * returns a multidimentional array of taxonomies grouped by the post type / taxonomy
1531 *
1532 * @author Elliot Condon
1533 * @since 3.0.2
1534 *
1535 *-------------------------------------------------------------------------------------*/
1536
1537 function get_taxonomies_for_select( $args = array() )
1538 {
1539 // vars
1540 $post_types = get_post_types();
1541 $choices = array();
1542 $defaults = array(
1543 'simple_value' => false,
1544 );
1545
1546 $options = array_merge($defaults, $args);
1547
1548
1549 if($post_types)
1550 {
1551 foreach($post_types as $post_type)
1552 {
1553 $post_type_object = get_post_type_object($post_type);
1554 $taxonomies = get_object_taxonomies($post_type);
1555 if($taxonomies)
1556 {
1557 foreach($taxonomies as $taxonomy)
1558 {
1559 if(!is_taxonomy_hierarchical($taxonomy)) continue;
1560 $terms = get_terms($taxonomy, array('hide_empty' => false));
1561 if($terms)
1562 {
1563 foreach($terms as $term)
1564 {
1565 $value = $taxonomy . ':' . $term->term_id;
1566
1567 if( $options['simple_value'] )
1568 {
1569 $value = $term->term_id;
1570 }
1571
1572 $choices[$post_type_object->label . ': ' . $taxonomy][$value] = $term->name;
1573 }
1574 }
1575 }
1576 }
1577 }
1578 }
1579
1580 return $choices;
1581 }
1582
1583
1584
1585 /*
1586 * get_all_image_sizes
1587 *
1588 * @description: returns an array holding all the image sizes
1589 * @since 3.2.8
1590 * @created: 6/07/12
1591 */
1592
1593 function get_all_image_sizes()
1594 {
1595 // find all sizes
1596 $all_sizes = get_intermediate_image_sizes();
1597
1598
1599 // define default sizes
1600 $image_sizes = array(
1601 'thumbnail' => __("Thumbnail",'acf'),
1602 'medium' => __("Medium",'acf'),
1603 'large' => __("Large",'acf'),
1604 'full' => __("Full",'acf')
1605 );
1606
1607
1608 // add extra registered sizes
1609 foreach($all_sizes as $size)
1610 {
1611 if (!isset($image_sizes[$size]))
1612 {
1613 $image_sizes[$size] = ucwords( str_replace('-', ' ', $size) );
1614 }
1615 }
1616
1617
1618 // return array
1619 return $image_sizes;
1620 }
1621
1622
1623
1624 }
1625 ?>