PluginProbe
Advanced Custom Fields: Extended / 0.8.8.2
Advanced Custom Fields: Extended v0.8.8.2
0.9.2.7 0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 All 92 releases
acf-extended / includes / modules / dev.php
dev.php
677 lines 20.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH'))
4 exit;
5
6 // Check setting
7 if((!acfe_is_dev() && !acfe_is_super_dev()) || !acf_get_setting('show_admin'))
8 return;
9
10 if(!class_exists('acfe_dev')):
11
12 class acfe_dev{
13
14 public $wp_meta = array();
15 public $acf_meta = array();
16
17 function __construct(){
18
19 // Script debug
20 if(!defined('SCRIPT_DEBUG'))
21 define('SCRIPT_DEBUG', true);
22
23 // Additional Enqueue
24 add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
25
26 // Post
27 add_action('load-post.php', array($this, 'load_post'));
28 add_action('load-post-new.php', array($this, 'load_post'));
29
30 // Term
31 add_action('load-term.php', array($this, 'load_term'));
32
33 // User
34 add_action('show_user_profile', array($this, 'load_user'), 20);
35 add_action('edit_user_profile', array($this, 'load_user'), 20);
36
37 // Options
38 add_action('acf/options_page/submitbox_before_major_actions', array($this, 'load_options'));
39
40 add_action('wp_ajax_acfe/delete_meta', array($this, 'ajax_delete_meta'));
41 add_action('wp_ajax_acfe/bulk_delete_meta', array($this, 'ajax_bulk_delete_meta'));
42
43 }
44
45 /*
46 * Enqueue Scripts
47 */
48 function admin_enqueue_scripts(){
49
50 // bail early if not valid screen
51 if(!acf_is_screen(array('profile-network', 'user-edit-network', 'user-network'))){
52 return;
53 }
54
55 // enqueue
56 acf_enqueue_scripts();
57
58 }
59
60 /*
61 * Post
62 */
63 function load_post(){
64
65 global $typenow;
66
67 // Remove WP post meta box
68 remove_meta_box('postcustom', false, 'normal');
69
70 $reserved = acfe_get_setting('reserved_post_types', array());
71
72 if(!acfe_is_super_dev() && in_array($typenow, $reserved))
73 return;
74
75 // actions
76 add_action('add_meta_boxes', array($this, 'edit_post'), 10, 2);
77
78 }
79
80 function edit_post($post_type, $post){
81
82 // Get Post ID
83 $post_id = $post->ID;
84
85 // Add Meta Boxes
86 $this->add_meta_boxes($post_id, $post_type);
87
88 }
89
90 /*
91 * Term
92 */
93 function load_term(){
94
95 $screen = get_current_screen();
96 $taxonomy = $screen->taxonomy;
97
98 // actions
99 add_action("{$taxonomy}_edit_form", array($this, 'edit_term'), 10, 2);
100
101 }
102
103 function edit_term($term, $taxonomy){
104
105 // Get Term ID
106 $post_id = 'term_' . $term->term_id;
107
108 // Add Meta Boxes
109 $this->add_meta_boxes($post_id, "edit-{$taxonomy}");
110
111 }
112
113 /*
114 * User
115 */
116 function load_user($user){
117
118 $post_id = 'user_' . $user->ID;
119
120 // Add Meta Boxes
121 $this->add_meta_boxes($post_id, array('profile', 'user-edit'));
122
123 }
124
125 /*
126 * Admin
127 */
128 function load_options($page){
129
130 $this->add_meta_boxes($page['post_id'], 'acf_options_page');
131
132 }
133
134 /*
135 * Add Meta Boxes
136 */
137 function add_meta_boxes($post_id, $object_type){
138
139 // Get Meta
140 $this->get_meta($post_id);
141
142 do_action('acfe/dev/add_meta_boxes', $post_id, $object_type);
143
144 $render_bulk = false;
145
146 // WP Metabox
147 if(!empty($this->wp_meta)){
148
149 if(empty($this->acf_meta))
150 $render_bulk = true;
151
152 $id = 'acfe-wp-custom-fields';
153 $title = 'WP Custom Fields';
154
155 if($object_type === 'acf_options_page'){
156 $title = 'WP Options Meta';
157 }
158
159 $title .= '<span class="acfe_dev_meta_count">' . count($this->wp_meta) . '</span>';
160 $context = 'normal';
161 $priority = 'low';
162
163 add_meta_box($id, $title, array($this, 'render_meta_box'), $object_type, $context, $priority, array('table_type' => 'wp', 'object_type' => $object_type, 'render_bulk' => $render_bulk));
164
165 }
166
167 // ACF Metabox
168 if(!empty($this->acf_meta)){
169
170 if(!$render_bulk)
171 $render_bulk = true;
172
173 $id = 'acfe-acf-custom-fields';
174 $title = 'ACF Custom Fields';
175
176 if($object_type === 'acf_options_page'){
177 $title = 'ACF Options Meta';
178 }
179
180 $title .= '<span class="acfe_dev_meta_count">' . count($this->acf_meta) . '</span>';
181 $context = 'normal';
182 $priority = 'low';
183
184 add_meta_box($id, $title, array($this, 'render_meta_box'), $object_type, $context, $priority, array('table_type' => 'acf', 'object_type' => $object_type, 'render_bulk' => $render_bulk));
185
186 }
187
188 }
189
190 function render_meta_box($post, $metabox){
191
192 $table_type = $metabox['args']['table_type'];
193 $object_type = $metabox['args']['object_type'];
194 $render_bulk = $metabox['args']['render_bulk'];
195
196 $is_options = ($object_type === 'acf_options_page');
197 $is_acf = ($table_type === 'acf');
198
199 $metas = $this->wp_meta;
200
201 if($is_acf)
202 $metas = $this->acf_meta;
203
204 ?>
205 <table class="wp-list-table widefat fixed striped" style="border:0;">
206
207 <thead>
208 <tr>
209
210 <?php if(current_user_can(acf_get_setting('capability'))){ ?>
211 <td scope="col" class="check-column"><input type="checkbox" /></td>
212 <?php } ?>
213
214 <th scope="col" style="width:30%;">Name</th>
215 <th scope="col" style="width:auto;">Value</th>
216
217 <?php if($is_acf){ ?>
218 <th scope="col" style="width:100px;">Field Type</th>
219 <th scope="col" style="width:120px;">Field group</th>
220 <?php } ?>
221
222 <?php if($is_options){ ?>
223 <th scope="col" style="width:65px;">Autoload</th>
224 <?php } ?>
225
226 </tr>
227 </thead>
228
229 <tbody>
230
231 <?php foreach($metas as $meta){ ?>
232
233 <?php
234
235 // WP Meta
236 $meta_key = $meta['key'];
237 $meta_id = $meta['id'];
238 $value = $this->render_meta_value($meta['value']);
239 $type = $meta['type'];
240
241 // ACF Meta
242 if($is_acf){
243
244 $field_type = acf_maybe_get($meta, 'field_type');
245
246 }
247
248
249 $field_group = acf_maybe_get($meta, 'field_group');
250
251 $nonce = wp_create_nonce('acfe_delete_meta_' . $meta_id);
252 ?>
253
254 <tr class="acfe_dev_meta_<?php echo $is_options ? $meta_key : $meta_id; ?>">
255
256 <?php if(current_user_can(acf_get_setting('capability'))){ ?>
257 <th scope="row" class="check-column">
258 <input type="checkbox" class="acfe_bulk_delete_meta" value="<?php echo $is_options ? $meta_key : $meta_id; ?>" />
259 </th>
260 <?php } ?>
261
262 <td>
263 <strong><?php echo esc_attr($meta_key); ?></strong>
264
265 <?php if(current_user_can(acf_get_setting('capability'))){ ?>
266
267 <div class="row-actions">
268
269 <?php if($is_options){ ?>
270 <span class="edit">
271 <a href="<?php echo admin_url('options-general.php?page=acfe-options&action=edit&option=' . $meta_id); ?>"><?php _e('Edit'); ?></a> |
272 </span>
273 <?php } ?>
274
275 <span class="delete">
276 <a href="#" class="acfe_delete_meta" data-meta-id="<?php echo $meta_id; ?>" data-meta-key="<?php echo $meta_key; ?>" data-type="<?php echo $type; ?>" data-nonce="<?php echo $nonce; ?>"><?php _e('Delete'); ?></a>
277 </span>
278
279 </div>
280
281 <?php } ?>
282
283 </td>
284
285 <td><?php echo $value; ?></td>
286
287 <?php if($is_acf){ ?>
288 <td><?php echo $field_type; ?></td>
289 <td><?php echo $field_group; ?></td>
290 <?php } ?>
291
292 <?php if($is_options){ ?>
293 <td><?php echo $meta['autoload']; ?></td>
294 <?php } ?>
295
296 </tr>
297
298 <?php } ?>
299
300 </tbody>
301
302 </table>
303
304 <?php if(current_user_can(acf_get_setting('capability')) && $render_bulk){ ?>
305
306 <div class="acfe_dev_bulk_actions tablenav bottom">
307
308 <div class="alignleft actions bulkactions">
309
310 <label for="bulk-action-selector-bottom" class="screen-reader-text"><?php _e('Select bulk action'); ?></label>
311
312 <input type="hidden" class="acfe_bulk_delete_meta_type" value="<?php echo $type; ?>" />
313
314 <?php $nonce = wp_create_nonce('acfe_bulk_delete_meta'); ?>
315 <input type="hidden" class="acfe_bulk_delete_meta_nonce" value="<?php echo $nonce; ?>" />
316
317 <select class="acfe_bulk_delete_meta_action">
318 <option value="-1"><?php _e('Bulk Actions'); ?></option>
319 <option value="delete"><?php _e('Delete'); ?></option>
320 </select>
321
322 <input type="submit" id="acfe_bulk_delete_meta_submit" class="button action" value="<?php _e('Apply'); ?>">
323
324 </div>
325
326 <br class="clear">
327
328 </div>
329
330 <?php } ?>
331
332 <?php
333
334 }
335
336 function render_meta_value($value){
337
338 $return = '';
339
340 // Empty
341 if(empty($value) && !is_numeric($value)){
342
343 $css = 'color:#aaa;';
344 $value = '(' . __('empty', 'acf') . ')';
345
346 $return = '<pre style="max-height:200px; overflow:auto; white-space: pre; ' . $css . '">' . print_r($value, true) . '</pre>';
347
348 }
349
350 // Serialized
351 elseif(is_serialized($value)){
352
353 $return = '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r(maybe_unserialize($value), true) . '</pre>';
354 $return .= '<pre style="max-height:200px; overflow:auto; white-space: unset; margin-top:10px; max-width:100%;">' . print_r($value, true) . '</pre>';
355
356 }
357
358 // HTML
359 elseif($value != strip_tags($value)){
360
361 $return = '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r(htmlentities($value), true) . '</pre>';
362
363 }
364
365 // Json
366 elseif(acfe_is_json($value)){
367
368 $return = '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r(json_decode($value), true) . '</pre>';
369 $return .= '<pre style="max-height:200px; overflow:auto; white-space: unset; margin-top:10px; max-width:100%;">' . print_r($value, true) . '</pre>';
370
371 }
372
373 // String
374 else{
375
376 $return = '<pre style="max-height:200px; overflow:auto; white-space: pre;">' . print_r($value, true) . '</pre>';
377
378 }
379
380 return $return;
381
382 }
383
384 function get_meta($post_id = 0){
385
386 if(!$post_id)
387 $post_id = acf_get_valid_post_id();
388
389 if(empty($post_id))
390 return;
391
392 $info = acf_get_post_id_info($post_id);
393
394 global $wpdb;
395
396 // Post
397 if($info['type'] === 'post'){
398
399 $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d ", $info['id']));
400
401 }
402
403 // Term
404 elseif($info['type'] === 'term'){
405
406 $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->termmeta WHERE term_id = %d ", $info['id']));
407
408 }
409
410 // User
411 elseif($info['type'] === 'user'){
412
413 $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d ", $info['id']));
414
415 }
416
417 // Option
418 elseif($info['type'] === 'option'){
419
420 $id = $info['id'];
421
422 $search = "{$id}_%";
423 $_search = "_{$id}_%";
424 $search_single = "{$id}";
425
426 $search = str_replace('_', '\_', $search);
427 $_search = str_replace('_', '\_', $_search);
428
429 $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s OR option_name = %s", $search, $_search, $search_single));
430
431 }
432
433 if(empty($get_meta))
434 return;
435
436 $wp_meta = array();
437
438 // Option
439 if($info['type'] === 'option'){
440
441 usort($get_meta, function($a, $b){
442 return strcmp($a->option_name, $b->option_name);
443 });
444
445 foreach($get_meta as $meta){
446
447 $wp_meta[$meta->option_name] = array(
448 'id' => $meta->option_id,
449 'key' => $meta->option_name,
450 'value' => $meta->option_value,
451 'autoload' => $meta->autoload,
452 'type' => $info['type'],
453 );
454
455 }
456
457 // Post / Term
458 }elseif($info['type'] === 'post' || $info['type'] === 'term'){
459
460 usort($get_meta, function($a, $b){
461 return strcmp($a->meta_key, $b->meta_key);
462 });
463
464 foreach($get_meta as $meta){
465
466 $wp_meta[$meta->meta_key] = array(
467 'id' => $meta->meta_id,
468 'key' => $meta->meta_key,
469 'value' => $meta->meta_value,
470 'type' => $info['type'],
471 );
472
473 }
474
475 // User
476 }elseif($info['type'] === 'user'){
477
478 usort($get_meta, function($a, $b){
479 return strcmp($a->meta_key, $b->meta_key);
480 });
481
482 foreach($get_meta as $meta){
483
484 $wp_meta[$meta->meta_key] = array(
485 'id' => $meta->umeta_id,
486 'key' => $meta->meta_key,
487 'value' => $meta->meta_value,
488 'type' => $info['type'],
489 );
490
491 }
492
493 }
494
495 $acf_meta = array();
496
497 foreach($wp_meta as $key => $meta){
498
499 // ACF Meta
500 if(isset($wp_meta["_$key"])){
501
502 $field = false;
503 $field_type_display = false;
504 $field_group_display = false;
505
506 $field_key = $wp_meta["_$key"]['value'];
507
508 // Value = field_abcde123456?
509 if(acf_is_field_key($field_key)){
510
511 $field = acf_get_field($field_key);
512
513 if(!$field){
514
515 $field_type_display = '<em>Undefined</em>';
516 $field_group_display = '<em>Undefined</em>';
517
518 // Check clone: field_123456abcdef_field_123456abcfed
519 $count = substr_count($field_key, 'field_');
520
521 if($count === 2){
522
523 $keys = explode('field_', $field_key);
524
525 $field_1 = 'field_' . substr($keys[1], 0, -1);
526 $field_2 = 'field_' . $keys[2];
527
528 $field = acf_get_field($field_2);
529
530 }
531
532 }
533
534 if($field){
535
536 $field_type = acf_get_field_type($field['type']);
537 $field_type_display = '<em>Undefined</em>';
538
539 if(isset($field_type->label))
540 $field_type_display = $field_type->label;
541
542 $field_group = acfe_get_field_group_from_field($field);
543 $field_group_display = '<em>Undefined</em>';
544
545 if($field_group){
546
547 $field_group_display = $field_group['title'];
548
549 if(!empty($field_group['ID'])){
550
551 $post_status = get_post_status($field_group['ID']);
552
553 if($post_status === 'publish' || $post_status === 'acf-disabled'){
554
555 $field_group_display = '<a href="' . admin_url('post.php?post=' . $field_group['ID'] . '&action=edit') . '">' . $field_group['title'] . '</a>';
556
557 }
558
559 }
560
561 }
562
563 }
564
565 }
566
567 $_meta = $wp_meta["_$key"];
568 $_meta['field_type'] = $field_type_display;
569 $_meta['field_group'] = $field_group_display;
570
571 $acf_meta[] = $_meta;
572
573 $_meta = $wp_meta[$key];
574 $_meta['field_type'] = $field_type_display;
575 $_meta['field_group'] = $field_group_display;
576
577 $acf_meta[] = $_meta;
578
579 // Unset WP Meta
580 unset($wp_meta["_$key"]);
581 unset($wp_meta[$key]);
582
583 }
584
585 }
586
587 $this->wp_meta = $wp_meta;
588 $this->acf_meta = $acf_meta;
589
590 }
591
592 function ajax_delete_meta(){
593
594 // Vars
595 $id = acf_maybe_get_POST('id');
596 $key = acf_maybe_get_POST('key');
597 $type = acf_maybe_get_POST('type');
598
599 // Check vars
600 if(!$id || !$key || !$type)
601 wp_die(0);
602
603 // Check referer
604 check_ajax_referer("acfe_delete_meta_$id");
605
606 if(!current_user_can(acf_get_setting('capability'))){
607 wp_die(-1);
608 }
609
610 // Delete option
611 if($type === 'option'){
612
613 if(delete_option($key))
614 wp_die(1);
615
616 // Delete meta
617 }else{
618
619 if(delete_metadata_by_mid($type, $id))
620 wp_die(1);
621
622 }
623
624 wp_die(0);
625
626 }
627
628 function ajax_bulk_delete_meta(){
629
630 // Vars
631 $ids = acf_maybe_get_POST('ids');
632 $type = acf_maybe_get_POST('type');
633
634 // Check vars
635 if(!$ids || !$type)
636 wp_die(0);
637
638 // Check referer
639 check_ajax_referer('acfe_bulk_delete_meta');
640
641 if(!current_user_can(acf_get_setting('capability'))){
642 wp_die(-1);
643 }
644
645 // Delete option
646 if($type === 'option'){
647
648 foreach($ids as $key){
649
650 delete_option($key);
651
652 }
653
654 wp_die(1);
655
656 // Delete meta
657 }else{
658
659 foreach($ids as $id){
660
661 delete_metadata_by_mid($type, $id);
662
663 }
664
665 wp_die(1);
666
667 }
668
669 wp_die(0);
670
671 }
672
673 }
674
675 new acfe_dev();
676
677 endif;