From 8002325c920458fe269863e1e3b75f0addecc8e0 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 16 Dec 2018 22:51:01 +0000 Subject: [PATCH] Meta Boxes: Add the `block_editor_meta_box_hidden_fields` action. Lacking an appropriate action in the classic editor, plugins that add meta boxes have historically hooked into various actions in order to add hidden input fields. This change also adds backwards compatibility for two of the most common: `edit_form_after_title`, and `edit_form_advanced`. Props pento, danielbachhuber. Merges [43882] to trunk. Fixes #45283. git-svn-id: https://develop.svn.wordpress.org/trunk@44241 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/edit-form-blocks.php | 4 --- src/wp-admin/includes/post.php | 49 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index 9e26086849..7b80b304f1 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -358,10 +358,6 @@ do_action( 'enqueue_block_editor_assets' ); require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' ); register_and_do_post_meta_boxes( $post ); -// Some meta boxes hook into the 'edit_form_advanced' filter. -/** This action is documented in wp-admin/edit-form-advanced.php */ -do_action( 'edit_form_advanced', $post ); - require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?> diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index ced29d4a72..aba113c7a5 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2307,6 +2307,43 @@ function the_block_editor_meta_box_post_form_hidden_fields( $post ) { $current_user = wp_get_current_user(); $user_id = $current_user->ID; wp_nonce_field( $nonce_action ); + + /* + * Some meta boxes hook into these actions to add hidden input fields in the classic post form. For backwards + * compatibility, we can capture the output from these actions, and extract the hidden input fields. + */ + $actions = array( + 'edit_form_after_title', + 'edit_form_advanced', + ); + + foreach ( $actions as $action ) { + ob_start(); + do_action_deprecated( + $action, + array( $post ), + '5.0.0', + 'block_editor_meta_box_hidden_fields', + __( 'This action is still supported in the classic editor, but is deprecated in the block editor.' ) + ); + $classic_output = ob_get_clean(); + + if ( ! $classic_output ) { + continue; + } + + $classic_elements = wp_html_split( $classic_output ); + $hidden_inputs = ''; + foreach( $classic_elements as $element ) { + if ( 0 !== strpos( $element, ' @@ -2324,4 +2361,16 @@ function the_block_editor_meta_box_post_form_hidden_fields( $post ) { wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); // Permalink title nonce. wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false ); + + /** + * Add hidden input fields to the meta box save form. + * + * Hook into this action to print `` fields, which will be POSTed back to + * the server when meta boxes are saved. + * + * @since 5.0.0 + * + * @params WP_Post $post The post that is being edited. + */ + do_action( 'block_editor_meta_box_hidden_fields', $post ); }