PluginProbe
Advanced Custom Fields (ACF®) / 4.1.1
Advanced Custom Fields (ACF®) v4.1.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 / everything_fields.php
everything_fields.php
678 lines 14.9 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 // save
36 add_action('create_term', array($this, 'save_taxonomy'));
37 add_action('edited_term', array($this, 'save_taxonomy'));
38 add_action('edit_user_profile_update', array($this, 'save_user'));
39 add_action('personal_options_update', array($this, 'save_user'));
40 add_action('user_register', array($this, 'save_user'));
41 add_filter("attachment_fields_to_save", array($this, 'save_attachment'), null , 2);
42
43
44 // shopp
45 add_action('shopp_category_saved', array($this, 'shopp_category_saved'));
46
47
48 // delete
49 add_action('delete_term', array($this, 'delete_term'), 10, 4);
50 }
51
52
53 /*
54 * validate_page
55 *
56 * @description: returns true | false. Used to stop a function from continuing
57 * @since 3.2.6
58 * @created: 23/06/12
59 */
60
61 function validate_page()
62 {
63 // global
64 global $pagenow;
65
66
67 // vars
68 $return = false;
69
70
71 // validate page
72 if( in_array( $pagenow, array( 'edit-tags.php', 'profile.php', 'user-new.php', 'user-edit.php', 'media.php' ) ) )
73 {
74 $return = true;
75 }
76
77
78 // validate page (Shopp)
79 if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" )
80 {
81 $return = true;
82 }
83
84
85 // return
86 return $return;
87 }
88
89
90 /*--------------------------------------------------------------------------------------
91 *
92 * admin_menu
93 *
94 * @author Elliot Condon
95 * @since 3.1.8
96 *
97 *-------------------------------------------------------------------------------------*/
98
99 function admin_menu()
100 {
101
102 global $pagenow;
103
104
105 // validate page
106 if( ! $this->validate_page() ) return;
107
108
109 // set page type
110 $filter = array();
111
112 if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" )
113 {
114
115 $this->data['page_type'] = "shopp_category";
116 $filter['ef_taxonomy'] = "shopp_category";
117
118 $this->data['page_action'] = "add";
119 $this->data['option_name'] = "";
120
121 if( $_GET['id'] != "new" )
122 {
123 $this->data['page_action'] = "edit";
124 $this->data['option_name'] = "shopp_category_" . $_GET['id'];
125 }
126
127 }
128 if( $pagenow == "edit-tags.php" && isset($_GET['taxonomy']) )
129 {
130
131 $this->data['page_type'] = "taxonomy";
132 $filter['ef_taxonomy'] = $_GET['taxonomy'];
133
134 $this->data['page_action'] = "add";
135 $this->data['option_name'] = "";
136
137 if(isset($_GET['action']) && $_GET['action'] == "edit")
138 {
139 $this->data['page_action'] = "edit";
140 $this->data['option_name'] = $_GET['taxonomy'] . "_" . $_GET['tag_ID'];
141 }
142
143 }
144 elseif( $pagenow == "profile.php" )
145 {
146
147 $this->data['page_type'] = "user";
148 $filter['ef_user'] = get_current_user_id();
149
150 $this->data['page_action'] = "edit";
151 $this->data['option_name'] = "user_" . get_current_user_id();
152
153 }
154 elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) )
155 {
156
157 $this->data['page_type'] = "user";
158 $filter['ef_user'] = $_GET['user_id'];
159
160 $this->data['page_action'] = "edit";
161 $this->data['option_name'] = "user_" . $_GET['user_id'];
162
163 }
164 elseif( $pagenow == "user-new.php" )
165 {
166 $this->data['page_type'] = "user";
167 $filter['ef_user'] ='all';
168
169 $this->data['page_action'] = "add";
170 $this->data['option_name'] = "";
171
172 }
173 elseif( $pagenow == "media.php" )
174 {
175
176 $this->data['page_type'] = "media";
177 $filter['ef_media'] = 'all';
178
179 $this->data['page_action'] = "add";
180 $this->data['option_name'] = "";
181
182 if(isset($_GET['attachment_id']))
183 {
184 $this->data['page_action'] = "edit";
185 $this->data['option_name'] = $_GET['attachment_id'];
186 }
187
188 }
189
190
191 // get field groups
192 $metabox_ids = array();
193 $this->data['metabox_ids'] = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
194
195
196 // dont continue if no ids were found
197 if( empty( $this->data['metabox_ids'] ) )
198 {
199 return false;
200 }
201
202
203 // actions
204 add_action('admin_enqueue_scripts', array($this,'admin_enqueue_scripts'));
205 add_action('admin_head', array($this,'admin_head'));
206
207
208 }
209
210
211 /*
212 * admin_enqueue_scripts
213 *
214 * @description:
215 * @since: 3.6
216 * @created: 30/01/13
217 */
218
219 function admin_enqueue_scripts()
220 {
221 do_action('acf/input/admin_enqueue_scripts');
222 }
223
224
225 /*--------------------------------------------------------------------------------------
226 *
227 * admin_head
228 *
229 * @author Elliot Condon
230 * @since 3.1.8
231 *
232 *-------------------------------------------------------------------------------------*/
233
234 function admin_head()
235 {
236 global $pagenow;
237
238
239 // add user js + css
240 do_action('acf/input/admin_head');
241
242
243 ?>
244 <script type="text/javascript">
245 (function($){
246
247 acf.data = {
248 action : 'acf/everything_fields',
249 metabox_ids : '<?php echo implode( ',', $this->data['metabox_ids'] ); ?>',
250 page_type : '<?php echo $this->data['page_type']; ?>',
251 page_action : '<?php echo $this->data['page_action']; ?>',
252 option_name : '<?php echo $this->data['option_name']; ?>'
253 };
254
255 $(document).ready(function(){
256
257 $.ajax({
258 url: ajaxurl,
259 data: acf.data,
260 type: 'post',
261 dataType: 'html',
262 success: function(html){
263
264 <?php
265 if($this->data['page_type'] == "user")
266 {
267 if($this->data['page_action'] == "add")
268 {
269 echo "$('#createuser > table.form-table:last > tbody').append( html );";
270 }
271 else
272 {
273 echo "$('#your-profile > p.submit').before( html );";
274 }
275 }
276 elseif($this->data['page_type'] == "shopp_category")
277 {
278 echo "$('#post-body-content').append( html );";
279 }
280 elseif($this->data['page_type'] == "taxonomy")
281 {
282 if($this->data['page_action'] == "add")
283 {
284 echo "$('#addtag > p.submit').before( html );";
285 }
286 else
287 {
288 echo "$('#edittag > table.form-table:last > tbody').append( html );";
289 }
290 }
291 elseif($this->data['page_type'] == "media")
292 {
293 if($this->data['page_action'] == "add")
294 {
295 echo "$('#addtag > p.submit').before( html );";
296 }
297 else
298 {
299 echo "$('#media-single-form table tbody tr.submit').before( html );";
300 }
301 }
302 ?>
303
304 setTimeout( function(){
305 $(document).trigger('acf/setup_fields', $('#wpbody') );
306 }, 200);
307
308 }
309 });
310
311
312 /*
313 * Taxonomy Add
314 *
315 * @description:
316 * @since: 3.6
317 * @created: 24/02/13
318 */
319
320 $(document).ajaxComplete(function(event, xhr, settings) {
321
322 // vars
323 data = acf.helpers.url_to_object(settings.data);
324
325
326 // validate
327 if( data.action != 'add-tag' )
328 {
329 return;
330 }
331
332
333 // clear WYSIWYG field
334 $('#addtag').find('.acf_wysiwyg textarea').each(function(){
335
336
337 // vars
338 var textarea = $(this),
339 id = textarea.attr('id'),
340 editor = tinyMCE.get( id );
341
342 editor.setContent('');
343 editor.save();
344
345
346 });
347
348 });
349
350 });
351
352
353 })(jQuery);
354 </script>
355 <?php
356 }
357
358
359
360 /*--------------------------------------------------------------------------------------
361 *
362 * save_taxonomy
363 *
364 * @author Elliot Condon
365 * @since 3.1.8
366 *
367 *-------------------------------------------------------------------------------------*/
368
369 function save_taxonomy( $term_id )
370 {
371 // verify nonce
372 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
373 {
374 return $term_id;
375 }
376
377
378 // for some weird reason, this is triggered by saving a menu...
379 if( !isset($_POST['taxonomy']) )
380 {
381 return;
382 }
383
384 // $post_id to save against
385 $post_id = $_POST['taxonomy'] . '_' . $term_id;
386
387 do_action('acf/save_post', $post_id);
388 }
389
390
391 /*--------------------------------------------------------------------------------------
392 *
393 * profile_save
394 *
395 * @author Elliot Condon
396 * @since 3.1.8
397 *
398 *-------------------------------------------------------------------------------------*/
399
400 function save_user( $user_id )
401 {
402 // verify nonce
403 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
404 {
405 return $user_id;
406 }
407
408
409 // $post_id to save against
410 $post_id = 'user_' . $user_id;
411
412
413 do_action('acf/save_post', $post_id);
414 }
415
416
417 /*--------------------------------------------------------------------------------------
418 *
419 * save_attachment
420 *
421 * @author Elliot Condon
422 * @since 3.1.8
423 *
424 *-------------------------------------------------------------------------------------*/
425
426 function save_attachment( $post, $attachment )
427 {
428 // verify nonce
429 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
430 {
431 return $post;
432 }
433
434
435 // $post_id to save against
436 $post_id = $post['ID'];
437
438
439 do_action('acf/save_post', $post_id);
440
441
442 return $post;
443 }
444
445
446 /*
447 * shopp_category_saved
448 *
449 * @description:
450 * @since 3.5.2
451 * @created: 27/11/12
452 */
453
454 function shopp_category_saved( $category )
455 {
456 // verify nonce
457 if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
458 {
459 return $category;
460 }
461
462
463 // $post_id to save against
464 $post_id = 'shopp_category_' . $category->id;
465
466
467 do_action('acf/save_post', $post_id);
468 }
469
470
471 /*--------------------------------------------------------------------------------------
472 *
473 * acf_everything_fields
474 *
475 * @description Ajax call that renders the html needed for the page
476 * @author Elliot Condon
477 * @since 3.1.8
478 *
479 *-------------------------------------------------------------------------------------*/
480
481 function acf_everything_fields()
482 {
483 // defaults
484 $defaults = array(
485 'metabox_ids' => '',
486 'page_type' => '',
487 'page_action' => '',
488 'option_name' => '',
489 );
490
491
492 // load post options
493 $options = array_merge($defaults, $_POST);
494
495
496 // metabox ids is a string with commas
497 $options['metabox_ids'] = explode( ',', $options['metabox_ids'] );
498
499
500 // get acfs
501 $acfs = apply_filters('acf/get_field_groups', false);
502
503
504 // layout
505 $layout = 'tr';
506 if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add")
507 {
508 $layout = 'div';
509 }
510 if( $options['page_type'] == "shopp_category")
511 {
512 $layout = 'metabox';
513 }
514
515
516 if( $acfs )
517 {
518 foreach( $acfs as $acf )
519 {
520 // load options
521 $acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
522
523
524 // only add the chosen field groups
525 if( !in_array( $acf['id'], $options['metabox_ids'] ) )
526 {
527 continue;
528 }
529
530
531 // title
532 if( $options['page_action'] == "edit" && $options['page_type'] == 'user' )
533 {
534 echo '<h3>' .$acf['title'] . '</h3><table class="form-table"><tbody>';
535 }
536
537
538 // wrapper
539 if( $layout == 'tr' )
540 {
541 //nonce
542 echo '<tr><td colspan="2"><input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" /></td></tr>';
543 }
544 else
545 {
546 //nonce
547 echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />';
548 }
549
550 if( $layout == 'metabox' )
551 {
552 echo '<div class="postbox acf_postbox" id="acf_'. $acf['id'] .'">';
553 echo '<div title="Click to toggle" class="handlediv"><br></div><h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
554 echo '<div class="inside">';
555 }
556
557
558 // load fields
559 $fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
560
561
562 if( is_array($fields) ){ foreach( $fields as $field ){
563
564 // if they didn't select a type, skip this field
565 if( !$field['type'] || $field['type'] == 'null' ) continue;
566
567
568 // set value
569 if( !isset($field['value']) )
570 {
571 $field['value'] = apply_filters('acf/load_value', false, $options['option_name'], $field);
572 $field['value'] = apply_filters('acf/format_value', $field['value'], $options['option_name'], $field);
573 }
574
575
576 // required
577 $required_class = "";
578 $required_label = "";
579
580 if( $field['required'] )
581 {
582 $required_class = ' required';
583 $required_label = ' <span class="required">*</span>';
584 }
585
586
587 if( $layout == 'metabox' )
588 {
589 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'] . '">';
590
591 echo '<p class="label">';
592 echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
593 echo $field['instructions'];
594 echo '</p>';
595
596 $field['name'] = 'fields[' . $field['key'] . ']';
597 do_action('acf/create_field', $field);
598
599 echo '</div>';
600 }
601 elseif( $layout == 'div' )
602 {
603 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'] . '">';
604
605 echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
606 $field['name'] = 'fields[' . $field['key'] . ']';
607 do_action('acf/create_field', $field );
608 if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>';
609
610 echo '</div>';
611 }
612 else
613 {
614 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'] . '">';
615 echo '<th valign="top" scope="row"><label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label></th>';
616 echo '<td>';
617 $field['name'] = 'fields[' . $field['key'] . ']';
618 do_action('acf/create_field', $field );
619
620 if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>';
621 echo '</td>';
622 echo '</tr>';
623
624 }
625
626 }}
627
628
629
630 // wrapper
631 if( $layout == 'metabox' )
632 {
633 echo '</div></div>';
634 }
635
636
637 // title
638 if( $options['page_action'] == "edit" && $options['page_type'] == 'user' )
639 {
640 echo '</tbody></table>';
641 }
642
643
644 }
645 // foreach($acfs as $acf)
646 }
647 // if($acfs)
648
649 // exit for ajax
650 die();
651
652 }
653
654
655 /*
656 * delete_term
657 *
658 * @description:
659 * @since: 3.5.7
660 * @created: 12/01/13
661 */
662
663 function delete_term( $term, $tt_id, $taxonomy, $deleted_term )
664 {
665 global $wpdb;
666
667 $values = $wpdb->query($wpdb->prepare(
668 "DELETE FROM $wpdb->options WHERE option_name LIKE %s",
669 '%' . $taxonomy . '_' . $term . '%'
670 ));
671 }
672
673
674 }
675
676 new acf_everything_fields();
677
678 ?>