Block Editor: Hide the Custom Fields meta box option if that meta box has been removed.

Some plugins remove the Custom Fields meta box, particularly when they provide functionality that replaces it. The block editor would correctly not display this meta box in these circumstances, but it still showed the option to display or hide it.

Props pento, noisysocks.
See #45282.




git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43885 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2018-11-12 03:37:41 +00:00
parent 26414eec37
commit 114a1eeb3f
3 changed files with 44 additions and 36 deletions

View File

@ -19,8 +19,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* @global WP_Post $post * @global WP_Post $post
* @global string $title * @global string $title
* @global array $editor_styles * @global array $editor_styles
* @global array $wp_meta_boxes
*/ */
global $post_type, $post_type_object, $post, $title, $editor_styles; global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
if ( ! empty( $post_type_object ) ) { if ( ! empty( $post_type_object ) ) {
$title = $post_type_object->labels->edit_item; $title = $post_type_object->labels->edit_item;
@ -316,36 +317,6 @@ if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post
} }
} }
$init_script = <<<JS
( function() {
window._wpLoadBlockEditor = new Promise( function( resolve ) {
wp.domReady( function() {
resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
} );
} );
} )();
JS;
/**
* Filters the settings to pass to the block editor.
*
* @since 5.0.0
*
* @param array $editor_settings Default editor settings.
* @param WP_Post $post Post being edited.
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
$script = sprintf(
$init_script,
$post->post_type,
$post->ID,
wp_json_encode( $editor_settings ),
wp_json_encode( $initial_edits )
);
wp_add_inline_script( 'wp-edit-post', $script );
/** /**
* Scripts * Scripts
*/ */
@ -378,6 +349,41 @@ do_action( 'enqueue_block_editor_assets' );
require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' ); require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
register_and_do_post_meta_boxes( $post ); register_and_do_post_meta_boxes( $post );
// Check if the Custom Fields meta box has been removed at some point.
$core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
unset( $editor_settings['enableCustomFields'] );
}
/**
* Filters the settings to pass to the block editor.
*
* @since 5.0.0
*
* @param array $editor_settings Default editor settings.
* @param WP_Post $post Post being edited.
*/
$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
$init_script = <<<JS
( function() {
window._wpLoadBlockEditor = new Promise( function( resolve ) {
wp.domReady( function() {
resolve( wp.editPost.initializeEditor( 'editor', "%s", %d, %s, %s ) );
} );
} );
} )();
JS;
$script = sprintf(
$init_script,
$post->post_type,
$post->ID,
wp_json_encode( $editor_settings ),
wp_json_encode( $initial_edits )
);
wp_add_inline_script( 'wp-edit-post', $script );
require_once( ABSPATH . 'wp-admin/admin-header.php' ); require_once( ABSPATH . 'wp-admin/admin-header.php' );
?> ?>

View File

@ -1361,10 +1361,11 @@ function register_and_do_post_meta_boxes( $post ) {
add_meta_box('trackbacksdiv', __('Send Trackbacks'), 'post_trackback_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) ); add_meta_box('trackbacksdiv', __('Send Trackbacks'), 'post_trackback_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
if ( post_type_supports($post_type, 'custom-fields') ) { if ( post_type_supports($post_type, 'custom-fields') ) {
$screen = get_current_screen(); $args = array(
if ( ! $screen || ! $screen->is_block_editor() || (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ) ) { '__back_compat_meta_box' => ! (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => false, '__block_editor_compatible_meta_box' => true ) ); '__block_editor_compatible_meta_box' => true
} );
add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', null, 'normal', 'core', $args );
} }
/** /**

View File

@ -1076,7 +1076,8 @@ function do_meta_boxes( $screen, $context, $object ) {
} }
$i++; $i++;
$hidden_class = in_array($box['id'], $hidden) ? ' hide-if-js' : ''; // get_hidden_meta_boxes() doesn't apply in the block editor.
$hidden_class = ( ! $screen->is_block_editor() && in_array( $box['id'], $hidden ) ) ? ' hide-if-js' : '';
echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . $hidden_class . '" ' . '>' . "\n"; echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . $hidden_class . '" ' . '>' . "\n";
if ( 'dashboard_browser_nag' != $box['id'] ) { if ( 'dashboard_browser_nag' != $box['id'] ) {
$widget_title = $box[ 'title' ]; $widget_title = $box[ 'title' ];