Make WordPress Core

Changeset 43885


Ignore:
Timestamp:
11/12/2018 03:37:41 AM (6 years ago)
Author:
pento
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.
See #45282.

Location:
branches/5.0/src/wp-admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-admin/edit-form-blocks.php

    r43884 r43885  
    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 ) ) {
     
    317318}
    318319
     320/**
     321 * Scripts
     322 */
     323wp_enqueue_media(
     324    array(
     325        'post' => $post->ID,
     326    )
     327);
     328wp_enqueue_editor();
     329
     330/**
     331 * Styles
     332 */
     333wp_enqueue_style( 'wp-edit-post' );
     334wp_enqueue_style( 'wp-format-library' );
     335
     336/**
     337 * Fires after block assets have been enqueued for the editing interface.
     338 *
     339 * Call `add_action` on any hook before 'admin_enqueue_scripts'.
     340 *
     341 * In the function call you supply, simply use `wp_enqueue_script` and
     342 * `wp_enqueue_style` to add your functionality to the block editor.
     343 *
     344 * @since 5.0.0
     345 */
     346do_action( 'enqueue_block_editor_assets' );
     347
     348// In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
     349require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
     350register_and_do_post_meta_boxes( $post );
     351
     352// Check if the Custom Fields meta box has been removed at some point.
     353$core_meta_boxes = $wp_meta_boxes[ $current_screen->id ]['normal']['core'];
     354if ( ! isset( $core_meta_boxes['postcustom'] ) || ! $core_meta_boxes['postcustom'] ) {
     355    unset( $editor_settings['enableCustomFields'] );
     356}
     357
     358/**
     359 * Filters the settings to pass to the block editor.
     360 *
     361 * @since 5.0.0
     362 *
     363 * @param array   $editor_settings Default editor settings.
     364 * @param WP_Post $post            Post being edited.
     365 */
     366$editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
     367
    319368$init_script = <<<JS
    320369( function() {
     
    327376JS;
    328377
    329 
    330 /**
    331  * Filters the settings to pass to the block editor.
    332  *
    333  * @since 5.0.0
    334  *
    335  * @param array   $editor_settings Default editor settings.
    336  * @param WP_Post $post            Post being edited.
    337  */
    338 $editor_settings = apply_filters( 'block_editor_settings', $editor_settings, $post );
    339 
    340378$script = sprintf(
    341379    $init_script,
     
    346384);
    347385wp_add_inline_script( 'wp-edit-post', $script );
    348 
    349 /**
    350  * Scripts
    351  */
    352 wp_enqueue_media(
    353     array(
    354         'post' => $post->ID,
    355     )
    356 );
    357 wp_enqueue_editor();
    358 
    359 /**
    360  * Styles
    361  */
    362 wp_enqueue_style( 'wp-edit-post' );
    363 wp_enqueue_style( 'wp-format-library' );
    364 
    365 /**
    366  * Fires after block assets have been enqueued for the editing interface.
    367  *
    368  * Call `add_action` on any hook before 'admin_enqueue_scripts'.
    369  *
    370  * In the function call you supply, simply use `wp_enqueue_script` and
    371  * `wp_enqueue_style` to add your functionality to the block editor.
    372  *
    373  * @since 5.0.0
    374  */
    375 do_action( 'enqueue_block_editor_assets' );
    376 
    377 // In order to duplicate classic meta box behaviour, we need to run the classic meta box actions.
    378 require_once( ABSPATH . 'wp-admin/includes/meta-boxes.php' );
    379 register_and_do_post_meta_boxes( $post );
    380386
    381387require_once( ABSPATH . 'wp-admin/admin-header.php' );
  • branches/5.0/src/wp-admin/includes/meta-boxes.php

    r43863 r43885  
    13621362
    13631363    if ( post_type_supports($post_type, 'custom-fields') ) {
    1364         $screen = get_current_screen();
    1365         if ( ! $screen || ! $screen->is_block_editor() || (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ) ) {
    1366             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 ) );
    1367         }
     1364        $args = array(
     1365            '__back_compat_meta_box' => ! (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ),
     1366            '__block_editor_compatible_meta_box' => true
     1367        );
     1368        add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', null, 'normal', 'core', $args );
    13681369    }
    13691370
  • branches/5.0/src/wp-admin/includes/template.php

    r43839 r43885  
    10771077
    10781078                    $i++;
    1079                     $hidden_class = in_array($box['id'], $hidden) ? ' hide-if-js' : '';
     1079                    // get_hidden_meta_boxes() doesn't apply in the block editor.
     1080                    $hidden_class = ( ! $screen->is_block_editor() && in_array( $box['id'], $hidden ) ) ? ' hide-if-js' : '';
    10801081                    echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . $hidden_class . '" ' . '>' . "\n";
    10811082                    if ( 'dashboard_browser_nag' != $box['id'] ) {
Note: See TracChangeset for help on using the changeset viewer.