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.
See #45283.


Built from https://develop.svn.wordpress.org/branches/5.0@43882


git-svn-id: http://core.svn.wordpress.org/branches/5.0@43711 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast 2018-11-09 09:06:47 +00:00
parent 09324db9d2
commit cd22d51724
3 changed files with 50 additions and 5 deletions

View File

@ -386,10 +386,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' );
?>

View File

@ -2185,6 +2185,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, '<input ') ) {
continue;
}
if ( preg_match( '/\stype=[\'"]hidden[\'"]\s/', $element ) ) {
echo $element;
}
}
}
?>
<input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" />
<input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" />
@ -2202,4 +2239,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 `<input type="hidden" ... />` 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 );
}

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.0-beta3-43881';
$wp_version = '5.0-beta3-43882';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.