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

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