Make WordPress Core

Changeset 44244


Ignore:
Timestamp:
12/16/2018 11:23:38 PM (6 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-admin/edit-form-blocks.php

    r44241 r44244  
    2020 * @global string       $title
    2121 * @global array        $editor_styles
    22  */
    23 global $post_type, $post_type_object, $post, $title, $editor_styles;
     22 * @global array        $wp_meta_boxes
     23 */
     24global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes;
    2425
    2526if ( ! empty( $post_type_object ) ) {
     
    297298}
    298299
     300/**
     301 * Scripts
     302 */
     303wp_enqueue_media(
     304    array(
     305        'post' => $post->ID,
     306    )
     307);
     308wp_enqueue_editor();
     309
     310/**
     311 * Styles
     312 */
     313wp_enqueue_style( 'wp-edit-post' );
     314wp_enqueue_style( 'wp-format-library' );
     315
     316/**
     317 * Fires after block assets have been enqueued for the editing interface.
     318 *
     319 * Call `add_action` on any hook before 'admin_enqueue_scripts'.
     320 *
     321 * In the function call you supply, simply use `wp_enqueue_script` and
     322 * `wp_enqueue_style` to add your functionality to the block editor.
     323 *
     324 * @since 5.0.0
     325 */
     326do_action( 'enqueue_block_editor_assets' );
     327
     328// In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
     329require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
     330register_and_do_post_meta_boxes( $post );
     331
     332// Check if the Custom Fields meta box has been removed at some point.
     333$core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
     334if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
     335    unset( $editor_settings['enableCustomFields'] );
     336}
     337
     338/**
     339 * Filters the settings to pass to the block editor.
     340 *
     341 * @since 5.0.0
     342 *
     343 * @param array   $editor_settings Default editor settings.
     344 * @param WP_Post $post            Post being edited.
     345 */
     346$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
     347
    299348$init_script = <<<JS
    300349( function() {
     
    307356JS;
    308357
    309 
    310 /**
    311  * Filters the settings to pass to the block editor.
    312  *
    313  * @since 5.0.0
    314  *
    315  * @param array   $editor_settings Default editor settings.
    316  * @param WP_Post $post            Post being edited.
    317  */
    318 $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
    319 
    320358$script = sprintf(
    321359    $init_script,
     
    326364);
    327365wp_add_inline_script( 'wp-edit-post', $script );
    328 
    329 /**
    330  * Scripts
    331  */
    332 wp_enqueue_media(
    333     array(
    334         'post' => $post->ID,
    335     )
    336 );
    337 wp_enqueue_editor();
    338 
    339 /**
    340  * Styles
    341  */
    342 wp_enqueue_style( 'wp-edit-post' );
    343 wp_enqueue_style( 'wp-format-library' );
    344 
    345 /**
    346  * Fires after block assets have been enqueued for the editing interface.
    347  *
    348  * Call `add_action` on any hook before 'admin_enqueue_scripts'.
    349  *
    350  * In the function call you supply, simply use `wp_enqueue_script` and
    351  * `wp_enqueue_style` to add your functionality to the block editor.
    352  *
    353  * @since 5.0.0
    354  */
    355 do_action( 'enqueue_block_editor_assets' );
    356 
    357 // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
    358 require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
    359 register_and_do_post_meta_boxes( $post );
    360366
    361367require_once( ABSPATH . 'wp-admin/admin-header.php' );
  • trunk/src/wp-admin/includes/meta-boxes.php

    r44214 r44244  
    14641464
    14651465    if ( post_type_supports( $post_type, 'custom-fields' ) ) {
    1466         add_meta_box( 'postcustom', __( 'Custom Fields' ), 'post_custom_meta_box', null, 'normal', 'core', array( '__back_compat_meta_box' => true ) );
     1466        $args = array(
     1467            '__back_compat_meta_box'             => ! (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
     1468            '__block_editor_compatible_meta_box' => true,
     1469        );
     1470        add_meta_box( 'postcustom', __( 'Custom Fields' ), 'post_custom_meta_box', null, 'normal', 'core', $args );
    14671471    }
    14681472
  • trunk/src/wp-admin/includes/template.php

    r44178 r44244  
    11621162
    11631163                    $i++;
    1164                     $hidden_class = in_array( $box['id'], $hidden ) ? ' hide-if-js' : '';
     1164                    // get_hidden_meta_boxes() doesn't apply in the block editor.
     1165                    $hidden_class = ( ! $screen->is_block_editor() && in_array( $box['id'], $hidden ) ) ? ' hide-if-js' : '';
    11651166                    echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes( $box['id'], $page ) . $hidden_class . '" ' . '>' . "\n";
    11661167                    if ( 'dashboard_browser_nag' != $box['id'] ) {
Note: See TracChangeset for help on using the changeset viewer.