Skip to content

Commit

Permalink
Block Library: Add a Post Tags block. (#19580)
Browse files Browse the repository at this point in the history
* 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
epiqueras committed Feb 24, 2020
1 parent e0c9aa5 commit cd851b2
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -203,6 +204,7 @@ export const __experimentalRegisterExperimentalCoreBlocks =
postDate,
postExcerpt,
postFeaturedImage,
postTags,
]
: [] ),
].forEach( registerBlock );
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-tags/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "core/post-tags",
"category": "layout"
}
42 changes: 42 additions & 0 deletions packages/block-library/src/post-tags/edit.js
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 />;
}
18 changes: 18 additions & 0 deletions packages/block-library/src/post-tags/index.js
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,
};
39 changes: 39 additions & 0 deletions packages/block-library/src/post-tags/index.php
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' );
1 change: 1 addition & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-tags.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-tags /-->
10 changes: 10 additions & 0 deletions packages/e2e-tests/fixtures/blocks/core__post-tags.json
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 packages/e2e-tests/fixtures/blocks/core__post-tags.parsed.json
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"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-tags /-->

0 comments on commit cd851b2

Please sign in to comment.