PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / forms / form-gutenberg.php
secure-custom-fields / includes / forms Last commit date
form-attachment.php 1 year ago form-comment.php 1 year ago form-customizer.php 1 year ago form-front.php 1 year ago form-gutenberg.php 1 year ago form-nav-menu.php 1 year ago form-post.php 1 year ago form-taxonomy.php 1 year ago form-user.php 1 year ago form-widget.php 1 year ago index.php 1 year ago
form-gutenberg.php
189 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 if ( ! class_exists( 'ACF_Form_Gutenberg' ) ) :
8
9 class ACF_Form_Gutenberg {
10
11 /**
12 * __construct
13 *
14 * Setup for class functionality.
15 *
16 * @date 13/12/18
17 * @since 5.8.0
18 *
19 * @param void
20 * @return void
21 */
22 function __construct() {
23
24 // Add actions.
25 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
26
27 // Ignore validation during meta-box-loader AJAX request.
28 add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 999 );
29 }
30
31 /**
32 * enqueue_block_editor_assets
33 *
34 * Allows a safe way to customize Guten-only functionality.
35 *
36 * @date 14/12/18
37 * @since 5.8.0
38 *
39 * @param void
40 * @return void
41 */
42 function enqueue_block_editor_assets() {
43
44 // Remove edit_form_after_title.
45 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 0 );
46
47 // Call edit_form_after_title manually.
48 add_action( 'block_editor_meta_box_hidden_fields', array( $this, 'block_editor_meta_box_hidden_fields' ) );
49
50 // Customize editor metaboxes.
51 add_filter( 'filter_block_editor_meta_boxes', array( $this, 'filter_block_editor_meta_boxes' ) );
52
53 // Trigger ACF enqueue scripts as the site editor doesn't trigger this from form-post.php
54 acf_enqueue_scripts(
55 array(
56 'uploader' => true,
57 )
58 );
59 }
60
61 /**
62 * add_meta_boxes
63 *
64 * Modify screen for Gutenberg.
65 *
66 * @date 13/12/18
67 * @since 5.8.0
68 *
69 * @param void
70 * @return void
71 */
72 function add_meta_boxes() {
73
74 // Remove 'edit_form_after_title' action.
75 remove_action( 'edit_form_after_title', array( acf_get_instance( 'ACF_Form_Post' ), 'edit_form_after_title' ) );
76 }
77
78 /**
79 * block_editor_meta_box_hidden_fields
80 *
81 * Modify screen for Gutenberg.
82 *
83 * @date 13/12/18
84 * @since 5.8.0
85 *
86 * @param void
87 * @return void
88 */
89 function block_editor_meta_box_hidden_fields() {
90
91 // Manually call 'edit_form_after_title' function.
92 acf_get_instance( 'ACF_Form_Post' )->edit_form_after_title();
93 }
94
95 /**
96 * filter_block_editor_meta_boxes
97 *
98 * description
99 *
100 * @date 5/4/19
101 * @since 5.7.14
102 *
103 * @param type $var Description. Default.
104 * @return type Description.
105 */
106 function filter_block_editor_meta_boxes( $wp_meta_boxes ) {
107
108 // Globals
109 global $current_screen;
110
111 // Move 'acf_after_title' metaboxes into 'normal' location.
112 if ( isset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ) ) {
113
114 // Extract locations.
115 $locations = $wp_meta_boxes[ $current_screen->id ];
116
117 // Ensure normal location exists.
118 if ( ! isset( $locations['normal'] ) ) {
119 $locations['normal'] = array();
120 }
121 if ( ! isset( $locations['normal']['high'] ) ) {
122 $locations['normal']['high'] = array();
123 }
124
125 // Append metaboxes.
126 foreach ( $locations['acf_after_title'] as $priority => $meta_boxes ) {
127 $locations['normal']['high'] = array_merge( $meta_boxes, $locations['normal']['high'] );
128 }
129
130 // Update original data.
131 $wp_meta_boxes[ $current_screen->id ] = $locations;
132 unset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] );
133
134 // Avoid conflicts with saved metabox order.
135 add_filter( 'get_user_option_meta-box-order_' . $current_screen->id, array( $this, 'modify_user_option_meta_box_order' ) );
136 }
137
138 // Return
139 return $wp_meta_boxes;
140 }
141
142 /**
143 * modify_user_option_meta_box_order
144 *
145 * Filters the `meta-box-order_{$post_type}` value by prepending "acf_after_title" data to "normal".
146 * Fixes a bug where metaboxes with position "acf_after_title" do not appear in the block editor.
147 *
148 * @date 11/7/19
149 * @since 5.8.2
150 *
151 * @param array $stored_meta_box_order User's existing meta box order.
152 * @return array Modified array with meta boxes moved around.
153 */
154 function modify_user_option_meta_box_order( $locations ) {
155 if ( ! empty( $locations['acf_after_title'] ) ) {
156 if ( ! empty( $locations['normal'] ) ) {
157 $locations['normal'] = $locations['acf_after_title'] . ',' . $locations['normal'];
158 } else {
159 $locations['normal'] = $locations['acf_after_title'];
160 }
161 unset( $locations['acf_after_title'] );
162 }
163 return $locations;
164 }
165
166 /**
167 * acf_validate_save_post
168 *
169 * Ignore errors during the Gutenberg "save metaboxes" AJAX request.
170 * Allows data to save and prevent UX issues.
171 *
172 * @date 16/12/18
173 * @since 5.8.0
174 *
175 * @param void
176 * @return void
177 */
178 function acf_validate_save_post() {
179
180 // Check if current request came from Gutenberg.
181 if ( isset( $_GET['meta-box-loader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified elsewhere.
182 acf_reset_validation_errors();
183 }
184 }
185 }
186
187 acf_new_instance( 'ACF_Form_Gutenberg' );
188 endif;
189