Skip to content

Commit

Permalink
[Block Library - Post Terms]: Add dynamic variations of custom taxono…
Browse files Browse the repository at this point in the history
…mies
  • Loading branch information
ntsekouras committed Apr 15, 2022
1 parent e80748e commit f245e83
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
4 changes: 1 addition & 3 deletions packages/block-library/src/post-terms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
39 changes: 39 additions & 0 deletions packages/block-library/src/post-terms/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
)
);
}
Expand Down
28 changes: 0 additions & 28 deletions packages/block-library/src/post-terms/variations.js

This file was deleted.

0 comments on commit f245e83

Please sign in to comment.