diff --git a/lib/blocks.php b/lib/blocks.php index 9f5246241fc7f..b7a4d15d4ea51 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -40,6 +40,7 @@ function gutenberg_reregister_core_block_types() { 'post-date.php' => 'core/post-date', 'post-excerpt.php' => 'core/post-excerpt', 'post-featured-image.php' => 'core/post-featured-image', + 'post-tags.php' => 'core/post-tags', ); $registry = WP_Block_Type_Registry::get_instance(); diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 1181bbceadfde..0761b2b11292f 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -73,6 +73,7 @@ import * as postCommentsForm from './post-comments-form'; import * as postDate from './post-date'; import * as postExcerpt from './post-excerpt'; import * as postFeaturedImage from './post-featured-image'; +import * as postTags from './post-tags'; /** * Function to register an individual block. @@ -203,6 +204,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = postDate, postExcerpt, postFeaturedImage, + postTags, ] : [] ), ].forEach( registerBlock ); diff --git a/packages/block-library/src/post-tags/block.json b/packages/block-library/src/post-tags/block.json new file mode 100644 index 0000000000000..29db26b80c767 --- /dev/null +++ b/packages/block-library/src/post-tags/block.json @@ -0,0 +1,4 @@ +{ + "name": "core/post-tags", + "category": "layout" +} diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js new file mode 100644 index 0000000000000..817ebf40579f8 --- /dev/null +++ b/packages/block-library/src/post-tags/edit.js @@ -0,0 +1,42 @@ +/** + * WordPress dependencies + */ +import { useEntityProp, useEntityId } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; + +function PostTagsDisplay() { + const [ tags ] = useEntityProp( 'postType', 'post', 'tags' ); + const tagLinks = useSelect( + ( select ) => { + const { getEntityRecord } = select( 'core' ); + let loaded = true; + const links = tags.map( ( tagId ) => { + const tag = getEntityRecord( 'taxonomy', 'post_tag', tagId ); + if ( ! tag ) { + return ( loaded = false ); + } + return ( + + { tag.name } + + ); + } ); + return loaded && links; + }, + [ tags ] + ); + return ( + tagLinks && + ( tagLinks.length === 0 + ? __( 'No tags.' ) + : tagLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr ] ) ) + ); +} + +export default function PostTagsEdit() { + if ( ! useEntityId( 'postType', 'post' ) ) { + return 'Post Tags Placeholder'; + } + return ; +} diff --git a/packages/block-library/src/post-tags/index.js b/packages/block-library/src/post-tags/index.js new file mode 100644 index 0000000000000..4e30ae78a5c55 --- /dev/null +++ b/packages/block-library/src/post-tags/index.js @@ -0,0 +1,18 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import metadata from './block.json'; +import edit from './edit'; + +const { name } = metadata; +export { metadata, name }; + +export const settings = { + title: __( 'Post Tags' ), + edit, +}; diff --git a/packages/block-library/src/post-tags/index.php b/packages/block-library/src/post-tags/index.php new file mode 100644 index 0000000000000..84a969dac7518 --- /dev/null +++ b/packages/block-library/src/post-tags/index.php @@ -0,0 +1,39 @@ +term_id ) . '">' . $tag->name . '' . ' | '; + } + return trim( $output, ' | ' ); + } +} + +/** + * Registers the `core/post-tags` block on the server. + */ +function register_block_core_post_tags() { + register_block_type( + 'core/post-tags', + array( + 'render_callback' => 'render_block_core_post_tags', + ) + ); +} +add_action( 'init', 'register_block_core_post_tags' ); diff --git a/packages/e2e-tests/fixtures/blocks/core__post-tags.html b/packages/e2e-tests/fixtures/blocks/core__post-tags.html new file mode 100644 index 0000000000000..e002e3cb053aa --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-tags.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__post-tags.json b/packages/e2e-tests/fixtures/blocks/core__post-tags.json new file mode 100644 index 0000000000000..a2edefa0c829f --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-tags.json @@ -0,0 +1,10 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/post-tags", + "isValid": true, + "attributes": {}, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__post-tags.parsed.json b/packages/e2e-tests/fixtures/blocks/core__post-tags.parsed.json new file mode 100644 index 0000000000000..8d1036d0930a2 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-tags.parsed.json @@ -0,0 +1,18 @@ +[ + { + "blockName": "core/post-tags", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + }, + { + "blockName": null, + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n", + "innerContent": [ + "\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__post-tags.serialized.html b/packages/e2e-tests/fixtures/blocks/core__post-tags.serialized.html new file mode 100644 index 0000000000000..e002e3cb053aa --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-tags.serialized.html @@ -0,0 +1 @@ +