From ad95b562040df45edf46ed4b780c065b1b58e741 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 29 Mar 2022 10:55:38 +0300 Subject: [PATCH 1/3] [Block Library - Post Terms]: Add dynamic variations of custom taxonomies --- .../block-library/src/post-terms/index.js | 4 +- .../block-library/src/post-terms/index.php | 39 +++++++++++++++++++ .../src/post-terms/variations.js | 28 ------------- 3 files changed, 40 insertions(+), 31 deletions(-) delete mode 100644 packages/block-library/src/post-terms/variations.js diff --git a/packages/block-library/src/post-terms/index.js b/packages/block-library/src/post-terms/index.js index 6749a1197c83c3..37f1a403eddd2b 100644 --- a/packages/block-library/src/post-terms/index.js +++ b/packages/block-library/src/post-terms/index.js @@ -3,18 +3,16 @@ */ import metadata from './block.json'; import edit from './edit'; -import variations from './variations'; /** * WordPress dependencies */ -import { postTerms as icon } from '@wordpress/icons'; +import { postCategories as icon } from '@wordpress/icons'; const { name } = metadata; export { metadata, name }; export const settings = { icon, - variations, edit, }; diff --git a/packages/block-library/src/post-terms/index.php b/packages/block-library/src/post-terms/index.php index 26475fa9280656..9167caf3bc3ab7 100644 --- a/packages/block-library/src/post-terms/index.php +++ b/packages/block-library/src/post-terms/index.php @@ -59,10 +59,49 @@ function render_block_core_post_terms( $attributes, $content, $block ) { * Registers the `core/post-terms` block on the server. */ function register_block_core_post_terms() { + $taxonomies = get_taxonomies( + array( + 'public' => true, + 'show_in_rest' => true, + ), + 'objects' + ); + + // Split the available taxonomies to `built_in` and custom ones, + // in order to prioritize the `built_in` taxonomies at the + // search results. + $built_ins = array(); + $custom_variations = array(); + + // Create and register the eligible taxonomies variations. + foreach ( $taxonomies as $taxonomy ) { + $variation = array( + 'name' => $taxonomy->name, + /* translators: %s: taxonomy's label */ + 'title' => sprintf( __( 'Post %s' ), $taxonomy->label ), + /* translators: %s: taxonomy's label */ + 'description' => sprintf( __( "Display a post's %s" ), $taxonomy->label ), + 'attributes' => array( + 'term' => $taxonomy->name, + ), + 'isActive' => array( 'term' ), + ); + // Set the category variation as the default one. + if ( 'category' === $taxonomy->name ) { + $variation['isDefault'] = true; + } + if ( $taxonomy->_builtin ) { + $built_ins[] = $variation; + } else { + $custom_variations[] = $variation; + } + } + register_block_type_from_metadata( __DIR__ . '/post-terms', array( 'render_callback' => 'render_block_core_post_terms', + 'variations' => array_merge( $built_ins, $custom_variations ), ) ); } diff --git a/packages/block-library/src/post-terms/variations.js b/packages/block-library/src/post-terms/variations.js deleted file mode 100644 index 14ddb3cb095147..00000000000000 --- a/packages/block-library/src/post-terms/variations.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * WordPress dependencies - */ -import { __ } from '@wordpress/i18n'; -import { postCategories, postTerms } from '@wordpress/icons'; - -const variations = [ - { - name: 'category', - title: __( 'Post Categories' ), - description: __( "Display a post's categories." ), - icon: postCategories, - isDefault: true, - attributes: { term: 'category' }, - isActive: ( blockAttributes ) => blockAttributes.term === 'category', - }, - - { - name: 'post_tag', - title: __( 'Post Tags' ), - description: __( "Display a post's tags." ), - icon: postTerms, - attributes: { term: 'post_tag' }, - isActive: ( blockAttributes ) => blockAttributes.term === 'post_tag', - }, -]; - -export default variations; From 18365ce5ff92d3c2e47e07e20368e4d8c7853712 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 15 Apr 2022 16:58:42 +0300 Subject: [PATCH 2/3] change labels + assign icons for categories and tags --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/package.json | 3 ++- .../block-library/src/categories/block.json | 2 +- .../block-library/src/post-terms/hooks.js | 27 +++++++++++++++++++ .../block-library/src/post-terms/index.js | 19 +++++++++---- .../block-library/src/post-terms/index.php | 5 ++-- 6 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 packages/block-library/src/post-terms/hooks.js diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 5ef3e04c65f204..3cb89708159cbe 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -71,7 +71,7 @@ A calendar of your site’s posts. ([Source](https://github.com/WordPress/gutenb - **Supports:** align - **Attributes:** month, year -## Categories +## Categories List Display a list of all categories. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/categories)) diff --git a/packages/block-library/package.json b/packages/block-library/package.json index eff8da5bdb7507..93a27fed229ccd 100644 --- a/packages/block-library/package.json +++ b/packages/block-library/package.json @@ -29,7 +29,8 @@ "src/**/*.scss", "src/navigation-link/index.js", "src/template-part/index.js", - "src/query/index.js" + "src/query/index.js", + "src/post-terms/index.js" ], "dependencies": { "@babel/runtime": "^7.16.0", diff --git a/packages/block-library/src/categories/block.json b/packages/block-library/src/categories/block.json index b66333a73b4f25..7abc2c84668724 100644 --- a/packages/block-library/src/categories/block.json +++ b/packages/block-library/src/categories/block.json @@ -2,7 +2,7 @@ "$schema": "https://schemas.wp.org/trunk/block.json", "apiVersion": 2, "name": "core/categories", - "title": "Categories", + "title": "Categories List", "category": "widgets", "description": "Display a list of all categories.", "textdomain": "default", diff --git a/packages/block-library/src/post-terms/hooks.js b/packages/block-library/src/post-terms/hooks.js new file mode 100644 index 00000000000000..539ea837a30b8d --- /dev/null +++ b/packages/block-library/src/post-terms/hooks.js @@ -0,0 +1,27 @@ +/** + * WordPress dependencies + */ +import { postCategories, postTerms } from '@wordpress/icons'; + +const variationIconMap = { + category: postCategories, + post_tag: postTerms, +}; + +// We add `icons` to categories and tags. The remaining ones use +// the block's default icon. +export default function enhanceVariations( settings, name ) { + if ( name !== 'core/post-terms' ) { + return settings; + } + const variations = settings.variations.map( ( variation ) => ( { + ...variation, + ...( variationIconMap[ variation.name ] && { + icon: variationIconMap[ variation.name ], + } ), + } ) ); + return { + ...settings, + variations, + }; +} diff --git a/packages/block-library/src/post-terms/index.js b/packages/block-library/src/post-terms/index.js index 37f1a403eddd2b..463dea44be3d4f 100644 --- a/packages/block-library/src/post-terms/index.js +++ b/packages/block-library/src/post-terms/index.js @@ -1,13 +1,15 @@ /** - * Internal dependencies + * WordPress dependencies */ -import metadata from './block.json'; -import edit from './edit'; +import { postCategories as icon } from '@wordpress/icons'; +import { addFilter } from '@wordpress/hooks'; /** - * WordPress dependencies + * Internal dependencies */ -import { postCategories as icon } from '@wordpress/icons'; +import metadata from './block.json'; +import edit from './edit'; +import enhanceVariations from './hooks'; const { name } = metadata; export { metadata, name }; @@ -16,3 +18,10 @@ export const settings = { icon, edit, }; + +// Importing this file includes side effects. This is whitelisted in block-library/package.json under sideEffects +addFilter( + 'blocks.registerBlockType', + 'core/template-part', + enhanceVariations +); diff --git a/packages/block-library/src/post-terms/index.php b/packages/block-library/src/post-terms/index.php index 9167caf3bc3ab7..dc499d1c1e867d 100644 --- a/packages/block-library/src/post-terms/index.php +++ b/packages/block-library/src/post-terms/index.php @@ -77,10 +77,9 @@ function register_block_core_post_terms() { foreach ( $taxonomies as $taxonomy ) { $variation = array( 'name' => $taxonomy->name, + 'title' => $taxonomy->label, /* translators: %s: taxonomy's label */ - 'title' => sprintf( __( 'Post %s' ), $taxonomy->label ), - /* translators: %s: taxonomy's label */ - 'description' => sprintf( __( "Display a post's %s" ), $taxonomy->label ), + 'description' => sprintf( __( 'Display the assigned %s' ), $taxonomy->label ), 'attributes' => array( 'term' => $taxonomy->name, ), From a142c172e816fde8f14a3adee57ecf219a1d7487 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 6 May 2022 11:57:44 +0300 Subject: [PATCH 3/3] update the block variation's description --- packages/block-library/src/post-terms/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-terms/index.php b/packages/block-library/src/post-terms/index.php index dc499d1c1e867d..a607981a09e8a5 100644 --- a/packages/block-library/src/post-terms/index.php +++ b/packages/block-library/src/post-terms/index.php @@ -79,7 +79,7 @@ function register_block_core_post_terms() { 'name' => $taxonomy->name, 'title' => $taxonomy->label, /* translators: %s: taxonomy's label */ - 'description' => sprintf( __( 'Display the assigned %s' ), $taxonomy->label ), + 'description' => sprintf( __( 'Display the assigned taxonomy: %s' ), $taxonomy->label ), 'attributes' => array( 'term' => $taxonomy->name, ),