PluginProbe
Advanced Custom Fields (ACF®) / 3.1.3
Advanced Custom Fields (ACF®) v3.1.3
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 in Advanced Custom Fields (ACF®) 3.1.3, at acf.php

1,756 lines 42.8 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.1.3
7 Author: Elliot Condon
8 Author URI: http://www.elliotcondon.com/
9 License: GPL
10 Copyright: Elliot Condon
11 */
12
13 //ini_set('error_reporting', E_ALL);
14
15 include('core/api.php');
16
17 $acf = new Acf();
18
19 class Acf
20 {
21 var $dir;
22 var $path;
23 var $siteurl;
24 var $wpadminurl;
25 var $version;
26 var $upgrade_version;
27 var $fields;
28 var $options_page;
29
30
31 /*--------------------------------------------------------------------------------------
32 *
33 * Constructor
34 *
35 * @author Elliot Condon
36 * @since 1.0.0
37 *
38 *-------------------------------------------------------------------------------------*/
39
40 function Acf()
41 {
42
43 // set class variables
44 $this->path = dirname(__FILE__).'';
45 $this->dir = plugins_url('',__FILE__);
46 $this->siteurl = get_bloginfo('url');
47 $this->wpadminurl = admin_url();
48 $this->version = '3.1.3';
49 $this->upgrade_version = '3.0.0'; // this is the latest version which requires an upgrade
50
51
52 // set text domain
53 //load_plugin_textdomain('acf', false, $this->path.'/lang' );
54 load_plugin_textdomain('acf', false, basename(dirname(__FILE__)).'/lang' );
55
56 // load options page
57 $this->setup_options_page();
58
59 // actions
60 add_filter('pre_get_posts', array($this, 'pre_get_posts'));
61 add_action('init', array($this, 'init'));
62 add_action('admin_menu', array($this,'admin_menu'));
63 add_action('admin_head', array($this,'admin_head'));
64 add_filter('name_save_pre', array($this, 'save_name'));
65 add_action('save_post', array($this, 'save_post'));
66 add_action('wp_ajax_get_input_metabox_ids', array($this, 'get_input_metabox_ids'));
67 add_action('wp_ajax_get_input_style', array($this, 'the_input_style'));
68 add_action('admin_footer', array($this, 'admin_footer'));
69 add_action('admin_print_scripts', array($this, 'admin_print_scripts'));
70 add_action('admin_print_styles', array($this, 'admin_print_styles'));
71 add_action('wp_ajax_acf_upgrade', array($this, 'upgrade_ajax'));
72
73 return true;
74 }
75
76
77 /*--------------------------------------------------------------------------------------
78 *
79 * pre_get_posts
80 *
81 * @author Elliot Condon
82 * @since 3.0.6
83 *
84 *-------------------------------------------------------------------------------------*/
85
86 function pre_get_posts($query)
87 {
88 global $pagenow;
89 if($pagenow == "edit.php" && isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'acf')
90 {
91 $query->query_vars['posts_per_page'] = 99;
92 }
93 return $query;
94 }
95
96
97
98 /*--------------------------------------------------------------------------------------
99 *
100 * setup_fields
101 *
102 * @author Elliot Condon
103 * @since 1.0.0
104 *
105 *-------------------------------------------------------------------------------------*/
106
107 function setup_fields()
108 {
109 // vars
110 $return = array();
111
112 // include parent field
113 include_once('core/fields/acf_field.php');
114
115 // include child fields
116 include_once('core/fields/acf_field.php');
117 include_once('core/fields/text.php');
118 include_once('core/fields/textarea.php');
119 include_once('core/fields/wysiwyg.php');
120 include_once('core/fields/image.php');
121 include_once('core/fields/file.php');
122 include_once('core/fields/select.php');
123 include_once('core/fields/checkbox.php');
124 include_once('core/fields/radio.php');
125 include_once('core/fields/true_false.php');
126 include_once('core/fields/page_link.php');
127 include_once('core/fields/post_object.php');
128 include_once('core/fields/relationship.php');
129 include_once('core/fields/date_picker/date_picker.php');
130 include_once('core/fields/color_picker.php');
131
132 $return['text'] = new acf_Text($this);
133 $return['textarea'] = new acf_Textarea($this);
134 $return['wysiwyg'] = new acf_Wysiwyg($this);
135 $return['image'] = new acf_Image($this);
136 $return['file'] = new acf_File($this);
137 $return['select'] = new acf_Select($this);
138 $return['checkbox'] = new acf_Checkbox($this);
139 $return['radio'] = new acf_Radio($this);
140 $return['true_false'] = new acf_True_false($this);
141 $return['page_link'] = new acf_Page_link($this);
142 $return['post_object'] = new acf_Post_object($this);
143 $return['relationship'] = new acf_Relationship($this);
144 $return['date_picker'] = new acf_Date_picker($this);
145 $return['color_picker'] = new acf_Color_picker($this);
146
147 if($this->is_field_unlocked('repeater'))
148 {
149 include_once('core/fields/repeater.php');
150 $return['repeater'] = new acf_Repeater($this);
151 }
152
153 if($this->is_field_unlocked('flexible_content'))
154 {
155 include_once('core/fields/flexible_content.php');
156 $return['flexible_content'] = new acf_flexible_content($this);
157 }
158
159 // hook to load in third party fields
160 $custom = apply_filters('acf_register_field',array());
161
162 if(!empty($custom))
163 {
164 foreach($custom as $v)
165 {
166 //var_dump($v['url']);
167 include($v['url']);
168 $name = $v['class'];
169 $custom_field = new $name($this);
170 $return[$custom_field->name] = $custom_field;
171 }
172 }
173
174 $this->fields = $return;
175 }
176
177
178 /*--------------------------------------------------------------------------------------
179 *
180 * setup_options_page
181 *
182 * @author Elliot Condon
183 * @since 1.0.0
184 *
185 *-------------------------------------------------------------------------------------*/
186
187 function setup_options_page()
188 {
189 include_once('core/options_page.php');
190 $this->options_page = new Options_page($this);
191 }
192
193
194 /*--------------------------------------------------------------------------------------
195 *
196 * admin_menu
197 *
198 * @author Elliot Condon
199 * @since 1.0.0
200 *
201 *-------------------------------------------------------------------------------------*/
202
203 function admin_menu() {
204
205 // add acf page to options menu
206 add_utility_page(__("Custom Fields",'acf'), __("Custom Fields",'acf'), 'manage_options', 'edit.php?post_type=acf');
207 add_submenu_page('edit.php?post_type=acf', __('Settings','wp3i'), __('Settings','wp3i'), 'manage_options','acf-settings',array($this,'admin_page_settings'));
208 add_submenu_page('edit.php?post_type=acf', __('Upgrade','wp3i'), __('Upgrade','wp3i'), 'manage_options','acf-upgrade',array($this,'admin_page_upgrade'));
209
210 }
211
212
213 /*--------------------------------------------------------------------------------------
214 *
215 * Init
216 *
217 * @author Elliot Condon
218 * @since 1.0.0
219 *
220 *-------------------------------------------------------------------------------------*/
221
222 function init()
223 {
224 include('core/actions/init.php');
225 }
226
227
228 /*--------------------------------------------------------------------------------------
229 *
230 * admin_page_upgrade
231 *
232 * @author Elliot Condon
233 * @since 1.0.0
234 *
235 *-------------------------------------------------------------------------------------*/
236
237 function admin_page_upgrade()
238 {
239 include('core/admin/upgrade.php');
240 }
241
242 /*--------------------------------------------------------------------------------------
243 *
244 * admin_page_settings
245 *
246 * @author Elliot Condon
247 * @since 3.0.5
248 *
249 *-------------------------------------------------------------------------------------*/
250
251 function admin_page_settings()
252 {
253 include('core/admin/page_settings.php');
254 }
255
256
257 /*--------------------------------------------------------------------------------------
258 *
259 * ajax_upgrade
260 *
261 * @author Elliot Condon
262 * @since 1.0.0
263 *
264 *-------------------------------------------------------------------------------------*/
265
266 function upgrade_ajax()
267 {
268 include('core/admin/upgrade_ajax.php');
269 }
270
271
272
273 /*--------------------------------------------------------------------------------------
274 *
275 * admin_print_scripts / admin_print_styles
276 *
277 * @author Elliot Condon
278 * @since 3.0.0
279 *
280 *-------------------------------------------------------------------------------------*/
281
282 function admin_print_scripts() {
283
284 // thickbox
285 if($GLOBALS['pagenow'] == 'edit.php' && isset($GLOBALS['post_type']) && $GLOBALS['post_type'] == 'acf')
286 {
287 wp_enqueue_script( 'jquery' );
288 wp_enqueue_script( 'thickbox' );
289 }
290
291 if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php')))
292 {
293 if($GLOBALS['post_type'] == 'acf')
294 {
295 // hmmm
296 }
297 else
298 {
299 // fields admin_head
300 foreach($this->fields as $field)
301 {
302 $this->fields[$field->name]->admin_print_scripts();
303 }
304 }
305 }
306
307 }
308
309 function admin_print_styles() {
310
311 // thickbox
312 if($GLOBALS['pagenow'] == 'edit.php' && isset($GLOBALS['post_type']) && $GLOBALS['post_type'] == 'acf')
313 {
314 wp_enqueue_style( 'thickbox' );
315 }
316
317 if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php')))
318 {
319 if($GLOBALS['post_type'] == 'acf')
320 {
321 // hmmm
322 }
323 else
324 {
325 // fields admin_head
326 foreach($this->fields as $field)
327 {
328 $this->fields[$field->name]->admin_print_styles();
329 }
330 }
331 }
332
333 }
334
335
336 /*--------------------------------------------------------------------------------------
337 *
338 * admin_head
339 *
340 * @author Elliot Condon
341 * @since 1.0.0
342 *
343 *-------------------------------------------------------------------------------------*/
344
345 function admin_head()
346 {
347 // vars
348 global $post, $pagenow;
349
350 // hide upgrade page from nav
351 echo '<style type="text/css">
352 #toplevel_page_edit-post_type-acf a[href="edit.php?post_type=acf&page=acf-upgrade"]{ display:none; }
353 #toplevel_page_edit-post_type-acf .wp-menu-image { background: url("../wp-admin/images/menu.png") no-repeat scroll 0 -33px transparent; }
354 #toplevel_page_edit-post_type-acf .wp-menu-image img { display:none; }
355 </style>';
356
357
358 // only add to edit pages
359 if(in_array($pagenow, array('post.php', 'post-new.php')))
360 {
361 // edit field
362 if($GLOBALS['post_type'] == 'acf')
363 {
364 echo '<script type="text/javascript" src="'.$this->dir.'/js/fields.js" ></script>';
365 echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/global.css" />';
366 echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/fields.css" />';
367
368 add_meta_box('acf_fields', 'Fields', array($this, 'meta_box_fields'), 'acf', 'normal', 'high');
369 add_meta_box('acf_location', 'Location </span><span class="description">- Add Fields to Edit Screens', array($this, 'meta_box_location'), 'acf', 'normal', 'high');
370 add_meta_box('acf_options', 'Options</span><span class="description">- Customise the edit page', array($this, 'meta_box_options'), 'acf', 'normal', 'high');
371
372 }
373 else
374 {
375
376 // find post type and add wysiwyg support
377 $post_type = get_post_type($post);
378
379 // get style for page
380 $metabox_ids = $this->get_input_metabox_ids(array('post_id' => $post->ID), false);
381 $style = isset($metabox_ids[0]) ? $this->get_input_style($metabox_ids[0]) : '';
382 echo '<style type="text/css" id="acf_style" >' .$style . '</style>';
383
384 // fields admin_head
385 foreach($this->fields as $field)
386 {
387 $this->fields[$field->name]->admin_head();
388 }
389
390 // add css + javascript
391 echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/global.css" />';
392 echo '<link rel="stylesheet" type="text/css" href="'.$this->dir.'/css/input.css" />';
393 echo '<script type="text/javascript" src="'.$this->dir.'/js/input.js" ></script>';
394 echo '<style type="text/css">.acf_postbox, .postbox[id*="acf_"] { display: none; }</style>';
395 echo '<script type="text/javascript">acf.validation_message = "' . __("Validation Failed. One or more fields below are required.",'acf') . '";</script>';
396
397 // get acf's
398 $acfs = $this->get_field_groups();
399 if($acfs)
400 {
401 foreach($acfs as $acf)
402 {
403 // hide / show
404 $show = in_array($acf['id'], $metabox_ids) ? "true" : "false";
405
406 // add meta box
407 add_meta_box(
408 'acf_' . $acf['id'],
409 $acf['title'],
410 array($this, 'meta_box_input'),
411 $post_type,
412 $acf['options']['position'],
413 'default',
414 array( 'fields' => $acf['fields'], 'options' => $acf['options'], 'show' => $show )
415 );
416 }
417 }
418 }
419 }
420 }
421
422
423 /*--------------------------------------------------------------------------------------
424 *
425 * get_field_groups
426 *
427 * This function returns an array of post objects found in the get_pages and the
428 * register_field_group calls.
429 *
430 * @author Elliot Condon
431 * @since 3.0.6
432 *
433 *-------------------------------------------------------------------------------------*/
434
435 function get_field_groups()
436 {
437 // return cache
438 $cache = wp_cache_get('acf_field_groups');
439 if($cache != false)
440 {
441 return $cache;
442 }
443
444 // vars
445 $acfs = array();
446
447 // get acf's
448 $result = get_pages(array(
449 'numberposts' => -1,
450 'post_type' => 'acf',
451 'sort_column' => 'menu_order',
452 'order' => 'ASC',
453 ));
454
455 // populate acfs
456 if($result)
457 {
458 foreach($result as $acf)
459 {
460 $acfs[] = array(
461 'id' => $acf->ID,
462 'title' => get_the_title($acf->ID),
463 'fields' => $this->get_acf_fields($acf->ID),
464 'location' => $this->get_acf_location($acf->ID),
465 'options' => $this->get_acf_options($acf->ID),
466 'menu_order' => $acf->menu_order,
467 );
468 }
469 }
470
471 // hook to load in registered field groups
472 $acfs = apply_filters('acf_register_field_group', $acfs);
473
474 // update cache
475 wp_cache_set('acf_field_groups', $acfs);
476
477 // return
478 if(empty($acfs))
479 {
480 return false;
481 }
482 return $acfs;
483 }
484
485
486 /*--------------------------------------------------------------------------------------
487 *
488 * admin_footer
489 *
490 * @author Elliot Condon
491 * @since 1.0.0
492 *
493 *-------------------------------------------------------------------------------------*/
494
495 function admin_footer()
496 {
497 // acf edit list
498 if($GLOBALS['pagenow'] == 'edit.php' && isset($GLOBALS['post_type']) && $GLOBALS['post_type'] == 'acf')
499 {
500 include('core/admin/page_acf.php');
501 }
502
503 // input meta boxes
504 if(in_array($GLOBALS['pagenow'], array('post.php', 'post-new.php')) && $GLOBALS['post_type'] != 'acf')
505 {
506 //wp_preload_dialogs( array( 'plugins' => 'safari,inlinepopups,spellchecker,paste,wordpress,media,fullscreen,wpeditimage,wpgallery,tabfocus' ) );
507 ?>
508 <script type="text/javascript">
509 (function($){
510
511 // add classes
512 $('#poststuff .postbox[id*="acf_"]').addClass('acf_postbox');
513 $('#adv-settings label[for*="acf_"]').addClass('acf_hide_label');
514
515 // hide acf stuff
516 $('#poststuff .acf_postbox').hide();
517 $('#adv-settings .acf_hide_label').hide();
518
519 // loop through acf metaboxes
520 $('#poststuff .postbox.acf_postbox').each(function(){
521
522 // vars
523 var options = $(this).find('.inside > .options');
524 var show = options.attr('data-show');
525 var layout = options.attr('data-layout');
526 var id = $(this).attr('id').replace('acf_', '');
527
528 // layout
529 $(this).addClass('acf_postbox').addClass(layout);
530
531 // show / hide
532 if(show == 'true')
533 {
534 $(this).show();
535 $('#adv-settings .acf_hide_label[for="acf_' + id + '-hide"]').show();
536 }
537
538 });
539
540 })(jQuery);
541 </script>
542 <?php
543 }
544
545 }
546
547
548 /*--------------------------------------------------------------------------------------
549 *
550 * meta_box_fields
551 *
552 * @author Elliot Condon
553 * @since 1.0.0
554 *
555 *-------------------------------------------------------------------------------------*/
556
557 function meta_box_fields()
558 {
559 include('core/admin/meta_box_fields.php');
560 }
561
562
563 /*--------------------------------------------------------------------------------------
564 *
565 * meta_box_location
566 *
567 * @author Elliot Condon
568 * @since 1.0.0
569 *
570 *-------------------------------------------------------------------------------------*/
571
572 function meta_box_location()
573 {
574 include('core/admin/meta_box_location.php');
575 }
576
577
578 /*--------------------------------------------------------------------------------------
579 *
580 * meta_box_options
581 *
582 * @author Elliot Condon
583 * @since 1.0.0
584 *
585 *-------------------------------------------------------------------------------------*/
586
587 function meta_box_options()
588 {
589 include('core/admin/meta_box_options.php');
590 }
591
592
593 /*--------------------------------------------------------------------------------------
594 *
595 * meta_box_input
596 *
597 * @author Elliot Condon
598 * @since 1.0.0
599 *
600 *-------------------------------------------------------------------------------------*/
601
602 function meta_box_input($post, $args)
603 {
604 include('core/admin/meta_box_input.php');
605 }
606
607
608 /*--------------------------------------------------------------------------------------
609 *
610 * get_acf_fields
611 * - returns an array of fields for a acf object
612 *
613 * @author Elliot Condon
614 * @since 1.0.0
615 *
616 *-------------------------------------------------------------------------------------*/
617
618 function get_acf_fields($post_id)
619 {
620 // vars
621 $return = array();
622 $keys = get_post_custom_keys($post_id);
623
624 if($keys)
625 {
626 foreach($keys as $key)
627 {
628 if(strpos($key, 'field_') !== false)
629 {
630 $field = $this->get_acf_field($key, $post_id);
631
632 $return[$field['order_no']] = $field;
633 }
634 }
635
636 ksort($return);
637 }
638 // return fields
639 return $return;
640
641 }
642
643
644 /*--------------------------------------------------------------------------------------
645 *
646 * get_acf_field
647 * - returns a field
648 *
649 * @author Elliot Condon
650 * @since 1.0.0
651 *
652 *-------------------------------------------------------------------------------------*/
653
654 function get_acf_field($field_name, $post_id = false)
655 {
656 // vars
657 $post_id = $post_id ? $post_id : $this->get_post_meta_post_id($field_name);
658 $field = false;
659
660 // if this acf ($post_id) is trashed don't use it's fields
661 if(get_post_status($post_id) != "trash")
662 {
663 $field = get_post_meta($post_id, $field_name, true);
664 }
665
666 // field could be registered via php, and not in db at all!
667 if(!$field)
668 {
669 // hook to load in registered field groups
670 $acfs = apply_filters('acf_register_field_group', array());
671 if($acfs)
672 {
673 // loop through acfs
674 foreach($acfs as $acf)
675 {
676 // loop through fields
677 if($acf['fields'])
678 {
679 foreach($acf['fields'] as $field)
680 {
681 if($field['key'] == $field_name)
682 {
683 return $field;
684 }
685 }
686 }
687 // if($acf['fields'])
688 }
689 // foreach($acfs as $acf)
690 }
691 // if($acfs)
692 }
693 // if(!$field)
694
695 return $field;
696
697 }
698
699
700 /*--------------------------------------------------------------------------------------
701 *
702 * get_post_meta_post_id
703 * - returns the post_id for a meta_key
704 *
705 * @author Elliot Condon
706 * @since 1.0.0
707 *
708 *-------------------------------------------------------------------------------------*/
709
710 function get_post_meta_post_id($field_name)
711 {
712 global $wpdb;
713 $post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $field_name) );
714
715 if($post_id) return (int)$post_id;
716
717 return false;
718 }
719
720
721 /*--------------------------------------------------------------------------------------
722 *
723 * create_field
724 *
725 * @author Elliot Condon
726 * @since 1.0.0
727 *
728 *-------------------------------------------------------------------------------------*/
729
730 function create_field($field)
731 {
732 if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']]))
733 {
734 _e('Error: Field Type does not exist!','acf');
735 return false;
736 }
737
738 // defaults
739 if(!isset($field['class'])) $field['class'] = $field['type'];
740
741 $this->fields[$field['type']]->create_field($field);
742 }
743
744
745 /*--------------------------------------------------------------------------------------
746 *
747 * get_acf_location
748 *
749 * @author Elliot Condon
750 * @since 1.0.0
751 *
752 *-------------------------------------------------------------------------------------*/
753
754 function get_acf_location($post_id)
755 {
756 // vars
757 $return = array(
758 'rules' => array(),
759 'allorany' => get_post_meta($post_id, 'allorany', true) ? get_post_meta($post_id, 'allorany', true) : 'all',
760 );
761
762 // get all fields
763 $rules = get_post_meta($post_id, 'rule', false);
764
765 if($rules)
766 {
767 foreach($rules as $rule)
768 {
769 $return['rules'][$rule['order_no']] = $rule;
770 }
771 }
772
773 ksort($return['rules']);
774
775 // return fields
776 return $return;
777
778 }
779
780
781 /*--------------------------------------------------------------------------------------
782 *
783 * get_acf_options
784 *
785 * @author Elliot Condon
786 * @since 1.0.0
787 *
788 *-------------------------------------------------------------------------------------*/
789
790 function get_acf_options($post_id)
791 {
792 // defaults
793 $options = array(
794 'position' => get_post_meta($post_id, 'position', true) ? get_post_meta($post_id, 'position', true) : 'normal',
795 'layout' => get_post_meta($post_id, 'layout', true) ? get_post_meta($post_id, 'layout', true) : 'default',
796 'show_on_page' => get_post_meta($post_id, 'show_on_page', true) ? get_post_meta($post_id, 'show_on_page', true) : array(),
797 );
798
799 // If this is a new acf, there will be no custom keys!
800 if(!get_post_custom_keys($post_id))
801 {
802 $options['show_on_page'] = array('the_content', 'discussion', 'custom_fields', 'comments', 'slug', 'author');
803 }
804
805 // return
806 return $options;
807 }
808
809
810 /*--------------------------------------------------------------------------------------
811 *
812 * save_post
813 *
814 * @author Elliot Condon
815 * @since 1.0.0
816 *
817 *-------------------------------------------------------------------------------------*/
818
819 function save_post($post_id)
820 {
821
822 // do not save if this is an auto save routine
823 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
824
825 // only save once! WordPress save's twice for some strange reason.
826 global $flag;
827 if ($flag != 0) return $post_id;
828 $flag = 1;
829
830 // set post ID if is a revision
831 if(wp_is_post_revision($post_id))
832 {
833 $post_id = wp_is_post_revision($post_id);
834 }
835
836 // include save files
837 if(isset($_POST['save_fields']) && $_POST['save_fields'] == 'true') include('core/actions/save_fields.php');
838 if(isset($_POST['save_input']) && $_POST['save_input'] == 'true') include('core/actions/save_input.php');
839
840 }
841
842
843 /*--------------------------------------------------------------------------------------
844 *
845 * save_name
846 * - this function intercepts the acf post obejct and adds an "acf_" to the start of
847 * it's name to stop conflicts between acf's and page's urls
848 *
849 * @author Elliot Condon
850 * @since 1.0.0
851 *
852 *-------------------------------------------------------------------------------------*/
853
854 function save_name($name)
855 {
856 if (isset($_POST['post_type']) && $_POST['post_type'] == 'acf')
857 {
858 $name = 'acf_' . sanitize_title_with_dashes($_POST['post_title']);
859 }
860 return $name;
861 }
862
863
864 /*--------------------------------------------------------------------------------------
865 *
866 * get_value
867 *
868 * @author Elliot Condon
869 * @since 3.0.0
870 *
871 *-------------------------------------------------------------------------------------*/
872
873 function get_value($post_id, $field)
874 {
875 if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']]))
876 {
877 return '';
878 }
879
880 return $this->fields[$field['type']]->get_value($post_id, $field);
881 }
882
883
884 /*--------------------------------------------------------------------------------------
885 *
886 * get_value_for_api
887 *
888 * @author Elliot Condon
889 * @since 3.0.0
890 *
891 *-------------------------------------------------------------------------------------*/
892
893 function get_value_for_api($post_id, $field)
894 {
895 if(!isset($this->fields[$field['type']]) || !is_object($this->fields[$field['type']]))
896 {
897 return '';
898 }
899
900 return $this->fields[$field['type']]->get_value_for_api($post_id, $field);
901 }
902
903
904 /*--------------------------------------------------------------------------------------
905 *
906 * update_value
907 *
908 * @author Elliot Condon
909 * @since 3.0.0
910 *
911 *-------------------------------------------------------------------------------------*/
912
913 function update_value($post_id, $field, $value)
914 {
915 $this->fields[$field['type']]->update_value($post_id, $field, $value);
916 }
917
918
919 /*--------------------------------------------------------------------------------------
920 *
921 * update_field
922 *
923 * @author Elliot Condon
924 * @since 3.0.0
925 *
926 *-------------------------------------------------------------------------------------*/
927
928 function update_field($post_id, $field)
929 {
930 // format the field (select, repeater, etc)
931 $field = $this->pre_save_field($field);
932
933 // save it!
934 update_post_meta($post_id, $field['key'], $field);
935 }
936
937
938 /*--------------------------------------------------------------------------------------
939 *
940 * pre_save_field
941 *
942 * @author Elliot Condon
943 * @since 3.0.0
944 *
945 *-------------------------------------------------------------------------------------*/
946
947 function pre_save_field($field)
948 {
949 // format the field (select, repeater, etc)
950 return $this->fields[$field['type']]->pre_save_field($field);
951 }
952
953
954 /*--------------------------------------------------------------------------------------
955 *
956 * format_value_for_input
957 *
958 * @author Elliot Condon
959 * @since 3.0.0
960 *
961 *-------------------------------------------------------------------------------------*/
962
963 //function format_value_for_input($value, $field)
964 //{
965 // return $this->fields[$field['type']]->format_value_for_input($value, $field);
966 //}
967
968
969 /*--------------------------------------------------------------------------------------
970 *
971 * format_value_for_api
972 *
973 * @author Elliot Condon
974 * @since 3.0.0
975 *
976 *-------------------------------------------------------------------------------------*/
977
978 function format_value_for_api($value, $field)
979 {
980 if(!isset($this))
981 {
982 // called form api!
983
984 }
985 else
986 {
987 // called from object
988 }
989 return $this->fields[$field['type']]->format_value_for_api($value, $field);
990 }
991
992
993 /*--------------------------------------------------------------------------------------
994 *
995 * create_format_data
996 *
997 * @author Elliot Condon
998 * @since 3.0.0
999 *
1000 *-------------------------------------------------------------------------------------*/
1001
1002 function create_format_data($field)
1003 {
1004 return $this->fields[$field['type']]->create_format_data($field);
1005 }
1006
1007
1008 /*--------------------------------------------------------------------------------------
1009 *
1010 * get_input_metabox_ids
1011 * - called by function.fields to hide / show metaboxes
1012 *
1013 * @author Elliot Condon
1014 * @since 2.0.5
1015 *
1016 *-------------------------------------------------------------------------------------*/
1017
1018 function get_input_metabox_ids($overrides = array(), $json = true)
1019 {
1020 // overrides
1021 if(isset($_POST))
1022 {
1023 if(isset($_POST['post_id']) && $_POST['post_id'] != 'false') $overrides['post_id'] = $_POST['post_id'];
1024 if(isset($_POST['page_template']) && $_POST['page_template'] != 'false') $overrides['page_template'] = $_POST['page_template'];
1025 if(isset($_POST['page_parent']) && $_POST['page_parent'] != 'false') $overrides['page_parent'] = $_POST['page_parent'];
1026 if(isset($_POST['page_type']) && $_POST['page_type'] != 'false') $overrides['page_type'] = $_POST['page_type'];
1027 if(isset($_POST['page']) && $_POST['page'] != 'false') $overrides['page'] = $_POST['page'];
1028 if(isset($_POST['post']) && $_POST['post'] != 'false') $overrides['post'] = $_POST['post'];
1029 if(isset($_POST['post_category']) && $_POST['post_category'] != 'false') $overrides['post_category'] = $_POST['post_category'];
1030 if(isset($_POST['post_format']) && $_POST['post_format'] != 'false') $overrides['post_format'] = $_POST['post_format'];
1031 if(isset($_POST['taxonomy']) && $_POST['taxonomy'] != 'false') $overrides['taxonomy'] = $_POST['taxonomy'];
1032 }
1033
1034 // create post object to match against
1035 $post = isset($overrides['post_id']) ? get_post($_POST['post_id']) : false;
1036
1037 // find all acf objects
1038 $acfs = $this->get_field_groups();
1039
1040 // blank array to hold acfs
1041 $return = array();
1042
1043 if($acfs)
1044 {
1045 foreach($acfs as $acf)
1046 {
1047 $add_box = false;
1048
1049 if($acf['location']['allorany'] == 'all')
1050 {
1051 // ALL
1052 $add_box = true;
1053
1054 if($acf['location']['rules'])
1055 {
1056 foreach($acf['location']['rules'] as $rule)
1057 {
1058
1059 // if any rules dont return true, dont add this acf
1060 if(!$this->match_location_rule($post, $rule, $overrides))
1061 {
1062 $add_box = false;
1063 }
1064 }
1065 }
1066
1067 }
1068 elseif($acf['location']['allorany'] == 'any')
1069 {
1070 // ANY
1071
1072 $add_box = false;
1073
1074 if($acf['location']['rules'])
1075 {
1076 foreach($acf['location']['rules'] as $rule)
1077 {
1078 // if any rules return true, add this acf
1079 if($this->match_location_rule($post, $rule, $overrides))
1080 {
1081 $add_box = true;
1082 }
1083 }
1084 }
1085 }
1086
1087 if($add_box == true)
1088 {
1089 $return[] = $acf['id'];
1090 }
1091
1092 }
1093 }
1094
1095 if($json)
1096 {
1097 echo json_encode($return);
1098 die;
1099 }
1100 else
1101 {
1102 return $return;
1103 }
1104
1105
1106 }
1107
1108
1109 /*--------------------------------------------------------------------------------------
1110 *
1111 * get_input_style
1112 * - called by admin_head to generate acf css style (hide other metaboxes)
1113 *
1114 * @author Elliot Condon
1115 * @since 2.0.5
1116 * @updated 3.0.6
1117 *
1118 *-------------------------------------------------------------------------------------*/
1119
1120 function get_input_style($acf_id = false)
1121 {
1122 // vars
1123 $acfs = $this->get_field_groups();
1124 $html = "";
1125
1126 // find acf
1127 if($acfs)
1128 {
1129 foreach($acfs as $acf)
1130 {
1131 if($acf['id'] == $acf_id)
1132 {
1133 // add style to html
1134 if(!in_array('the_content',$acf['options']['show_on_page']))
1135 {
1136 $html .= '#postdivrich {display: none;} ';
1137 }
1138 if(!in_array('custom_fields',$acf['options']['show_on_page']))
1139 {
1140 $html .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } ';
1141 }
1142 if(!in_array('discussion',$acf['options']['show_on_page']))
1143 {
1144 $html .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} ';
1145 }
1146 if(!in_array('comments',$acf['options']['show_on_page']))
1147 {
1148 $html .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} ';
1149 }
1150 if(!in_array('slug',$acf['options']['show_on_page']))
1151 {
1152 $html .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} ';
1153 }
1154 if(!in_array('author',$acf['options']['show_on_page']))
1155 {
1156 $html .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} ';
1157 }
1158
1159 break;
1160 }
1161 // if($acf['id'] == $acf_id)
1162 }
1163 // foreach($acfs as $acf)
1164 }
1165 //if($acfs)
1166
1167 return $html;
1168 }
1169
1170
1171 /*--------------------------------------------------------------------------------------
1172 *
1173 * the_input_style
1174 * - called by function.fields to hide / show other metaboxes
1175 *
1176 * @author Elliot Condon
1177 * @since 2.0.5
1178 *
1179 *-------------------------------------------------------------------------------------*/
1180
1181 function the_input_style()
1182 {
1183 // overrides
1184 if(isset($_POST['acf_id']))
1185 {
1186 echo $this->get_input_style($_POST['acf_id']);
1187 }
1188
1189 die;
1190
1191 }
1192
1193
1194 /*--------------------------------------------------------------------------------------
1195 *
1196 * match_location_rule
1197 *
1198 * @author Elliot Condon
1199 * @since 2.0.0
1200 *
1201 *-------------------------------------------------------------------------------------*/
1202
1203 function match_location_rule($post, $rule, $overrides = array())
1204 {
1205
1206 if(!$post)
1207 {
1208 // post is false! that's okay if the rule is for user_type or options_page
1209 if($rule['param'] != 'user_type' && $rule['param'] != 'options_page')
1210 {
1211 return false;
1212 }
1213 }
1214
1215
1216
1217
1218 switch ($rule['param']) {
1219
1220 // POST TYPE
1221 case "post_type":
1222
1223 $post_type = isset($overrides['post_type']) ? $overrides['post_type'] : get_post_type($post);
1224
1225 if($rule['operator'] == "==")
1226 {
1227 if($post_type == $rule['value'])
1228 {
1229 return true;
1230 }
1231
1232 return false;
1233 }
1234 elseif($rule['operator'] == "!=")
1235 {
1236 if($post_type != $rule['value'])
1237 {
1238 return true;
1239 }
1240
1241 return false;
1242 }
1243
1244 break;
1245
1246 // PAGE
1247 case "page":
1248
1249 $page = isset($overrides['page']) ? $overrides['page'] : $post->ID;
1250
1251 if($rule['operator'] == "==")
1252 {
1253 if($page == $rule['value'])
1254 {
1255 return true;
1256 }
1257
1258 return false;
1259 }
1260 elseif($rule['operator'] == "!=")
1261 {
1262 if($page != $rule['value'])
1263 {
1264 return true;
1265 }
1266
1267 return false;
1268 }
1269
1270 break;
1271
1272 // PAGE
1273 case "page_type":
1274
1275 $page_type = isset($overrides['page_type']) ? $overrides['page_type'] : $post->post_parent;
1276
1277 if($rule['operator'] == "==")
1278 {
1279 if($rule['value'] == "parent" && $page_type == "0")
1280 {
1281 return true;
1282 }
1283
1284 if($rule['value'] == "child" && $page_type != "0")
1285 {
1286 return true;
1287 }
1288
1289 return false;
1290 }
1291 elseif($rule['operator'] == "!=")
1292 {
1293 if($rule['value'] == "parent" && $page_type != "0")
1294 {
1295 return true;
1296 }
1297
1298 if($rule['value'] == "child" && $page_type == "0")
1299 {
1300 return true;
1301 }
1302
1303 return false;
1304 }
1305
1306 break;
1307
1308 // PAGE PARENT
1309 case "page_parent":
1310
1311 $page_parent = isset($overrides['page_parent']) ? $overrides['page_parent'] : $post->post_parent;
1312
1313 if($rule['operator'] == "==")
1314 {
1315 if($page_parent == $rule['value'])
1316 {
1317 return true;
1318 }
1319
1320 return false;
1321
1322 }
1323 elseif($rule['operator'] == "!=")
1324 {
1325 if($page_parent != $rule['value'])
1326 {
1327 return true;
1328 }
1329
1330 return false;
1331 }
1332
1333 break;
1334
1335 // PAGE
1336 case "page_template":
1337
1338 $page_template = isset($overrides['page_template']) ? $overrides['page_template'] : get_post_meta($post->ID,'_wp_page_template',true);
1339
1340 if($rule['operator'] == "==")
1341 {
1342 if($page_template == $rule['value'])
1343 {
1344 return true;
1345 }
1346
1347 if($rule['value'] == "default" && !$page_template)
1348 {
1349 return true;
1350 }
1351
1352 return false;
1353 }
1354 elseif($rule['operator'] == "!=")
1355 {
1356 if($page_template != $rule['value'])
1357 {
1358 return true;
1359 }
1360
1361 return false;
1362 }
1363
1364 break;
1365
1366 // POST
1367 case "post":
1368
1369 $post_id = isset($overrides['post']) ? $overrides['post'] : $post->ID;
1370
1371 if($rule['operator'] == "==")
1372 {
1373 if($post_id == $rule['value'])
1374 {
1375 return true;
1376 }
1377
1378 return false;
1379 }
1380 elseif($rule['operator'] == "!=")
1381 {
1382 if($post_id != $rule['value'])
1383 {
1384 return true;
1385 }
1386
1387 return false;
1388 }
1389
1390 break;
1391
1392 // POST CATEGORY
1393 case "post_category":
1394
1395 $cats = array();
1396
1397 if(isset($overrides['post_category']))
1398 {
1399 $cats = $overrides['post_category'];
1400 }
1401 else
1402 {
1403 $all_cats = get_the_category($post->ID);
1404 foreach($all_cats as $cat)
1405 {
1406 $cats[] = $cat->term_id;
1407 }
1408 }
1409 if($rule['operator'] == "==")
1410 {
1411 if($cats)
1412 {
1413 if(in_array($rule['value'], $cats))
1414 {
1415 return true;
1416 }
1417 }
1418
1419 return false;
1420 }
1421 elseif($rule['operator'] == "!=")
1422 {
1423 if($cats)
1424 {
1425 if(!in_array($rule['value'], $cats))
1426 {
1427 return true;
1428 }
1429 }
1430
1431 return false;
1432 }
1433
1434 break;
1435
1436 // PAGE PARENT
1437 /*
1438 case "post_format":
1439
1440 $post_format = isset($overrides['post_format']) ? $overrides['post_format'] : get_post_format();
1441
1442 if($rule['operator'] == "==")
1443 {
1444 if($post_format == $rule['value'])
1445 {
1446 return true;
1447 }
1448
1449 return false;
1450
1451 }
1452 elseif($post_format == "!=")
1453 {
1454 if($post->post_parent != $rule['value'])
1455 {
1456 return true;
1457 }
1458
1459 return false;
1460 }
1461
1462 break;
1463 */
1464
1465 // USER TYPE
1466 case "user_type":
1467
1468 if($rule['operator'] == "==")
1469 {
1470 if(current_user_can($rule['value']))
1471 {
1472 return true;
1473 }
1474
1475 return false;
1476 }
1477 elseif($rule['operator'] == "!=")
1478 {
1479 if(!current_user_can($rule['value']))
1480 {
1481 return true;
1482 }
1483
1484 return false;
1485 }
1486
1487 break;
1488
1489 // Options Page
1490 case "options_page":
1491
1492 if($rule['operator'] == "==")
1493 {
1494 if(get_admin_page_title() == $rule['value'])
1495 {
1496 return true;
1497 }
1498
1499 return false;
1500 }
1501 elseif($rule['operator'] == "!=")
1502 {
1503 if(get_admin_page_title() != $rule['value'])
1504 {
1505 return true;
1506 }
1507
1508 return false;
1509 }
1510
1511 break;
1512
1513
1514 // Post Format
1515 case "post_format":
1516
1517
1518 $post_format = isset($overrides['post_format']) ? has_post_format($overrides['post_format'],$post->ID) : has_post_format($rule['value'],$post->ID);
1519
1520 if($rule['operator'] == "==")
1521 {
1522 if($post_format)
1523 {
1524 return true;
1525 }
1526
1527 return false;
1528 }
1529 elseif($rule['operator'] == "!=")
1530 {
1531 if(!$post_format)
1532 {
1533 return true;
1534 }
1535
1536 return false;
1537 }
1538
1539 break;
1540
1541 // Taxonomy
1542 case "taxonomy":
1543
1544 $terms = array();
1545
1546 if(isset($overrides['taxonomy']))
1547 {
1548 $terms = $overrides['taxonomy'];
1549 }
1550 else
1551 {
1552 $taxonomies = get_object_taxonomies($post->post_type);
1553 if($taxonomies)
1554 {
1555 foreach($taxonomies as $tax)
1556 {
1557 $all_terms = get_the_terms($post->ID, $tax);
1558 if($all_terms)
1559 {
1560 foreach($all_terms as $all_term)
1561 {
1562 $terms[] = $all_term->term_id;
1563 }
1564 }
1565 }
1566 }
1567 }
1568
1569 if($rule['operator'] == "==")
1570 {
1571 if($terms)
1572 {
1573 if(in_array($rule['value'], $terms))
1574 {
1575 return true;
1576 }
1577 }
1578
1579 return false;
1580 }
1581 elseif($rule['operator'] == "!=")
1582 {
1583 if($terms)
1584 {
1585 if(!in_array($rule['value'], $terms))
1586 {
1587 return true;
1588 }
1589 }
1590
1591 return false;
1592 }
1593
1594
1595 break;
1596
1597 }
1598
1599 }
1600
1601
1602 /*--------------------------------------------------------------------------------------
1603 *
1604 * is_field_unlocked
1605 *
1606 * @author Elliot Condon
1607 * @since 3.0.0
1608 *
1609 *-------------------------------------------------------------------------------------*/
1610
1611 function is_field_unlocked($field_name)
1612 {
1613 switch ($field_name) {
1614 case 'repeater':
1615 if(md5($this->get_license_key($field_name)) == "bbefed143f1ec106ff3a11437bd73432"){ return true; }else{ return false; }
1616 break;
1617 case 'options_page':
1618 if(md5($this->get_license_key($field_name)) == "1fc8b993548891dc2b9a63ac057935d8"){ return true; }else{ return false; }
1619 break;
1620 case 'flexible_content':
1621 if(md5($this->get_license_key($field_name)) == "d067e06c2b4b32b1c1f5b6f00e0d61d6"){ return true; }else{ return false; }
1622 break;
1623 }
1624 }
1625
1626 /*--------------------------------------------------------------------------------------
1627 *
1628 * is_field_unlocked
1629 *
1630 * @author Elliot Condon
1631 * @since 3.0.0
1632 *
1633 *-------------------------------------------------------------------------------------*/
1634
1635 function get_license_key($field_name)
1636 {
1637 return get_option('acf_' . $field_name . '_ac');
1638 }
1639
1640
1641 /*--------------------------------------------------------------------------------------
1642 *
1643 * admin_message
1644 *
1645 * @author Elliot Condon
1646 * @since 2.0.5
1647 *
1648 *-------------------------------------------------------------------------------------*/
1649
1650 function admin_message($message = "", $type = 'updated')
1651 {
1652 $GLOBALS['acf_mesage'] = $message;
1653 $GLOBALS['acf_mesage_type'] = $type;
1654
1655 function my_admin_notice()
1656 {
1657 echo '<div class="' . $GLOBALS['acf_mesage_type'] . '" id="message">'.$GLOBALS['acf_mesage'].'</div>';
1658 }
1659 add_action('admin_notices', 'my_admin_notice');
1660 }
1661
1662
1663
1664 /*--------------------------------------------------------------------------------------
1665 *
1666 * get_taxonomies_for_select
1667 *
1668 *---------------------------------------------------------------------------------------
1669 *
1670 * returns a multidimentional array of taxonomies grouped by the post type / taxonomy
1671 *
1672 * @author Elliot Condon
1673 * @since 3.0.2
1674 *
1675 *-------------------------------------------------------------------------------------*/
1676
1677 function get_taxonomies_for_select()
1678 {
1679 $post_types = get_post_types();
1680 $choices = array();
1681
1682 if($post_types)
1683 {
1684 foreach($post_types as $post_type)
1685 {
1686 $post_type_object = get_post_type_object($post_type);
1687 $taxonomies = get_object_taxonomies($post_type);
1688 if($taxonomies)
1689 {
1690 foreach($taxonomies as $taxonomy)
1691 {
1692 if(!is_taxonomy_hierarchical($taxonomy)) continue;
1693 $terms = get_terms($taxonomy, array('hide_empty' => false));
1694 if($terms)
1695 {
1696 foreach($terms as $term)
1697 {
1698 $choices[$post_type_object->label . ': ' . $taxonomy][$term->term_id] = $term->name;
1699 }
1700 }
1701 }
1702 }
1703 }
1704 }
1705
1706 return $choices;
1707 }
1708
1709
1710 function in_taxonomy($post, $ids)
1711 {
1712 $terms = array();
1713
1714 $taxonomies = get_object_taxonomies($post->post_type);
1715 if($taxonomies)
1716 {
1717 foreach($taxonomies as $tax)
1718 {
1719 $all_terms = get_the_terms($post->ID, $tax);
1720 if($all_terms)
1721 {
1722 foreach($all_terms as $all_term)
1723 {
1724 $terms[] = $all_term->term_id;
1725 }
1726 }
1727 }
1728 }
1729
1730 if($terms)
1731 {
1732 if(is_array($ids))
1733 {
1734 foreach($ids as $id)
1735 {
1736 if(in_array($id, $terms))
1737 {
1738 return true;
1739 }
1740 }
1741 }
1742 else
1743 {
1744 if(in_array($ids, $terms))
1745 {
1746 return true;
1747 }
1748 }
1749 }
1750
1751 return false;
1752
1753 }
1754
1755 }
1756 ?>