PluginProbe
Advanced Custom Fields (ACF®) / 4.4.12
Advanced Custom Fields (ACF®) v4.4.12
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 / everything_fields.php
everything_fields.php
882 lines 18.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class acf_everything_fields
4 {
5
6 var $settings,
7 $data;
8
9
10 /*
11 * __construct
12 *
13 * @description:
14 * @since 3.1.8
15 * @created: 23/06/12
16 */
17
18 function __construct()
19 {
20 // data for passing variables
21 $this->data = array(
22 'page_id' => '', // a string used to load values
23 'metabox_ids' => array(),
24 'page_type' => '', // taxonomy / user / media
25 'page_action' => '', // add / edit
26 'option_name' => '', // key used to find value in wp_options table. eg: user_1, category_4
27 );
28
29
30 // actions
31 add_action('admin_menu', array($this,'admin_menu'));
32 add_action('wp_ajax_acf/everything_fields', array($this, 'acf_everything_fields'));
33
34
35 // attachment
36 add_filter('attachment_fields_to_edit', array($this, 'attachment_fields_to_edit'), 10, 2);
37 add_filter('attachment_fields_to_save', array($this, 'save_attachment'), 10, 2);
38
39
40 // save
41 add_action('create_term', array($this, 'save_taxonomy'));
42 add_action('edited_term', array($this, 'save_taxonomy'));
43 add_action('edit_user_profile_update', array($this, 'save_user'));
44 add_action('personal_options_update', array($this, 'save_user'));
45 add_action('user_register', array($this, 'save_user'));
46
47
48 // shopp
49 add_action('shopp_category_saved', array($this, 'shopp_category_saved'));
50
51
52 // delete
53 add_action('delete_term', array($this, 'delete_term'), 10, 4);
54 }
55
56
57 /*
58 * attachment_fields_to_edit
59 *
60 * Adds ACF fields to the attachment form fields
61 *
62 * @type filter
63 * @date 14/07/13
64 *
65 * @param {array} $form_fields
66 * @return {object} $post
67 */
68
69 function attachment_fields_to_edit( $form_fields, $post )
70 {
71 // vars
72 $screen = get_current_screen();
73 $post_id = $post->ID;
74
75
76 if( $screen && $screen->base == 'post' ) {
77
78 return $form_fields;
79
80 }
81
82
83 // get field groups
84 $filter = array( 'post_type' => 'attachment' );
85 $metabox_ids = array();
86 $metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
87
88
89 // validate
90 if( empty($metabox_ids) )
91 {
92 return $form_fields;
93 }
94
95
96 $acfs = apply_filters('acf/get_field_groups', array());
97
98
99 if( is_array($acfs) ){ foreach( $acfs as $acf ){
100
101 // only add the chosen field groups
102 if( !in_array( $acf['id'], $metabox_ids ) )
103 {
104 continue;
105 }
106
107
108 // load fields
109 $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
110
111
112 if( is_array($fields) ){ foreach( $fields as $i => $field ){
113
114 // if they didn't select a type, skip this field
115 if( !$field || !$field['type'] || $field['type'] == 'null' )
116 {
117 continue;
118 }
119
120
121 // set value
122 if( !isset($field['value']) )
123 {
124 $field['value'] = apply_filters('acf/load_value', false, $post_id, $field);
125 $field['value'] = apply_filters('acf/format_value', $field['value'], $post_id, $field);
126 }
127
128
129 // create field
130 $field['name'] = 'fields[' . $field['key'] . ']';
131
132 ob_start();
133
134 do_action('acf/create_field', $field);
135
136 $html = ob_get_contents();
137
138 ob_end_clean();
139
140
141 $form_fields[ $field['name'] ] = array(
142 'label' => $field['label'],
143 'input' => 'html',
144 'html' => $html
145 );
146
147 }};
148
149
150 }}
151
152
153 // return
154 return $form_fields;
155 }
156
157
158 /*
159 * save_attachment
160 *
161 * Triggers the acf/save_post action
162 *
163 * @type action
164 * @date 14/07/13
165 *
166 * @param {array} $post
167 * @return {array} $attachment
168 */
169
170 function save_attachment( $post, $attachment )
171 {
172 // verify nonce
173 /*
174 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
175 {
176 return $post;
177 }
178 */
179
180
181 // $post_id to save against
182 $post_id = $post['ID'];
183
184
185 // update the post
186 do_action('acf/save_post', $post_id);
187
188
189 return $post;
190 }
191
192
193 /*
194 * validate_page
195 *
196 * @description: returns true | false. Used to stop a function from continuing
197 * @since 3.2.6
198 * @created: 23/06/12
199 */
200
201 function validate_page()
202 {
203 // global
204 global $pagenow, $wp_version;
205
206
207 // vars
208 $return = false;
209
210
211 // validate page
212 if( in_array( $pagenow, array( 'edit-tags.php', 'term.php', 'profile.php', 'user-new.php', 'user-edit.php', 'media.php' ) ) )
213 {
214 $return = true;
215 }
216
217
218 // validate page (Shopp)
219 if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" )
220 {
221 $return = true;
222 }
223
224
225 // WP4
226 if( $pagenow === 'upload.php' && version_compare($wp_version, '4.0', '>=') ) {
227
228 $return = true;
229
230 }
231
232
233 // return
234 return $return;
235 }
236
237
238 /*--------------------------------------------------------------------------------------
239 *
240 * admin_menu
241 *
242 * @author Elliot Condon
243 * @since 3.1.8
244 *
245 *-------------------------------------------------------------------------------------*/
246
247 function admin_menu()
248 {
249
250 global $pagenow;
251
252
253 // validate page
254 if( ! $this->validate_page() ) return;
255
256
257 // set page type
258 $filter = array();
259
260 if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" )
261 {
262 // filter
263 $_GET['id'] = filter_var($_GET['id'], FILTER_SANITIZE_STRING);
264
265
266 $this->data['page_type'] = "shopp_category";
267 $filter['ef_taxonomy'] = "shopp_category";
268
269 $this->data['page_action'] = "add";
270 $this->data['option_name'] = "";
271
272 if( $_GET['id'] != "new" )
273 {
274 $this->data['page_action'] = "edit";
275 $this->data['option_name'] = "shopp_category_" . $_GET['id'];
276 }
277
278 } elseif( $pagenow == "edit-tags.php" || $pagenow == "term.php" ) {
279
280 // vars
281 $taxonomy = 'post_tag';
282 $term_id = 0;
283
284
285 // $_GET
286 if( !empty($_GET['taxonomy']) ) {
287
288 $taxonomy = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING);
289
290 }
291
292 if( !empty($_GET['tag_ID']) ) {
293
294 $term_id = filter_var($_GET['tag_ID'], FILTER_SANITIZE_NUMBER_INT);
295
296 }
297
298
299 // update filter
300 $filter['ef_taxonomy'] = $taxonomy;
301
302
303 // add
304 $this->data['page_type'] = "taxonomy";
305 $this->data['page_action'] = "add";
306 $this->data['option_name'] = "";
307
308
309 // edit
310 if( $term_id ) {
311
312 $this->data['page_action'] = "edit";
313 $this->data['option_name'] = $taxonomy . "_" . $term_id;
314
315 }
316
317 }
318 elseif( $pagenow == "profile.php" )
319 {
320
321 $this->data['page_type'] = "user";
322 $filter['ef_user'] = get_current_user_id();
323
324 $this->data['page_action'] = "edit";
325 $this->data['option_name'] = "user_" . get_current_user_id();
326
327 }
328 elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) )
329 {
330 // filter
331 $_GET['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT);
332
333
334 $this->data['page_type'] = "user";
335 $filter['ef_user'] = $_GET['user_id'];
336
337 $this->data['page_action'] = "edit";
338 $this->data['option_name'] = "user_" . $_GET['user_id'];
339
340 }
341 elseif( $pagenow == "user-new.php" )
342 {
343 $this->data['page_type'] = "user";
344 $filter['ef_user'] ='all';
345
346 $this->data['page_action'] = "add";
347 $this->data['option_name'] = "";
348
349 }
350 elseif( $pagenow == "media.php" || $pagenow == 'upload.php' )
351 {
352
353 $this->data['page_type'] = "media";
354 $filter['post_type'] = 'attachment';
355
356 $this->data['page_action'] = "add";
357 $this->data['option_name'] = "";
358
359 if(isset($_GET['attachment_id']))
360 {
361 // filter
362 $_GET['attachment_id'] = filter_var($_GET['attachment_id'], FILTER_SANITIZE_NUMBER_INT);
363
364
365 $this->data['page_action'] = "edit";
366 $this->data['option_name'] = $_GET['attachment_id'];
367 }
368
369 }
370
371
372 // get field groups
373 $metabox_ids = array();
374 $this->data['metabox_ids'] = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
375
376
377 // dont continue if no ids were found
378 if( empty( $this->data['metabox_ids'] ) )
379 {
380 return false;
381 }
382
383
384 // actions
385 add_action('admin_enqueue_scripts', array($this,'admin_enqueue_scripts'));
386 add_action('admin_head', array($this,'admin_head'));
387
388
389 }
390
391
392 /*
393 * admin_enqueue_scripts
394 *
395 * @description:
396 * @since: 3.6
397 * @created: 30/01/13
398 */
399
400 function admin_enqueue_scripts()
401 {
402 do_action('acf/input/admin_enqueue_scripts');
403 }
404
405
406 /*--------------------------------------------------------------------------------------
407 *
408 * admin_head
409 *
410 * @author Elliot Condon
411 * @since 3.1.8
412 *
413 *-------------------------------------------------------------------------------------*/
414
415 function admin_head()
416 {
417 global $pagenow;
418
419
420 // add user js + css
421 do_action('acf/input/admin_head');
422
423
424 ?>
425 <script type="text/javascript">
426 (function($){
427
428 acf.data = {
429 action : 'acf/everything_fields',
430 metabox_ids : '<?php echo implode( ',', $this->data['metabox_ids'] ); ?>',
431 page_type : '<?php echo $this->data['page_type']; ?>',
432 page_action : '<?php echo $this->data['page_action']; ?>',
433 option_name : '<?php echo $this->data['option_name']; ?>'
434 };
435
436 $(document).ready(function(){
437
438 $.ajax({
439 url: ajaxurl,
440 data: acf.data,
441 type: 'post',
442 dataType: 'html',
443 success: function(html){
444
445 <?php
446 if($this->data['page_type'] == "user")
447 {
448 if($this->data['page_action'] == "add")
449 {
450 echo "$('#createuser > table.form-table:last > tbody').append( html );";
451 }
452 else
453 {
454 echo "$('#your-profile .form-table:last').after( html );";
455 }
456 }
457 elseif($this->data['page_type'] == "shopp_category")
458 {
459 echo "$('#post-body-content').append( html );";
460 }
461 elseif($this->data['page_type'] == "taxonomy")
462 {
463 if($this->data['page_action'] == "add")
464 {
465 echo "$('#addtag > p.submit').before( html );";
466 }
467 else
468 {
469 echo "$('#edittag > table.form-table:first > tbody').append( html );";
470 }
471 }
472 elseif($this->data['page_type'] == "media")
473 {
474 if($this->data['page_action'] == "add")
475 {
476 echo "$('#addtag > p.submit').before( html );";
477 }
478 else
479 {
480 echo "$('#media-single-form table tbody tr.submit').before( html );";
481 }
482 }
483 ?>
484
485 setTimeout( function(){
486 $(document).trigger('acf/setup_fields', $('#wpbody') );
487 }, 200);
488
489 }
490 });
491
492
493 /*
494 * Taxonomy Add
495 *
496 * @description:
497 * @since: 3.6
498 * @created: 24/02/13
499 */
500
501 $(document).ajaxComplete(function(event, xhr, settings) {
502
503 // vars
504 data = acf.helpers.url_to_object(settings.data);
505
506
507 // validate
508 if( data.action != 'add-tag' )
509 {
510 return;
511 }
512
513
514 // vars
515 var $el = $('#addtag');
516
517
518 // clear WYSIWYG field
519 $el.find('.acf_wysiwyg textarea').each(function(){
520
521
522 // vars
523 var textarea = $(this),
524 id = textarea.attr('id'),
525 editor = tinyMCE.get( id );
526
527 if( editor )
528 {
529 editor.setContent('');
530 editor.save();
531 }
532
533 });
534
535
536 // clear image / file fields
537 $el.find('.field .active').removeClass('active');
538
539
540 // clear checkbox
541 $el.find('input[type="checkbox"]').removeAttr('checked');
542
543 });
544
545 });
546
547
548 })(jQuery);
549 </script>
550 <?php
551 }
552
553
554
555 /*--------------------------------------------------------------------------------------
556 *
557 * save_taxonomy
558 *
559 * @author Elliot Condon
560 * @since 3.1.8
561 *
562 *-------------------------------------------------------------------------------------*/
563
564 function save_taxonomy( $term_id )
565 {
566 // verify nonce
567 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
568 {
569 return $term_id;
570 }
571
572
573 // for some weird reason, this is triggered by saving a menu...
574 if( !isset($_POST['taxonomy']) )
575 {
576 return;
577 }
578
579 // $post_id to save against
580 $post_id = $_POST['taxonomy'] . '_' . $term_id;
581
582
583 // update the post
584 do_action('acf/save_post', $post_id);
585
586 }
587
588
589 /*--------------------------------------------------------------------------------------
590 *
591 * profile_save
592 *
593 * @author Elliot Condon
594 * @since 3.1.8
595 *
596 *-------------------------------------------------------------------------------------*/
597
598 function save_user( $user_id )
599 {
600 // verify nonce
601 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
602 {
603 return $user_id;
604 }
605
606
607 // $post_id to save against
608 $post_id = 'user_' . $user_id;
609
610
611 // update the post
612 do_action('acf/save_post', $post_id);
613
614 }
615
616
617 /*
618 * shopp_category_saved
619 *
620 * @description:
621 * @since 3.5.2
622 * @created: 27/11/12
623 */
624
625 function shopp_category_saved( $category )
626 {
627 // verify nonce
628 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
629 {
630 return $category;
631 }
632
633
634 // $post_id to save against
635 $post_id = 'shopp_category_' . $category->id;
636
637
638 // update the post
639 do_action('acf/save_post', $post_id);
640
641
642 }
643
644
645 /*--------------------------------------------------------------------------------------
646 *
647 * acf_everything_fields
648 *
649 * @description Ajax call that renders the html needed for the page
650 * @author Elliot Condon
651 * @since 3.1.8
652 *
653 *-------------------------------------------------------------------------------------*/
654
655 function acf_everything_fields()
656 {
657 // defaults
658 $defaults = array(
659 'metabox_ids' => '',
660 'page_type' => '',
661 'page_action' => '',
662 'option_name' => '',
663 );
664
665
666 // load post options
667 $options = array_merge($defaults, $_POST);
668
669
670 // metabox ids is a string with commas
671 $options['metabox_ids'] = explode( ',', $options['metabox_ids'] );
672
673
674 // get acfs
675 $acfs = apply_filters('acf/get_field_groups', false);
676
677
678 // layout
679 $layout = 'tr';
680 if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add")
681 {
682 $layout = 'div';
683 }
684 if( $options['page_type'] == "shopp_category")
685 {
686 $layout = 'metabox';
687 }
688
689
690 if( $acfs )
691 {
692 foreach( $acfs as $acf )
693 {
694 // load options
695 $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
696
697
698 // only add the chosen field groups
699 if( !in_array( $acf['id'], $options['metabox_ids'] ) )
700 {
701 continue;
702 }
703
704
705 // layout dictates heading
706 $title = true;
707
708 if( $acf['options']['layout'] == 'no_box' )
709 {
710 $title = false;
711 }
712
713
714 // title
715 if( $options['page_action'] == "edit" && $options['page_type'] == 'user' )
716 {
717 if( $title )
718 {
719 echo '<h3>' .$acf['title'] . '</h3>';
720 }
721
722 echo '<table class="form-table"><tbody>';
723 }
724
725
726 // wrapper
727 if( $layout == 'tr' )
728 {
729 //nonce
730 echo '<tr style="display:none;"><td colspan="2"><input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" /></td></tr>';
731 }
732 else
733 {
734 //nonce
735 echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />';
736 }
737
738 if( $layout == 'metabox' )
739 {
740 echo '<div class="postbox acf_postbox" id="acf_'. $acf['id'] .'">';
741 echo '<div title="Click to toggle" class="handlediv"><br></div><h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
742 echo '<div class="inside">';
743 }
744
745
746 // load fields
747 $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
748
749
750 if( is_array($fields) ){ foreach( $fields as $field ){
751
752 // if they didn't select a type, skip this field
753 if( !$field['type'] || $field['type'] == 'null' ) continue;
754
755
756 // set value
757 if( !isset($field['value']) )
758 {
759 $field['value'] = apply_filters('acf/load_value', false, $options['option_name'], $field);
760 $field['value'] = apply_filters('acf/format_value', $field['value'], $options['option_name'], $field);
761 }
762
763
764 // required
765 $required_class = "";
766 $required_label = "";
767
768 if( $field['required'] )
769 {
770 $required_class = ' required';
771 $required_label = ' <span class="required">*</span>';
772 }
773
774
775 if( $layout == 'metabox' )
776 {
777 echo '<div id="acf-' . $field['name'] . '" class="field field_type-' . $field['type'] . ' field_key-' . $field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">';
778
779 echo '<p class="label">';
780 echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
781 echo $field['instructions'];
782 echo '</p>';
783
784 $field['name'] = 'fields[' . $field['key'] . ']';
785 do_action('acf/create_field', $field);
786
787 echo '</div>';
788 }
789 elseif( $layout == 'div' )
790 {
791 echo '<div id="acf-' . $field['name'] . '" class="form-field field field_type-' . $field['type'] . ' field_key-' . $field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">';
792
793 echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
794 $field['name'] = 'fields[' . $field['key'] . ']';
795 do_action('acf/create_field', $field );
796 if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>';
797
798 echo '</div>';
799 }
800 else
801 {
802 echo '<tr id="acf-' . $field['name'] . '" class="form-field field field_type-' . $field['type'] . ' field_key-'.$field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">';
803 echo '<th valign="top" scope="row"><label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label></th>';
804 echo '<td>';
805 $field['name'] = 'fields[' . $field['key'] . ']';
806 do_action('acf/create_field', $field );
807
808 if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>';
809 echo '</td>';
810 echo '</tr>';
811
812 }
813
814 }}
815
816
817
818 // wrapper
819 if( $layout == 'metabox' )
820 {
821 echo '</div></div>';
822 }
823
824
825 // title
826 if( $options['page_action'] == "edit" && $options['page_type'] == 'user' )
827 {
828 echo '</tbody></table>';
829 }
830
831
832 }
833 // foreach($acfs as $acf)
834 }
835 // if($acfs)
836
837 // exit for ajax
838 die();
839
840 }
841
842
843 /*
844 * delete_term
845 *
846 * @description:
847 * @since: 3.5.7
848 * @created: 12/01/13
849 */
850
851 function delete_term( $term, $tt_id, $taxonomy, $deleted_term ) {
852
853 // globals
854 global $wpdb;
855
856
857 // vars
858 $search = $taxonomy . '_' . $term . '_%';
859 $_search = '_' . $search;
860
861
862 // escape '_'
863 // http://stackoverflow.com/questions/2300285/how-do-i-escape-in-sql-server
864 $search = str_replace('_', '\_', $search);
865 $_search = str_replace('_', '\_', $_search);
866
867
868 // delete
869 $result = $wpdb->query($wpdb->prepare(
870 "DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
871 $search,
872 $_search
873 ));
874
875 }
876
877
878 }
879
880 new acf_everything_fields();
881
882 ?>