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/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/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 f93b7eec81732f..cf62c6ec05008a 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..1688f8746668d9 --- /dev/null +++ b/lib/navigation.php @@ -0,0 +1,380 @@ +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']; + } + } + } + + // Everything else belongs in `wp_update_nav_menu_item()`. + + $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. + * + * @return object Updated menu item object. + */ +function gutenberg_setup_block_nav_menu_item( $menu_item ) { + if ( 'block' === $menu_item->type ) { + $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 = __( '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. + * + * @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 ) { + $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. + * + * @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' ) ) { + 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 ); + +/** + * 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 Updated menu items, sorted by each menu item's menu order. + */ +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. + * + * @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' ) ) { + 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 = { 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 ) {