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