From 3a47a1a46d066c21949d7babf083f264964679f4 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Wed, 12 Aug 2020 11:49:34 +1000 Subject: [PATCH 1/8] Navigation screen: Add opt-in Navigation block rendering Allows themes to opt-in to having `wp_nav_menu()` render a complete Navigation block in place of the menu. This allows arbitrarily complex Navigation block structures can be used in an existing theme. When a theme declares `add_theme_support( 'block-nav-menus' )`, users are able to add non-link blocks to a Navigation using the Navigation screen. This block tree is stored as menu items and re-assembled into a Navigation block for display using `render_block()` in the frontend. Non-link blocks are stored using a menu item with type `'block'` which renders properly in `nav-menus.php`. This allows users to switch to a theme that does not support block menus and still see their data in WP Admin. --- lib/class-wp-rest-menu-items-controller.php | 10 +- lib/compat.php | 108 ------ lib/load.php | 1 + lib/navigation-page.php | 1 + lib/navigation.php | 356 ++++++++++++++++++ packages/edit-navigation/src/index.js | 25 +- packages/edit-navigation/src/store/actions.js | 2 +- .../edit-navigation/src/store/resolvers.js | 8 +- 8 files changed, 390 insertions(+), 121 deletions(-) create mode 100644 lib/navigation.php diff --git a/lib/class-wp-rest-menu-items-controller.php b/lib/class-wp-rest-menu-items-controller.php index 7eee6c533cfe9d..c0f70208c4279a 100644 --- a/lib/class-wp-rest-menu-items-controller.php +++ b/lib/class-wp-rest-menu-items-controller.php @@ -440,10 +440,10 @@ protected function prepare_item_for_database( $request ) { } } - // If menu item is type html, then content is required. - if ( 'html' === $prepared_nav_item['menu-item-type'] ) { + // If menu item is type block, then content is required. + if ( 'block' === $prepared_nav_item['menu-item-type'] ) { if ( empty( $prepared_nav_item['menu-item-content'] ) ) { - return new WP_Error( 'rest_content_required', __( 'Content required if menu item of type html.', 'gutenberg' ), array( 'status' => 400 ) ); + return new WP_Error( 'rest_content_required', __( 'Content required if menu item of type block.', 'gutenberg' ), array( 'status' => 400 ) ); } } @@ -819,7 +819,7 @@ public function get_item_schema() { $schema['properties']['type'] = array( 'description' => __( 'The family of objects originally represented, such as "post_type" or "taxonomy".', 'gutenberg' ), 'type' => 'string', - 'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom', 'html' ), + 'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom', 'block' ), 'context' => array( 'view', 'edit', 'embed' ), 'default' => 'custom', ); @@ -894,7 +894,7 @@ public function get_item_schema() { ); $schema['properties']['content'] = array( - 'description' => __( 'HTML content to display for this menu item. May contain blocks.', 'gutenberg' ), + 'description' => __( 'HTML content to display for this block menu item.', 'gutenberg' ), 'context' => array( 'view', 'edit', 'embed' ), 'type' => 'object', 'arg_options' => array( diff --git a/lib/compat.php b/lib/compat.php index 5941bbb1f23b88..d7aa95dfe0c5a5 100644 --- a/lib/compat.php +++ b/lib/compat.php @@ -532,114 +532,6 @@ function gutenberg_render_block_with_assigned_block_context( $pre_render, $parse } add_filter( 'pre_render_block', 'gutenberg_render_block_with_assigned_block_context', 9, 2 ); -/** - * Shim that hooks into `wp_update_nav_menu_item` and makes it so that nav menu - * items support a 'content' field. This field contains HTML and is used by nav - * menu items with `type` set to `'html'`. - * - * Specifically, this shim makes it so that: - * - * 1) The `wp_update_nav_menu_item()` function supports setting - * `'menu-item-content'` on a menu item. When merged to Core, this functionality - * should exist in `wp_update_nav_menu_item()`. - * - * 2) The `customize_save` ajax action supports setting `'content'` on a nav - * menu item. When merged to Core, this functionality should exist in - * `WP_Customize_Manager::save()`. - * - * This shim can be removed when the Gutenberg plugin requires a WordPress - * version that has the ticket below. - * - * @see https://core.trac.wordpress.org/ticket/50544 - * - * @param int $menu_id ID of the updated menu. - * @param int $menu_item_db_id ID of the new menu item. - * @param array $args An array of arguments used to update/add the menu item. - */ -function gutenberg_update_nav_menu_item_content( $menu_id, $menu_item_db_id, $args ) { - global $wp_customize; - - // Support setting content in customize_save admin-ajax.php requests by - // grabbing the unsanitized $_POST values. - if ( isset( $wp_customize ) ) { - $values = $wp_customize->unsanitized_post_values(); - if ( isset( $values[ "nav_menu_item[$menu_item_db_id]" ]['content'] ) ) { - if ( is_string( $values[ "nav_menu_item[$menu_item_db_id]" ]['content'] ) ) { - $args['menu-item-content'] = $values[ "nav_menu_item[$menu_item_db_id]" ]['content']; - } elseif ( isset( $values[ "nav_menu_item[$menu_item_db_id]" ]['content']['raw'] ) ) { - $args['menu-item-content'] = $values[ "nav_menu_item[$menu_item_db_id]" ]['content']['raw']; - } - } - } - - $defaults = array( - 'menu-item-content' => '', - ); - - $args = wp_parse_args( $args, $defaults ); - - update_post_meta( $menu_item_db_id, '_menu_item_content', wp_slash( $args['menu-item-content'] ) ); -} -add_action( 'wp_update_nav_menu_item', 'gutenberg_update_nav_menu_item_content', 10, 3 ); - -/** - * Shim that hooks into `wp_setup_nav_menu_items` and makes it so that nav menu - * items have a 'content' field. This field contains HTML and is used by nav - * menu items with `type` set to `'html'`. - * - * Specifically, this shim makes it so that the `wp_setup_nav_menu_item()` - * function sets `content` on the returned menu item. When merged to Core, this - * functionality should exist in `wp_setup_nav_menu_item()`. - * - * This shim can be removed when the Gutenberg plugin requires a WordPress - * version that has the ticket below. - * - * @see https://core.trac.wordpress.org/ticket/50544 - * - * @param object $menu_item The menu item object. - */ -function gutenberg_setup_html_nav_menu_item( $menu_item ) { - if ( 'html' === $menu_item->type ) { - $menu_item->type_label = __( 'HTML', 'gutenberg' ); - $menu_item->content = ! isset( $menu_item->content ) ? get_post_meta( $menu_item->db_id, '_menu_item_content', true ) : $menu_item->content; - } - - return $menu_item; -} -add_filter( 'wp_setup_nav_menu_item', 'gutenberg_setup_html_nav_menu_item' ); - -/** - * Shim that hooks into `walker_nav_menu_start_el` and makes it so that the - * default walker which renders a menu will correctly render the HTML associated - * with any navigation menu item that has `type` set to `'html`'. - * - * Specifically, this shim makes it so that `Walker_Nav_Menu::start_el()` - * renders the `content` of a nav menu item when its `type` is `'html'`. When - * merged to Core, this functionality should exist in - * `Walker_Nav_Menu::start_el()`. - * - * This shim can be removed when the Gutenberg plugin requires a WordPress - * version that has the ticket below. - * - * @see https://core.trac.wordpress.org/ticket/50544 - * - * @param string $item_output The menu item's starting HTML output. - * @param WP_Post $item Menu item data object. - * @param int $depth Depth of menu item. Used for padding. - * @param stdClass $args An object of wp_nav_menu() arguments. - */ -function gutenberg_output_html_nav_menu_item( $item_output, $item, $depth, $args ) { - if ( 'html' === $item->type ) { - $item_output = $args->before; - /** This filter is documented in wp-includes/post-template.php */ - $item_output .= apply_filters( 'the_content', $item->content ); - $item_output .= $args->after; - } - - return $item_output; -} -add_filter( 'walker_nav_menu_start_el', 'gutenberg_output_html_nav_menu_item', 10, 4 ); - /** * Amends the paths to preload when initializing edit post. * diff --git a/lib/load.php b/lib/load.php index 2765d56b35189f..146f2d032aee66 100644 --- a/lib/load.php +++ b/lib/load.php @@ -108,6 +108,7 @@ function gutenberg_is_experiment_enabled( $name ) { require dirname( __FILE__ ) . '/block-directory.php'; require dirname( __FILE__ ) . '/demo.php'; require dirname( __FILE__ ) . '/widgets.php'; +require dirname( __FILE__ ) . '/navigation.php'; require dirname( __FILE__ ) . '/navigation-page.php'; require dirname( __FILE__ ) . '/experiments-page.php'; require dirname( __FILE__ ) . '/customizer.php'; diff --git a/lib/navigation-page.php b/lib/navigation-page.php index 74227fcfbd1cd9..e2e37f16f2859a 100644 --- a/lib/navigation-page.php +++ b/lib/navigation-page.php @@ -63,6 +63,7 @@ function gutenberg_navigation_init( $hook ) { 'imageSizes' => $available_image_sizes, 'isRTL' => is_rtl(), 'maxUploadFileSize' => $max_upload_size, + 'blockNavMenus' => get_theme_support( 'block-nav-menus' ), ); list( $color_palette, ) = (array) get_theme_support( 'editor-color-palette' ); diff --git a/lib/navigation.php b/lib/navigation.php new file mode 100644 index 00000000000000..e3d930d5ce39dc --- /dev/null +++ b/lib/navigation.php @@ -0,0 +1,356 @@ +unsanitized_post_values(); + if ( isset( $values[ "nav_menu_item[$menu_item_db_id]" ]['content'] ) ) { + if ( is_string( $values[ "nav_menu_item[$menu_item_db_id]" ]['content'] ) ) { + $args['menu-item-content'] = $values[ "nav_menu_item[$menu_item_db_id]" ]['content']; + } elseif ( isset( $values[ "nav_menu_item[$menu_item_db_id]" ]['content']['raw'] ) ) { + $args['menu-item-content'] = $values[ "nav_menu_item[$menu_item_db_id]" ]['content']['raw']; + } + } + } + + $defaults = array( + 'menu-item-content' => '', + ); + + $args = wp_parse_args( $args, $defaults ); + + update_post_meta( $menu_item_db_id, '_menu_item_content', wp_slash( $args['menu-item-content'] ) ); +} +add_action( 'wp_update_nav_menu_item', 'gutenberg_update_nav_menu_item_content', 10, 3 ); + +/** + * Shim that hooks into `wp_setup_nav_menu_items` and makes it so that nav menu + * items have a 'content' field. This field contains HTML and is used by nav + * menu items with `type` set to `'block'`. + * + * Specifically, this shim makes it so that the `wp_setup_nav_menu_item()` + * function sets `content` on the returned menu item. When merged to Core, this + * functionality should exist in `wp_setup_nav_menu_item()`. + * + * This shim can be removed when the Gutenberg plugin requires a WordPress + * version that has the ticket below. + * + * @see https://core.trac.wordpress.org/ticket/50544 + * + * @param object $menu_item The menu item object. + */ +function gutenberg_setup_block_nav_menu_item( $menu_item ) { + if ( 'block' === $menu_item->type ) { + $menu_item->type_label = __( 'Gutenberg Block', 'gutenberg' ); + $menu_item->content = ! isset( $menu_item->content ) ? get_post_meta( $menu_item->db_id, '_menu_item_content', true ) : $menu_item->content; + + // Set to make the menu item display nicely in nav-menus.php. + $menu_item->object = 'block'; + $menu_item->title = __( 'Gutenberg Block', 'gutenberg' ); + } + + return $menu_item; +} +add_filter( 'wp_setup_nav_menu_item', 'gutenberg_setup_block_nav_menu_item' ); + +/** + * Shim that hooks into `walker_nav_menu_start_el` and makes it so that the + * default walker which renders a menu will correctly render the HTML associated + * with any navigation menu item that has `type` set to `'block`'. + * + * Specifically, this shim makes it so that `Walker_Nav_Menu::start_el()` + * renders the `content` of a nav menu item when its `type` is `'block'`. When + * merged to Core, this functionality should exist in + * `Walker_Nav_Menu::start_el()`. + * + * This shim can be removed when the Gutenberg plugin requires a WordPress + * version that has the ticket below. + * + * @see https://core.trac.wordpress.org/ticket/50544 + * + * @param string $item_output The menu item's starting HTML output. + * @param WP_Post $item Menu item data object. + * @param int $depth Depth of menu item. Used for padding. + * @param stdClass $args An object of wp_nav_menu() arguments. + */ +function gutenberg_output_block_nav_menu_item( $item_output, $item, $depth, $args ) { + if ( 'block' === $item->type ) { + $item_output = $args->before; + /** This filter is documented in wp-includes/post-template.php */ + $item_output .= apply_filters( 'the_content', $item->content ); + $item_output .= $args->after; + } + + return $item_output; +} +add_filter( 'walker_nav_menu_start_el', 'gutenberg_output_block_nav_menu_item', 10, 4 ); + +/** + * Shim that prevents menu items with type `'block'` from being rendered in the + * frontend when the theme does not support block menus. + * + * Specifically, this shim makes it so that `wp_nav_menu()` will remove any menu + * items that have a `type` of `'block'` from `$sorted_menu_items`. When merged + * to Core, this functionality should exist in `wp_nav_menu()`. + * + * This shim can be removed when the Gutenberg plugin requires a WordPress + * version that has the ticket below. + * + * @see https://core.trac.wordpress.org/ticket/50544 + * + * @param array $menu_items The menu items, sorted by each menu item's menu order. + * @param stdClass $args An object containing wp_nav_menu() arguments. + */ +function gutenberg_remove_block_nav_menu_items( $menu_items, $args ) { + if ( current_theme_supports( 'block-nav-menus' ) ) { + return $menu_items; + } + + return array_filter( + $menu_items, + function( $menu_item ) { + return 'block' !== $menu_item->type; + } + ); +} +add_filter( 'wp_nav_menu_objects', 'gutenberg_remove_block_nav_menu_items', 10, 2 ); + +/** + * Recursively converts a list of menu items into a list of blocks. This is a + * helper function used by `gutenberg_output_block_nav_menu()`. + * + * Transformation depends on the menu item type. Link menu items are turned into + * a `core/navigation-link` block. Block menu items are simply parsed. + * + * @param array $menu_items The menu items to convert, sorted by each menu item's menu order. + * @param array $menu_items_by_parent_id All menu items, indexed by their parent's ID. + * @return array + */ +function gutenberg_convert_menu_items_to_blocks( + $menu_items, + &$menu_items_by_parent_id +) { + if ( empty( $menu_items ) ) { + return array(); + } + + $blocks = array(); + + foreach ( $menu_items as $menu_item ) { + if ( 'block' === $menu_item->type ) { + $parsed_blocks = parse_blocks( $menu_item->content ); + + if ( count( $parsed_blocks ) ) { + $block = $parsed_blocks[0]; + } else { + $block = array( + 'blockName' => 'core/freeform', + 'attrs' => array( + 'originalContent' => $menu_item->content, + ), + ); + } + } else { + $block = array( + 'blockName' => 'core/navigation-link', + 'attrs' => array( + 'label' => $menu_item->title, + 'url' => $menu_item->url, + ), + ); + } + + $block['innerBlocks'] = gutenberg_convert_menu_items_to_blocks( + isset( $menu_items_by_parent_id[ $menu_item->ID ] ) + ? $menu_items_by_parent_id[ $menu_item->ID ] + : array(), + $menu_items_by_parent_id + ); + + $blocks[] = $block; + } + + return $blocks; +}; + +/** + * Shim that causes `wp_nav_menu()` to output a Navigation block instead of a + * nav menu when the theme supports block menus. The Navigation block is + * constructed by transforming the stored tree of menu items into a tree of + * blocks. + * + * Specifically, this shim makes it so that `wp_nav_menu()` returns early when + * the theme supports block menus. When merged to Core, this functionality + * should exist in `wp_nav_menu()` after `$sorted_menu_items` is set. The + * duplicated code (marked using BEGIN and END) can be deleted. + * + * This shim can be removed when the Gutenberg plugin requires a WordPress + * version that has the ticket below. + * + * @see https://core.trac.wordpress.org/ticket/50544 + * + * @param string|null $output Nav menu output to short-circuit with. Default null. + * @param stdClass $args An object containing wp_nav_menu() arguments. + */ +function gutenberg_output_block_nav_menu( $output, $args ) { + if ( ! current_theme_supports( 'block-nav-menus' ) ) { + return null; + } + + // BEGIN: Code that already exists in wp_nav_menu(). + + // Get the nav menu based on the requested menu. + $menu = wp_get_nav_menu_object( $args->menu ); + + // Get the nav menu based on the theme_location. + $locations = get_nav_menu_locations(); + if ( ! $menu && $args->theme_location && $locations && isset( $locations[ $args->theme_location ] ) ) { + $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); + } + + // Get the first menu that has items if we still can't find a menu. + if ( ! $menu && ! $args->theme_location ) { + $menus = wp_get_nav_menus(); + foreach ( $menus as $menu_maybe ) { + $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ); + if ( $menu_items ) { + $menu = $menu_maybe; + break; + } + } + } + + if ( empty( $args->menu ) ) { + $args->menu = $menu; + } + + // If the menu exists, get its items. + if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) ) { + $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); + } + + // Set up the $menu_item variables. + _wp_menu_item_classes_by_context( $menu_items ); + + $sorted_menu_items = array(); + foreach ( (array) $menu_items as $menu_item ) { + $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; + } + + unset( $menu_items, $menu_item ); + + // END: Code that already exists in wp_nav_menu(). + + $menu_items_by_parent_id = array(); + foreach ( $sorted_menu_items as $menu_item ) { + $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item; + } + + $navigation_block = array( + 'blockName' => 'core/navigation', + 'attrs' => array(), + 'innerBlocks' => gutenberg_convert_menu_items_to_blocks( + isset( $menu_items_by_parent_id[0] ) + ? $menu_items_by_parent_id[0] + : array(), + $menu_items_by_parent_id + ), + ); + + return render_block( $navigation_block ); +} +add_filter( 'pre_wp_nav_menu', 'gutenberg_output_block_nav_menu', 10, 2 ); + +/** + * Shim that makes nav-menus.php nicely display a menu item with its `type` set to + * `'block'`. + * + * Specifically, this shim makes it so that `Walker_Nav_Menu_Edit::start_el()` + * outputs extra form fields. When merged to Core, this markup should exist in + * `Walker_Nav_Menu_Edit::start_el()`. + * + * This shim can be removed when the Gutenberg plugin requires a WordPress + * version that has the ticket below. + * + * @see https://core.trac.wordpress.org/ticket/50544 + * + * @param int $item_id Menu item ID. + * @param WP_Post $item Menu item data object. + */ +function gutenberg_output_block_menu_item_custom_fields( $item_id, $item ) { + if ( 'block' === $item->type ) { + ?> +

