Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Tags: Fix bug where no tags are rendered #24082

Merged
merged 14 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions packages/block-library/src/post-tags/block.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "core/post-tags",
"category": "design",
"usesContext": [
"postId"
],
"usesContext": [ "postId", "postType" ],
"supports": {
"html": false
"html": false,
"lightBlockWrapper": true
}
}
51 changes: 39 additions & 12 deletions packages/block-library/src/post-tags/edit.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { useEntityProp } from '@wordpress/core-data';
import { Warning, __experimentalBlock as Block } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

function PostTagsDisplay() {
const [ tags ] = useEntityProp( 'postType', 'post', 'tags' );
export default function PostTagsEdit( { context } ) {
const [ tags ] = useEntityProp(
'postType',
context.postType,
'tags',
context.postId
);
const tagLinks = useSelect(
( select ) => {
const { getEntityRecord } = select( 'core' );
let loaded = true;
const links = tags.map( ( tagId ) => {

const links = tags?.map( ( tagId ) => {
const tag = getEntityRecord( 'taxonomy', 'post_tag', tagId );
if ( ! tag ) {
return ( loaded = false );
Expand All @@ -22,21 +29,41 @@ function PostTagsDisplay() {
</a>
);
} );

return loaded && links;
},
[ tags ]
);
return (

let display =
tagLinks &&
( tagLinks.length === 0
? __( 'No tags.' )
: tagLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr ] ) )
);
}
: tagLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr ] ) );

export default function PostTagsEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
return __( 'Post Tags' );
if ( ! context.postType || ! context.postId ) {
display = (
<Warning>
{ __( 'Post tags block: No post found for this block.' ) }
</Warning>
);
} else if ( context.postType !== 'post' ) {
/**
jeyip marked this conversation as resolved.
Show resolved Hide resolved
* Do not render the block when viewing a page (as opposed to a post)
*
* @todo By default, only posts can be grouped by tags. Therefore, without any configuration,
* the post tags block will have no tags for pages. Plugins, however, can modify this behavior.
* In the future, instead of only evaluating posts, we should check whether the
* post_tag taxonomy is registered for the current post type.
*/
jeyip marked this conversation as resolved.
Show resolved Hide resolved
display = (
<Warning>
{ __(
'Post tags block: Tags are not available for this post type.'
) }
</Warning>
);
}
return <PostTagsDisplay />;

return <Block.div>{ display }</Block.div>;
}
10 changes: 8 additions & 2 deletions packages/block-library/src/post-tags/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ function render_block_core_post_tags( $attributes, $content, $block ) {

$post_tags = get_the_tags( $block->context['postId'] );
if ( ! empty( $post_tags ) ) {
$output = '';
$classes = 'wp-block-post-tags';
$output = sprintf( '<div class="%1$s">', esc_attr( $classes ) );
jeyip marked this conversation as resolved.
Show resolved Hide resolved

foreach ( $post_tags as $tag ) {
$output .= '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>' . ' | ';
}
return trim( $output, ' | ' );

$output = trim( $output, ' | ' );
$output .= '</div>';

return $output;
}
}

Expand Down