form-attachment.php
5 months ago
form-comment.php
4 weeks ago
form-customizer.php
5 months ago
form-front.php
2 months ago
form-gutenberg.php
5 months ago
form-nav-menu.php
5 months ago
form-post.php
3 months ago
form-taxonomy.php
2 months ago
form-user.php
4 weeks ago
form-widget.php
5 months ago
index.php
2 years ago
form-gutenberg.php
198 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | |
| 16 | if ( ! class_exists( 'ACF_Form_Gutenberg' ) ) : |
| 17 | |
| 18 | class ACF_Form_Gutenberg { |
| 19 | |
| 20 | /** |
| 21 | * __construct |
| 22 | * |
| 23 | * Setup for class functionality. |
| 24 | * |
| 25 | * @date 13/12/18 |
| 26 | * @since 5.8.0 |
| 27 | * |
| 28 | * @param void |
| 29 | * @return void |
| 30 | */ |
| 31 | function __construct() { |
| 32 | |
| 33 | // Add actions. |
| 34 | add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) ); |
| 35 | |
| 36 | // Ignore validation during meta-box-loader AJAX request. |
| 37 | add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 999 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * enqueue_block_editor_assets |
| 42 | * |
| 43 | * Allows a safe way to customize Guten-only functionality. |
| 44 | * |
| 45 | * @date 14/12/18 |
| 46 | * @since 5.8.0 |
| 47 | * |
| 48 | * @param void |
| 49 | * @return void |
| 50 | */ |
| 51 | function enqueue_block_editor_assets() { |
| 52 | |
| 53 | // Remove edit_form_after_title. |
| 54 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 0 ); |
| 55 | |
| 56 | // Call edit_form_after_title manually. |
| 57 | add_action( 'block_editor_meta_box_hidden_fields', array( $this, 'block_editor_meta_box_hidden_fields' ) ); |
| 58 | |
| 59 | // Customize editor metaboxes. |
| 60 | add_filter( 'filter_block_editor_meta_boxes', array( $this, 'filter_block_editor_meta_boxes' ) ); |
| 61 | |
| 62 | // Trigger ACF enqueue scripts as the site editor doesn't trigger this from form-post.php |
| 63 | acf_enqueue_scripts( |
| 64 | array( |
| 65 | 'uploader' => true, |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * add_meta_boxes |
| 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 add_meta_boxes() { |
| 82 | |
| 83 | // Remove 'edit_form_after_title' action. |
| 84 | remove_action( 'edit_form_after_title', array( acf_get_instance( 'ACF_Form_Post' ), 'edit_form_after_title' ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * block_editor_meta_box_hidden_fields |
| 89 | * |
| 90 | * Modify screen for Gutenberg. |
| 91 | * |
| 92 | * @date 13/12/18 |
| 93 | * @since 5.8.0 |
| 94 | * |
| 95 | * @param void |
| 96 | * @return void |
| 97 | */ |
| 98 | function block_editor_meta_box_hidden_fields() { |
| 99 | |
| 100 | // Manually call 'edit_form_after_title' function. |
| 101 | acf_get_instance( 'ACF_Form_Post' )->edit_form_after_title(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * filter_block_editor_meta_boxes |
| 106 | * |
| 107 | * description |
| 108 | * |
| 109 | * @date 5/4/19 |
| 110 | * @since 5.7.14 |
| 111 | * |
| 112 | * @param type $var Description. Default. |
| 113 | * @return type Description. |
| 114 | */ |
| 115 | function filter_block_editor_meta_boxes( $wp_meta_boxes ) { |
| 116 | |
| 117 | // Globals |
| 118 | global $current_screen; |
| 119 | |
| 120 | // Move 'acf_after_title' metaboxes into 'normal' location. |
| 121 | if ( isset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ) ) { |
| 122 | |
| 123 | // Extract locations. |
| 124 | $locations = $wp_meta_boxes[ $current_screen->id ]; |
| 125 | |
| 126 | // Ensure normal location exists. |
| 127 | if ( ! isset( $locations['normal'] ) ) { |
| 128 | $locations['normal'] = array(); |
| 129 | } |
| 130 | if ( ! isset( $locations['normal']['high'] ) ) { |
| 131 | $locations['normal']['high'] = array(); |
| 132 | } |
| 133 | |
| 134 | // Append metaboxes. |
| 135 | foreach ( $locations['acf_after_title'] as $priority => $meta_boxes ) { |
| 136 | $locations['normal']['high'] = array_merge( $meta_boxes, $locations['normal']['high'] ); |
| 137 | } |
| 138 | |
| 139 | // Update original data. |
| 140 | $wp_meta_boxes[ $current_screen->id ] = $locations; |
| 141 | unset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ); |
| 142 | |
| 143 | // Avoid conflicts with saved metabox order. |
| 144 | add_filter( 'get_user_option_meta-box-order_' . $current_screen->id, array( $this, 'modify_user_option_meta_box_order' ) ); |
| 145 | } |
| 146 | |
| 147 | // Return |
| 148 | return $wp_meta_boxes; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * modify_user_option_meta_box_order |
| 153 | * |
| 154 | * Filters the `meta-box-order_{$post_type}` value by prepending "acf_after_title" data to "normal". |
| 155 | * Fixes a bug where metaboxes with position "acf_after_title" do not appear in the block editor. |
| 156 | * |
| 157 | * @date 11/7/19 |
| 158 | * @since 5.8.2 |
| 159 | * |
| 160 | * @param array $stored_meta_box_order User's existing meta box order. |
| 161 | * @return array Modified array with meta boxes moved around. |
| 162 | */ |
| 163 | function modify_user_option_meta_box_order( $locations ) { |
| 164 | if ( ! empty( $locations['acf_after_title'] ) ) { |
| 165 | if ( ! empty( $locations['normal'] ) ) { |
| 166 | $locations['normal'] = $locations['acf_after_title'] . ',' . $locations['normal']; |
| 167 | } else { |
| 168 | $locations['normal'] = $locations['acf_after_title']; |
| 169 | } |
| 170 | unset( $locations['acf_after_title'] ); |
| 171 | } |
| 172 | return $locations; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * acf_validate_save_post |
| 177 | * |
| 178 | * Ignore errors during the Gutenberg "save metaboxes" AJAX request. |
| 179 | * Allows data to save and prevent UX issues. |
| 180 | * |
| 181 | * @date 16/12/18 |
| 182 | * @since 5.8.0 |
| 183 | * |
| 184 | * @param void |
| 185 | * @return void |
| 186 | */ |
| 187 | function acf_validate_save_post() { |
| 188 | |
| 189 | // Check if current request came from Gutenberg. |
| 190 | if ( isset( $_GET['meta-box-loader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified elsewhere. |
| 191 | acf_reset_validation_errors(); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | acf_new_instance( 'ACF_Form_Gutenberg' ); |
| 197 | endif; |
| 198 |