PluginProbe
Advanced Custom Fields (ACF®) / 3.3.1
Advanced Custom Fields (ACF®) v3.3.1
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / core / controllers / input.php
input.php
626 lines 12.6 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: All the functionality for adding fields to a page / post
7 * @since 3.2.6
8 * @created: 23/06/12
9 */
10
11
12 class acf_input
13 {
14
15 var $parent,
16 $data;
17
18
19 /*
20 * __construct
21 *
22 * @description:
23 * @since 3.1.8
24 * @created: 23/06/12
25 */
26
27 function __construct($parent)
28 {
29
30 // vars
31 $this->parent = $parent;
32
33
34 // actions
35 add_action('admin_print_scripts', array($this,'admin_print_scripts'));
36 add_action('admin_print_styles', array($this,'admin_print_styles'));
37 add_action('admin_head', array($this,'admin_head'));
38 add_action('save_post', array($this, 'save_post'));
39
40
41 // custom actions (added in 3.1.8)
42 add_action('acf_head-input', array($this, 'acf_head_input'));
43 add_action('acf_print_scripts-input', array($this, 'acf_print_scripts_input'));
44 add_action('acf_print_styles-input', array($this, 'acf_print_styles_input'));
45
46
47 // ajax
48 add_action('wp_ajax_acf_input', array($this, 'ajax_acf_input'));
49 add_action('wp_ajax_get_input_style', array($this, 'ajax_get_input_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 // return
92 return $return;
93 }
94
95
96 /*
97 * admin_print_scripts
98 *
99 * @description:
100 * @since 3.1.8
101 * @created: 23/06/12
102 */
103
104 function admin_print_scripts()
105 {
106 // validate page
107 if( ! $this->validate_page() ) return;
108
109 do_action('acf_print_scripts-input');
110 }
111
112
113 /*
114 * admin_print_styles
115 *
116 * @description:
117 * @since 3.1.8
118 * @created: 23/06/12
119 */
120
121 function admin_print_styles()
122 {
123 // validate page
124 if( ! $this->validate_page() ) return;
125
126 do_action('acf_print_styles-input');
127 }
128
129
130 /*
131 * admin_head
132 *
133 * @description:
134 * @since 3.1.8
135 * @created: 23/06/12
136 */
137
138 function admin_head()
139 {
140 // validate page
141 if( ! $this->validate_page() ) return;
142
143
144 // globals
145 global $post;
146
147
148 // vars
149 $post_type = get_post_type($post);
150
151
152 // get style for page
153 $metabox_ids = $this->parent->get_input_metabox_ids( array( 'post_id' => $post->ID ), false);
154 $style = isset($metabox_ids[0]) ? $this->get_input_style($metabox_ids[0]) : '';
155 echo '<style type="text/css" id="acf_style" >' .$style . '</style>';
156
157
158 // Style
159 echo '<link rel="stylesheet" type="text/css" href="' . $this->parent->dir . '/css/global.css?ver=' . $this->parent->version . '" />';
160 echo '<link rel="stylesheet" type="text/css" href="' . $this->parent->dir . '/css/input.css?ver=' . $this->parent->version . '" />';
161 echo '<style type="text/css">.acf_postbox, .postbox[id*="acf_"] { display: none; }</style>';
162
163
164 // Javascript
165 echo '<script type="text/javascript" src="' . $this->parent->dir . '/js/input-actions.js?ver=' . $this->parent->version . '" ></script>';
166 echo '<script type="text/javascript" src="' . $this->parent->dir . '/js/input-ajax.js?ver=' . $this->parent->version . '" ></script>';
167 echo '<script type="text/javascript">acf.post_id = ' . $post->ID . ';</script>';
168
169
170 // add user js + css
171 do_action('acf_head-input');
172
173
174 // get acf's
175 $acfs = $this->parent->get_field_groups();
176 if($acfs)
177 {
178 foreach($acfs as $acf)
179 {
180 // hide / show
181 $show = in_array($acf['id'], $metabox_ids) ? "true" : "false";
182
183 // add meta box
184 add_meta_box(
185 'acf_' . $acf['id'],
186 $acf['title'],
187 array($this, 'meta_box_input'),
188 $post_type,
189 $acf['options']['position'],
190 'high',
191 array( 'fields' => $acf['fields'], 'options' => $acf['options'], 'show' => $show, 'post_id' => $post->ID )
192 );
193 }
194 // foreach($acfs as $acf)
195 }
196 // if($acfs)
197 }
198
199
200 /*
201 * get_input_style
202 *
203 * @description: called by admin_head to generate acf css style (hide other metaboxes)
204 * @since 2.0.5
205 * @created: 23/06/12
206 */
207
208 function get_input_style($acf_id = false)
209 {
210 // vars
211 $acfs = $this->parent->get_field_groups();
212 $html = "";
213
214 // find acf
215 if($acfs)
216 {
217 foreach($acfs as $acf)
218 {
219 if($acf['id'] != $acf_id) continue;
220
221
222 // add style to html
223 if( in_array('the_content',$acf['options']['hide_on_screen']) )
224 {
225 $html .= '#postdivrich {display: none;} ';
226 }
227 if( in_array('excerpt',$acf['options']['hide_on_screen']) )
228 {
229 $html .= '#postexcerpt, #screen-meta label[for=postexcerpt-hide] {display: none;} ';
230 }
231 if( in_array('custom_fields',$acf['options']['hide_on_screen']) )
232 {
233 $html .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } ';
234 }
235 if( in_array('discussion',$acf['options']['hide_on_screen']) )
236 {
237 $html .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} ';
238 }
239 if( in_array('comments',$acf['options']['hide_on_screen']) )
240 {
241 $html .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} ';
242 }
243 if( in_array('slug',$acf['options']['hide_on_screen']) )
244 {
245 $html .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} ';
246 }
247 if( in_array('author',$acf['options']['hide_on_screen']) )
248 {
249 $html .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} ';
250 }
251 if( in_array('format',$acf['options']['hide_on_screen']) )
252 {
253 $html .= '#formatdiv, #screen-meta label[for=formatdiv-hide] {display: none;} ';
254 }
255 if( in_array('featured_image',$acf['options']['hide_on_screen']) )
256 {
257 $html .= '#postimagediv, #screen-meta label[for=postimagediv-hide] {display: none;} ';
258 }
259
260
261 break;
262
263 }
264 // foreach($acfs as $acf)
265 }
266 //if($acfs)
267
268 return $html;
269 }
270
271
272 /*
273 * the_input_style
274 *
275 * @description: called by input-actions.js to hide / show other metaboxes
276 * @since 2.0.5
277 * @created: 23/06/12
278 */
279
280 function ajax_get_input_style()
281 {
282 // overrides
283 if(isset($_POST['acf_id']))
284 {
285 echo $this->get_input_style($_POST['acf_id']);
286 }
287
288 die;
289 }
290
291
292 /*
293 * meta_box_input
294 *
295 * @description:
296 * @since 1.0.0
297 * @created: 23/06/12
298 */
299
300 function meta_box_input($post, $args)
301 {
302 // vars
303 $fields = isset($args['args']['fields']) ? $args['args']['fields'] : false ;
304 $options = isset($args['args']['options']) ? $args['args']['options'] : false;
305 $show = isset($args['args']['show']) ? $args['args']['show'] : "false";
306 $post_id = isset($args['args']['post_id']) ? $args['args']['post_id'] : false;
307
308
309 // defaults
310 if(!$options)
311 {
312 $options = array(
313 'layout' => 'default'
314 );
315 }
316
317 if($fields)
318 {
319 echo '<input type="hidden" name="save_input" value="true" />';
320 echo '<div class="options" data-layout="' . $options['layout'] . '" data-show="' . $show . '" style="display:none"></div>';
321
322 if($show == "false")
323 {
324 // don't create fields
325 echo '<div class="acf-replace-with-fields"><div class="acf-loading"></div></div>';
326 }
327 else
328 {
329
330
331 $this->parent->render_fields_for_input($fields, $post_id);
332 }
333 }
334 }
335
336
337 /*
338 * ajax_acf_input
339 *
340 * @description:
341 * @since 3.1.6
342 * @created: 23/06/12
343 */
344
345 function ajax_acf_input()
346 {
347
348 // defaults
349 $defaults = array(
350 'acf_id' => null,
351 'post_id' => null,
352 );
353
354 // load post options
355 $options = array_merge($defaults, $_POST);
356
357 // required
358 if(!$options['acf_id'] || !$options['post_id'])
359 {
360 echo "";
361 die();
362 }
363
364 // get fields
365 $fields = $this->parent->get_acf_fields($options['acf_id']);
366
367 $this->parent->render_fields_for_input($fields, $options['post_id']);
368
369 die();
370
371 }
372
373
374 /*
375 * save_post
376 *
377 * @description: Saves the field / location / option data for a field group
378 * @since 1.0.0
379 * @created: 23/06/12
380 */
381
382 function save_post($post_id)
383 {
384
385 // do not save if this is an auto save routine
386 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
387
388
389 // only for save acf
390 if( ! isset($_POST['save_input']) || $_POST['save_input'] != 'true')
391 {
392 return $post_id;
393 }
394
395
396 // only save once! WordPress save's twice for some strange reason.
397 global $acf_flag;
398 if ($acf_flag != 0)
399 {
400 return $post_id;
401 }
402 $acf_flag = 1;
403
404
405 // set post ID if is a revision
406 if(wp_is_post_revision($post_id))
407 {
408 $post_id = wp_is_post_revision($post_id);
409 }
410
411 // save fields
412 $fields = $_POST['fields'];
413
414 if($fields)
415 {
416 foreach($fields as $key => $value)
417 {
418 // get field
419 $field = $this->parent->get_acf_field($key);
420
421 $this->parent->update_value($post_id, $field, $value);
422 }
423 // foreach($fields as $key => $value)
424 }
425 // if($fields)
426 }
427
428
429 /*--------------------------------------------------------------------------------------
430 *
431 * acf_head_input
432 *
433 * This is fired from an action: acf_head-input
434 *
435 * @author Elliot Condon
436 * @since 3.0.6
437 *
438 *-------------------------------------------------------------------------------------*/
439
440 function acf_head_input()
441 {
442
443 ?>
444 <script type="text/javascript">
445
446 // admin url
447 acf.admin_url = "<?php echo admin_url(); ?>";
448
449 // messages
450 acf.text.validation_error = "<?php _e("Validation Failed. One or more fields below are required.",'acf'); ?>";
451 acf.text.file_tb_title_add = "<?php _e("Add File to Field",'acf'); ?>";
452 acf.text.file_tb_title_edit = "<?php _e("Edit File",'acf'); ?>";
453 acf.text.image_tb_title_add = "<?php _e("Add Image to Field",'acf'); ?>";
454 acf.text.image_tb_title_edit = "<?php _e("Edit Image",'acf'); ?>";
455 acf.text.relationship_max_alert = "<?php _e("Maximum values reached ( {max} values )",'acf'); ?>";
456 acf.text.gallery_tb_title_add = "<?php _e("Add Image to Gallery",'acf'); ?>";
457 acf.text.gallery_tb_title_edit = "<?php _e("Edit Image",'acf'); ?>";
458
459 </script>
460 <?php
461
462 foreach($this->parent->fields as $field)
463 {
464 $field->admin_head();
465 }
466 }
467
468
469 /*--------------------------------------------------------------------------------------
470 *
471 * acf_print_scripts
472 *
473 * @author Elliot Condon
474 * @since 3.1.8
475 *
476 *-------------------------------------------------------------------------------------*/
477
478 function acf_print_scripts_input()
479 {
480 foreach($this->parent->fields as $field)
481 {
482 $field->admin_print_scripts();
483 }
484 }
485
486
487 /*--------------------------------------------------------------------------------------
488 *
489 * acf_print_styles
490 *
491 * @author Elliot Condon
492 * @since 3.1.8
493 *
494 *-------------------------------------------------------------------------------------*/
495
496 function acf_print_styles_input()
497 {
498 foreach($this->parent->fields as $field)
499 {
500 $field->admin_print_styles();
501 }
502 }
503
504
505 /*
506 * admin_head_upload
507 *
508 * @description:
509 * @since 3.2.6
510 * @created: 3/07/12
511 */
512
513 function admin_head_upload()
514 {
515 // vars
516 $defaults = array(
517 'acf_action' => null,
518 'acf_field' => '',
519 );
520
521 $options = array_merge($defaults, wp_parse_args( wp_get_referer() ));
522
523
524 // validate
525 if( $options['acf_action'] != 'edit_attachment')
526 {
527 return false;
528 }
529
530
531 // call the apropriate field action
532 do_action('acf_head-update_attachment-' . $options['acf_field']);
533
534 ?>
535 <script type="text/javascript">
536
537 // reset global
538 self.parent.acf_edit_attachment = null;
539
540 // remove tb
541 self.parent.tb_remove();
542
543 </script>
544 </head>
545 <body>
546
547 <div class="updated" id="message"><p>Attachment updated.</div>
548
549 </body>
550 </html
551 <?php
552
553 die;
554 }
555
556
557 /*
558 * admin_head_media
559 *
560 * @description:
561 * @since 3.2.6
562 * @created: 3/07/12
563 */
564
565 function admin_head_media()
566 {
567
568 // vars
569 $defaults = array(
570 'acf_action' => null,
571 'acf_field' => '',
572 );
573
574 $options = array_merge($defaults, $_GET);
575
576
577 // validate
578 if( $options['acf_action'] != 'edit_attachment')
579 {
580 return false;
581 }
582
583 ?>
584 <style type="text/css">
585 #wpadminbar,
586 #adminmenuback,
587 #adminmenuwrap,
588 #footer,
589 #media-single-form > .submit:first-child,
590 #media-single-form td.savesend,
591 .add-new-h2 {
592 display: none;
593 }
594
595 #wpcontent {
596 margin-left: 0px;
597 }
598
599 .wrap {
600 margin: 20px 15px;
601 }
602
603 html.wp-toolbar {
604 padding-top: 0px;
605 }
606 </style>
607 <script type="text/javascript">
608 (function($){
609
610 $(document).ready( function(){
611
612 $('#media-single-form').append('<input type="hidden" name="acf_action" value="<?php echo $options['acf_action']; ?>" />');
613 $('#media-single-form').append('<input type="hidden" name="acf_field" value="<?php echo $options['acf_field']; ?>" />');
614
615 });
616
617 })(jQuery);
618 </script>
619 <?php
620
621 do_action('acf_head-edit_attachment');
622 }
623
624 }
625
626 ?>