class-acf-ajax-check-screen.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Ajax_Check_Screen' ) ) :
  6. class ACF_Ajax_Check_Screen extends ACF_Ajax {
  7. /** @var string The AJAX action name. */
  8. var $action = 'acf/ajax/check_screen';
  9. /** @var bool Prevents access for non-logged in users. */
  10. var $public = false;
  11. /**
  12. * get_response
  13. *
  14. * Returns the response data to sent back.
  15. *
  16. * @date 31/7/18
  17. * @since 5.7.2
  18. *
  19. * @param array $request The request args.
  20. * @return mixed The response data or WP_Error.
  21. */
  22. function get_response( $request ) {
  23. // vars
  24. $args = wp_parse_args(
  25. $this->request,
  26. array(
  27. 'screen' => '',
  28. 'post_id' => 0,
  29. 'ajax' => true,
  30. 'exists' => array(),
  31. )
  32. );
  33. // vars
  34. $response = array(
  35. 'results' => array(),
  36. 'style' => '',
  37. );
  38. // get field groups
  39. $field_groups = acf_get_field_groups( $args );
  40. // loop through field groups
  41. if ( $field_groups ) {
  42. foreach ( $field_groups as $i => $field_group ) {
  43. // vars
  44. $item = array(
  45. 'id' => 'acf-' . $field_group['key'],
  46. 'key' => $field_group['key'],
  47. 'title' => $field_group['title'],
  48. 'position' => $field_group['position'],
  49. 'classes' => postbox_classes( 'acf-' . $field_group['key'], $args['screen'] ),
  50. 'style' => $field_group['style'],
  51. 'label' => $field_group['label_placement'],
  52. 'edit' => acf_get_field_group_edit_link( $field_group['ID'] ),
  53. 'html' => '',
  54. );
  55. $hidden_metaboxes = get_hidden_meta_boxes( $args['screen'] );
  56. if ( is_array( $hidden_metaboxes ) && in_array( $item['id'], $hidden_metaboxes ) ) {
  57. $item['classes'] = trim( $item['classes'] . ' hide-if-js' );
  58. }
  59. // append html if doesnt already exist on page
  60. if ( ! in_array( $field_group['key'], $args['exists'] ) ) {
  61. // load fields
  62. $fields = acf_get_fields( $field_group );
  63. // get field HTML
  64. ob_start();
  65. // render
  66. acf_render_fields( $fields, $args['post_id'], 'div', $field_group['instruction_placement'] );
  67. $item['html'] = ob_get_clean();
  68. }
  69. // append
  70. $response['results'][] = $item;
  71. }
  72. // Get style from first field group.
  73. $response['style'] = acf_get_field_group_style( $field_groups[0] );
  74. }
  75. // Custom metabox order.
  76. if ( $this->get( 'screen' ) == 'post' ) {
  77. $response['sorted'] = get_user_option( 'meta-box-order_' . $this->get( 'post_type' ) );
  78. }
  79. // return
  80. return $response;
  81. }
  82. }
  83. acf_new_instance( 'ACF_Ajax_Check_Screen' );
  84. endif; // class_exists check