From a76b7ce983cec87c85cdcc793361afc10e3e644f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 31 Mar 2021 16:38:28 +0800 Subject: [PATCH 1/8] Add post format variation to nav link block --- packages/block-library/src/navigation-link/edit.js | 2 ++ .../src/navigation-link/fallback-variations.js | 8 ++++++++ .../block-library/src/navigation-link/index.php | 14 ++++++++++---- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index 09b06798b59615..4598a9ba3a5f6d 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -117,6 +117,8 @@ function getSuggestionsQuery( type, kind ) { return { type: 'term', subtype: 'category' }; case 'tag': return { type: 'term', subtype: 'post_tag' }; + case 'format': + return { type: 'post-format' }; default: if ( kind === 'taxonomy' ) { return { type: 'term', subtype: type }; diff --git a/packages/block-library/src/navigation-link/fallback-variations.js b/packages/block-library/src/navigation-link/fallback-variations.js index 1214102801922c..a945d7d978ba40 100644 --- a/packages/block-library/src/navigation-link/fallback-variations.js +++ b/packages/block-library/src/navigation-link/fallback-variations.js @@ -7,6 +7,7 @@ import { page as pageIcon, postTitle as postIcon, tag as tagIcon, + blockDefault as formatIcon, } from '@wordpress/icons'; // FALLBACK: this is only used when the server does not understand the variations property in the @@ -49,6 +50,13 @@ const fallbackVariations = [ description: __( 'A link to a tag.' ), attributes: { type: 'tag' }, }, + { + name: 'format', + icon: formatIcon, + title: __( 'Post Format Link' ), + description: __( 'A link to a post format.' ), + attributes: { type: 'format' }, + }, ]; /** diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 5309df8a510270..7ddca99efe1fa8 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -233,7 +233,14 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { * @return array */ function build_variation_for_navigation_link( $entity, $kind ) { - $name = 'post_tag' === $entity->name ? 'tag' : $entity->name; + $custom_variation_names = array( + 'post_tag' => 'tag', + 'post_format' => 'format', + ); + + $name = array_key_exists( $entity->name, $custom_variation_names ) + ? $custom_variation_names[ $entity->name ] + : $entity->name; $title = ''; $description = ''; @@ -263,7 +270,6 @@ function build_variation_for_navigation_link( $entity, $kind ) { * @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' ); $built_ins = array(); @@ -272,7 +278,7 @@ function register_block_core_navigation_link() { if ( $post_types ) { foreach ( $post_types as $post_type ) { $variation = build_variation_for_navigation_link( $post_type, 'post-type' ); - if ( 'post' === $variation['name'] || 'page' === $variation['name'] ) { + if ( 'post' === $variation['name'] || 'page' === $variation['name'] || 'format' === $variation['name'] ) { $built_ins[] = $variation; } else { $variations[] = $variation; @@ -282,7 +288,7 @@ function register_block_core_navigation_link() { if ( $taxonomies ) { foreach ( $taxonomies as $taxonomy ) { $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); - if ( 'category' === $variation['name'] || 'tag' === $variation['name'] ) { + if ( 'category' === $variation['name'] || 'tag' === $variation['name'] || 'post_' ) { $built_ins[] = $variation; } else { $variations[] = $variation; From 105b6c996abf0d710684eaab9c8cfee329e03030 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 31 Mar 2021 17:30:22 +0800 Subject: [PATCH 2/8] post format is a taxonomy --- 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 7ddca99efe1fa8..aa70c56d9c8be8 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -278,7 +278,7 @@ function register_block_core_navigation_link() { if ( $post_types ) { foreach ( $post_types as $post_type ) { $variation = build_variation_for_navigation_link( $post_type, 'post-type' ); - if ( 'post' === $variation['name'] || 'page' === $variation['name'] || 'format' === $variation['name'] ) { + if ( 'post' === $variation['name'] || 'page' === $variation['name'] ) { $built_ins[] = $variation; } else { $variations[] = $variation; @@ -288,7 +288,7 @@ function register_block_core_navigation_link() { if ( $taxonomies ) { foreach ( $taxonomies as $taxonomy ) { $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); - if ( 'category' === $variation['name'] || 'tag' === $variation['name'] || 'post_' ) { + if ( 'category' === $variation['name'] || 'tag' === $variation['name'] || 'format' === $variation['name'] ) { $built_ins[] = $variation; } else { $variations[] = $variation; From 96533f20d104cd59c0fe5866efd1ec924a8f1a67 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 31 Mar 2021 17:33:58 +0800 Subject: [PATCH 3/8] Switch to using custom post type icon for format variation --- .../block-library/src/navigation-link/fallback-variations.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation-link/fallback-variations.js b/packages/block-library/src/navigation-link/fallback-variations.js index a945d7d978ba40..8f35219eea7bd9 100644 --- a/packages/block-library/src/navigation-link/fallback-variations.js +++ b/packages/block-library/src/navigation-link/fallback-variations.js @@ -7,7 +7,7 @@ import { page as pageIcon, postTitle as postIcon, tag as tagIcon, - blockDefault as formatIcon, + customPostType as formatIcon, } from '@wordpress/icons'; // FALLBACK: this is only used when the server does not understand the variations property in the From c7de627c6e120e760c3a359367366f3b796247e7 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 31 Mar 2021 18:16:04 +0800 Subject: [PATCH 4/8] Override some values for post_format and post_tag --- .../src/navigation-link/index.php | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index aa70c56d9c8be8..45eca981d3bd1a 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -233,15 +233,6 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { * @return array */ function build_variation_for_navigation_link( $entity, $kind ) { - $custom_variation_names = array( - 'post_tag' => 'tag', - 'post_format' => 'format', - ); - - $name = array_key_exists( $entity->name, $custom_variation_names ) - ? $custom_variation_names[ $entity->name ] - : $entity->name; - $title = ''; $description = ''; @@ -252,15 +243,46 @@ function build_variation_for_navigation_link( $entity, $kind ) { $description = $entity->labels->item_link_description; } - return array( - 'name' => $name, + $variation = array( + 'name' => $entity->name, 'title' => $title, 'description' => $description, 'attributes' => array( - 'type' => $name, + 'type' => $entity->name, 'kind' => $kind, ), ); + + // Tweak some value for the variations. + $variation_overrides = array( + 'post_tag' => array( + 'name' => 'tag', + 'attributes' => array( + 'type' => 'tag', + 'kind' => $kind, + ), + ), + 'post_format' => array( + 'name' => 'format', + // The item_link and item_link_description for post formats is the + // same as for tags, so need to be overriden. + 'title' => __( 'Post Format Link' ), + 'description' => __( 'A link to a post format' ), + 'attributes' => array( + 'type' => 'format', + 'kind' => $kind, + ), + ), + ); + + if ( array_key_exists( $entity->name, $variation_overrides ) ) { + $variation = array_merge( + $variation, + $variation_overrides[ $entity->name ] + ); + } + + return $variation; } /** From a06ff3ce01222a9aa146f5675f01091d477f3425 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 31 Mar 2021 18:18:36 +0800 Subject: [PATCH 5/8] Update test snapshot for fallback variations --- .../test/__snapshots__/hooks.js.snap | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap index dc1669c095e524..9f93f13f76b1f4 100644 --- a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap +++ b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap @@ -85,6 +85,23 @@ Object { "name": "tag", "title": "Tag Link", }, + Object { + "attributes": Object { + "type": "format", + }, + "description": "A link to a post format.", + "icon": + + , + "isActive": [Function], + "name": "format", + "title": "Post Format Link", + }, ], } `; From c9d875a6def7ad525ce3267b825dc5015b30acbc Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 1 Apr 2021 14:33:38 +0800 Subject: [PATCH 6/8] Add comment --- packages/block-library/src/navigation-link/index.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 45eca981d3bd1a..632ed633e53bc5 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -294,6 +294,11 @@ function build_variation_for_navigation_link( $entity, $kind ) { 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' ); + + // Use two separate arrays as a way to order the variations in the UI. + // Known variations (like Post Link and Page Link) are added to the + // `built_ins` array. Variations for custom post types and taxonomies are + // added to the `variations` array and will always appear after `built-ins. $built_ins = array(); $variations = array(); From 04b3395a39dc749002623cd2abef4e5d68513d4f Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 1 Apr 2021 15:22:51 +0800 Subject: [PATCH 7/8] Use post_format as name and type for variation --- packages/block-library/src/navigation-link/edit.js | 2 +- .../src/navigation-link/fallback-variations.js | 2 +- packages/block-library/src/navigation-link/index.php | 7 +++---- .../src/navigation-link/test/__snapshots__/hooks.js.snap | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index 4598a9ba3a5f6d..0348ec76cfd31d 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -117,7 +117,7 @@ function getSuggestionsQuery( type, kind ) { return { type: 'term', subtype: 'category' }; case 'tag': return { type: 'term', subtype: 'post_tag' }; - case 'format': + case 'post_format': return { type: 'post-format' }; default: if ( kind === 'taxonomy' ) { diff --git a/packages/block-library/src/navigation-link/fallback-variations.js b/packages/block-library/src/navigation-link/fallback-variations.js index 8f35219eea7bd9..04ad46bc6ca53f 100644 --- a/packages/block-library/src/navigation-link/fallback-variations.js +++ b/packages/block-library/src/navigation-link/fallback-variations.js @@ -51,7 +51,7 @@ const fallbackVariations = [ attributes: { type: 'tag' }, }, { - name: 'format', + name: 'post_format', icon: formatIcon, title: __( 'Post Format Link' ), description: __( 'A link to a post format.' ), diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 632ed633e53bc5..2720ea870ddcaa 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -263,13 +263,12 @@ function build_variation_for_navigation_link( $entity, $kind ) { ), ), 'post_format' => array( - 'name' => 'format', // The item_link and item_link_description for post formats is the - // same as for tags, so need to be overriden. + // same as for tags, so need to be overridden. 'title' => __( 'Post Format Link' ), 'description' => __( 'A link to a post format' ), 'attributes' => array( - 'type' => 'format', + 'type' => 'post_format', 'kind' => $kind, ), ), @@ -315,7 +314,7 @@ function register_block_core_navigation_link() { if ( $taxonomies ) { foreach ( $taxonomies as $taxonomy ) { $variation = build_variation_for_navigation_link( $taxonomy, 'taxonomy' ); - if ( 'category' === $variation['name'] || 'tag' === $variation['name'] || 'format' === $variation['name'] ) { + if ( 'category' === $variation['name'] || 'tag' === $variation['name'] || 'post_format' === $variation['name'] ) { $built_ins[] = $variation; } else { $variations[] = $variation; diff --git a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap index 9f93f13f76b1f4..8f10ea5a874d7b 100644 --- a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap +++ b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap @@ -99,7 +99,7 @@ Object { /> , "isActive": [Function], - "name": "format", + "name": "post_format", "title": "Post Format Link", }, ], From 256d3e833c7371856c4fb6fd88b4db9da9db7da5 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 1 Apr 2021 16:51:33 +0800 Subject: [PATCH 8/8] Remove post format fallback variation --- .../src/navigation-link/fallback-variations.js | 8 -------- .../test/__snapshots__/hooks.js.snap | 17 ----------------- 2 files changed, 25 deletions(-) diff --git a/packages/block-library/src/navigation-link/fallback-variations.js b/packages/block-library/src/navigation-link/fallback-variations.js index 04ad46bc6ca53f..1214102801922c 100644 --- a/packages/block-library/src/navigation-link/fallback-variations.js +++ b/packages/block-library/src/navigation-link/fallback-variations.js @@ -7,7 +7,6 @@ import { page as pageIcon, postTitle as postIcon, tag as tagIcon, - customPostType as formatIcon, } from '@wordpress/icons'; // FALLBACK: this is only used when the server does not understand the variations property in the @@ -50,13 +49,6 @@ const fallbackVariations = [ description: __( 'A link to a tag.' ), attributes: { type: 'tag' }, }, - { - name: 'post_format', - icon: formatIcon, - title: __( 'Post Format Link' ), - description: __( 'A link to a post format.' ), - attributes: { type: 'format' }, - }, ]; /** diff --git a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap index 8f10ea5a874d7b..dc1669c095e524 100644 --- a/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap +++ b/packages/block-library/src/navigation-link/test/__snapshots__/hooks.js.snap @@ -85,23 +85,6 @@ Object { "name": "tag", "title": "Tag Link", }, - Object { - "attributes": Object { - "type": "format", - }, - "description": "A link to a post format.", - "icon": - - , - "isActive": [Function], - "name": "post_format", - "title": "Post Format Link", - }, ], } `;