-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Library: Add a Post Tags block. (#19580)
* Block Library: Add a Post Tags block. * Block Library: Fix issue with Post Tags block when there are no tags. * Fixtures & Linting
- Loading branch information
Showing
10 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "core/post-tags", | ||
"category": "layout" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<a key={ tagId } href={ tag.link }> | ||
{ tag.name } | ||
</a> | ||
); | ||
} ); | ||
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 <PostTagsDisplay />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-tags` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-tags` block on the server. | ||
* | ||
* @return string Returns the filtered post tags for the current post wrapped inside "a" tags. | ||
*/ | ||
function render_block_core_post_tags() { | ||
$post = gutenberg_get_post_from_context(); | ||
if ( ! $post ) { | ||
return ''; | ||
} | ||
$post_tags = get_the_tags(); | ||
if ( ! empty( $post_tags ) ) { | ||
$output = ''; | ||
foreach ( $post_tags as $tag ) { | ||
$output .= '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>' . ' | '; | ||
} | ||
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-tags /--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-tags", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/fixtures/blocks/core__post-tags.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-tags", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__post-tags.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-tags /--> |