Keine Beschreibung

form-gutenberg.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly.
  4. }
  5. if ( ! class_exists( 'ACF_Form_Gutenberg' ) ) :
  6. class ACF_Form_Gutenberg {
  7. /**
  8. * __construct
  9. *
  10. * Setup for class functionality.
  11. *
  12. * @date 13/12/18
  13. * @since 5.8.0
  14. *
  15. * @param void
  16. * @return void
  17. */
  18. function __construct() {
  19. // Add actions.
  20. add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
  21. // Ignore validation during meta-box-loader AJAX request.
  22. add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 999 );
  23. }
  24. /**
  25. * enqueue_block_editor_assets
  26. *
  27. * Allows a safe way to customize Guten-only functionality.
  28. *
  29. * @date 14/12/18
  30. * @since 5.8.0
  31. *
  32. * @param void
  33. * @return void
  34. */
  35. function enqueue_block_editor_assets() {
  36. // Remove edit_form_after_title.
  37. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 0 );
  38. // Call edit_form_after_title manually.
  39. add_action( 'block_editor_meta_box_hidden_fields', array( $this, 'block_editor_meta_box_hidden_fields' ) );
  40. // Cusotmize editor metaboxes.
  41. add_filter( 'filter_block_editor_meta_boxes', array( $this, 'filter_block_editor_meta_boxes' ) );
  42. }
  43. /**
  44. * add_meta_boxes
  45. *
  46. * Modify screen for Gutenberg.
  47. *
  48. * @date 13/12/18
  49. * @since 5.8.0
  50. *
  51. * @param void
  52. * @return void
  53. */
  54. function add_meta_boxes() {
  55. // Remove 'edit_form_after_title' action.
  56. remove_action( 'edit_form_after_title', array( acf_get_instance( 'ACF_Form_Post' ), 'edit_form_after_title' ) );
  57. }
  58. /**
  59. * block_editor_meta_box_hidden_fields
  60. *
  61. * Modify screen for Gutenberg.
  62. *
  63. * @date 13/12/18
  64. * @since 5.8.0
  65. *
  66. * @param void
  67. * @return void
  68. */
  69. function block_editor_meta_box_hidden_fields() {
  70. // Manually call 'edit_form_after_title' function.
  71. acf_get_instance( 'ACF_Form_Post' )->edit_form_after_title();
  72. }
  73. /**
  74. * filter_block_editor_meta_boxes
  75. *
  76. * description
  77. *
  78. * @date 5/4/19
  79. * @since 5.7.14
  80. *
  81. * @param type $var Description. Default.
  82. * @return type Description.
  83. */
  84. function filter_block_editor_meta_boxes( $wp_meta_boxes ) {
  85. // Globals
  86. global $current_screen;
  87. // Move 'acf_after_title' metaboxes into 'normal' location.
  88. if ( isset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ) ) {
  89. // Extract locations.
  90. $locations = $wp_meta_boxes[ $current_screen->id ];
  91. // Ensure normal location exists.
  92. if ( ! isset( $locations['normal'] ) ) {
  93. $locations['normal'] = array();
  94. }
  95. if ( ! isset( $locations['normal']['high'] ) ) {
  96. $locations['normal']['high'] = array();
  97. }
  98. // Append metaboxes.
  99. foreach ( $locations['acf_after_title'] as $priority => $meta_boxes ) {
  100. $locations['normal']['high'] = array_merge( $meta_boxes, $locations['normal']['high'] );
  101. }
  102. // Update original data.
  103. $wp_meta_boxes[ $current_screen->id ] = $locations;
  104. unset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] );
  105. // Avoid conflicts with saved metabox order.
  106. add_filter( 'get_user_option_meta-box-order_' . $current_screen->id, array( $this, 'modify_user_option_meta_box_order' ) );
  107. }
  108. // Return
  109. return $wp_meta_boxes;
  110. }
  111. /**
  112. * modify_user_option_meta_box_order
  113. *
  114. * Filters the `meta-box-order_{$post_type}` value by prepending "acf_after_title" data to "normal".
  115. * Fixes a bug where metaboxes with position "acf_after_title" do not appear in the block editor.
  116. *
  117. * @date 11/7/19
  118. * @since 5.8.2
  119. *
  120. * @param array $stored_meta_box_order User's existing meta box order.
  121. * @return array Modified array with meta boxes moved around.
  122. */
  123. function modify_user_option_meta_box_order( $locations ) {
  124. if ( ! empty( $locations['acf_after_title'] ) ) {
  125. if ( ! empty( $locations['normal'] ) ) {
  126. $locations['normal'] = $locations['acf_after_title'] . ',' . $locations['normal'];
  127. } else {
  128. $locations['normal'] = $locations['acf_after_title'];
  129. }
  130. unset( $locations['acf_after_title'] );
  131. }
  132. return $locations;
  133. }
  134. /**
  135. * acf_validate_save_post
  136. *
  137. * Ignore errors during the Gutenberg "save metaboxes" AJAX request.
  138. * Allows data to save and prevent UX issues.
  139. *
  140. * @date 16/12/18
  141. * @since 5.8.0
  142. *
  143. * @param void
  144. * @return void
  145. */
  146. function acf_validate_save_post() {
  147. // Check if current request came from Gutenberg.
  148. if ( isset( $_GET['meta-box-loader'] ) ) {
  149. acf_reset_validation_errors();
  150. }
  151. }
  152. }
  153. acf_new_instance( 'ACF_Form_Gutenberg' );
  154. endif;