From 6ef54307b13439c296da2defb7f4682b9e8bf2aa Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 17 Dec 2018 03:21:21 +0000 Subject: [PATCH] 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. Merges [43885] to trunk. See #45282. Built from https://develop.svn.wordpress.org/trunk@44244 git-svn-id: http://core.svn.wordpress.org/trunk@44074 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/edit-form-blocks.php | 68 +++++++++++++++++--------------- wp-admin/includes/meta-boxes.php | 6 ++- wp-admin/includes/template.php | 3 +- wp-includes/version.php | 2 +- 4 files changed, 45 insertions(+), 34 deletions(-) diff --git a/wp-admin/edit-form-blocks.php b/wp-admin/edit-form-blocks.php index 7b80b304f1..2ee0ba988a 100644 --- a/wp-admin/edit-form-blocks.php +++ b/wp-admin/edit-form-blocks.php @@ -19,8 +19,9 @@ if ( ! defined( 'ABSPATH' ) ) { * @global WP_Post $post * @global string $title * @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 ) ) { $title = $post_type_object->labels->edit_item; @@ -296,36 +297,6 @@ if ( $is_new_post && ! isset( $editor_settings['template'] ) && 'post' === $post } } -$init_script = <<post_type, - $post->ID, - wp_json_encode( $editor_settings ), - wp_json_encode( $initial_edits ) -); -wp_add_inline_script( 'wp-edit-post', $script ); - /** * Scripts */ @@ -358,6 +329,41 @@ do_action( 'enqueue_block_editor_assets' ); require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' ); 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 = <<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' ); ?> diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index a3e61acd4f..0f175892d6 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/wp-admin/includes/meta-boxes.php @@ -1463,7 +1463,11 @@ function register_and_do_post_meta_boxes( $post ) { } if ( post_type_supports( $post_type, 'custom-fields' ) ) { - add_meta_box( 'postcustom', __( 'Custom Fields' ), 'post_custom_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) ); + $args = array( + '__back_compat_meta_box' => ! (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ), + '__block_editor_compatible_meta_box' => true, + ); + add_meta_box( 'postcustom', __( 'Custom Fields' ), 'post_custom_meta_box', null, 'normal', 'core', $args ); } /** diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index 65c268b3e8..6dbd0d24ab 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -1161,7 +1161,8 @@ function do_meta_boxes( $screen, $context, $object ) { } $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 '
' . "\n"; if ( 'dashboard_browser_nag' != $box['id'] ) { $widget_title = $box['title']; diff --git a/wp-includes/version.php b/wp-includes/version.php index a5d5880c67..49e13360b0 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.1-alpha-44243'; +$wp_version = '5.1-alpha-44244'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.