+ +

+ } List of suggestions */ -const fetchLinkSuggestions = async ( search, { perPage = 20 } = {} ) => { +async function fetchLinkSuggestions( search, { perPage = 20 } = {} ) { const posts = await apiFetch( { path: addQueryArgs( '/wp/v2/search', { search, @@ -50,14 +58,25 @@ const fetchLinkSuggestions = async ( search, { perPage = 20 } = {} ) => { title: decodeEntities( post.title ) || __( '(no title)' ), type: post.subtype || post.type, } ) ); -}; +} export function initialize( id, settings ) { + if ( ! settings.blockNavMenus ) { + addFilter( + 'blocks.registerBlockType', + 'core/edit-navigation/disable-inserting-non-navigation-blocks', + disableInsertingNonNavigationBlocks + ); + } + registerCoreBlocks(); + if ( process.env.GUTENBERG_PHASE === 2 ) { __experimentalRegisterExperimentalCoreBlocks( settings ); } + settings.__experimentalFetchLinkSuggestions = fetchLinkSuggestions; + render( , document.getElementById( id ) diff --git a/packages/edit-navigation/src/store/actions.js b/packages/edit-navigation/src/store/actions.js index 4f8d49fc66eecb..beb7c76cc2f7c4 100644 --- a/packages/edit-navigation/src/store/actions.js +++ b/packages/edit-navigation/src/store/actions.js @@ -210,7 +210,7 @@ function computeCustomizedAttribute( blocks, menuId, menuItemsByClientId ) { }; } else { attributes = { - type: 'html', + type: 'block', content: serialize( block ), }; } diff --git a/packages/edit-navigation/src/store/resolvers.js b/packages/edit-navigation/src/store/resolvers.js index 50b9aa411c3ad2..ab346ac089365e 100644 --- a/packages/edit-navigation/src/store/resolvers.js +++ b/packages/edit-navigation/src/store/resolvers.js @@ -113,16 +113,16 @@ function createNavigationBlock( menuItems ) { } function convertMenuItemToBlock( menuItem, innerBlocks = [] ) { - if ( menuItem.type === 'html' ) { - const parsedBlocks = parse( menuItem.content.raw ); + if ( menuItem.type === 'block' ) { + const [ block ] = parse( menuItem.content.raw ); - if ( parsedBlocks.length !== 1 ) { + if ( ! block ) { return createBlock( 'core/freeform', { originalContent: menuItem.content.raw, } ); } - return parsedBlocks[ 0 ]; + return createBlock( block.name, block.attributes, innerBlocks ); } const attributes = { From bb572169c679a63443cb56fcd6c907858f7fa77b Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Thu, 13 Aug 2020 17:13:01 +1000 Subject: [PATCH 2/8] Navigation: Remove unused argument --- lib/navigation.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index e3d930d5ce39dc..f1a133b3f729f2 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -130,10 +130,9 @@ function gutenberg_output_block_nav_menu_item( $item_output, $item, $depth, $arg * * @see https://core.trac.wordpress.org/ticket/50544 * - * @param array $menu_items The menu items, sorted by each menu item's menu order. - * @param stdClass $args An object containing wp_nav_menu() arguments. + * @param array $menu_items The menu items, sorted by each menu item's menu order. */ -function gutenberg_remove_block_nav_menu_items( $menu_items, $args ) { +function gutenberg_remove_block_nav_menu_items( $menu_items ) { if ( current_theme_supports( 'block-nav-menus' ) ) { return $menu_items; } @@ -145,7 +144,7 @@ function( $menu_item ) { } ); } -add_filter( 'wp_nav_menu_objects', 'gutenberg_remove_block_nav_menu_items', 10, 2 ); +add_filter( 'wp_nav_menu_objects', 'gutenberg_remove_block_nav_menu_items', 10 ); /** * Recursively converts a list of menu items into a list of blocks. This is a From c955c6c4f913fe92fb8d8c768db050960a48e165 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Fri, 14 Aug 2020 12:03:42 +1000 Subject: [PATCH 3/8] Navigation: Fix phpunit tests --- ...class-rest-nav-menu-items-controller-test.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/phpunit/class-rest-nav-menu-items-controller-test.php b/phpunit/class-rest-nav-menu-items-controller-test.php index 233114d5a074fe..dcc8c5ef4a3fa5 100644 --- a/phpunit/class-rest-nav-menu-items-controller-test.php +++ b/phpunit/class-rest-nav-menu-items-controller-test.php @@ -451,16 +451,16 @@ public function test_create_item_invalid_custom_link_url() { } /** - * Tests that a HTML menu item can be created. + * Tests that a block menu item can be created. */ - public function test_create_item_html() { + public function test_create_item_block() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/__experimental/menu-items' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $params = $this->set_menu_item_data( array( - 'type' => 'html', - 'content' => '

HTML content

', + 'type' => 'block', + 'content' => '

Block content

', ) ); $request->set_body_params( $params ); @@ -469,15 +469,15 @@ public function test_create_item_html() { } /** - * Tests that a HTML menu item can be created. + * Tests that a block menu item can be created. */ - public function test_create_item_invalid_html_content() { + public function test_create_item_invalid_block_content() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/__experimental/menu-items' ); $request->add_header( 'content-type', 'application/x-www-form-urlencoded' ); $params = $this->set_menu_item_data( array( - 'type' => 'html', + 'type' => 'block', ) ); $request->set_body_params( $params ); @@ -735,7 +735,7 @@ protected function check_menu_item_data( $post, $data, $context, $links ) { } // Check content. - if ( 'html' === $data['type'] ) { + if ( 'block' === $data['type'] ) { $menu_item_content = get_post_meta( $post->ID, '_menu_item_content', true ); $this->assertEquals( apply_filters( 'the_content', $menu_item_content ), $data['content']['rendered'] ); if ( 'edit' === $context ) { From 50d192fff20700b5e2e43fc981a2e989928743c9 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 18 Aug 2020 11:36:43 +1000 Subject: [PATCH 4/8] Shorten 'Gutenberg Block' label to 'Block' --- lib/class-wp-widget-block.php | 2 +- lib/navigation.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-widget-block.php b/lib/class-wp-widget-block.php index 3d0d2342215850..a9eb407abddc4c 100644 --- a/lib/class-wp-widget-block.php +++ b/lib/class-wp-widget-block.php @@ -37,7 +37,7 @@ public function __construct() { 'width' => 400, 'height' => 350, ); - parent::__construct( 'block', __( 'Gutenberg Block', 'gutenberg' ), $widget_ops, $control_ops ); + parent::__construct( 'block', __( 'Block', 'gutenberg' ), $widget_ops, $control_ops ); } /** diff --git a/lib/navigation.php b/lib/navigation.php index f1a133b3f729f2..5750fe2d2f73df 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -73,12 +73,12 @@ function gutenberg_update_nav_menu_item_content( $menu_id, $menu_item_db_id, $ar */ function gutenberg_setup_block_nav_menu_item( $menu_item ) { if ( 'block' === $menu_item->type ) { - $menu_item->type_label = __( 'Gutenberg Block', 'gutenberg' ); + $menu_item->type_label = __( 'Block', 'gutenberg' ); $menu_item->content = ! isset( $menu_item->content ) ? get_post_meta( $menu_item->db_id, '_menu_item_content', true ) : $menu_item->content; // Set to make the menu item display nicely in nav-menus.php. $menu_item->object = 'block'; - $menu_item->title = __( 'Gutenberg Block', 'gutenberg' ); + $menu_item->title = __( 'Block', 'gutenberg' ); } return $menu_item; From 38da717124d14baf03f714b1674e5d3c8ed07547 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 18 Aug 2020 11:48:55 +1000 Subject: [PATCH 5/8] Navigation: Add @return inline documentation --- lib/navigation.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 5750fe2d2f73df..6248e4bbc64515 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -70,6 +70,8 @@ function gutenberg_update_nav_menu_item_content( $menu_id, $menu_item_db_id, $ar * @see https://core.trac.wordpress.org/ticket/50544 * * @param object $menu_item The menu item object. + * + * @return object Updated menu item object. */ function gutenberg_setup_block_nav_menu_item( $menu_item ) { if ( 'block' === $menu_item->type ) { @@ -104,6 +106,8 @@ function gutenberg_setup_block_nav_menu_item( $menu_item ) { * @param WP_Post $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param stdClass $args An object of wp_nav_menu() arguments. + * + * @return string The menu item's updated HTML output. */ function gutenberg_output_block_nav_menu_item( $item_output, $item, $depth, $args ) { if ( 'block' === $item->type ) { @@ -131,6 +135,8 @@ function gutenberg_output_block_nav_menu_item( $item_output, $item, $depth, $arg * @see https://core.trac.wordpress.org/ticket/50544 * * @param array $menu_items The menu items, sorted by each menu item's menu order. + * + * @return array Updated menu items, sorted by each menu item's menu order. */ function gutenberg_remove_block_nav_menu_items( $menu_items ) { if ( current_theme_supports( 'block-nav-menus' ) ) { @@ -155,7 +161,8 @@ function( $menu_item ) { * * @param array $menu_items The menu items to convert, sorted by each menu item's menu order. * @param array $menu_items_by_parent_id All menu items, indexed by their parent's ID. - * @return array + + * @return array Updated menu items, sorted by each menu item's menu order. */ function gutenberg_convert_menu_items_to_blocks( $menu_items, @@ -222,6 +229,8 @@ function gutenberg_convert_menu_items_to_blocks( * * @param string|null $output Nav menu output to short-circuit with. Default null. * @param stdClass $args An object containing wp_nav_menu() arguments. + * + * @return string|null Nav menu output to short-circuit with. */ function gutenberg_output_block_nav_menu( $output, $args ) { if ( ! current_theme_supports( 'block-nav-menus' ) ) { From c6b0a8813b462f189f08615cc2b6d48d3cdd4fbc Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 18 Aug 2020 11:57:44 +1000 Subject: [PATCH 6/8] Navigation: Add label to content textarea --- lib/navigation.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/navigation.php b/lib/navigation.php index 6248e4bbc64515..7b4cedbf732896 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -321,7 +321,10 @@ function gutenberg_output_block_menu_item_custom_fields( $item_id, $item ) { if ( 'block' === $item->type ) { ?>

- +

Date: Tue, 18 Aug 2020 12:04:41 +1000 Subject: [PATCH 7/8] Navigation: Hide all non-content fields from Block menu items --- lib/navigation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/navigation.php b/lib/navigation.php index 7b4cedbf732896..87edbf9c50ae88 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -320,7 +320,7 @@ function gutenberg_output_block_nav_menu( $output, $args ) { function gutenberg_output_block_menu_item_custom_fields( $item_id, $item ) { if ( 'block' === $item->type ) { ?> -

+