From 409809312bfbcd1bad517e38f7ec25e1701479c5 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Fri, 26 Oct 2018 13:52:49 +1100 Subject: [PATCH 1/9] Advanced Panels: Add support for the 'Custom Fields' meta box - Adds an option which allows the user to enable the existing 'postcustom' meta box included in WordPress. - Don't load the 'postcustom' assets when the option is disabled. - Stores the option as a site option. - Reload the editor when the option is changed. --- lib/client-assets.php | 15 +++- lib/meta-box-partial-page.php | 52 +++++++++++- .../src/components/options-modal/index.js | 29 +++++-- .../src/components/options-modal/options.js | 85 ------------------- .../components/options-modal/options/base.js | 17 ++++ .../options-modal/options/deferred.js | 36 ++++++++ .../options/enable-custom-fields.js | 48 +++++++++++ .../options-modal/options/enable-panel.js | 19 +++++ .../options/enable-publish-sidebar.js | 26 ++++++ .../options-modal/options/enable-tips.js | 27 ++++++ .../components/options-modal/options/index.js | 4 + .../test/__snapshots__/index.js.snap | 17 ++-- 12 files changed, 275 insertions(+), 100 deletions(-) delete mode 100644 packages/edit-post/src/components/options-modal/options.js create mode 100644 packages/edit-post/src/components/options-modal/options/base.js create mode 100644 packages/edit-post/src/components/options-modal/options/deferred.js create mode 100644 packages/edit-post/src/components/options-modal/options/enable-custom-fields.js create mode 100644 packages/edit-post/src/components/options-modal/options/enable-panel.js create mode 100644 packages/edit-post/src/components/options-modal/options/enable-publish-sidebar.js create mode 100644 packages/edit-post/src/components/options-modal/options/enable-tips.js create mode 100644 packages/edit-post/src/components/options-modal/options/index.js diff --git a/lib/client-assets.php b/lib/client-assets.php index b68c2fa5c9903..246f9f4cfa2d7 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -1602,14 +1602,27 @@ function gutenberg_editor_scripts_and_styles( $hook ) { 'maxUploadFileSize' => $max_upload_size, 'allowedMimeTypes' => get_allowed_mime_types(), 'styles' => $styles, - 'postLock' => $lock_details, // Ideally, we'd remove this and rely on a REST API endpoint. + 'postLock' => $lock_details, 'postLockUtils' => array( 'nonce' => wp_create_nonce( 'lock-post_' . $post->ID ), 'unlockNonce' => wp_create_nonce( 'update-post_' . $post->ID ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), ), + + // Whether or not to load the 'postcustom' meta box is stored as a site option + // so that we're not always loading its assets. + 'customFields' => array( + 'isEnabled' => get_option( 'enable_custom_fields', false ), + 'toggleURL' => add_query_arg( + array( + 'action' => 'toggle_custom_fields', + '_wpnonce' => wp_create_nonce( 'toggle_custom_fields' ), + ), + admin_url( 'admin-post.php' ) + ), + ), ); $post_autosave = gutenberg_get_autosave_newer_than_post_save( $post ); diff --git a/lib/meta-box-partial-page.php b/lib/meta-box-partial-page.php index e50aaffb8c3a4..db1ec8b500e7c 100644 --- a/lib/meta-box-partial-page.php +++ b/lib/meta-box-partial-page.php @@ -113,7 +113,6 @@ function gutenberg_filter_meta_boxes( $meta_boxes ) { $core_normal_meta_boxes = array( 'revisionsdiv', 'postexcerpt', - 'postcustom', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv', @@ -121,6 +120,13 @@ function gutenberg_filter_meta_boxes( $meta_boxes ) { 'authordiv', ); + // Whether or not to load the 'postcustom' meta box is stored as a site option + // so that we're not always loading its assets. + $enable_custom_fields = get_option( 'enable_custom_fields', false ); + if ( ! $enable_custom_fields ) { + $core_normal_meta_boxes[] = 'postcustom'; + } + $taxonomy_callbacks_to_unset = array( 'post_tags_meta_box', 'post_categories_meta_box', @@ -359,6 +365,32 @@ function the_gutenberg_metaboxes() { printf( "\n", trim( $script ) ); } + /** + * If the 'postcustom' meta box is enabled, then we need to perform some + * extra initialization on it. + */ + $enable_custom_fields = get_option( 'enable_custom_fields', false ); + if ( $enable_custom_fields ) { + $script = << - { metaBoxes.length !== 0 && ( -
- { map( metaBoxes, ( { title, id } ) => ( - - ) ) } -
- ) } +
+ + { map( + metaBoxes, + ( { title, id } ) => + // The 'Custom Fields' meta box is a special case handled above. + id !== 'postcustom' && ( + + ) + ) } +
); } diff --git a/packages/edit-post/src/components/options-modal/options.js b/packages/edit-post/src/components/options-modal/options.js deleted file mode 100644 index 7f9cd5845c968..0000000000000 --- a/packages/edit-post/src/components/options-modal/options.js +++ /dev/null @@ -1,85 +0,0 @@ -/** - * WordPress dependencies - */ -import { CheckboxControl } from '@wordpress/components'; -import { Component } from '@wordpress/element'; -import { compose } from '@wordpress/compose'; -import { withSelect, withDispatch } from '@wordpress/data'; -import { ifViewportMatches } from '@wordpress/viewport'; - -function Option( { label, isChecked, onChange } ) { - return ( - - ); -} - -class DeferredOption extends Component { - constructor( { isChecked } ) { - super( ...arguments ); - this.state = { - isChecked, - }; - } - - componentWillUnmount() { - if ( this.state.isChecked !== this.props.isChecked ) { - this.props.onChange( this.state.isChecked ); - } - } - - render() { - return ( -