From 2a2b51810ba7d15d813a03e14678f542fd435386 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Wed, 17 Feb 2021 14:54:05 -0800 Subject: [PATCH 01/12] Navigation Link: populate variation types like post link, page link, category link in server from register_block_type_from_metadata instead of hardcoded file. --- .../src/navigation-link/block.json | 3 + .../block-library/src/navigation-link/edit.js | 15 ++++- .../src/navigation-link/hooks.js | 49 +++++++++++++++ .../src/navigation-link/index.js | 15 +++-- .../src/navigation-link/index.php | 40 +++++++++++++ .../src/navigation-link/variations.js | 60 ------------------- 6 files changed, 116 insertions(+), 66 deletions(-) create mode 100644 packages/block-library/src/navigation-link/hooks.js delete mode 100644 packages/block-library/src/navigation-link/variations.js diff --git a/packages/block-library/src/navigation-link/block.json b/packages/block-library/src/navigation-link/block.json index 17ea418528737a..dc31e79bf798a5 100644 --- a/packages/block-library/src/navigation-link/block.json +++ b/packages/block-library/src/navigation-link/block.json @@ -28,6 +28,9 @@ }, "title": { "type": "string" + }, + "kind": { + "type": "string" } }, "usesContext": [ diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index 7b1892b67e2322..89a2337620a8c5 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -103,9 +103,10 @@ const useIsDraggingWithin = ( elementRef ) => { * /wp/v2/search. * * @param {string} type Link block's type attribute. + * @param {string} kind Link block's entity of kind (post-type|taxonomy) * @return {{ type?: string, subtype?: string }} Search query params. */ -function getSuggestionsQuery( type ) { +function getSuggestionsQuery( type, kind ) { switch ( type ) { case 'post': case 'page': @@ -115,6 +116,12 @@ function getSuggestionsQuery( type ) { case 'tag': return { type: 'term', subtype: 'post_tag' }; default: + if ( kind === 'taxonomy' ) { + return { type: 'term', subtype: type }; + } + if ( kind === 'post-type' ) { + return { type: 'post', subtype: type }; + } return {}; } } @@ -137,6 +144,7 @@ export default function NavigationLinkEdit( { description, rel, title, + kind, } = attributes; const link = { url, @@ -501,7 +509,10 @@ export default function NavigationLinkEdit( { } } noDirectEntry={ !! type } noURLSuggestion={ !! type } - suggestionsQuery={ getSuggestionsQuery( type ) } + suggestionsQuery={ getSuggestionsQuery( + type, + kind + ) } onChange={ ( { title: newTitle = '', url: newURL = '', diff --git a/packages/block-library/src/navigation-link/hooks.js b/packages/block-library/src/navigation-link/hooks.js new file mode 100644 index 00000000000000..96e60b1ed97e03 --- /dev/null +++ b/packages/block-library/src/navigation-link/hooks.js @@ -0,0 +1,49 @@ +/** + * WordPress dependencies + */ +import { addFilter } from '@wordpress/hooks'; +import { category, page, postTitle, tag } from '@wordpress/icons'; + +const ICON_MAP = { + category, + page, + post: postTitle, + tag, + //TODO: add a generic CPT icon, remember to ask folks for a new icon +}; + +function enhanceNavigationLinkVariations( settings, name ) { + if ( name !== 'core/navigation-link' ) { + return settings; + } + + if ( settings?.variations ) { + const variations = settings.variations.map( ( variation ) => { + return { + ...variation, + ...( ! variation.icon && + ICON_MAP[ variation.name ] && { + icon: ICON_MAP[ variation.name ], + } ), + ...( ! variation.isActive && { + isActive: ( blockAttributes, variationAttributes ) => { + return ( + blockAttributes.type === variationAttributes.type + ); + }, + } ), + }; + } ); + return { + ...settings, + variations, + }; + } + return settings; +} + +addFilter( + 'blocks.registerBlockType', + 'core/navigation-link', + enhanceNavigationLinkVariations +); diff --git a/packages/block-library/src/navigation-link/index.js b/packages/block-library/src/navigation-link/index.js index cadfa271abd1f0..f2ce4c670bed70 100644 --- a/packages/block-library/src/navigation-link/index.js +++ b/packages/block-library/src/navigation-link/index.js @@ -11,11 +11,13 @@ import { InnerBlocks } from '@wordpress/block-editor'; import metadata from './block.json'; import edit from './edit'; import save from './save'; -import variations from './variations'; +import './hooks'; const { name } = metadata; -export { metadata, name }; +// If we export metadata, this calls unstable__bootstrapServerSideBlockDefinitions a second time, and overwrites +// what was injected on the page +export { name }; export const settings = { title: _x( 'Link', 'block title' ), @@ -24,8 +26,6 @@ export const settings = { description: __( 'Add a page, link, or another item to your navigation.' ), - variations, - __experimentalLabel: ( { label } ) => label, merge( leftAttributes, { label: rightLabel = '' } ) { @@ -39,6 +39,13 @@ export const settings = { save, + example: { + attributes: { + label: _x( 'Example Link', 'navigation link preview example' ), + url: 'https://example.com', + }, + }, + deprecated: [ { isEligible( attributes ) { diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 45e63478292616..78161ee61f999f 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -226,6 +226,30 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { return $html; } +/** + * Returns a navigation link variation + * + * @param $entity WP_Taxonomy|WP_Post_Type post type or taxonomy entity. + * @param $kind string string of value 'taxonomy' or 'post-type' + * @param $name string entity name eg: post, page, post_tag, category + * + * @return array + */ +function build_variation_for_navigation_link( $entity, $kind ) { + $name = 'post_tag' === $entity->name ? 'tag' : $entity->name; + return array( + 'name' => $name, + /* translators: %s: Entity type, eg: Post Link, Page Link, Category Link, Tag Link, Portfolio Link etc */ + 'title' => sprintf( __( '%s Link' ), $entity->labels->singular_name ), + /* translators: %s: Entity type, eg: A link to a post. A link to a page. */ + 'description' => sprintf( __( 'A link to a %s.' ), $entity->labels->singular_name ), + 'attributes' => array( + 'type' => $name, + 'kind' => $kind, + ), + ); +} + /** * Register the navigation link block. * @@ -233,10 +257,26 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { * @throws WP_Error An WP_Error exception parsing the block definition. */ function register_block_core_navigation_link() { + + $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); + $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); + $variations = array(); + if ( $post_types ) { + foreach ( $post_types as $post_type ) { + $variations[] = build_variation_for_navigation_link( $post_type, 'post-type' ); + } + } + if ( $taxonomies ) { + foreach ( $taxonomies as $taxonomy ) { + $variations[] = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); + } + } + register_block_type_from_metadata( __DIR__ . '/navigation-link', array( 'render_callback' => 'render_block_core_navigation_link', + 'variations' => $variations, ) ); } diff --git a/packages/block-library/src/navigation-link/variations.js b/packages/block-library/src/navigation-link/variations.js deleted file mode 100644 index 56dbf576a9fced..00000000000000 --- a/packages/block-library/src/navigation-link/variations.js +++ /dev/null @@ -1,60 +0,0 @@ -/** - * WordPress dependencies - */ -import { __ } from '@wordpress/i18n'; -import { - category as categoryIcon, - page as pageIcon, - postTitle as postIcon, - tag as tagIcon, -} from '@wordpress/icons'; -const variations = [ - { - name: 'link', - isDefault: true, - title: __( 'Link' ), - description: __( 'A link to a URL.' ), - attributes: {}, - }, - { - name: 'post', - icon: postIcon, - title: __( 'Post Link' ), - description: __( 'A link to a post.' ), - attributes: { type: 'post' }, - }, - { - name: 'page', - icon: pageIcon, - title: __( 'Page Link' ), - description: __( 'A link to a page.' ), - attributes: { type: 'page' }, - }, - { - name: 'category', - icon: categoryIcon, - title: __( 'Category Link' ), - description: __( 'A link to a category.' ), - attributes: { type: 'category' }, - }, - { - name: 'tag', - icon: tagIcon, - title: __( 'Tag Link' ), - description: __( 'A link to a tag.' ), - attributes: { type: 'tag' }, - }, -]; - -/** - * Add `isActive` function to all `navigation link` variations, if not defined. - * `isActive` function is used to find a variation match from a created - * Block by providing its attributes. - */ -variations.forEach( ( variation ) => { - if ( variation.isActive ) return; - variation.isActive = ( blockAttributes, variationAttributes ) => - blockAttributes.type === variationAttributes.type; -} ); - -export default variations; From fb39f6bc59a30fd9387bf0195d3380d19b953e4f Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Tue, 23 Feb 2021 10:02:42 -0800 Subject: [PATCH 02/12] favor changes in #29213 --- packages/block-library/src/navigation-link/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.js b/packages/block-library/src/navigation-link/index.js index f2ce4c670bed70..2b10eb72277c65 100644 --- a/packages/block-library/src/navigation-link/index.js +++ b/packages/block-library/src/navigation-link/index.js @@ -15,9 +15,7 @@ import './hooks'; const { name } = metadata; -// If we export metadata, this calls unstable__bootstrapServerSideBlockDefinitions a second time, and overwrites -// what was injected on the page -export { name }; +export { name, metadata }; export const settings = { title: _x( 'Link', 'block title' ), From 6e1a3fc38558d71eaae8920ab2260a8b51361a9a Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Fri, 19 Feb 2021 14:45:33 -0800 Subject: [PATCH 03/12] tidy up phpcs lint errors --- packages/block-library/src/navigation-link/index.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 78161ee61f999f..ad41055ef6d728 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -229,9 +229,8 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { /** * Returns a navigation link variation * - * @param $entity WP_Taxonomy|WP_Post_Type post type or taxonomy entity. - * @param $kind string string of value 'taxonomy' or 'post-type' - * @param $name string entity name eg: post, page, post_tag, category + * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity. + * @param string $kind string of value 'taxonomy' or 'post-type'. * * @return array */ From 9925d700388662056e99288b19d6da844c53687b Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 22 Feb 2021 08:53:42 -0800 Subject: [PATCH 04/12] Add custom post type icon, courtesy of @jasmussen --- .../src/navigation-link/hooks.js | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/block-library/src/navigation-link/hooks.js b/packages/block-library/src/navigation-link/hooks.js index 96e60b1ed97e03..3198ade0587795 100644 --- a/packages/block-library/src/navigation-link/hooks.js +++ b/packages/block-library/src/navigation-link/hooks.js @@ -2,15 +2,28 @@ * WordPress dependencies */ import { addFilter } from '@wordpress/hooks'; -import { category, page, postTitle, tag } from '@wordpress/icons'; - -const ICON_MAP = { +import { category, page, - post: postTitle, + postTitle, tag, - //TODO: add a generic CPT icon, remember to ask folks for a new icon -}; + customPostType, +} from '@wordpress/icons'; + +function getIcon( variationName ) { + switch ( variationName ) { + case 'post': + return postTitle; + case 'page': + return page; + case 'tag': + return tag; + case 'category': + return category; + default: + return customPostType; + } +} function enhanceNavigationLinkVariations( settings, name ) { if ( name !== 'core/navigation-link' ) { @@ -21,10 +34,9 @@ function enhanceNavigationLinkVariations( settings, name ) { const variations = settings.variations.map( ( variation ) => { return { ...variation, - ...( ! variation.icon && - ICON_MAP[ variation.name ] && { - icon: ICON_MAP[ variation.name ], - } ), + ...( ! variation.icon && { + icon: getIcon( variation.name ), + } ), ...( ! variation.isActive && { isActive: ( blockAttributes, variationAttributes ) => { return ( From ad227f6a00b7239bd92eb26d77ce280377d226d7 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Wed, 24 Feb 2021 13:44:18 -0800 Subject: [PATCH 05/12] Use item_link and item_link_description labels if available, use fallback otherwise --- .../src/navigation-link/index.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index ad41055ef6d728..c82af14cc275c5 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -236,12 +236,23 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { */ function build_variation_for_navigation_link( $entity, $kind ) { $name = 'post_tag' === $entity->name ? 'tag' : $entity->name; + + /* translators: %s: Entity type, eg: Post Link, Page Link, Category Link, Tag Link, Portfolio Link etc */ + $fallback_title = sprintf( __( '%s Link' ), $entity->labels->singular_name ); + /* translators: %s: Entity type, eg: A link to a post. A link to a page. */ + $fallback_description = sprintf( __( 'A link to a %s.' ), $entity->labels->singular_name ); + + if ( property_exists( $entity->labels, 'item_link' ) ) { + $title = $entity->labels->item_link; + } + if ( property_exists( $entity->labels, 'item_link_description' ) ) { + $description = $entity->labels->item_link_description; + } + return array( 'name' => $name, - /* translators: %s: Entity type, eg: Post Link, Page Link, Category Link, Tag Link, Portfolio Link etc */ - 'title' => sprintf( __( '%s Link' ), $entity->labels->singular_name ), - /* translators: %s: Entity type, eg: A link to a post. A link to a page. */ - 'description' => sprintf( __( 'A link to a %s.' ), $entity->labels->singular_name ), + 'title' => $title ? $title : $fallback_title, + 'description' => $description ? $description : $fallback_description, 'attributes' => array( 'type' => $name, 'kind' => $kind, From 6533ea2cf175a9b825d84515b3f02b272ad8ef35 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Thu, 25 Feb 2021 10:56:39 -0800 Subject: [PATCH 06/12] guard against $title and $description being undefined --- packages/block-library/src/navigation-link/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index c82af14cc275c5..6fec7e7befde24 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -251,8 +251,8 @@ function build_variation_for_navigation_link( $entity, $kind ) { return array( 'name' => $name, - 'title' => $title ? $title : $fallback_title, - 'description' => $description ? $description : $fallback_description, + 'title' => isset( $title ) ? $title : $fallback_title, + 'description' => isset( $description ) ? $description : $fallback_description, 'attributes' => array( 'type' => $name, 'kind' => $kind, From 6b07ad6271c6ec3f723b2473660806f2dca01d30 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Fri, 26 Feb 2021 08:31:31 -0800 Subject: [PATCH 07/12] Use hardcoded fallback when server does not understand variations in register_block_type_from_metadata --- .../navigation-link/fallback-variations.js | 65 +++++++++++++++++++ .../src/navigation-link/hooks.js | 17 ++++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 packages/block-library/src/navigation-link/fallback-variations.js diff --git a/packages/block-library/src/navigation-link/fallback-variations.js b/packages/block-library/src/navigation-link/fallback-variations.js new file mode 100644 index 00000000000000..b6074f10e4e38a --- /dev/null +++ b/packages/block-library/src/navigation-link/fallback-variations.js @@ -0,0 +1,65 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { + category as categoryIcon, + page as pageIcon, + postTitle as postIcon, + tag as tagIcon, +} from '@wordpress/icons'; + +// FALLBACK: this is only used when the server does not understand the variations property in the +// register_block_type_from_metadata call. see navigation-link/index.php. +// Delete this file when supported WP ranges understand the `variations` property when passed to +// register_block_type_from_metadata in index.php +const fallbackVariations = [ + { + name: 'link', + isDefault: true, + title: __( 'Link' ), + description: __( 'A link to a URL.' ), + attributes: {}, + }, + { + name: 'post', + icon: postIcon, + title: __( 'Post Link' ), + description: __( 'A link to a post.' ), + attributes: { type: 'post' }, + }, + { + name: 'page', + icon: pageIcon, + title: __( 'Page Link' ), + description: __( 'A link to a page.' ), + attributes: { type: 'page' }, + }, + { + name: 'category', + icon: categoryIcon, + title: __( 'Category Link' ), + description: __( 'A link to a category.' ), + attributes: { type: 'category' }, + }, + { + name: 'tag', + icon: tagIcon, + title: __( 'Tag Link' ), + description: __( 'A link to a tag.' ), + attributes: { type: 'tag' }, + }, +]; + +/** + * Add `isActive` function to all `navigation link` variations, if not defined. + * `isActive` function is used to find a variation match from a created + * Block by providing its attributes. + */ +fallbackVariations.forEach( ( variation ) => { + if ( variation.isActive ) return; + variation.isActive = ( blockAttributes, variationAttributes ) => + blockAttributes.type === variationAttributes.type; +} ); + +export default fallbackVariations; diff --git a/packages/block-library/src/navigation-link/hooks.js b/packages/block-library/src/navigation-link/hooks.js index 3198ade0587795..c2edd698f2d809 100644 --- a/packages/block-library/src/navigation-link/hooks.js +++ b/packages/block-library/src/navigation-link/hooks.js @@ -10,6 +10,11 @@ import { customPostType, } from '@wordpress/icons'; +/** + * Internal dependencies + */ +import fallbackVariations from './fallback-variations'; + function getIcon( variationName ) { switch ( variationName ) { case 'post': @@ -30,7 +35,17 @@ function enhanceNavigationLinkVariations( settings, name ) { return settings; } - if ( settings?.variations ) { + // Fallback handling may be deleted after supported WP ranges understand the `variations` + // property when passed to register_block_type_from_metadata in index.php + if ( ! settings.variations ) { + return { + ...settings, + variations: [ ...fallbackVariations ], + }; + } + + // Otherwise decorate server passed variations with an icon and isActive function + if ( settings.variations ) { const variations = settings.variations.map( ( variation ) => { return { ...variation, From 2a65e2e26002e099bf290f546e1933a537127681 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Fri, 26 Feb 2021 08:38:47 -0800 Subject: [PATCH 08/12] avoid import order change --- packages/block-library/src/navigation-link/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation-link/index.js b/packages/block-library/src/navigation-link/index.js index 2b10eb72277c65..95de1e963db52f 100644 --- a/packages/block-library/src/navigation-link/index.js +++ b/packages/block-library/src/navigation-link/index.js @@ -15,7 +15,7 @@ import './hooks'; const { name } = metadata; -export { name, metadata }; +export { metadata, name }; export const settings = { title: _x( 'Link', 'block title' ), From 9191e5e65e3795cd3ca7a0f41c83699a8e50f373 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Fri, 26 Feb 2021 10:14:30 -0800 Subject: [PATCH 09/12] do not render custom post type links if post is draft --- .../src/navigation-link/index.php | 11 ++- ...ass-block-library-navigation-link-test.php | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 6fec7e7befde24..9289bc8240843c 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -104,13 +104,12 @@ function block_core_navigation_link_render_submenu_icon() { * @return string Returns the post content with the legacy widget added. */ function render_block_core_navigation_link( $attributes, $content, $block ) { + $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] ); + $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind']; + $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] ); + // Don't render the block's subtree if it is a draft. - if ( - isset( $attributes['id'] ) && - is_numeric( $attributes['id'] ) && - isset( $attributes['type'] ) && - ( 'post' === $attributes['type'] || 'page' === $attributes['type'] ) - ) { + if ( $is_post_type && $navigation_link_has_id ) { $post = get_post( $attributes['id'] ); if ( 'publish' !== $post->post_status ) { return ''; diff --git a/phpunit/class-block-library-navigation-link-test.php b/phpunit/class-block-library-navigation-link-test.php index 84a1809076573c..df6335d29d3d58 100644 --- a/phpunit/class-block-library-navigation-link-test.php +++ b/phpunit/class-block-library-navigation-link-test.php @@ -13,6 +13,8 @@ class Block_Library_Navigation_Link_Test extends WP_UnitTestCase { private static $category; private static $page; private static $draft; + private static $custom_draft; + private static $custom_post; private static $pages; private static $terms; @@ -31,6 +33,30 @@ public static function wpSetUpBeforeClass() { ); self::$pages[] = self::$draft; + self::$custom_draft = self::factory()->post->create_and_get( + array( + 'post_type' => 'cats', + 'post_status' => 'draft', + 'post_name' => 'metalcat', + 'post_title' => 'Metal Cat', + 'post_content' => 'Metal Cat content', + 'post_excerpt' => 'Metal Cat', + ) + ); + self::$pages[] = self::$custom_draft; + + self::$custom_post = self::factory()->post->create_and_get( + array( + 'post_type' => 'dogs', + 'post_status' => 'publish', + 'post_name' => 'metaldog', + 'post_title' => 'Metal Dog', + 'post_content' => 'Metal Dog content', + 'post_excerpt' => 'Metal Dog', + ) + ); + self::$pages[] = self::$custom_post; + self::$page = self::factory()->post->create_and_get( array( 'post_type' => 'page', @@ -166,4 +192,46 @@ function test_returns_link_for_plain_link() { ) !== false ); } + + function test_returns_empty_when_custom_post_type_draft() { + $page_id = self::$custom_draft->ID; + + $parsed_blocks = parse_blocks( + "" + ); + $this->assertEquals( 1, count( $parsed_blocks ) ); + + $navigation_link_block = new WP_Block( $parsed_blocks[0], array() ); + + $this->assertEquals( + '', + gutenberg_render_block_core_navigation_link( + $navigation_link_block->attributes, + array(), + $navigation_link_block + ) + ); + } + + function test_returns_link_when_custom_post_is_published() { + $page_id = self::$custom_post->ID; + + $parsed_blocks = parse_blocks( + "" + ); + $this->assertEquals( 1, count( $parsed_blocks ) ); + + $navigation_link_block = new WP_Block( $parsed_blocks[0], array() ); + $this->assertEquals( + true, + strpos( + gutenberg_render_block_core_navigation_link( + $navigation_link_block->attributes, + array(), + $navigation_link_block + ), + 'Metal Dogs' + ) !== false + ); + } } From 7123e64975b77836e6f43b179bb00201782b0d14 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 1 Mar 2021 09:40:21 -0800 Subject: [PATCH 10/12] remove fallback label detection --- packages/block-library/src/navigation-link/index.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 9289bc8240843c..ba83435c788158 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -236,10 +236,8 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { function build_variation_for_navigation_link( $entity, $kind ) { $name = 'post_tag' === $entity->name ? 'tag' : $entity->name; - /* translators: %s: Entity type, eg: Post Link, Page Link, Category Link, Tag Link, Portfolio Link etc */ - $fallback_title = sprintf( __( '%s Link' ), $entity->labels->singular_name ); - /* translators: %s: Entity type, eg: A link to a post. A link to a page. */ - $fallback_description = sprintf( __( 'A link to a %s.' ), $entity->labels->singular_name ); + $title = ''; + $description = ''; if ( property_exists( $entity->labels, 'item_link' ) ) { $title = $entity->labels->item_link; @@ -250,8 +248,8 @@ function build_variation_for_navigation_link( $entity, $kind ) { return array( 'name' => $name, - 'title' => isset( $title ) ? $title : $fallback_title, - 'description' => isset( $description ) ? $description : $fallback_description, + 'title' => $title, + 'description' => $description, 'attributes' => array( 'type' => $name, 'kind' => $kind, From 5247f5666806f7f393ae9619436740dcdee5337b Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 1 Mar 2021 10:39:35 -0800 Subject: [PATCH 11/12] define isActive outside of map --- packages/block-library/src/navigation-link/hooks.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/block-library/src/navigation-link/hooks.js b/packages/block-library/src/navigation-link/hooks.js index c2edd698f2d809..410bf8541369cc 100644 --- a/packages/block-library/src/navigation-link/hooks.js +++ b/packages/block-library/src/navigation-link/hooks.js @@ -40,12 +40,15 @@ function enhanceNavigationLinkVariations( settings, name ) { if ( ! settings.variations ) { return { ...settings, - variations: [ ...fallbackVariations ], + variations: fallbackVariations, }; } // Otherwise decorate server passed variations with an icon and isActive function if ( settings.variations ) { + const isActive = ( blockAttributes, variationAttributes ) => { + return blockAttributes.type === variationAttributes.type; + }; const variations = settings.variations.map( ( variation ) => { return { ...variation, @@ -53,11 +56,7 @@ function enhanceNavigationLinkVariations( settings, name ) { icon: getIcon( variation.name ), } ), ...( ! variation.isActive && { - isActive: ( blockAttributes, variationAttributes ) => { - return ( - blockAttributes.type === variationAttributes.type - ); - }, + isActive, } ), }; } ); From b293fe63b5a3bd1b132f617292c87062311d44a3 Mon Sep 17 00:00:00 2001 From: Kerry Liu Date: Mon, 1 Mar 2021 12:37:30 -0800 Subject: [PATCH 12/12] Make sure page, post, tag, category variants are first --- .../src/navigation-link/index.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index ba83435c788158..b241b720babbba 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -267,15 +267,27 @@ function register_block_core_navigation_link() { $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); + $built_ins = array(); $variations = array(); + if ( $post_types ) { foreach ( $post_types as $post_type ) { - $variations[] = build_variation_for_navigation_link( $post_type, 'post-type' ); + $variation = build_variation_for_navigation_link( $post_type, 'post-type' ); + if ( 'post' === $variation['name'] || 'page' === $variation['name'] ) { + $built_ins[] = $variation; + } else { + $variations[] = $variation; + } } } if ( $taxonomies ) { foreach ( $taxonomies as $taxonomy ) { - $variations[] = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); + $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); + if ( 'category' === $variation['name'] || 'tag' === $variation['name'] ) { + $built_ins[] = $variation; + } else { + $variations[] = $variation; + } } } @@ -283,7 +295,7 @@ function register_block_core_navigation_link() { __DIR__ . '/navigation-link', array( 'render_callback' => 'render_block_core_navigation_link', - 'variations' => $variations, + 'variations' => array_merge( $built_ins, $variations ), ) ); }