author.php
4 years ago
autosync.php
4 years ago
block-types.php
4 years ago
dev.php
4 years ago
forms-action-custom.php
4 years ago
forms-action-email.php
4 years ago
forms-action-post.php
4 years ago
forms-action-redirect.php
4 years ago
forms-action-term.php
4 years ago
forms-action-user.php
4 years ago
forms-cheatsheet.php
4 years ago
forms-front.php
4 years ago
forms-helpers.php
4 years ago
forms-hooks.php
4 years ago
forms.php
4 years ago
module.php
4 years ago
options-pages.php
4 years ago
options.class.php
4 years ago
options.php
4 years ago
post-types.php
4 years ago
single-meta.php
4 years ago
taxonomies.php
4 years ago
ui-settings.php
4 years ago
ui-term.php
4 years ago
ui-user.php
4 years ago
ui.php
4 years ago
dev.php
869 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('acfe_dev')): |
| 7 | |
| 8 | class acfe_dev{ |
| 9 | |
| 10 | public $wp_meta = array(); |
| 11 | public $acf_meta = array(); |
| 12 | |
| 13 | function __construct(){ |
| 14 | |
| 15 | // check settings |
| 16 | if((!acfe_is_dev() && !acfe_is_super_dev()) || !acf_current_user_can_admin()){ |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // network enqueue |
| 21 | add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 22 | |
| 23 | // load |
| 24 | add_action('acfe/load_post', array($this, 'load_post')); |
| 25 | add_action('acfe/load_post', array($this, 'clean_meta')); |
| 26 | add_action('acfe/load_posts', array($this, 'clean_meta')); |
| 27 | add_action('acfe/load_term', array($this, 'clean_meta')); |
| 28 | add_action('acfe/load_terms', array($this, 'clean_meta')); |
| 29 | add_action('acfe/load_user', array($this, 'clean_meta')); |
| 30 | add_action('acfe/load_users', array($this, 'clean_meta')); |
| 31 | add_action('acfe/load_settings', array($this, 'clean_meta')); |
| 32 | add_action('acfe/load_option', array($this, 'clean_meta')); |
| 33 | add_action('acfe/load_attachments', array($this, 'clean_meta')); |
| 34 | |
| 35 | // add meta boxes |
| 36 | add_action('acfe/add_post_meta_boxes', array($this, 'add_post_meta_boxes'), 10, 2); |
| 37 | add_action('acfe/add_posts_meta_boxes', array($this, 'add_posts_meta_boxes')); |
| 38 | add_action('acfe/add_term_meta_boxes', array($this, 'add_term_meta_boxes'), 10, 2); |
| 39 | add_action('acfe/add_terms_meta_boxes', array($this, 'add_terms_meta_boxes')); |
| 40 | add_action('acfe/add_user_meta_boxes', array($this, 'add_user_meta_boxes')); |
| 41 | add_action('acfe/add_option_meta_boxes', array($this, 'add_option_meta_boxes')); |
| 42 | |
| 43 | add_action('acfe/add_settings_meta_boxes', array($this, 'add_settings_meta_boxes')); |
| 44 | add_action('acfe/add_attachments_meta_boxes', array($this, 'add_attachments_meta_boxes')); |
| 45 | add_action('acfe/add_users_meta_boxes', array($this, 'add_users_meta_boxes')); |
| 46 | |
| 47 | // ajax |
| 48 | add_action('wp_ajax_acfe/delete_meta', array($this, 'ajax_delete_meta')); |
| 49 | add_action('wp_ajax_acfe/bulk_delete_meta', array($this, 'ajax_bulk_delete_meta')); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Enqueue Scripts |
| 55 | */ |
| 56 | function admin_enqueue_scripts(){ |
| 57 | |
| 58 | // bail early if not network screen |
| 59 | if(!acf_is_screen(array('profile-network', 'user-edit-network', 'user-network'))){ |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // enqueue |
| 64 | acf_enqueue_scripts(); |
| 65 | |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Load Post |
| 70 | */ |
| 71 | function load_post(){ |
| 72 | |
| 73 | // force remove wp post meta metabox |
| 74 | remove_meta_box('postcustom', false, 'normal'); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Clean Meta |
| 80 | */ |
| 81 | function clean_meta(){ |
| 82 | |
| 83 | $post_id = acfe_maybe_get_REQUEST('acfe_dev_clean'); |
| 84 | $nonce = acfe_maybe_get_REQUEST('acfe_dev_clean_nonce'); |
| 85 | |
| 86 | if($post_id && wp_verify_nonce($nonce, 'acfe_dev_clean')){ |
| 87 | |
| 88 | $deleted = acfe_delete_orphan_meta($post_id); |
| 89 | |
| 90 | set_transient('acfe_dev_clean', $deleted, 3600); // 1 hour |
| 91 | |
| 92 | // remove args |
| 93 | $url = remove_query_arg(array( |
| 94 | 'acfe_dev_clean', |
| 95 | 'acfe_dev_clean_nonce' |
| 96 | )); |
| 97 | |
| 98 | // add message |
| 99 | $url = add_query_arg(array( |
| 100 | 'message' => 'acfe_dev_clean' |
| 101 | ), $url); |
| 102 | |
| 103 | // redirect |
| 104 | wp_redirect($url); |
| 105 | exit; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | // success message |
| 110 | if(acf_maybe_get_GET('message') === 'acfe_dev_clean'){ |
| 111 | |
| 112 | $deleted = acf_get_array(get_transient('acfe_dev_clean')); |
| 113 | $count = count($deleted); |
| 114 | |
| 115 | if(isset($deleted['single_meta']) || isset($deleted['normal'])){ |
| 116 | |
| 117 | $count = 0; |
| 118 | $count += count(acf_maybe_get($deleted, 'single_meta', array())); |
| 119 | $count += count(acf_maybe_get($deleted, 'normal', array())); |
| 120 | |
| 121 | } |
| 122 | |
| 123 | if(!$deleted){ |
| 124 | |
| 125 | acf_add_admin_notice(__('No orphan meta found', 'acfe'), 'warning'); |
| 126 | |
| 127 | }else{ |
| 128 | |
| 129 | $link = ' <a href="#" data-acfe-modal="clean-meta-debug" data-acfe-modal-title="' . __('Deleted meta', 'acfe') . '" data-acfe-modal-footer="' . __('Close', 'acfe') . '">' . __('View', 'acfe') . '</a>'; |
| 130 | |
| 131 | add_action('admin_footer', function() use($deleted){ |
| 132 | ?> |
| 133 | <div class="acfe-modal" data-acfe-modal="clean-meta-debug"> |
| 134 | <div class="acfe-modal-spacer"> |
| 135 | <pre><?php print_r($deleted); ?></pre> |
| 136 | </div> |
| 137 | </div> |
| 138 | <?php |
| 139 | }); |
| 140 | |
| 141 | acf_add_admin_notice("{$count} meta cleaned.{$link}", 'success'); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | delete_transient('acfe_dev_clean'); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Post Meta Boxes |
| 153 | */ |
| 154 | function add_post_meta_boxes($post_type, $post){ |
| 155 | |
| 156 | // check restricted post types |
| 157 | if(acfe_is_post_type_reserved_dev($post_type)) return; |
| 158 | |
| 159 | // post id |
| 160 | $post_id = $post->ID; |
| 161 | |
| 162 | // add meta boxes |
| 163 | $this->add_meta_boxes($post_id, $post_type); |
| 164 | |
| 165 | if(acfe_is_single_meta_enabled($post_id) && !acf_is_filter_enabled('acfe/dev/clean_metabox')){ |
| 166 | |
| 167 | add_meta_box('acfe-clean-meta', 'Single Meta', array($this, 'render_clean_metabox'), $post_type, 'side', 'core', array('post_id' => $post_id)); |
| 168 | |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Post Type List |
| 175 | */ |
| 176 | function add_posts_meta_boxes($post_type){ |
| 177 | |
| 178 | // post id |
| 179 | $post_id = "{$post_type}_options"; |
| 180 | |
| 181 | // add meta boxes |
| 182 | $this->add_meta_boxes($post_id, 'edit'); |
| 183 | |
| 184 | if(acfe_is_single_meta_enabled($post_id) && !acf_is_filter_enabled('acfe/dev/clean_metabox')){ |
| 185 | |
| 186 | // enable sidebar |
| 187 | acf_enable_filter('acfe/post_type_list/side'); |
| 188 | acf_enable_filter('acfe/post_type_list/submitdiv'); |
| 189 | |
| 190 | add_meta_box('acfe-clean-meta', 'Single Meta', array($this, 'render_clean_metabox'), 'edit', 'side', 'core', array('post_id' => $post_id)); |
| 191 | |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Term Meta Boxes |
| 198 | */ |
| 199 | function add_term_meta_boxes($taxonomy, $term){ |
| 200 | |
| 201 | // post id |
| 202 | $post_id = "term_{$term->term_id}"; |
| 203 | |
| 204 | // add meta boxes |
| 205 | $this->add_meta_boxes($post_id, "edit-{$taxonomy}"); |
| 206 | |
| 207 | if(acfe_is_single_meta_enabled($post_id) && !acf_is_filter_enabled('acfe/dev/clean_metabox')){ |
| 208 | |
| 209 | add_meta_box('acfe-clean-meta', 'Single Meta', array($this, 'render_clean_metabox'), "edit-{$taxonomy}", 'side', 'core', array('post_id' => $post_id)); |
| 210 | |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Taxonomy List |
| 217 | */ |
| 218 | function add_terms_meta_boxes($taxonomy){ |
| 219 | |
| 220 | // post id |
| 221 | $post_id = "tax_{$taxonomy}_options"; |
| 222 | |
| 223 | // add meta boxes |
| 224 | $this->add_meta_boxes($post_id, 'edit'); |
| 225 | |
| 226 | if(acfe_is_single_meta_enabled($post_id) && !acf_is_filter_enabled('acfe/dev/clean_metabox')){ |
| 227 | |
| 228 | // enable sidebar |
| 229 | acf_enable_filter('acfe/taxonomy_list/side'); |
| 230 | acf_enable_filter('acfe/taxonomy_list/submitdiv'); |
| 231 | |
| 232 | add_meta_box('acfe-clean-meta', 'Single Meta', array($this, 'render_clean_metabox'), 'edit', 'side', 'core', array('post_id' => $post_id)); |
| 233 | |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * User Meta Boxes |
| 240 | */ |
| 241 | function add_user_meta_boxes($user){ |
| 242 | |
| 243 | // post id |
| 244 | $post_id = "user_{$user->ID}"; |
| 245 | |
| 246 | // add meta boxes |
| 247 | $this->add_meta_boxes($post_id, array('profile', 'user-edit')); |
| 248 | |
| 249 | if(acfe_is_single_meta_enabled($post_id) && !acf_is_filter_enabled('acfe/dev/clean_metabox')){ |
| 250 | |
| 251 | add_meta_box('acfe-clean-meta', 'Single Meta', array($this, 'render_clean_metabox'), array('profile', 'user-edit'), 'side', 'core', array('post_id' => $post_id)); |
| 252 | |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Options Page |
| 259 | */ |
| 260 | function add_option_meta_boxes($page){ |
| 261 | |
| 262 | // post id |
| 263 | $post_id = $page['post_id']; |
| 264 | |
| 265 | // add meta boxes |
| 266 | $this->add_meta_boxes($post_id, 'acf_options_page'); |
| 267 | |
| 268 | if(acfe_is_single_meta_enabled($post_id) && !acf_is_filter_enabled('acfe/dev/clean_metabox')){ |
| 269 | |
| 270 | add_meta_box('acfe-clean-meta', 'Single Meta', array($this, 'render_clean_metabox'), 'acf_options_page', 'side', 'core', array('post_id' => $post_id)); |
| 271 | |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Settings Page |
| 278 | */ |
| 279 | function add_settings_meta_boxes($page){ |
| 280 | |
| 281 | // post id |
| 282 | $post_id = $page; |
| 283 | |
| 284 | // add meta boxes |
| 285 | $this->add_meta_boxes($post_id, $page); |
| 286 | |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Attachment List |
| 291 | */ |
| 292 | function add_attachments_meta_boxes(){ |
| 293 | |
| 294 | // post id |
| 295 | $post_id = "attachment_options"; |
| 296 | |
| 297 | // add meta boxes |
| 298 | $this->add_meta_boxes($post_id, 'edit'); |
| 299 | |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * User List |
| 304 | */ |
| 305 | function add_users_meta_boxes(){ |
| 306 | |
| 307 | // post id |
| 308 | $post_id = "user_options"; |
| 309 | |
| 310 | // add meta boxes |
| 311 | $this->add_meta_boxes($post_id, 'edit'); |
| 312 | |
| 313 | } |
| 314 | |
| 315 | function render_clean_metabox($post, $metabox){ |
| 316 | |
| 317 | $post_id = $metabox['args']['post_id']; |
| 318 | |
| 319 | ?> |
| 320 | <a href="<?php echo add_query_arg(array('acfe_dev_clean' => $post_id, 'acfe_dev_clean_nonce' => wp_create_nonce('acfe_dev_clean'))); ?>" class="button acf-button"> |
| 321 | Clean orphan meta |
| 322 | </a> |
| 323 | <?php |
| 324 | |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Add Meta Boxes |
| 329 | */ |
| 330 | function add_meta_boxes($post_id, $object_type){ |
| 331 | |
| 332 | // Get Meta |
| 333 | $this->get_meta($post_id); |
| 334 | |
| 335 | do_action('acfe/dev/add_meta_boxes', $post_id, $object_type); |
| 336 | |
| 337 | $render_bulk = false; |
| 338 | |
| 339 | // WP Metabox |
| 340 | if(!empty($this->wp_meta)){ |
| 341 | |
| 342 | acf_enable_filter('acfe/post_type_list'); |
| 343 | acf_enable_filter('acfe/taxonomy_list'); |
| 344 | acf_enable_filter('acfe/user_list'); |
| 345 | acf_enable_filter('acfe/attachment_list'); |
| 346 | |
| 347 | if(empty($this->acf_meta)){ |
| 348 | $render_bulk = true; |
| 349 | } |
| 350 | |
| 351 | $id = 'acfe-wp-custom-fields'; |
| 352 | $title = 'WP Custom Fields'; |
| 353 | |
| 354 | if($object_type === 'acf_options_page'){ |
| 355 | $title = 'WP Options Meta'; |
| 356 | } |
| 357 | |
| 358 | $title .= '<span class="acfe_dev_meta_count">' . count($this->wp_meta) . '</span>'; |
| 359 | $context = 'normal'; |
| 360 | $priority = 'low'; |
| 361 | |
| 362 | 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)); |
| 363 | |
| 364 | } |
| 365 | |
| 366 | // ACF Metabox |
| 367 | if(!empty($this->acf_meta)){ |
| 368 | |
| 369 | acf_enable_filter('acfe/post_type_list'); |
| 370 | acf_enable_filter('acfe/taxonomy_list'); |
| 371 | acf_enable_filter('acfe/user_list'); |
| 372 | acf_enable_filter('acfe/attachment_list'); |
| 373 | |
| 374 | if(!$render_bulk){ |
| 375 | $render_bulk = true; |
| 376 | } |
| 377 | |
| 378 | $id = 'acfe-acf-custom-fields'; |
| 379 | $title = 'ACF Custom Fields'; |
| 380 | |
| 381 | if($object_type === 'acf_options_page'){ |
| 382 | $title = 'ACF Options Meta'; |
| 383 | } |
| 384 | |
| 385 | $title .= '<span class="acfe_dev_meta_count">' . count($this->acf_meta) . '</span>'; |
| 386 | $context = 'normal'; |
| 387 | $priority = 'low'; |
| 388 | |
| 389 | 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)); |
| 390 | |
| 391 | } |
| 392 | |
| 393 | } |
| 394 | |
| 395 | function render_meta_box($post, $metabox){ |
| 396 | |
| 397 | $table_type = $metabox['args']['table_type']; |
| 398 | $object_type = $metabox['args']['object_type']; |
| 399 | $render_bulk = $metabox['args']['render_bulk']; |
| 400 | |
| 401 | $is_options = $object_type === 'acf_options_page'; |
| 402 | $is_acf = $table_type === 'acf'; |
| 403 | |
| 404 | $metas = $is_acf ? $this->acf_meta : $this->wp_meta; |
| 405 | |
| 406 | ?> |
| 407 | <table class="wp-list-table widefat fixed striped"> |
| 408 | |
| 409 | <thead> |
| 410 | <tr> |
| 411 | |
| 412 | <?php if(current_user_can(acf_get_setting('capability'))){ ?> |
| 413 | <td scope="col" class="check-column"><input type="checkbox" /></td> |
| 414 | <?php } ?> |
| 415 | |
| 416 | <th scope="col" class="col-name">Name</th> |
| 417 | <th scope="col" class="col-value">Value</th> |
| 418 | |
| 419 | <?php if($is_acf){ ?> |
| 420 | <th scope="col" class="col-field-type">Field Type</th> |
| 421 | <th scope="col" class="col-field-group">Field group</th> |
| 422 | <?php } ?> |
| 423 | |
| 424 | <?php if($is_options){ ?> |
| 425 | <th scope="col" class="col-autoload">Autoload</th> |
| 426 | <?php } ?> |
| 427 | |
| 428 | </tr> |
| 429 | </thead> |
| 430 | |
| 431 | <tbody> |
| 432 | |
| 433 | <?php foreach($metas as $meta){ ?> |
| 434 | |
| 435 | <?php |
| 436 | |
| 437 | // WP Meta |
| 438 | $meta_key = $meta['key']; |
| 439 | $meta_id = $meta['id']; |
| 440 | $value = $this->render_meta_value($meta['value']); |
| 441 | $type = $meta['type']; |
| 442 | |
| 443 | // ACF |
| 444 | $field_type = acf_maybe_get($meta, 'field_type'); |
| 445 | $field_group = acf_maybe_get($meta, 'field_group'); |
| 446 | |
| 447 | $nonce = wp_create_nonce('acfe_delete_meta_' . $meta_id); |
| 448 | ?> |
| 449 | |
| 450 | <tr class="acfe_dev_meta_<?php echo $is_options ? $meta_key : $meta_id; ?>"> |
| 451 | |
| 452 | <?php if(current_user_can(acf_get_setting('capability'))){ ?> |
| 453 | <th scope="row" class="check-column"> |
| 454 | <input type="checkbox" class="acfe_bulk_delete_meta" value="<?php echo $is_options ? $meta_key : $meta_id; ?>" /> |
| 455 | </th> |
| 456 | <?php } ?> |
| 457 | |
| 458 | <td> |
| 459 | <strong><?php echo esc_attr($meta_key); ?></strong> |
| 460 | |
| 461 | <?php if(current_user_can(acf_get_setting('capability'))){ ?> |
| 462 | |
| 463 | <div class="row-actions"> |
| 464 | |
| 465 | <?php if($is_options){ ?> |
| 466 | <span class="edit"> |
| 467 | <a href="<?php echo admin_url('options-general.php?page=acfe-options&action=edit&option=' . $meta_id); ?>"><?php _e('Edit'); ?></a> | |
| 468 | </span> |
| 469 | <?php } ?> |
| 470 | |
| 471 | <span class="delete"> |
| 472 | <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> |
| 473 | </span> |
| 474 | |
| 475 | </div> |
| 476 | |
| 477 | <?php } ?> |
| 478 | |
| 479 | </td> |
| 480 | |
| 481 | <td><?php echo $value; ?></td> |
| 482 | |
| 483 | <?php if($is_acf){ ?> |
| 484 | <td><?php echo $field_type; ?></td> |
| 485 | <td><?php echo $field_group; ?></td> |
| 486 | <?php } ?> |
| 487 | |
| 488 | <?php if($is_options){ ?> |
| 489 | <td><?php echo $meta['autoload']; ?></td> |
| 490 | <?php } ?> |
| 491 | |
| 492 | </tr> |
| 493 | |
| 494 | <?php } ?> |
| 495 | |
| 496 | </tbody> |
| 497 | |
| 498 | </table> |
| 499 | |
| 500 | <?php if(current_user_can(acf_get_setting('capability')) && $render_bulk){ ?> |
| 501 | |
| 502 | <div class="acfe_dev_bulk_actions tablenav bottom"> |
| 503 | |
| 504 | <div class="alignleft actions bulkactions"> |
| 505 | |
| 506 | <label for="bulk-action-selector-bottom" class="screen-reader-text"><?php _e('Select bulk action'); ?></label> |
| 507 | |
| 508 | <input type="hidden" class="acfe_bulk_delete_meta_type" value="<?php echo $type; ?>" /> |
| 509 | |
| 510 | <?php $nonce = wp_create_nonce('acfe_bulk_delete_meta'); ?> |
| 511 | <input type="hidden" class="acfe_bulk_delete_meta_nonce" value="<?php echo $nonce; ?>" /> |
| 512 | |
| 513 | <select class="acfe_bulk_delete_meta_action"> |
| 514 | <option value="-1"><?php _e('Bulk Actions'); ?></option> |
| 515 | <option value="delete"><?php _e('Delete'); ?></option> |
| 516 | </select> |
| 517 | |
| 518 | <input type="submit" id="acfe_bulk_delete_meta_submit" class="button action" value="<?php _e('Apply'); ?>"> |
| 519 | |
| 520 | </div> |
| 521 | |
| 522 | <br class="clear"> |
| 523 | |
| 524 | </div> |
| 525 | |
| 526 | <?php } ?> |
| 527 | |
| 528 | <?php |
| 529 | |
| 530 | } |
| 531 | |
| 532 | function render_meta_value($value){ |
| 533 | |
| 534 | // Default: String |
| 535 | $return = '<pre>' . print_r($value, true) . '</pre>'; |
| 536 | |
| 537 | // Empty |
| 538 | if(empty($value) && !is_numeric($value)){ |
| 539 | |
| 540 | $css = 'color:#aaa;'; |
| 541 | $value = '(' . __('empty', 'acf') . ')'; |
| 542 | |
| 543 | $return = '<pre style="' . $css . '">' . print_r($value, true) . '</pre>'; |
| 544 | |
| 545 | } |
| 546 | |
| 547 | // Serialized |
| 548 | elseif(is_serialized($value)){ |
| 549 | |
| 550 | $return = '<pre>' . print_r(maybe_unserialize($value), true) . '</pre>'; |
| 551 | $return .= '<pre class="raw">' . print_r($value, true) . '</pre>'; |
| 552 | |
| 553 | } |
| 554 | |
| 555 | // HTML |
| 556 | elseif($value != strip_tags($value)){ |
| 557 | |
| 558 | $return = '<pre>' . print_r(htmlentities($value), true) . '</pre>'; |
| 559 | |
| 560 | } |
| 561 | |
| 562 | // Json |
| 563 | elseif(acfe_is_json($value)){ |
| 564 | |
| 565 | $return = '<pre>' . print_r(json_decode($value), true) . '</pre>'; |
| 566 | $return .= '<pre class="raw">' . print_r($value, true) . '</pre>'; |
| 567 | |
| 568 | } |
| 569 | |
| 570 | return $return; |
| 571 | |
| 572 | } |
| 573 | |
| 574 | function get_meta($post_id = 0){ |
| 575 | |
| 576 | // Validate post id |
| 577 | $post_id = acf_get_valid_post_id($post_id); |
| 578 | |
| 579 | // Post id empty |
| 580 | if(empty($post_id)) return; |
| 581 | |
| 582 | // Decode post id |
| 583 | $info = acf_decode_post_id($post_id); |
| 584 | |
| 585 | global $wpdb; |
| 586 | |
| 587 | // Post |
| 588 | if($info['type'] === 'post'){ |
| 589 | |
| 590 | $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE post_id = %d ", $info['id'])); |
| 591 | |
| 592 | } |
| 593 | |
| 594 | // Term |
| 595 | elseif($info['type'] === 'term'){ |
| 596 | |
| 597 | $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->termmeta WHERE term_id = %d ", $info['id'])); |
| 598 | |
| 599 | } |
| 600 | |
| 601 | // User |
| 602 | elseif($info['type'] === 'user'){ |
| 603 | |
| 604 | $get_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d ", $info['id'])); |
| 605 | |
| 606 | } |
| 607 | |
| 608 | // Option |
| 609 | elseif($info['type'] === 'option'){ |
| 610 | |
| 611 | $id = $info['id']; |
| 612 | |
| 613 | $search = "{$id}_%"; |
| 614 | $_search = "_{$id}_%"; |
| 615 | $search_single = "{$id}"; |
| 616 | |
| 617 | $search = str_replace('_', '\_', $search); |
| 618 | $_search = str_replace('_', '\_', $_search); |
| 619 | |
| 620 | $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)); |
| 621 | |
| 622 | } |
| 623 | |
| 624 | // No meta found |
| 625 | if(empty($get_meta)) return; |
| 626 | |
| 627 | $wp_meta = array(); |
| 628 | |
| 629 | // Option |
| 630 | if($info['type'] === 'option'){ |
| 631 | |
| 632 | usort($get_meta, function($a, $b){ |
| 633 | return strcmp($a->option_name, $b->option_name); |
| 634 | }); |
| 635 | |
| 636 | foreach($get_meta as $meta){ |
| 637 | |
| 638 | $wp_meta[$meta->option_name] = array( |
| 639 | 'id' => $meta->option_id, |
| 640 | 'key' => $meta->option_name, |
| 641 | 'value' => $meta->option_value, |
| 642 | 'autoload' => $meta->autoload, |
| 643 | 'type' => $info['type'], |
| 644 | ); |
| 645 | |
| 646 | } |
| 647 | |
| 648 | // Post / Term |
| 649 | }elseif($info['type'] === 'post' || $info['type'] === 'term'){ |
| 650 | |
| 651 | usort($get_meta, function($a, $b){ |
| 652 | return strcmp($a->meta_key, $b->meta_key); |
| 653 | }); |
| 654 | |
| 655 | foreach($get_meta as $meta){ |
| 656 | |
| 657 | $wp_meta[$meta->meta_key] = array( |
| 658 | 'id' => $meta->meta_id, |
| 659 | 'key' => $meta->meta_key, |
| 660 | 'value' => $meta->meta_value, |
| 661 | 'type' => $info['type'], |
| 662 | ); |
| 663 | |
| 664 | } |
| 665 | |
| 666 | // User |
| 667 | }elseif($info['type'] === 'user'){ |
| 668 | |
| 669 | usort($get_meta, function($a, $b){ |
| 670 | return strcmp($a->meta_key, $b->meta_key); |
| 671 | }); |
| 672 | |
| 673 | foreach($get_meta as $meta){ |
| 674 | |
| 675 | $wp_meta[$meta->meta_key] = array( |
| 676 | 'id' => $meta->umeta_id, |
| 677 | 'key' => $meta->meta_key, |
| 678 | 'value' => $meta->meta_value, |
| 679 | 'type' => $info['type'], |
| 680 | ); |
| 681 | |
| 682 | } |
| 683 | |
| 684 | } |
| 685 | |
| 686 | $acf_meta = array(); |
| 687 | |
| 688 | foreach($wp_meta as $key => $meta){ |
| 689 | |
| 690 | // Bail early if no prefix found |
| 691 | if(!isset($wp_meta["_$key"])) continue; |
| 692 | |
| 693 | // Check if key is field_abcde123456? |
| 694 | if(!acf_is_field_key($wp_meta["_$key"]['value'])) continue; |
| 695 | |
| 696 | // Vars |
| 697 | $field_key = $wp_meta["_$key"]['value']; |
| 698 | $field_type = '<em>Undefined</em>'; |
| 699 | $field_group_title = '<em>Undefined</em>'; |
| 700 | |
| 701 | // Get field |
| 702 | $field = acf_get_field($field_key); |
| 703 | |
| 704 | // Check clone in sub field: field_123456abcdef_field_123456abcfed |
| 705 | if(!$field && substr_count($field_key, 'field_') === 2){ |
| 706 | |
| 707 | // get field key (last part) |
| 708 | $_field_key = substr($field_key, strrpos($field_key, 'field_')); |
| 709 | |
| 710 | // get field |
| 711 | $field = acf_get_field($_field_key); |
| 712 | |
| 713 | } |
| 714 | |
| 715 | // Found field |
| 716 | if($field){ |
| 717 | |
| 718 | // Field type |
| 719 | $field_type = acf_get_field_type($field['type']); |
| 720 | $field_type = acfe_maybe_get($field_type, 'label', '<em>Undefined</em>'); |
| 721 | |
| 722 | // Field Group |
| 723 | $field_group = acfe_get_field_group_from_field($field); |
| 724 | |
| 725 | if($field_group){ |
| 726 | |
| 727 | $field_group_title = $field_group['title']; |
| 728 | |
| 729 | // no id setting, try to get raw field group from db |
| 730 | if(!$field_group['ID']){ |
| 731 | $field_group = acf_get_raw_field_group($field_group['key']); |
| 732 | } |
| 733 | |
| 734 | // found db field group |
| 735 | if($field_group['ID']){ |
| 736 | |
| 737 | $post_status = get_post_status($field_group['ID']); |
| 738 | |
| 739 | if($post_status === 'publish' || $post_status === 'acf-disabled'){ |
| 740 | |
| 741 | $field_group_title = '<a href="' . admin_url('post.php?post=' . $field_group['ID'] . '&action=edit') . '">' . $field_group['title'] . '</a>'; |
| 742 | |
| 743 | } |
| 744 | |
| 745 | } |
| 746 | |
| 747 | } |
| 748 | |
| 749 | } |
| 750 | |
| 751 | // Assign ACF meta: prefix |
| 752 | $_meta = $wp_meta["_$key"]; |
| 753 | $_meta['field_type'] = $field_type; |
| 754 | $_meta['field_group'] = $field_group_title; |
| 755 | |
| 756 | $acf_meta[] = $_meta; |
| 757 | |
| 758 | // Assign ACF meta: normal |
| 759 | $_meta = $wp_meta[$key]; |
| 760 | $_meta['field_type'] = $field_type; |
| 761 | $_meta['field_group'] = $field_group_title; |
| 762 | |
| 763 | $acf_meta[] = $_meta; |
| 764 | |
| 765 | // Unset WP Meta |
| 766 | unset($wp_meta["_$key"]); |
| 767 | unset($wp_meta[$key]); |
| 768 | |
| 769 | } |
| 770 | |
| 771 | $this->wp_meta = $wp_meta; |
| 772 | $this->acf_meta = $acf_meta; |
| 773 | |
| 774 | } |
| 775 | |
| 776 | function ajax_delete_meta(){ |
| 777 | |
| 778 | // Vars |
| 779 | $id = acf_maybe_get_POST('id'); |
| 780 | $key = acf_maybe_get_POST('key'); |
| 781 | $type = acf_maybe_get_POST('type'); |
| 782 | |
| 783 | // Check vars |
| 784 | if(!$id || !$key || !$type){ |
| 785 | wp_die(0); |
| 786 | } |
| 787 | |
| 788 | // Check referer |
| 789 | check_ajax_referer("acfe_delete_meta_$id"); |
| 790 | |
| 791 | if(!current_user_can(acf_get_setting('capability'))){ |
| 792 | wp_die(-1); |
| 793 | } |
| 794 | |
| 795 | // Delete option |
| 796 | if($type === 'option'){ |
| 797 | |
| 798 | if(delete_option($key)){ |
| 799 | wp_die(1); |
| 800 | } |
| 801 | |
| 802 | // Delete meta |
| 803 | }else{ |
| 804 | |
| 805 | if(delete_metadata_by_mid($type, $id)){ |
| 806 | wp_die(1); |
| 807 | } |
| 808 | |
| 809 | } |
| 810 | |
| 811 | wp_die(0); |
| 812 | |
| 813 | } |
| 814 | |
| 815 | function ajax_bulk_delete_meta(){ |
| 816 | |
| 817 | // Vars |
| 818 | $ids = acf_maybe_get_POST('ids'); |
| 819 | $type = acf_maybe_get_POST('type'); |
| 820 | |
| 821 | // Check vars |
| 822 | if(!$ids || !$type){ |
| 823 | wp_die(0); |
| 824 | } |
| 825 | |
| 826 | // Check referer |
| 827 | check_ajax_referer('acfe_bulk_delete_meta'); |
| 828 | |
| 829 | if(!current_user_can(acf_get_setting('capability'))){ |
| 830 | wp_die(-1); |
| 831 | } |
| 832 | |
| 833 | // Delete option |
| 834 | if($type === 'option'){ |
| 835 | |
| 836 | foreach($ids as $key){ |
| 837 | delete_option($key); |
| 838 | } |
| 839 | |
| 840 | wp_die(1); |
| 841 | |
| 842 | } |
| 843 | |
| 844 | // Delete meta |
| 845 | foreach($ids as $id){ |
| 846 | delete_metadata_by_mid($type, $id); |
| 847 | } |
| 848 | |
| 849 | wp_die(1); |
| 850 | |
| 851 | } |
| 852 | |
| 853 | } |
| 854 | |
| 855 | acf_new_instance('acfe_dev'); |
| 856 | |
| 857 | endif; |
| 858 | |
| 859 | function acfe_dev_get_wp_meta(){ |
| 860 | return acf_get_instance('acfe_dev')->wp_meta; |
| 861 | } |
| 862 | |
| 863 | function acfe_dev_get_acf_meta(){ |
| 864 | return acf_get_instance('acfe_dev')->acf_meta; |
| 865 | } |
| 866 | |
| 867 | function acfe_dev_count_meta(){ |
| 868 | return count(acfe_dev_get_wp_meta()) + count(acfe_dev_get_acf_meta()); |
| 869 | } |