From f245e83b6f00300abfc4f7aa47481d4b41c186a2 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 29 Mar 2022 10:55:38 +0300 Subject: [PATCH] [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 d1902b93a82f98..97cc1ebc0ffdb8 100644 --- a/packages/block-library/src/post-terms/index.php +++ b/packages/block-library/src/post-terms/index.php @@ -49,10 +49,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;