Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EDITOR_SETTINGS_DEFAULTS makes it impossible to hide the custom fields toggle #33830

Closed
3 tasks done
mreishus opened this issue Aug 2, 2021 · 3 comments · Fixed by #33931
Closed
3 tasks done

EDITOR_SETTINGS_DEFAULTS makes it impossible to hide the custom fields toggle #33830

mreishus opened this issue Aug 2, 2021 · 3 comments · Fixed by #33931
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen [Type] Bug An existing feature does not function as intended

Comments

@mreishus
Copy link
Contributor

mreishus commented Aug 2, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Have you tried deactivating all plugins except Gutenberg?

  • I have tested with all plugins deactivated.

Have you tried replicating the bug using a default theme e.g. Twenty Twenty?

  • I have tested with a default theme.

Description

Summary

The "Custom Fields" settings is designed to be hidden by an editor setting set in WP.org core PHP code, however it cannot be hidden, due to these factors:

  • gutenberg JS strictly checks for an undefined value
  • If PHP sends a null or false value, those don't match a === check against undefined
  • If PHP does not send a value, gutenberg's default settings apply a false to the value, which is also !== undefined.

Therefore, to hide the "Custom Fields" setting, it wants an undefined to be sent in getEditorSettings().enableCustomFields, but it is impossible to send one.

Field In Question

Visit the Post Editor -> Click the three dots menu in the top right -> Click preferences at the bottom -> A modal appears -> Click the Panels section on the left side -> See a "custom fields" toggle.
2021-08-05_10-08

Mechanism

  • This toggle has a mechanism for it to be hidden in some circumstances. If getEditorSettings().enableCustomFields === undefined, then it is hidden. Code 1, Code 2.
    • I am ignoring the metaBoxes condition here. It works fine and it doesn't need to be examined for this bug.
  • WP.org core code takes care of setting the enableCustomFields editor setting.
    • Most of the time, it comes from a user meta setting.
    • In some circumstances, it is unset, which I take to mean it should be hidden completely.
      • This is the clause being run on WP.com (which uses an unmodified version of this file from WP.org core). It does not support this feature and wants the toggle to be hidden.
      • The guard clause in meta-boxes-section.js checks for getEditorSettings().enableCustomFields === undefined, so this seems like a perfect match. However, its state is initialized with a default value of false, so if php does not send a value, false is the value seen by meta-boxes-section.js. PHP does not have undefined, it has null, but that's not sufficient to pass the check.
WPORG edit-form-blocks.php $editor_settings['enableCustomFields'] gutenberg meta-boxes-section.js getEditorSettings().enableCustomFields notes
true true
false false
null false ☹️
(used unset() to remove value) false ☹️
"1234" "1234"
1234 1234
undefined this is what we need, but it seems impossible

Step-by-step reproduction instructions

  1. Have a WordPress.org installation.

  2. Create wp-content/plugins/disable-custom-fields.php

<?php
/**
 * Plugin Name: Disable Custom Fields
 * Plugin URI: http://www.mywebsite.com/disable-custom-fields
 * Description: Disable Custom Fields
 * Version: 1.0
 * Author: Your Name
 * Author URI: http://www.mywebsite.com
 */

add_action( 'add_meta_boxes', 'plugin_disable_custom_fields', 1 );

function plugin_disable_custom_fields() {
    remove_post_type_support( get_post_type(), 'custom-fields' );
}


add_action( 'do_meta_boxes', 'plugin_meta_boxes', 10, 2 );

function plugin_meta_boxes( $page, $context ) {
    remove_meta_box( 'postcustom', get_post_type(), 'normal' );
}
  1. Visit /wp-admin and activate this plugin.
  2. Visit post editor on WordPress.org site. With this plugin enabled, this if condition will always trigger, and the unset( $editor_settings['enableCustomFields'] ); always runs.
  3. Click the three dots menu in the top right
  4. Click "preferences" at the bottom
    2021-08-05_10-07
  5. A modal appears. Click Panels on the left
  6. Turn on "Custom Fields"
    2021-08-05_10-08
    2021-08-05_10-08_1
  7. See message asking to enable and reload, click the button
  8. Page reloads
  9. Re-visit the custom fields setting, see that it is turned off. (Expected to see: The entire "Custom Settings" option does not appear, because $editor_settings['enableCustomFields'] is unset by this core code.)
    2021-08-02_13-33_1

Expected Behavior

Expected to see: The entire "Custom Settings" option does not appear, because $editor_settings['enableCustomFields'] is unset by this core code, and the display of the field is guarded by enableCustomFields being undefined: part1, part2.

Current Behavior

Actually see: The "Custom Settings" open does appear, even if I do my best to make $editor_settings['enableCustomFields'] undefined.

I believe it's because the default settings ensure that enableCustomFields becomes false if not set. I highly suspect it's impossible to send $editor_settings['enableCustomFields'] === undefined to gutenberg, because the defaults kick in and always overwrite unset values with false, the default, and php does not have an undefined value, only null or not set at all.

Screenshots or screen recording (optional)

No response

Code snippet (optional)

No response

WordPress Information

No response

Gutenberg Information

No response

What browsers are you seeing the problem on?

No response

Device Information

No response

Operating System Information

No response

@mreishus mreishus changed the title EDITOR_SETTINGS_DEFAULTS makes it impossible to hide the custom fields issue EDITOR_SETTINGS_DEFAULTS makes it impossible to hide the custom fields toggle Aug 2, 2021
@Mamaduka Mamaduka added [Feature] Meta Boxes A draggable box shown on the post editing screen [Type] Bug An existing feature does not function as intended labels Aug 3, 2021
@skorasaurus
Copy link
Member

related #31848

@mreishus
Copy link
Contributor Author

mreishus commented Aug 5, 2021

Updated steps to include adding short plugin to a fresh wp.org install instead of editing core files.

@mreishus
Copy link
Contributor Author

mreishus commented Aug 5, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Meta Boxes A draggable box shown on the post editing screen [Type] Bug An existing feature does not function as intended
Projects
None yet
3 participants