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

Block Library: Add a Post Tags block. #19580

Merged
merged 3 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Block Library: Add a Post Tags block.
  • Loading branch information
epiqueras authored and ockham committed Feb 24, 2020
commit bf4b537d4d2a3db5b1ec5e79a73fd4a9eae7fde2
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"
}
36 changes: 36 additions & 0 deletions packages/block-library/src/post-tags/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import { useEntityProp, useEntityId } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

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 }>
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
{ tag.name }
</a>
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe ultimately, we could offer a way to add/remove tags directly here. Let's land a readonly block first though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

} );
return loaded && links;
},
[ tags ]
);
return tagLinks && tagLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr ] );
}

export default function PostTagsEdit() {
if ( ! useEntityId( 'postType', 'post' ) ) {
return 'Post Tags Placeholder';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i18n?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
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 ) {
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
$output .= '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>' . ' | ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get to a point where we support the common customization the theme function tags offer: https://codex.wordpress.org/Function_Reference/the_tags

}
return trim( $output, ' | ' );
epiqueras marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* 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' );