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

input.php in Advanced Custom Fields (ACF®) 4.1.2, at core/controllers/input.php

787 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Input
5 *
6 * @description: controller for adding field HTML to edit screens
7 * @since: 3.6
8 * @created: 25/01/13
9 */
10
11 class acf_input
12 {
13
14 var $action;
15
16
17 /*
18 * __construct
19 *
20 * @description:
21 * @since 3.1.8
22 * @created: 23/06/12
23 */
24
25 function __construct()
26 {
27 // actions
28 add_action('admin_enqueue_scripts', array($this,'admin_enqueue_scripts'));
29
30
31 // save
32 add_action('save_post', array($this, 'save_post'), 10, 1);
33
34
35 // actions
36 add_action('acf/input/admin_head', array($this, 'input_admin_head'));
37 add_action('acf/input/admin_enqueue_scripts', array($this, 'input_admin_enqueue_scripts'));
38
39
40 add_action('wp_restore_post_revision', array($this, 'wp_restore_post_revision'), 10, 2 );
41
42
43 // filters
44 add_filter('_wp_post_revision_fields', array($this, 'wp_post_revision_fields') );
45
46
47 // ajax acf/update_field_groups
48 add_action('wp_ajax_acf/input/render_fields', array($this, 'ajax_render_fields'));
49 add_action('wp_ajax_acf/input/get_style', array($this, 'ajax_get_style'));
50
51
52 // edit attachment hooks (used by image / file / gallery)
53 add_action('admin_head-media.php', array($this, 'admin_head_media'));
54 add_action('admin_head-upload.php', array($this, 'admin_head_upload'));
55 }
56
57
58 /*
59 * validate_page
60 *
61 * @description: returns true | false. Used to stop a function from continuing
62 * @since 3.2.6
63 * @created: 23/06/12
64 */
65
66 function validate_page()
67 {
68 // global
69 global $pagenow, $typenow;
70
71
72 // vars
73 $return = false;
74
75
76 // validate page
77 if( in_array( $pagenow, array('post.php', 'post-new.php') ) )
78 {
79
80 // validate post type
81 global $typenow;
82
83 if( $typenow != "acf" )
84 {
85 $return = true;
86 }
87
88 }
89
90
91 // validate page (Shopp)
92 if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) )
93 {
94 $return = true;
95 }
96
97
98 // return
99 return $return;
100 }
101
102
103 /*
104 * admin_enqueue_scripts
105 *
106 * @description: run after post query but before any admin script / head actions. A good place to register all actions.
107 * @since: 3.6
108 * @created: 26/01/13
109 */
110
111 function admin_enqueue_scripts()
112 {
113 // validate page
114 if( ! $this->validate_page() ){ return; }
115
116
117 // only "edit post" input pages need the ajax
118 wp_enqueue_script(array(
119 'acf-input-ajax',
120 ));
121
122
123 // actions
124 do_action('acf/input/admin_enqueue_scripts');
125 add_action('admin_head', array($this,'admin_head'));
126 }
127
128
129 /*
130 * admin_head
131 *
132 * @description:
133 * @since 3.1.8
134 * @created: 23/06/12
135 */
136
137 function admin_head()
138 {
139 // globals
140 global $post, $pagenow, $typenow;
141
142
143 // shopp
144 if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) )
145 {
146 $typenow = "shopp_product";
147 }
148
149
150 // vars
151 $post_id = $post ? $post->ID : 0;
152
153
154 // get field groups
155 $filter = array(
156 'post_id' => $post_id,
157 'post_type' => $typenow
158 );
159 $metabox_ids = array();
160 $metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
161
162
163 // get style of first field group
164 $style = '';
165 if( isset($metabox_ids[0]) )
166 {
167 $style = $this->get_style( $metabox_ids[0] );
168 }
169
170
171 // Style
172 echo '<style type="text/css" id="acf_style" >' . $style . '</style>';
173 echo '<style type="text/css">.acf_postbox, .postbox[id*="acf_"] { display: none; }</style>';
174
175
176 // add user js + css
177 do_action('acf/input/admin_head');
178
179
180 // get field groups
181 $acfs = apply_filters('acf/get_field_groups', array());
182
183
184 if( $acfs )
185 {
186 foreach( $acfs as $acf )
187 {
188 // load options
189 $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
190
191
192 // vars
193 $show = in_array( $acf['id'], $metabox_ids ) ? 1 : 0;
194 $priority = 'high';
195 if( $acf['options']['position'] == 'side' )
196 {
197 $priority = 'core';
198 }
199
200
201 // add meta box
202 add_meta_box(
203 'acf_' . $acf['id'],
204 $acf['title'],
205 array($this, 'meta_box_input'),
206 $typenow,
207 $acf['options']['position'],
208 $priority,
209 array( 'field_group' => $acf, 'show' => $show, 'post_id' => $post_id )
210 );
211
212 }
213 // foreach($acfs as $acf)
214 }
215 // if($acfs)
216 }
217
218
219 /*
220 * get_style
221 *
222 * @description: called by admin_head to generate acf css style (hide other metaboxes)
223 * @since 2.0.5
224 * @created: 23/06/12
225 */
226
227 function get_style( $acf_id )
228 {
229 // vars
230 $options = apply_filters('acf/field_group/get_options', array(), $acf_id);
231 $html = '';
232
233
234 // add style to html
235 if( in_array('the_content',$options['hide_on_screen']) )
236 {
237 $html .= '#postdivrich {display: none;} ';
238 }
239 if( in_array('excerpt',$options['hide_on_screen']) )
240 {
241 $html .= '#postexcerpt, #screen-meta label[for=postexcerpt-hide] {display: none;} ';
242 }
243 if( in_array('custom_fields',$options['hide_on_screen']) )
244 {
245 $html .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } ';
246 }
247 if( in_array('discussion',$options['hide_on_screen']) )
248 {
249 $html .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} ';
250 }
251 if( in_array('comments',$options['hide_on_screen']) )
252 {
253 $html .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} ';
254 }
255 if( in_array('slug',$options['hide_on_screen']) )
256 {
257 $html .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} ';
258 }
259 if( in_array('author',$options['hide_on_screen']) )
260 {
261 $html .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} ';
262 }
263 if( in_array('format',$options['hide_on_screen']) )
264 {
265 $html .= '#formatdiv, #screen-meta label[for=formatdiv-hide] {display: none;} ';
266 }
267 if( in_array('featured_image',$options['hide_on_screen']) )
268 {
269 $html .= '#postimagediv, #screen-meta label[for=postimagediv-hide] {display: none;} ';
270 }
271 if( in_array('revisions',$options['hide_on_screen']) )
272 {
273 $html .= '#revisionsdiv, #screen-meta label[for=revisionsdiv-hide] {display: none;} ';
274 }
275 if( in_array('categories',$options['hide_on_screen']) )
276 {
277 $html .= '#categorydiv, #screen-meta label[for=categorydiv-hide] {display: none;} ';
278 }
279 if( in_array('tags',$options['hide_on_screen']) )
280 {
281 $html .= '#tagsdiv-post_tag, #screen-meta label[for=tagsdiv-post_tag-hide] {display: none;} ';
282 }
283 if( in_array('send-trackbacks',$options['hide_on_screen']) )
284 {
285 $html .= '#trackbacksdiv, #screen-meta label[for=trackbacksdiv-hide] {display: none;} ';
286 }
287
288
289 return $html;
290 }
291
292
293 /*
294 * ajax_get_input_style
295 *
296 * @description: called by input-actions.js to hide / show other metaboxes
297 * @since 2.0.5
298 * @created: 23/06/12
299 */
300
301 function ajax_get_style()
302 {
303 // vars
304 $options = array(
305 'acf_id' => 0,
306 'nonce' => ''
307 );
308
309 // load post options
310 $options = array_merge($options, $_POST);
311
312
313 // verify nonce
314 if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
315 {
316 die(0);
317 }
318
319
320 // return style
321 echo $this->get_style( $options['acf_id'] );
322
323
324 // die
325 die;
326 }
327
328
329 /*
330 * meta_box_input
331 *
332 * @description:
333 * @since 1.0.0
334 * @created: 23/06/12
335 */
336
337 function meta_box_input( $post, $args )
338 {
339 // vars
340 $options = $args['args'];
341
342 echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />';
343 echo '<div class="options" data-layout="' . $options['field_group']['options']['layout'] . '" data-show="' . $options['show'] . '" style="display:none"></div>';
344
345 if( $options['show'] )
346 {
347 $fields = apply_filters('acf/field_group/get_fields', array(), $options['field_group']['id']);
348
349 do_action('acf/create_fields', $fields, $options['post_id']);
350 }
351 else
352 {
353 echo '<div class="acf-replace-with-fields"><div class="acf-loading"></div></div>';
354 }
355 }
356
357
358 /*
359 * ajax_render_fields
360 *
361 * @description:
362 * @since 3.1.6
363 * @created: 23/06/12
364 */
365
366 function ajax_render_fields()
367 {
368
369 // defaults
370 $options = array(
371 'acf_id' => 0,
372 'post_id' => 0,
373 'nonce' => ''
374 );
375
376
377 // load post options
378 $options = array_merge($options, $_POST);
379
380
381 // verify nonce
382 if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
383 {
384 die(0);
385 }
386
387
388 // get acfs
389 $acfs = apply_filters('acf/get_field_groups', array());
390 if( $acfs )
391 {
392 foreach( $acfs as $acf )
393 {
394 if( $acf['id'] == $options['acf_id'] )
395 {
396 $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
397
398 do_action('acf/create_fields', $fields, $options['post_id']);
399
400 break;
401 }
402 }
403 }
404
405 die();
406
407 }
408
409
410 /*
411 * save_post
412 *
413 * @description: Saves the field / location / option data for a field group
414 * @since 1.0.0
415 * @created: 23/06/12
416 */
417
418 function save_post( $post_id )
419 {
420
421 // do not save if this is an auto save routine
422 if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
423 {
424 return $post_id;
425 }
426
427
428 // verify nonce
429 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
430 {
431 return $post_id;
432 }
433
434
435 // update the post (may even be a revision / autosave preview)
436 do_action('acf/save_post', $post_id);
437
438
439 }
440
441
442
443 /*--------------------------------------------------------------------------------------
444 *
445 * input_admin_head
446 *
447 * This is fired from an action: acf/input/admin_head
448 *
449 * @author Elliot Condon
450 * @since 3.0.6
451 *
452 *-------------------------------------------------------------------------------------*/
453
454 function input_admin_head()
455 {
456 // global
457 global $wp_version, $post;
458
459
460 // vars
461 $toolbars = apply_filters( 'acf/fields/wysiwyg/toolbars', array() );
462 $post_id = 0;
463 if( $post )
464 {
465 $post_id = $post->ID;
466 }
467
468 ?>
469 <script type="text/javascript">
470
471 // vars
472 acf.post_id = <?php echo $post_id; ?>;
473 acf.nonce = "<?php echo wp_create_nonce( 'acf_nonce' ); ?>";
474 acf.admin_url = "<?php echo admin_url(); ?>";
475 acf.ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
476 acf.wp_version = "<?php echo $wp_version; ?>";
477
478
479 // text
480 acf.validation.text.error = "<?php _e("Validation Failed. One or more fields below are required.",'acf'); ?>";
481
482 acf.fields.relationship.max = "<?php _e("Maximum values reached ( {max} values )",'acf'); ?>";
483
484 acf.fields.image.text.title_add = "Select Image";
485 acf.fields.image.text.title_edit = "Edit Image";
486 acf.fields.image.text.button_add = "Select Image";
487
488 acf.fields.file.text.title_add = "Select File";
489 acf.fields.file.text.title_edit = "Edit File";
490 acf.fields.file.text.button_add = "Select File";
491
492
493 // WYSIWYG
494 <?php
495
496 if( is_array($toolbars) ):
497 foreach( $toolbars as $label => $rows ):
498 $name = sanitize_title( $label );
499 $name = str_replace('-', '_', $name);
500 ?>
501 acf.fields.wysiwyg.toolbars.<?php echo $name; ?> = {};
502 <?php if( is_array($rows) ):
503 foreach( $rows as $k => $v ): ?>
504 acf.fields.wysiwyg.toolbars.<?php echo $name; ?>.theme_advanced_buttons<?php echo $k; ?> = '<?php echo implode(',', $v); ?>';
505 <?php endforeach;
506 endif;
507 endforeach;
508 endif;
509
510 ?>
511 </script>
512 <?php
513 }
514
515
516 /*
517 * input_admin_enqueue_scripts
518 *
519 * @description:
520 * @since: 3.6
521 * @created: 30/01/13
522 */
523
524 function input_admin_enqueue_scripts()
525 {
526
527 // scripts
528 wp_enqueue_script(array(
529 'jquery',
530 'jquery-ui-core',
531 'jquery-ui-tabs',
532 'jquery-ui-sortable',
533 'farbtastic',
534 'thickbox',
535 'media-upload',
536 'acf-input',
537 'acf-datepicker',
538 ));
539
540
541 // 3.5 media gallery
542 if( function_exists('wp_enqueue_media') && !did_action( 'wp_enqueue_media' ))
543 {
544 wp_enqueue_media();
545 }
546
547
548 // styles
549 wp_enqueue_style(array(
550 'thickbox',
551 'farbtastic',
552 'acf-global',
553 'acf-input',
554 'acf-datepicker',
555 ));
556 }
557
558
559 /*
560 * admin_head_upload
561 *
562 * @description:
563 * @since 3.2.6
564 * @created: 3/07/12
565 */
566
567 function admin_head_upload()
568 {
569 // vars
570 $defaults = array(
571 'acf_action' => null,
572 'acf_field' => '',
573 );
574
575 $options = array_merge($defaults, wp_parse_args( wp_get_referer() ));
576
577
578 // validate
579 if( $options['acf_action'] != 'edit_attachment')
580 {
581 return false;
582 }
583
584
585 // call the apropriate field action
586 do_action('acf_head-update_attachment-' . $options['acf_field']);
587
588 ?>
589 <script type="text/javascript">
590
591 // remove tb
592 self.parent.tb_remove();
593
594 </script>
595 </head>
596 <body>
597
598 </body>
599 </html>
600 <?php
601
602 die;
603 }
604
605
606 /*
607 * admin_head_media
608 *
609 * @description:
610 * @since 3.2.6
611 * @created: 3/07/12
612 */
613
614 function admin_head_media()
615 {
616
617 // vars
618 $defaults = array(
619 'acf_action' => null,
620 'acf_field' => '',
621 );
622
623 $options = array_merge($defaults, $_GET);
624
625
626 // validate
627 if( $options['acf_action'] != 'edit_attachment')
628 {
629 return false;
630 }
631
632 ?>
633 <style type="text/css">
634 #wpadminbar,
635 #adminmenuback,
636 #adminmenuwrap,
637 #footer,
638 #wpfooter,
639 #media-single-form > .submit:first-child,
640 #media-single-form td.savesend,
641 .add-new-h2 {
642 display: none;
643 }
644
645 #wpcontent {
646 margin-left: 0px !important;
647 }
648
649 .wrap {
650 margin: 20px 15px;
651 }
652
653 html.wp-toolbar {
654 padding-top: 0px;
655 }
656 </style>
657 <script type="text/javascript">
658 (function($){
659
660 $(document).ready( function(){
661
662 $('#media-single-form').append('<input type="hidden" name="acf_action" value="<?php echo $options['acf_action']; ?>" />');
663 $('#media-single-form').append('<input type="hidden" name="acf_field" value="<?php echo $options['acf_field']; ?>" />');
664
665 });
666
667 })(jQuery);
668 </script>
669 <?php
670
671 do_action('acf_head-edit_attachment');
672 }
673
674
675 /*
676 * wp_restore_post_revision
677 *
678 * @description:
679 * @since 3.4.4
680 * @created: 4/09/12
681 */
682
683 function wp_restore_post_revision( $parent_id, $revision_id )
684 {
685 global $wpdb;
686
687
688 // get field from postmeta
689 $rows = $wpdb->get_results( $wpdb->prepare(
690 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key NOT LIKE %s",
691 $revision_id,
692 '\_%'
693 ), ARRAY_A);
694
695
696 if( $rows )
697 {
698 foreach( $rows as $row )
699 {
700 update_post_meta( $parent_id, $row['meta_key'], $row['meta_value'] );
701 }
702 }
703
704 }
705
706
707 /*
708 * wp_post_revision_fields
709 *
710 * @description:
711 * @since 3.4.4
712 * @created: 4/09/12
713 */
714
715 function wp_post_revision_fields( $fields ) {
716
717 global $post, $wpdb, $revision, $left_revision, $right_revision, $pagenow;
718
719
720 if( $pagenow != "revision.php" )
721 {
722 return $fields;
723 }
724
725
726 // get field from postmeta
727 $rows = $wpdb->get_results( $wpdb->prepare(
728 "SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d AND meta_key NOT LIKE %s",
729 $post->ID,
730 '\_%'
731 ), ARRAY_A);
732
733
734 if( $rows )
735 {
736 foreach( $rows as $row )
737 {
738 $fields[ $row['meta_key'] ] = ucwords( str_replace('_', ' ', $row['meta_key']) );
739
740
741 // left vs right
742 if( isset($_GET['left']) && isset($_GET['right']) )
743 {
744 $left = get_metadata( 'post', $_GET['left'], $row['meta_key'], true );
745 $right = get_metadata( 'post', $_GET['right'], $row['meta_key'], true );
746
747 // format arrays
748 if( is_array($left) )
749 {
750 $left = implode(', ', $left);
751 }
752 if( is_array($right) )
753 {
754 $right = implode(', ', $right);
755 }
756
757
758 $left_revision->$row['meta_key'] = $left;
759 $right_revision->$row['meta_key'] = $right;
760 }
761 else
762 {
763 $left = get_metadata( 'post', $revision->ID, $row['meta_key'], true );
764
765 // format arrays
766 if( is_array($left) )
767 {
768 $left = implode(', ', $left);
769 }
770
771 $revision->$row['meta_key'] = $left;
772 }
773
774 }
775 }
776
777
778 return $fields;
779
780 }
781
782
783 }
784
785 new acf_input();
786
787 ?>