WC_Order.php
8 hours ago
form-attachment.php
1 year ago
form-comment.php
7 months ago
form-customizer.php
10 months ago
form-front.php
3 weeks ago
form-gutenberg.php
1 year ago
form-nav-menu.php
7 months ago
form-post.php
1 month ago
form-taxonomy.php
2 weeks ago
form-user.php
7 months ago
form-widget.php
10 months ago
index.php
1 year ago
form-gutenberg.php
184 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 ACF 5.8.0 |
| 18 | * |
| 19 | * @return void |
| 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 ACF 5.8.0 |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | function enqueue_block_editor_assets() { |
| 41 | |
| 42 | // Remove edit_form_after_title. |
| 43 | add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 0 ); |
| 44 | |
| 45 | // Call edit_form_after_title manually. |
| 46 | add_action( 'block_editor_meta_box_hidden_fields', array( $this, 'block_editor_meta_box_hidden_fields' ) ); |
| 47 | |
| 48 | // Customize editor metaboxes. |
| 49 | add_filter( 'filter_block_editor_meta_boxes', array( $this, 'filter_block_editor_meta_boxes' ) ); |
| 50 | |
| 51 | // Trigger ACF enqueue scripts as the site editor doesn't trigger this from form-post.php |
| 52 | acf_enqueue_scripts( |
| 53 | array( |
| 54 | 'uploader' => true, |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * add_meta_boxes |
| 61 | * |
| 62 | * Modify screen for Gutenberg. |
| 63 | * |
| 64 | * @date 13/12/18 |
| 65 | * @since ACF 5.8.0 |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | function add_meta_boxes() { |
| 70 | |
| 71 | // Remove 'edit_form_after_title' action. |
| 72 | remove_action( 'edit_form_after_title', array( acf_get_instance( 'ACF_Form_Post' ), 'edit_form_after_title' ) ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * block_editor_meta_box_hidden_fields |
| 77 | * |
| 78 | * Modify screen for Gutenberg. |
| 79 | * |
| 80 | * @date 13/12/18 |
| 81 | * @since ACF 5.8.0 |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | function block_editor_meta_box_hidden_fields() { |
| 86 | |
| 87 | // Manually call 'edit_form_after_title' function. |
| 88 | acf_get_instance( 'ACF_Form_Post' )->edit_form_after_title(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * filter_block_editor_meta_boxes |
| 93 | * |
| 94 | * description |
| 95 | * |
| 96 | * @date 5/4/19 |
| 97 | * @since ACF 5.7.14 |
| 98 | * |
| 99 | * @param type $var Description. Default. |
| 100 | * @return type Description. |
| 101 | */ |
| 102 | function filter_block_editor_meta_boxes( $wp_meta_boxes ) { |
| 103 | |
| 104 | // Globals |
| 105 | global $current_screen; |
| 106 | |
| 107 | // Move 'acf_after_title' metaboxes into 'normal' location. |
| 108 | if ( isset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ) ) { |
| 109 | |
| 110 | // Extract locations. |
| 111 | $locations = $wp_meta_boxes[ $current_screen->id ]; |
| 112 | |
| 113 | // Ensure normal location exists. |
| 114 | if ( ! isset( $locations['normal'] ) ) { |
| 115 | $locations['normal'] = array(); |
| 116 | } |
| 117 | if ( ! isset( $locations['normal']['high'] ) ) { |
| 118 | $locations['normal']['high'] = array(); |
| 119 | } |
| 120 | |
| 121 | // Append metaboxes. |
| 122 | foreach ( $locations['acf_after_title'] as $priority => $meta_boxes ) { |
| 123 | $locations['normal']['high'] = array_merge( $meta_boxes, $locations['normal']['high'] ); |
| 124 | } |
| 125 | |
| 126 | // Update original data. |
| 127 | $wp_meta_boxes[ $current_screen->id ] = $locations; |
| 128 | unset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ); |
| 129 | |
| 130 | // Avoid conflicts with saved metabox order. |
| 131 | add_filter( 'get_user_option_meta-box-order_' . $current_screen->id, array( $this, 'modify_user_option_meta_box_order' ) ); |
| 132 | } |
| 133 | |
| 134 | // Return |
| 135 | return $wp_meta_boxes; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * modify_user_option_meta_box_order |
| 140 | * |
| 141 | * Filters the `meta-box-order_{$post_type}` value by prepending "acf_after_title" data to "normal". |
| 142 | * Fixes a bug where metaboxes with position "acf_after_title" do not appear in the block editor. |
| 143 | * |
| 144 | * @date 11/7/19 |
| 145 | * @since ACF 5.8.2 |
| 146 | * |
| 147 | * @param array $stored_meta_box_order User's existing meta box order. |
| 148 | * @return array Modified array with meta boxes moved around. |
| 149 | */ |
| 150 | function modify_user_option_meta_box_order( $locations ) { |
| 151 | if ( ! empty( $locations['acf_after_title'] ) ) { |
| 152 | if ( ! empty( $locations['normal'] ) ) { |
| 153 | $locations['normal'] = $locations['acf_after_title'] . ',' . $locations['normal']; |
| 154 | } else { |
| 155 | $locations['normal'] = $locations['acf_after_title']; |
| 156 | } |
| 157 | unset( $locations['acf_after_title'] ); |
| 158 | } |
| 159 | return $locations; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * acf_validate_save_post |
| 164 | * |
| 165 | * Ignore errors during the Gutenberg "save metaboxes" AJAX request. |
| 166 | * Allows data to save and prevent UX issues. |
| 167 | * |
| 168 | * @date 16/12/18 |
| 169 | * @since ACF 5.8.0 |
| 170 | * |
| 171 | * @return void |
| 172 | */ |
| 173 | function acf_validate_save_post() { |
| 174 | |
| 175 | // Check if current request came from Gutenberg. |
| 176 | if ( isset( $_GET['meta-box-loader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified elsewhere. |
| 177 | acf_reset_validation_errors(); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | acf_new_instance( 'ACF_Form_Gutenberg' ); |
| 183 | endif; |
| 184 |