From 83e6ded94d41acf4dc529ee2ef149e4987c738dc Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 13:48:04 +0100 Subject: [PATCH 1/9] Allow filtering of Theme JSON final config --- lib/class-wp-theme-json-resolver-gutenberg.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index a208cbd726d88e..5cf8ca3e2b56b4 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -412,7 +412,7 @@ public static function get_merged_data( $settings = array(), $origin = 'user' ) if ( ! get_theme_support( 'experimental-link-color' ) && // link color support needs the presets CSS variables regardless of the presence of theme.json file. - ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() + ! self::theme_has_support() ) { return $result; } @@ -424,6 +424,8 @@ public static function get_merged_data( $settings = array(), $origin = 'user' ) $result->merge( self::get_user_data() ); } + $result = apply_filters( 'theme_json_resolver_merged_data', $result ); + return $result; } From 53100bc7bcee14914a122deb2fd61be8264bbb27 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:26:04 +0100 Subject: [PATCH 2/9] Allow block-level settings to be added to valid settings config --- lib/class-wp-theme-json-gutenberg.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 29cca882ffa138..aae00315812b15 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -78,13 +78,16 @@ class WP_Theme_JSON_Gutenberg { ); const VALID_SETTINGS = array( - 'border' => array( + 'core/navigation' => array( + 'hasSubmenuIndicatorSetting' => null, + ), + 'border' => array( 'customColor' => null, 'customRadius' => null, 'customStyle' => null, 'customWidth' => null, ), - 'color' => array( + 'color' => array( 'background' => null, 'custom' => null, 'customDuotone' => null, @@ -95,19 +98,19 @@ class WP_Theme_JSON_Gutenberg { 'palette' => null, 'text' => null, ), - 'custom' => null, - 'layout' => array( + 'custom' => null, + 'layout' => array( 'contentSize' => null, 'wideSize' => null, ), - 'spacing' => array( + 'spacing' => array( 'blockGap' => null, 'customMargin' => null, 'customPadding' => null, 'units' => null, 'blockGap' => null, ), - 'typography' => array( + 'typography' => array( 'customFontSize' => null, 'customFontStyle' => null, 'customFontWeight' => null, @@ -315,11 +318,14 @@ private static function sanitize( $input, $valid_block_names, $valid_element_nam } $schema_styles_blocks = array(); $schema_settings_blocks = array(); + foreach ( $valid_block_names as $block ) { - $schema_settings_blocks[ $block ] = self::VALID_SETTINGS; + $schema_settings_blocks[ $block ] = self::VALID_SETTINGS[ $block ] ? array_merge( self::VALID_SETTINGS, self::VALID_SETTINGS[ $block ] ) : self::VALID_SETTINGS; $schema_styles_blocks[ $block ] = self::VALID_STYLES; $schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements; } + + $schema['styles'] = self::VALID_STYLES; $schema['styles']['blocks'] = $schema_styles_blocks; $schema['styles']['elements'] = $schema_styles_elements; From 5b0a576aa92bf1a7fd0b8cc12a74f452e0451e92 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:26:24 +0100 Subject: [PATCH 3/9] Allow for filtering of Theme-level theme JSON settings. --- lib/class-wp-theme-json-resolver-gutenberg.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-resolver-gutenberg.php b/lib/class-wp-theme-json-resolver-gutenberg.php index 5cf8ca3e2b56b4..19258d2e80a086 100644 --- a/lib/class-wp-theme-json-resolver-gutenberg.php +++ b/lib/class-wp-theme-json-resolver-gutenberg.php @@ -420,12 +420,14 @@ public static function get_merged_data( $settings = array(), $origin = 'user' ) $theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( $settings ); $result->merge( self::get_theme_data( $theme_support_data ) ); + $filter_data = new WP_Theme_JSON_Gutenberg( apply_filters( 'theme_json_resolver_merged_data', $result->get_raw_data() ) ); + + $result->merge( $filter_data ); + if ( 'user' === $origin ) { $result->merge( self::get_user_data() ); } - $result = apply_filters( 'theme_json_resolver_merged_data', $result ); - return $result; } From 4a96cdb1edd1e165efe15c7c07cc3eec0b8978cb Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:27:53 +0100 Subject: [PATCH 4/9] Get setting from modified Theme JSON rather than block level prop --- packages/block-library/src/navigation/edit.js | 5 ++++- .../src/filters/remove-edit-unsupported-features.js | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/edit.js b/packages/block-library/src/navigation/edit.js index b9e9fd8e53184e..c541d7f0b14137 100644 --- a/packages/block-library/src/navigation/edit.js +++ b/packages/block-library/src/navigation/edit.js @@ -24,6 +24,7 @@ import { PanelColorSettings, ContrastChecker, getColorClassName, + useSetting, } from '@wordpress/block-editor'; import { useDispatch, withSelect, withDispatch } from '@wordpress/data'; import { PanelBody, ToggleControl, ToolbarGroup } from '@wordpress/components'; @@ -103,7 +104,6 @@ function Navigation( { // These props are used by the navigation editor to override specific // navigation block settings. - hasSubmenuIndicatorSetting = true, hasItemJustificationControls = true, hasColorSettings = true, customPlaceholder: CustomPlaceholder = null, @@ -116,6 +116,9 @@ function Navigation( { false ); + const hasSubmenuIndicatorSetting = + useSetting( 'hasSubmenuIndicatorSetting' ) || true; // retain original prop default of "true" + const { selectBlock } = useDispatch( blockEditorStore ); const navRef = useRef(); diff --git a/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js b/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js index 2c58b995158d4d..f12293e37f00f3 100644 --- a/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js +++ b/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js @@ -13,7 +13,6 @@ const removeNavigationBlockEditUnsupportedFeatures = createHigherOrderComponent( return ( From 5a24c37c3ad3140c879505651518f511c45de657 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:36:17 +0100 Subject: [PATCH 5/9] Default to previous prop value default only if setting is nullish --- packages/block-library/src/navigation/edit.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/edit.js b/packages/block-library/src/navigation/edit.js index c541d7f0b14137..68f8196e95c048 100644 --- a/packages/block-library/src/navigation/edit.js +++ b/packages/block-library/src/navigation/edit.js @@ -104,7 +104,6 @@ function Navigation( { // These props are used by the navigation editor to override specific // navigation block settings. - hasItemJustificationControls = true, hasColorSettings = true, customPlaceholder: CustomPlaceholder = null, customAppender: CustomAppender = null, @@ -115,9 +114,11 @@ function Navigation( { const [ isResponsiveMenuOpen, setResponsiveMenuVisibility ] = useState( false ); - const hasSubmenuIndicatorSetting = - useSetting( 'hasSubmenuIndicatorSetting' ) || true; // retain original prop default of "true" + useSetting( 'hasSubmenuIndicatorSetting' ) ?? true; // retain original prop default of "true" if there is no setting defined. + + const hasItemJustificationControls = + useSetting( 'hasItemJustificationControls' ) ?? true; // retain original prop default of "true" if there is no setting defined. const { selectBlock } = useDispatch( blockEditorStore ); From 06db44a857091f6dffad965eea0e001b8a9c0d9f Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:36:31 +0100 Subject: [PATCH 6/9] Allow Theme JSON to manage justification controls --- lib/class-wp-theme-json-gutenberg.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index aae00315812b15..8db37b177379de 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -80,6 +80,7 @@ class WP_Theme_JSON_Gutenberg { const VALID_SETTINGS = array( 'core/navigation' => array( 'hasSubmenuIndicatorSetting' => null, + 'hasItemJustificationControls' => null, ), 'border' => array( 'customColor' => null, From 9ae1bf2b2b583dce54f9ddbfb55bfbcbd3b46621 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:42:32 +0100 Subject: [PATCH 7/9] Add Theme JSON filter to main navigation file --- lib/navigation.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/navigation.php b/lib/navigation.php index 0a4ddab251efa1..8f3a0c778c5475 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -384,3 +384,29 @@ function gutenberg_add_block_menu_item_styles_to_nav_menus( $hook ) { } } add_action( 'admin_enqueue_scripts', 'gutenberg_add_block_menu_item_styles_to_nav_menus' ); + + +/** + * Filters the Theme JSON settings for the Navigation block + * when used in the Nav Editor. + */ + + +$is_nav_editor_screen = true; + +// TODO: only apply filter when on Nav Editor screen. +if ( $is_nav_editor_screen ) { + add_filter( + 'theme_json_resolver_merged_data', + function( $data ) { + + $data['settings']['blocks']['core/navigation'] = array( + 'hasSubmenuIndicatorSetting' => false, + 'hasItemJustificationControls' => false, + 'hasColorSettings' => false, + ); + + return $data; + } + ); +} From 31a2dace6dffe1777242934b69ab3e0c0be42371 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:42:45 +0100 Subject: [PATCH 8/9] Allow color settings to be controlled via Theme JSON --- lib/class-wp-theme-json-gutenberg.php | 1 + packages/block-library/src/navigation/edit.js | 3 ++- .../src/filters/remove-edit-unsupported-features.js | 8 +------- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 8db37b177379de..1d3e91a3a1684d 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -81,6 +81,7 @@ class WP_Theme_JSON_Gutenberg { 'core/navigation' => array( 'hasSubmenuIndicatorSetting' => null, 'hasItemJustificationControls' => null, + 'hasColorSettings' => null, ), 'border' => array( 'customColor' => null, diff --git a/packages/block-library/src/navigation/edit.js b/packages/block-library/src/navigation/edit.js index 68f8196e95c048..26b6cc72a8fe68 100644 --- a/packages/block-library/src/navigation/edit.js +++ b/packages/block-library/src/navigation/edit.js @@ -104,7 +104,6 @@ function Navigation( { // These props are used by the navigation editor to override specific // navigation block settings. - hasColorSettings = true, customPlaceholder: CustomPlaceholder = null, customAppender: CustomAppender = null, } ) { @@ -120,6 +119,8 @@ function Navigation( { const hasItemJustificationControls = useSetting( 'hasItemJustificationControls' ) ?? true; // retain original prop default of "true" if there is no setting defined. + const hasColorSettings = useSetting( 'hasColorSettings' ) ?? true; // retain original prop default of "true" if there is no setting defined. + const { selectBlock } = useDispatch( blockEditorStore ); const navRef = useRef(); diff --git a/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js b/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js index f12293e37f00f3..d06fdd7f16a486 100644 --- a/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js +++ b/packages/edit-navigation/src/filters/remove-edit-unsupported-features.js @@ -10,13 +10,7 @@ const removeNavigationBlockEditUnsupportedFeatures = createHigherOrderComponent( return ; } - return ( - - ); + return ; }, 'removeNavigationBlockEditUnsupportedFeatures' ); From 4aff550d1aa5d901953ef5b5b8ca164d96321f63 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 13 Sep 2021 14:55:04 +0100 Subject: [PATCH 9/9] Conditionalise filtering of Theme JSON to Navigation editor screen --- lib/navigation-page.php | 34 +++++++++++++++++++++++++++++++++- lib/navigation.php | 24 ------------------------ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 568fca8cb3bbfa..ca1c990891fae2 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -74,7 +74,7 @@ function gutenberg_navigation_init( $hook ) { * @return bool Filtered decision about loading block assets. */ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) { - if ( is_callable( 'get_current_screen' ) && get_current_screen() && 'gutenberg_page_gutenberg-navigation' === get_current_screen()->base ) { + if ( is_navigation_editor_screen() ) { return true; } @@ -82,3 +82,35 @@ function gutenberg_navigation_editor_load_block_editor_scripts_and_styles( $is_b } add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_navigation_editor_load_block_editor_scripts_and_styles' ); + +/** + * Filters the Theme JSON to alter the settings of the Navigation block. + * + * @param Array $data the raw Theme JSON config at the Theme level. + * @return Array the amended Theme JSON config. + */ +function gutenberg_navigation_editor_filter_navigation_block_settings( $data ) { + if ( is_navigation_editor_screen() ) { + $data['settings']['blocks']['core/navigation'] = array( + 'hasSubmenuIndicatorSetting' => false, + 'hasItemJustificationControls' => false, + 'hasColorSettings' => false, + ); + } + + return $data; +} + +add_filter( + 'theme_json_resolver_merged_data', + 'gutenberg_navigation_editor_filter_navigation_block_settings' +); + +/** + * Determines whether we are on the navigation editor screen. + * + * @return boolean whether or not we are on the navigation editor screen. + */ +function is_navigation_editor_screen() { + return is_callable( 'get_current_screen' ) && get_current_screen() && 'gutenberg_page_gutenberg-navigation' === get_current_screen()->base; +} diff --git a/lib/navigation.php b/lib/navigation.php index 8f3a0c778c5475..322e4b58e76309 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -386,27 +386,3 @@ function gutenberg_add_block_menu_item_styles_to_nav_menus( $hook ) { add_action( 'admin_enqueue_scripts', 'gutenberg_add_block_menu_item_styles_to_nav_menus' ); -/** - * Filters the Theme JSON settings for the Navigation block - * when used in the Nav Editor. - */ - - -$is_nav_editor_screen = true; - -// TODO: only apply filter when on Nav Editor screen. -if ( $is_nav_editor_screen ) { - add_filter( - 'theme_json_resolver_merged_data', - function( $data ) { - - $data['settings']['blocks']['core/navigation'] = array( - 'hasSubmenuIndicatorSetting' => false, - 'hasItemJustificationControls' => false, - 'hasColorSettings' => false, - ); - - return $data; - } - ); -}