| 1 |
<?php |
| 2 |
|
| 3 |
if(!defined('ABSPATH')){ |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if(!class_exists('acfe_dev')): |
| 8 |
|
| 9 |
class acfe_dev{ |
| 10 |
|
| 11 |
var $wp_meta = array(), |
| 12 |
$acf_meta = array(), |
| 13 |
$type = ''; |
| 14 |
|
| 15 |
function __construct(){ |
| 16 |
|
| 17 |
// check settings |
| 18 |
if((!acfe_is_dev() && !acfe_is_super_dev()) || !acf_current_user_can_admin()){ |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
// enqueue |
| 23 |
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); |
| 24 |
|
| 25 |
// load |
| 26 |
add_action('acfe/load_post', array($this, 'load_post')); |
| 27 |
|
| 28 |
// add meta boxes |
| 29 |
add_action('acfe/add_post_meta_boxes', array($this, 'add_post_meta_boxes'), 10, 2); |
| 30 |
add_action('acfe/add_posts_meta_boxes', array($this, 'add_posts_meta_boxes')); |
| 31 |
add_action('acfe/add_term_meta_boxes', array($this, 'add_term_meta_boxes'), 10, 2); |
| 32 |
add_action('acfe/add_terms_meta_boxes', array($this, 'add_terms_meta_boxes')); |
| 33 |
add_action('acfe/add_user_meta_boxes', array($this, 'add_user_meta_boxes')); |
| 34 |
add_action('acfe/add_option_meta_boxes', array($this, 'add_option_meta_boxes')); |
| 35 |
|
| 36 |
add_action('acfe/add_settings_meta_boxes', array($this, 'add_settings_meta_boxes')); |
| 37 |
add_action('acfe/add_attachment_meta_boxes', array($this, 'add_attachment_meta_boxes')); |
| 38 |
add_action('acfe/add_attachments_meta_boxes', array($this, 'add_attachments_meta_boxes')); |
| 39 |
add_action('acfe/add_users_meta_boxes', array($this, 'add_users_meta_boxes')); |
| 40 |
|
| 41 |
// table render |
| 42 |
add_filter('acfe/dev/meta/columns', array($this, 'meta_columns'), 10, 3); |
| 43 |
add_action('acfe/dev/meta/render_column', array($this, 'meta_render_column'), 10, 4); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* admin_enqueue_scripts |
| 50 |
*/ |
| 51 |
function admin_enqueue_scripts(){ |
| 52 |
|
| 53 |
// enqueue acf on network screen |
| 54 |
if(acf_is_screen(array('profile-network', 'user-edit-network', 'user-network'))){ |
| 55 |
acf_enqueue_scripts(); |
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* load_post |
| 63 |
* |
| 64 |
* acfe/load_post |
| 65 |
*/ |
| 66 |
function load_post(){ |
| 67 |
|
| 68 |
// force remove wp post meta metabox |
| 69 |
remove_meta_box('postcustom', false, 'normal'); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* add_post_meta_boxes |
| 76 |
* |
| 77 |
* acfe/add_post_meta_boxes |
| 78 |
* |
| 79 |
* @param $post_type |
| 80 |
* @param $post |
| 81 |
*/ |
| 82 |
function add_post_meta_boxes($post_type, $post){ |
| 83 |
|
| 84 |
// check restricted post types |
| 85 |
if(acfe_is_post_type_reserved_dev($post_type)){ |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// post id |
| 90 |
$post_id = $post->ID; |
| 91 |
|
| 92 |
// add meta boxes |
| 93 |
$this->add_meta_boxes($post_id, $post_type); |
| 94 |
|
| 95 |
// action |
| 96 |
do_action('acfe/dev/add_meta_boxes', $post_id, $post_type, 'post'); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* add_posts_meta_boxes |
| 103 |
* |
| 104 |
* acfe/add_posts_meta_boxes |
| 105 |
* |
| 106 |
* @param $post_type |
| 107 |
*/ |
| 108 |
function add_posts_meta_boxes($post_type){ |
| 109 |
|
| 110 |
// post id |
| 111 |
$post_id = "{$post_type}_options"; |
| 112 |
|
| 113 |
// add meta boxes |
| 114 |
$this->add_meta_boxes($post_id, 'edit'); |
| 115 |
|
| 116 |
// action |
| 117 |
do_action('acfe/dev/add_meta_boxes', $post_id, 'edit', 'posts'); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* add_term_meta_boxes |
| 124 |
* |
| 125 |
* acfe/add_term_meta_boxes |
| 126 |
* |
| 127 |
* @param $taxonomy |
| 128 |
* @param $term |
| 129 |
*/ |
| 130 |
function add_term_meta_boxes($taxonomy, $term){ |
| 131 |
|
| 132 |
// post id |
| 133 |
$post_id = "term_{$term->term_id}"; |
| 134 |
|
| 135 |
// add meta boxes |
| 136 |
$this->add_meta_boxes($post_id, "edit-{$taxonomy}"); |
| 137 |
|
| 138 |
// action |
| 139 |
do_action('acfe/dev/add_meta_boxes', $post_id, "edit-{$taxonomy}", 'term'); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* add_terms_meta_boxes |
| 146 |
* |
| 147 |
* acfe/add_terms_meta_boxes |
| 148 |
* |
| 149 |
* @param $taxonomy |
| 150 |
*/ |
| 151 |
function add_terms_meta_boxes($taxonomy){ |
| 152 |
|
| 153 |
// post id |
| 154 |
$post_id = "tax_{$taxonomy}_options"; |
| 155 |
|
| 156 |
// add meta boxes |
| 157 |
$this->add_meta_boxes($post_id, 'edit'); |
| 158 |
|
| 159 |
// action |
| 160 |
do_action('acfe/dev/add_meta_boxes', $post_id, 'edit', 'terms'); |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* add_user_meta_boxes |
| 167 |
* |
| 168 |
* acfe/add_user_meta_boxes |
| 169 |
* |
| 170 |
* @param $user |
| 171 |
*/ |
| 172 |
function add_user_meta_boxes($user){ |
| 173 |
|
| 174 |
// post id |
| 175 |
$post_id = "user_{$user->ID}"; |
| 176 |
|
| 177 |
// add meta boxes |
| 178 |
$this->add_meta_boxes($post_id, array('profile', 'user-edit')); |
| 179 |
|
| 180 |
// action |
| 181 |
do_action('acfe/dev/add_meta_boxes', $post_id, array('profile', 'user-edit'), 'user'); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* add_option_meta_boxes |
| 188 |
* |
| 189 |
* acfe/add_option_meta_boxes |
| 190 |
* |
| 191 |
* @param $page |
| 192 |
*/ |
| 193 |
function add_option_meta_boxes($page){ |
| 194 |
|
| 195 |
// post id |
| 196 |
$post_id = $page['post_id']; |
| 197 |
|
| 198 |
// add meta boxes |
| 199 |
$this->add_meta_boxes($post_id, 'acf_options_page'); |
| 200 |
|
| 201 |
// action |
| 202 |
do_action('acfe/dev/add_meta_boxes', $post_id, 'acf_options_page', 'option'); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* add_settings_meta_boxes |
| 209 |
* |
| 210 |
* acfe/add_settings_meta_boxes |
| 211 |
* |
| 212 |
* @param $page |
| 213 |
*/ |
| 214 |
function add_settings_meta_boxes($page){ |
| 215 |
|
| 216 |
// post id |
| 217 |
$post_id = $page; |
| 218 |
|
| 219 |
// add meta boxes |
| 220 |
$this->add_meta_boxes($post_id, $page); |
| 221 |
|
| 222 |
do_action('acfe/dev/add_meta_boxes', $post_id, $page, 'settings'); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* add_attachment_meta_boxes |
| 229 |
* |
| 230 |
* acfe/add_attachment_meta_boxes |
| 231 |
* |
| 232 |
* @param $post |
| 233 |
*/ |
| 234 |
function add_attachment_meta_boxes($post){ |
| 235 |
|
| 236 |
// vars |
| 237 |
$post_id = $post->ID; |
| 238 |
$post_type = $post->post_type; |
| 239 |
|
| 240 |
// add meta boxes |
| 241 |
$this->add_meta_boxes($post_id, $post_type); |
| 242 |
|
| 243 |
do_action('acfe/dev/add_meta_boxes', $post_id, $post_type, 'attachment'); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* add_attachments_meta_boxes |
| 250 |
* |
| 251 |
* acfe/add_attachments_meta_boxes |
| 252 |
*/ |
| 253 |
function add_attachments_meta_boxes(){ |
| 254 |
|
| 255 |
// post id |
| 256 |
$post_id = "attachment_options"; |
| 257 |
|
| 258 |
// add meta boxes |
| 259 |
$this->add_meta_boxes($post_id, 'edit'); |
| 260 |
|
| 261 |
do_action('acfe/dev/add_meta_boxes', $post_id, 'edit', 'attachments'); |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* add_users_meta_boxes |
| 268 |
* |
| 269 |
* acfe/add_users_meta_boxes |
| 270 |
*/ |
| 271 |
function add_users_meta_boxes(){ |
| 272 |
|
| 273 |
// post id |
| 274 |
$post_id = "user_options"; |
| 275 |
|
| 276 |
// add meta boxes |
| 277 |
$this->add_meta_boxes($post_id, 'edit'); |
| 278 |
|
| 279 |
do_action('acfe/dev/add_meta_boxes', $post_id, 'edit', 'users'); |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* add_meta_boxes |
| 286 |
* |
| 287 |
* @param $post_id |
| 288 |
* @param $screen |
| 289 |
*/ |
| 290 |
function add_meta_boxes($post_id, $screen){ |
| 291 |
|
| 292 |
// setup meta |
| 293 |
$this->setup_meta($post_id); |
| 294 |
|
| 295 |
// do action |
| 296 |
// do_action('acfe/dev/add_meta_boxes', $post_id, $screen); |
| 297 |
|
| 298 |
// vars |
| 299 |
$bulk = false; |
| 300 |
$context = 'normal'; |
| 301 |
$priority = 'low'; |
| 302 |
|
| 303 |
// wp meta |
| 304 |
if(!empty($this->wp_meta)){ |
| 305 |
|
| 306 |
// display metabox on object lists screen |
| 307 |
acf_set_filters(array( |
| 308 |
'acfe/post_type_list' => true, |
| 309 |
'acfe/taxonomy_list' => true, |
| 310 |
'acfe/user_list' => true, |
| 311 |
'acfe/attachment_list' => true, |
| 312 |
)); |
| 313 |
|
| 314 |
if(empty($this->acf_meta)){ |
| 315 |
$bulk = true; |
| 316 |
} |
| 317 |
|
| 318 |
// vars |
| 319 |
$id = 'acfe-wp-custom-fields'; |
| 320 |
$title = $this->type === 'option' ? __('WP Options Meta', 'acfe') : __('WP Custom Fields', 'acfe'); |
| 321 |
$title .= '<span class="acfe-dev-meta-count">' . count($this->wp_meta) . '</span>'; |
| 322 |
|
| 323 |
add_meta_box($id, $title, array($this, 'render_meta_box'), $screen, $context, $priority, array('table' => 'wp_meta', 'type' => $this->type, 'bulk' => $bulk)); |
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
// acf meta |
| 328 |
if(!empty($this->acf_meta)){ |
| 329 |
|
| 330 |
// display metabox on object lists screen |
| 331 |
acf_set_filters(array( |
| 332 |
'acfe/post_type_list' => true, |
| 333 |
'acfe/taxonomy_list' => true, |
| 334 |
'acfe/user_list' => true, |
| 335 |
'acfe/attachment_list' => true, |
| 336 |
)); |
| 337 |
|
| 338 |
if(!$bulk){ |
| 339 |
$bulk = true; |
| 340 |
} |
| 341 |
|
| 342 |
$id = 'acfe-acf-custom-fields'; |
| 343 |
$title = $this->type === 'option' ? __('ACF Options Meta', 'acfe') : __('ACF Custom Fields', 'acfe'); |
| 344 |
$title .= '<span class="acfe-dev-meta-count">' . count($this->acf_meta) . '</span>'; |
| 345 |
|
| 346 |
add_meta_box($id, $title, array($this, 'render_meta_box'), $screen, $context, $priority, array('table' => 'acf_meta', 'type' => $this->type, 'bulk' => $bulk)); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
/** |
| 354 |
* render_meta_box |
| 355 |
* |
| 356 |
* @param $post |
| 357 |
* @param $metabox |
| 358 |
*/ |
| 359 |
function render_meta_box($post, $metabox){ |
| 360 |
|
| 361 |
//vars |
| 362 |
$args = $metabox['args']; |
| 363 |
$columns = apply_filters('acfe/dev/meta/columns', array(), $args); |
| 364 |
|
| 365 |
?> |
| 366 |
<table class="wp-list-table widefat fixed striped"> |
| 367 |
|
| 368 |
<thead> |
| 369 |
<tr> |
| 370 |
|
| 371 |
<?php foreach($columns as $column_name => $column_label): ?> |
| 372 |
|
| 373 |
<?php |
| 374 |
$el = $column_name === 'checkbox' ? 'td' : 'th'; |
| 375 |
$class = $column_name === 'checkbox' ? 'check-column' : "col-{$column_name}"; |
| 376 |
|
| 377 |
echo "<{$el} scope='col' class='{$class}'>{$column_label}</{$el}>"; |
| 378 |
?> |
| 379 |
|
| 380 |
<?php endforeach; ?> |
| 381 |
|
| 382 |
</tr> |
| 383 |
</thead> |
| 384 |
|
| 385 |
<tbody> |
| 386 |
|
| 387 |
<?php foreach($this->{$args['table']} as $meta): ?> |
| 388 |
|
| 389 |
<tr> |
| 390 |
|
| 391 |
<?php foreach($columns as $column_name => $column_label): ?> |
| 392 |
|
| 393 |
<?php |
| 394 |
$el = $column_name === 'checkbox' ? 'th' : 'td'; |
| 395 |
$attrs = $column_name === 'checkbox' ? array('scope' => 'row', 'class' => 'check-column') : array('class' => "col-{$column_name}"); |
| 396 |
|
| 397 |
echo "<{$el} " . acf_esc_atts($attrs) . ">"; |
| 398 |
do_action('acfe/dev/meta/render_column', $column_name, $meta, $args); |
| 399 |
echo "</{$el}>"; |
| 400 |
?> |
| 401 |
|
| 402 |
<?php endforeach; ?> |
| 403 |
|
| 404 |
</tr> |
| 405 |
|
| 406 |
<?php endforeach; ?> |
| 407 |
|
| 408 |
</tbody> |
| 409 |
|
| 410 |
</table> |
| 411 |
|
| 412 |
<?php do_action('acfe/dev/meta/after_table', $args); ?> |
| 413 |
<script type="text/javascript"> |
| 414 |
if(typeof acf !== 'undefined'){ |
| 415 |
acf.newPostbox(<?php echo json_encode(array('id' => $metabox['id'])); ?>); |
| 416 |
} |
| 417 |
</script> |
| 418 |
<?php |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
/** |
| 424 |
* meta_columns |
| 425 |
* |
| 426 |
* acfe/dev/meta/columns |
| 427 |
* |
| 428 |
* @param $columns |
| 429 |
* @param $args |
| 430 |
* |
| 431 |
* @return array|string[] |
| 432 |
*/ |
| 433 |
function meta_columns($columns, $args){ |
| 434 |
|
| 435 |
$columns = array( |
| 436 |
'name' => __('Name', 'acfe'), |
| 437 |
'value' => __('Value', 'acfe'), |
| 438 |
); |
| 439 |
|
| 440 |
if(current_user_can(acf_get_setting('capability'))){ |
| 441 |
$columns = array_merge(array('checkbox' => '<input type="checkbox" />'), $columns); |
| 442 |
} |
| 443 |
|
| 444 |
if($args['table'] === 'acf_meta'){ |
| 445 |
$columns['field-type'] = __('Field Type', 'acf'); |
| 446 |
$columns['field-group'] = __('Field Group', 'acf'); |
| 447 |
} |
| 448 |
|
| 449 |
if($args['type'] === 'option'){ |
| 450 |
$columns['autoload'] = __('Autoload', 'acfe'); |
| 451 |
} |
| 452 |
|
| 453 |
return $columns; |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
/** |
| 459 |
* meta_render_column |
| 460 |
* |
| 461 |
* acfe/dev/meta/render_column |
| 462 |
* |
| 463 |
* @param $column_name |
| 464 |
* @param $meta |
| 465 |
* @param $args |
| 466 |
*/ |
| 467 |
function meta_render_column($column_name, $meta, $args){ |
| 468 |
|
| 469 |
switch($column_name){ |
| 470 |
|
| 471 |
case 'checkbox': { |
| 472 |
|
| 473 |
?> |
| 474 |
<input type="checkbox" class="acfe-dev-bulk-checkbox" value="<?php echo $meta['id']; ?>" /> |
| 475 |
<?php |
| 476 |
break; |
| 477 |
|
| 478 |
} |
| 479 |
|
| 480 |
case 'name': { |
| 481 |
|
| 482 |
?> |
| 483 |
<strong><?php echo esc_attr($meta['key']); ?></strong> |
| 484 |
<?php |
| 485 |
|
| 486 |
$row_actions = apply_filters('acfe/dev/meta/row_actions', array(), $meta, $args); |
| 487 |
|
| 488 |
if($row_actions){ |
| 489 |
|
| 490 |
echo '<div class="row-actions">'; |
| 491 |
|
| 492 |
echo implode(' | ', array_map(function($action_name, $action){ |
| 493 |
return "<span class='{$action_name}'>{$action}</span>"; |
| 494 |
}, array_keys($row_actions), $row_actions)); |
| 495 |
|
| 496 |
echo '</div>'; |
| 497 |
|
| 498 |
} |
| 499 |
|
| 500 |
break; |
| 501 |
|
| 502 |
} |
| 503 |
|
| 504 |
case 'value': { |
| 505 |
|
| 506 |
echo $this->render_meta_value($meta['value']); |
| 507 |
break; |
| 508 |
|
| 509 |
} |
| 510 |
|
| 511 |
case 'field-type': { |
| 512 |
|
| 513 |
echo acf_maybe_get($meta, 'field_type'); |
| 514 |
break; |
| 515 |
|
| 516 |
} |
| 517 |
|
| 518 |
case 'field-group': { |
| 519 |
|
| 520 |
echo acf_maybe_get($meta, 'field_group'); |
| 521 |
break; |
| 522 |
|
| 523 |
} |
| 524 |
|
| 525 |
case 'autoload': { |
| 526 |
|
| 527 |
echo $meta['autoload']; |
| 528 |
break; |
| 529 |
|
| 530 |
} |
| 531 |
|
| 532 |
} |
| 533 |
|
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* render_meta_value |
| 539 |
* |
| 540 |
* @param $value |
| 541 |
* |
| 542 |
* @return string |
| 543 |
*/ |
| 544 |
function render_meta_value($value){ |
| 545 |
|
| 546 |
// raw |
| 547 |
$raw = map_deep($value, '_wp_specialchars'); |
| 548 |
|
| 549 |
// string (default) |
| 550 |
$return = '<pre>' . print_r($raw, true) . '</pre>'; |
| 551 |
|
| 552 |
// empty value |
| 553 |
if(acf_is_empty($value)){ |
| 554 |
|
| 555 |
$return = '<pre style="color:#aaa;">(' . __('empty', 'acf') . ')</pre>'; |
| 556 |
|
| 557 |
// serialized value |
| 558 |
}elseif(is_serialized($value)){ |
| 559 |
|
| 560 |
$value = maybe_unserialize($value); |
| 561 |
|
| 562 |
if(is_object($value) && is_a($value, '__PHP_Incomplete_Class')){ |
| 563 |
// do nothing |
| 564 |
}else{ |
| 565 |
$value = @map_deep($value, '_wp_specialchars'); |
| 566 |
} |
| 567 |
|
| 568 |
$return = '<pre>' . print_r($value, true) . '</pre>'; |
| 569 |
$return .= '<pre class="raw">' . print_r($raw, true) . '</pre>'; |
| 570 |
|
| 571 |
// html value |
| 572 |
}elseif(acfe_is_html($value)){ |
| 573 |
|
| 574 |
$return = '<pre>' . print_r($raw, true) . '</pre>'; |
| 575 |
|
| 576 |
// json value |
| 577 |
}elseif(acfe_is_json($value)){ |
| 578 |
|
| 579 |
$value = json_decode($value); |
| 580 |
$value = @map_deep($value, '_wp_specialchars'); |
| 581 |
|
| 582 |
$return = '<pre>' . print_r($value, true) . '</pre>'; |
| 583 |
$return .= '<pre class="raw">' . print_r($raw, true) . '</pre>'; |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
// return |
| 588 |
return $return; |
| 589 |
|
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
/** |
| 594 |
* setup_meta |
| 595 |
* |
| 596 |
* @param $post_id |
| 597 |
*/ |
| 598 |
function setup_meta($post_id = 0){ |
| 599 |
|
| 600 |
// validate |
| 601 |
$post_id = acf_get_valid_post_id($post_id); |
| 602 |
|
| 603 |
// bail early |
| 604 |
if(empty($post_id)){ |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
// extract decoded post_id |
| 609 |
// $id |
| 610 |
// $type |
| 611 |
extract(acf_decode_post_id($post_id)); |
| 612 |
|
| 613 |
// set global type |
| 614 |
$this->type = $type; |
| 615 |
|
| 616 |
// get meta |
| 617 |
$all_meta = $this->get_meta($id, $type); |
| 618 |
$wp_meta = $this->sort_meta($all_meta, $type); |
| 619 |
|
| 620 |
// bail early |
| 621 |
if(empty($wp_meta)){ |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
// vars |
| 626 |
$acf_meta = array(); |
| 627 |
|
| 628 |
// loop to prepare acf_meta |
| 629 |
foreach($wp_meta as $key => $meta){ |
| 630 |
|
| 631 |
$ref = false; |
| 632 |
$ref_found = false; |
| 633 |
|
| 634 |
// no prefix, so not acf meta |
| 635 |
if(isset($wp_meta["_$key"])){ |
| 636 |
$ref = $wp_meta["_$key"]; |
| 637 |
$ref_found = true; |
| 638 |
} |
| 639 |
|
| 640 |
// filters |
| 641 |
$ref = apply_filters('acfe/dev/meta_ref', $ref, $wp_meta, $type, $key, $id, $post_id); |
| 642 |
|
| 643 |
if(!$ref){ |
| 644 |
continue; |
| 645 |
} |
| 646 |
|
| 647 |
// check if key is field_abcde123456 |
| 648 |
if(!acf_is_field_key($ref['value'])){ |
| 649 |
continue; |
| 650 |
} |
| 651 |
|
| 652 |
// vars |
| 653 |
$field_key = $ref['value']; |
| 654 |
$field_type = '<em>' . __('Undefined', 'acfe') . '</em>'; |
| 655 |
$field_group_title = '<em>' . __('Undefined', 'acfe') . '</em>'; |
| 656 |
|
| 657 |
// get field |
| 658 |
$field = acf_get_field($field_key); |
| 659 |
|
| 660 |
// check clone in sub field: field_123456abcdef_field_123456abcfed |
| 661 |
if(!$field && substr_count($field_key, 'field_') > 1){ |
| 662 |
|
| 663 |
// get field key (last key) |
| 664 |
$_field_key = substr($field_key, strrpos($field_key, 'field_')); |
| 665 |
|
| 666 |
// get field |
| 667 |
$field = acf_get_field($_field_key); |
| 668 |
|
| 669 |
} |
| 670 |
|
| 671 |
// found field |
| 672 |
if($field){ |
| 673 |
|
| 674 |
// Field type |
| 675 |
$field_type = acf_get_field_type($field['type']); |
| 676 |
$field_type = acfe_maybe_get($field_type, 'label', '<em>Undefined</em>'); |
| 677 |
|
| 678 |
// Field Group |
| 679 |
$field_group = acfe_get_field_group_from_field($field); |
| 680 |
|
| 681 |
if($field_group){ |
| 682 |
|
| 683 |
$field_group_title = $field_group['title']; |
| 684 |
|
| 685 |
// no id setting, try to get raw field group from db |
| 686 |
if(!$field_group['ID']){ |
| 687 |
$field_group = acf_get_raw_field_group($field_group['key']); |
| 688 |
} |
| 689 |
|
| 690 |
// found db field group |
| 691 |
if($field_group && $field_group['ID']){ |
| 692 |
|
| 693 |
$post_status = get_post_status($field_group['ID']); |
| 694 |
|
| 695 |
if($post_status === 'publish' || $post_status === 'acf-disabled'){ |
| 696 |
|
| 697 |
$field_group_title = '<a href="' . admin_url('post.php?post=' . $field_group['ID'] . '&action=edit') . '">' . $field_group['title'] . '</a>'; |
| 698 |
|
| 699 |
} |
| 700 |
|
| 701 |
} |
| 702 |
|
| 703 |
} |
| 704 |
|
| 705 |
} |
| 706 |
|
| 707 |
// assign acf meta: prefix |
| 708 |
if($ref_found){ |
| 709 |
|
| 710 |
unset($wp_meta["_$key"]); |
| 711 |
|
| 712 |
$_meta = $ref; |
| 713 |
$_meta['field_type'] = $field_type; |
| 714 |
$_meta['field_group'] = $field_group_title; |
| 715 |
|
| 716 |
$acf_meta[] = $_meta; |
| 717 |
|
| 718 |
} |
| 719 |
|
| 720 |
// assign acf meta: normal |
| 721 |
$_meta = $wp_meta[ $key ]; |
| 722 |
$_meta['field_type'] = $field_type; |
| 723 |
$_meta['field_group'] = $field_group_title; |
| 724 |
|
| 725 |
$acf_meta[] = $_meta; |
| 726 |
|
| 727 |
// unset wp meta |
| 728 |
unset($wp_meta[ $key ]); |
| 729 |
|
| 730 |
} |
| 731 |
|
| 732 |
// assign global |
| 733 |
$this->wp_meta = $wp_meta; |
| 734 |
$this->acf_meta = $acf_meta; |
| 735 |
|
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
/** |
| 740 |
* get_meta |
| 741 |
* |
| 742 |
* @param $id |
| 743 |
* @param $type |
| 744 |
* |
| 745 |
* @return array|object|stdClass|null |
| 746 |
*/ |
| 747 |
function get_meta($id, $type){ |
| 748 |
|
| 749 |
global $wpdb; |
| 750 |
|
| 751 |
$all_meta = null; |
| 752 |
|
| 753 |
switch($type){ |
| 754 |
|
| 755 |
case 'post': { |
| 756 |
|
| 757 |
$all_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE `post_id` = %d ", $id)); |
| 758 |
break; |
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
case 'term': { |
| 763 |
|
| 764 |
$all_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->termmeta} WHERE `term_id` = %d ", $id)); |
| 765 |
break; |
| 766 |
|
| 767 |
} |
| 768 |
|
| 769 |
case 'user': { |
| 770 |
|
| 771 |
$all_meta = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->usermeta} WHERE `user_id` = %d ", $id)); |
| 772 |
break; |
| 773 |
|
| 774 |
} |
| 775 |
|
| 776 |
case 'option': { |
| 777 |
|
| 778 |
$search_ = $wpdb->esc_like("{$id}_") . '%'; |
| 779 |
$_search_ = $wpdb->esc_like("_{$id}_") . '%'; |
| 780 |
|
| 781 |
$all_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_, $id)); |
| 782 |
break; |
| 783 |
|
| 784 |
} |
| 785 |
|
| 786 |
} |
| 787 |
|
| 788 |
return $all_meta; |
| 789 |
|
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
/** |
| 794 |
* sort_meta |
| 795 |
* |
| 796 |
* @param $all_meta |
| 797 |
* @param $type |
| 798 |
* |
| 799 |
* @return array|false |
| 800 |
*/ |
| 801 |
function sort_meta($all_meta, $type){ |
| 802 |
|
| 803 |
if(empty($all_meta)){ |
| 804 |
return false; |
| 805 |
} |
| 806 |
|
| 807 |
$wp_meta = array(); |
| 808 |
|
| 809 |
// re-order |
| 810 |
switch($type){ |
| 811 |
|
| 812 |
case 'post': |
| 813 |
case 'term': { |
| 814 |
|
| 815 |
usort($all_meta, function($a, $b){ |
| 816 |
return strcmp($a->meta_key, $b->meta_key); |
| 817 |
}); |
| 818 |
|
| 819 |
foreach($all_meta as $meta){ |
| 820 |
|
| 821 |
$wp_meta[ $meta->meta_key ] = array( |
| 822 |
'id' => $meta->meta_id, |
| 823 |
'key' => $meta->meta_key, |
| 824 |
'value' => $meta->meta_value, |
| 825 |
'type' => $type, |
| 826 |
); |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
break; |
| 831 |
|
| 832 |
} |
| 833 |
|
| 834 |
case 'user': { |
| 835 |
|
| 836 |
usort($all_meta, function($a, $b){ |
| 837 |
return strcmp($a->meta_key, $b->meta_key); |
| 838 |
}); |
| 839 |
|
| 840 |
foreach($all_meta as $meta){ |
| 841 |
|
| 842 |
$wp_meta[ $meta->meta_key ] = array( |
| 843 |
'id' => $meta->umeta_id, |
| 844 |
'key' => $meta->meta_key, |
| 845 |
'value' => $meta->meta_value, |
| 846 |
'type' => $type, |
| 847 |
); |
| 848 |
|
| 849 |
} |
| 850 |
|
| 851 |
break; |
| 852 |
|
| 853 |
} |
| 854 |
|
| 855 |
case 'option': { |
| 856 |
|
| 857 |
usort($all_meta, function($a, $b){ |
| 858 |
return strcmp($a->option_name, $b->option_name); |
| 859 |
}); |
| 860 |
|
| 861 |
foreach($all_meta as $meta){ |
| 862 |
|
| 863 |
$wp_meta[$meta->option_name] = array( |
| 864 |
'id' => $meta->option_id, |
| 865 |
'key' => $meta->option_name, |
| 866 |
'value' => $meta->option_value, |
| 867 |
'autoload' => $meta->autoload, |
| 868 |
'type' => $type, |
| 869 |
); |
| 870 |
|
| 871 |
} |
| 872 |
|
| 873 |
break; |
| 874 |
|
| 875 |
} |
| 876 |
|
| 877 |
} |
| 878 |
|
| 879 |
return $wp_meta; |
| 880 |
|
| 881 |
} |
| 882 |
|
| 883 |
} |
| 884 |
|
| 885 |
acf_new_instance('acfe_dev'); |
| 886 |
|
| 887 |
endif; |
| 888 |
|
| 889 |
/** |
| 890 |
* acfe_dev_get_wp_meta |
| 891 |
* |
| 892 |
* @return mixed |
| 893 |
*/ |
| 894 |
function acfe_dev_get_wp_meta(){ |
| 895 |
return acf_get_instance('acfe_dev')->wp_meta; |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
/** |
| 900 |
* acfe_dev_get_acf_meta |
| 901 |
* |
| 902 |
* @return mixed |
| 903 |
*/ |
| 904 |
function acfe_dev_get_acf_meta(){ |
| 905 |
return acf_get_instance('acfe_dev')->acf_meta; |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
/** |
| 910 |
* acfe_dev_count_meta |
| 911 |
* |
| 912 |
* @return int |
| 913 |
*/ |
| 914 |
function acfe_dev_count_meta(){ |
| 915 |
return count(acfe_dev_get_wp_meta()) + count(acfe_dev_get_acf_meta()); |
| 916 |
} |