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