WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 23 months ago

#45282 closed defect (bug) (fixed)

Block Editor: Don't show the Custom Fields meta box option if the meta box has been removed

Reported by: pento Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version: 5.0
Component: Editor Keywords: has-patch fixed-5.0
Focuses: Cc:

Description (last modified by pento)

Some plugins remove the Custom Fields meta box, overriding whether the CPT has set the postcustom feature.

When this is the case, the block editor Options dialog shouldn't show an option to display the Custom Fields meta box.

Related: GB11386.

Attachments (1)

45282.diff (4.0 KB) - added by pento 2 years ago.

Download all attachments as: .zip

Change History (14)

@pento
2 years ago

#1 @pento
2 years ago

  • Description modified (diff)
  • Keywords has-patch added

45282.diff removes the enableCustomFields setting from the editor settings, based on whether the Custom Fields meta box exists or not.

It can be tested with this snippet:

<?php
add_action( 'add_meta_boxes', function() {
        remove_meta_box( 'postcustom', false, 'normal' );
} );

This patch requires changes in the relevant @wordpress package to hide the option when enableCustomFields is undefined.

#2 @pento
2 years ago

45282.diff also fixes a bug where any meta box flagged to show in the block editor (eg, __back_compat_meta_box is set to false) would be hidden if they were hidden in the classic editor, regardless of their block editor display setting.

#3 @noisysocks
2 years ago

Nice! I think 45282.diff is the right approach, and it works well in my testing.

To test this, I:

  1. Created a new post.
  2. Ran wp.data.select( 'core/editor' ).getEditorSettings().enableCustomFields in the console. It was false.
  3. Ran document.getElementById( 'toggle-custom-fields-form' ).submit() in the console to toggle the custom fields setting.
  4. Ran wp.data.select( 'core/editor' ).getEditorSettings().enableCustomFields again. It was true this time.
  5. Installed, activated, and set up the ACF plugin which I know deregisters the postcustom meta box.
  6. Created another post.
  7. Ran wp.data.select( 'core/editor' ).getEditorSettings().enableCustomFields again. It was now undefined.

The code looks good. Some comments:

  1. It wasn't clear to me at first that wp.editPost.initializeEditor() was enqueued after the block_editor_settings filter had triggered. Could we move $init_script's declaration in edit-form-blocks.php:335 closer to where it is used in edit-form-blocks.php:395?
  2. Instead of setting $editor_settings['enableCustomFields'] in edit-form-blocks.php:300 and then later unsetting it in edit-form-blocks.php:382, I think it would be clearer if we conditionally set enableCustomFields in the one place after the meta box hook triggers. We do this for other editor settings e.g. autosave, colors, fontSizes.
  3. What does the change in templates.php:1079 do? Never mind, didn't see your comment!
Last edited 2 years ago by noisysocks (previous) (diff)

#5 @websupporter
2 years ago

$hidden_class = ( ! $screen->is_block_editor() && in_array( $box['id'], $hidden ) ) ? ' hide-if-js' : '';

to me it looks like this implicitly solves #45285

#6 @pento
2 years ago

In 43884:

Block Editor: Update @wordpress dependencies to the latest version.

Changes of note:

  • Includes the new Annotations API package.
  • wp-polyfill-ecmascript.js is renamed to wp-polyfill.js.
  • strip_dynamic_blocks() has been removed in favour of excerpt_remove_blocks().
  • The PHP block parser is now syncing from the block-serialization-default-parser package.
  • do_blocks() uses the new parser.
  • The do_block filter has been removed from do_blocks(), in favour of a render_block filter in render_block().

See #45145, #45190, #45264, #45282.

#7 @pento
2 years ago

Thanks for the review, @noisysocks!

  1. Good catch, done!
  2. We can't move it, unfortunately. $wp_meta_boxes will be altered when register_and_do_post_meta_boxes() is run, so we need to check for the postcustom meta box after that. However register_and_do_post_meta_boxes() needs to be run as late as possible, to ensure meta boxes have been registered.

#8 @pento
2 years ago

In 43885:

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.

#9 @pento
2 years ago

  • Keywords fixed-5.0 added

#10 @SergeyBiryukov
23 months ago

In 44244:

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.

#11 @SergeyBiryukov
23 months ago

[43884] still needs a merge.

#12 @desrosj
23 months ago

In 44261:

Block Editor: Update @wordpress dependencies.

Changes of note:

  • Includes the new Annotations API package.
  • wp-polyfill-ecmascript.js is renamed to wp-polyfill.js.
  • strip_dynamic_blocks() has been removed in favor of excerpt_remove_blocks().
  • The PHP block parser is now syncing from the block-serialization-default-parser package.
  • do_blocks() uses the new parser.
  • The do_block filter has been removed from do_blocks(), in favor of a render_block filter in render_block().

Also, a little cleanup to render_block(). Always normalize $block['attrs’] to array in ’render_block’ filter.
Props pento, azaozz.

Merges [43884] and [43888] to trunk.

See #45145, #45190, #45264, #45282.

#13 @SergeyBiryukov
23 months ago

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.