From c39ec61b09f02ef77251950b66879b47bb7e4e65 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Mon, 20 Jul 2020 17:09:16 -0700 Subject: [PATCH 01/14] Explicitly pass context to editable post tag block --- .../block-library/src/post-tags/block.json | 3 ++- packages/block-library/src/post-tags/edit.js | 25 +++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/post-tags/block.json b/packages/block-library/src/post-tags/block.json index b3b0e6772c4e9..ebdf2839a162b 100644 --- a/packages/block-library/src/post-tags/block.json +++ b/packages/block-library/src/post-tags/block.json @@ -2,7 +2,8 @@ "name": "core/post-tags", "category": "design", "usesContext": [ - "postId" + "postId", + "postType" ], "supports": { "html": false diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index 38787e46c5db1..e95f1806c0b32 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -1,12 +1,18 @@ /** * WordPress dependencies */ -import { useEntityProp, useEntityId } from '@wordpress/core-data'; +import { useEntityProp } from '@wordpress/core-data'; +import { Warning } from '@wordpress/block-editor'; import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; -function PostTagsDisplay() { - const [ tags ] = useEntityProp( 'postType', 'post', 'tags' ); +function PostTagsDisplay( { context } ) { + const [ tags ] = useEntityProp( + 'postType', + context.postType, + 'tags', + context.postId + ); const tagLinks = useSelect( ( select ) => { const { getEntityRecord } = select( 'core' ); @@ -34,9 +40,14 @@ function PostTagsDisplay() { ); } -export default function PostTagsEdit() { - if ( ! useEntityId( 'postType', 'post' ) ) { - return __( 'Post Tags' ); +export default function PostTagsEdit( { context } ) { + if ( ! context.postType || ! context.postId ) { + return ( + + { __( 'Post tags block: No post found for this block.' ) } + + ); } - return ; + + return ; } From ac6ace7ed1ce2d6580bcfba03478dd5f5adb8313 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Mon, 20 Jul 2020 17:16:28 -0700 Subject: [PATCH 02/14] Add wrapping div to post tags rendered in front-end Post tags were previously misaligned when rendered in the front-end. The wrapping div now constrains the tags within the predefined margins of site editor content. --- packages/block-library/src/post-tags/index.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-tags/index.php b/packages/block-library/src/post-tags/index.php index 5a5e35debc382..98730961b8d5f 100644 --- a/packages/block-library/src/post-tags/index.php +++ b/packages/block-library/src/post-tags/index.php @@ -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 = ''; + $output = ''; + $output .= '
'; + foreach ( $post_tags as $tag ) { $output .= '' . $tag->name . '' . ' | '; } - return trim( $output, ' | ' ); + + $output = trim( $output, ' | ' ); + $output .= '
'; + + return $output; } } From 31ba000d9d2529cb6654258f74063880e52464ce Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Tue, 21 Jul 2020 00:23:31 -0700 Subject: [PATCH 03/14] Add warning if user is adding post tags block to a page --- packages/block-library/src/post-tags/edit.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index e95f1806c0b32..68e3c294f7f19 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -47,6 +47,14 @@ export default function PostTagsEdit( { context } ) { { __( 'Post tags block: No post found for this block.' ) } ); + } else if ( context.postType !== 'post' ) { + return ( + + { __( + 'Post tags block: Tags are only available in posts. Please add this block to a post instead.' + ) } + + ); } return ; From c5661b333d6c32286e259a2015020baee7c060a4 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Thu, 23 Jul 2020 10:59:37 -0700 Subject: [PATCH 04/14] Add comment to check post_tag taxonomy --- packages/block-library/src/post-tags/edit.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index 68e3c294f7f19..9e34bc8d21e3a 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -47,6 +47,15 @@ export default function PostTagsEdit( { context } ) { { __( 'Post tags block: No post found for this block.' ) } ); + + /** + * 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 page post type. + */ } else if ( context.postType !== 'post' ) { return ( From 78d467d8e52a4250668a23c0ec404e1f4c9226e0 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Thu, 23 Jul 2020 10:57:06 -0700 Subject: [PATCH 05/14] Update packages/block-library/src/post-tags/edit.js Update phrasing for warning Co-authored-by: Bernie Reiter --- packages/block-library/src/post-tags/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index 9e34bc8d21e3a..d263664da6145 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -60,7 +60,7 @@ export default function PostTagsEdit( { context } ) { return ( { __( - 'Post tags block: Tags are only available in posts. Please add this block to a post instead.' + 'Post tags block: Tags are not available for this post type.' ) } ); From ad8fd26410bb16c52876dba48c0c99accb9fc84a Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Thu, 23 Jul 2020 12:54:13 -0700 Subject: [PATCH 06/14] Update comment phrasing to account for custom post types --- packages/block-library/src/post-tags/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index d263664da6145..c0ceafe39a658 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -54,7 +54,7 @@ export default function PostTagsEdit( { context } ) { * @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 page post type. + * post_tag taxonomy is registered for the current post type. */ } else if ( context.postType !== 'post' ) { return ( From 7d5c6e9f4e2670bf1dbc8a553d818ffdbec7c26b Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Thu, 23 Jul 2020 14:19:27 -0700 Subject: [PATCH 07/14] Undo changes to server side rendered post tags blocks --- packages/block-library/src/post-tags/index.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/post-tags/index.php b/packages/block-library/src/post-tags/index.php index 98730961b8d5f..5a5e35debc382 100644 --- a/packages/block-library/src/post-tags/index.php +++ b/packages/block-library/src/post-tags/index.php @@ -20,17 +20,11 @@ function render_block_core_post_tags( $attributes, $content, $block ) { $post_tags = get_the_tags( $block->context['postId'] ); if ( ! empty( $post_tags ) ) { - $output = ''; - $output .= '
'; - + $output = ''; foreach ( $post_tags as $tag ) { $output .= '' . $tag->name . '' . ' | '; } - - $output = trim( $output, ' | ' ); - $output .= '
'; - - return $output; + return trim( $output, ' | ' ); } } From 030ebbaedda2cbe0001644dc34464056a6b553ad Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Tue, 28 Jul 2020 12:16:58 -0700 Subject: [PATCH 08/14] Add light block wrapper to post tags --- packages/block-library/src/post-tags/block.json | 8 +++----- packages/block-library/src/post-tags/edit.js | 11 +++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/post-tags/block.json b/packages/block-library/src/post-tags/block.json index ebdf2839a162b..cf33f797bb687 100644 --- a/packages/block-library/src/post-tags/block.json +++ b/packages/block-library/src/post-tags/block.json @@ -1,11 +1,9 @@ { "name": "core/post-tags", "category": "design", - "usesContext": [ - "postId", - "postType" - ], + "usesContext": [ "postId", "postType" ], "supports": { - "html": false + "html": false, + "lightBlockWrapper": true } } diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index c0ceafe39a658..0f965dda16c6b 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { useEntityProp } from '@wordpress/core-data'; -import { Warning } from '@wordpress/block-editor'; +import { Warning, __experimentalBlock as Block } from '@wordpress/block-editor'; import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; @@ -32,6 +32,7 @@ function PostTagsDisplay( { context } ) { }, [ tags ] ); + return ( tagLinks && ( tagLinks.length === 0 @@ -41,8 +42,10 @@ function PostTagsDisplay( { context } ) { } export default function PostTagsEdit( { context } ) { + let display = ; + if ( ! context.postType || ! context.postId ) { - return ( + display = ( { __( 'Post tags block: No post found for this block.' ) } @@ -57,7 +60,7 @@ export default function PostTagsEdit( { context } ) { * post_tag taxonomy is registered for the current post type. */ } else if ( context.postType !== 'post' ) { - return ( + display = ( { __( 'Post tags block: Tags are not available for this post type.' @@ -66,5 +69,5 @@ export default function PostTagsEdit( { context } ) { ); } - return ; + return { display }; } From cf2b2893dc658fed6e36c0a225a48d9d84afa310 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Tue, 28 Jul 2020 12:18:02 -0700 Subject: [PATCH 09/14] Reintroduce wrapping div to server side rendered block --- packages/block-library/src/post-tags/index.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-tags/index.php b/packages/block-library/src/post-tags/index.php index 5a5e35debc382..98730961b8d5f 100644 --- a/packages/block-library/src/post-tags/index.php +++ b/packages/block-library/src/post-tags/index.php @@ -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 = ''; + $output = ''; + $output .= '
'; + foreach ( $post_tags as $tag ) { $output .= '' . $tag->name . '' . ' | '; } - return trim( $output, ' | ' ); + + $output = trim( $output, ' | ' ); + $output .= '
'; + + return $output; } } From 4e7145f953c50f6de91aa90bb3505e580d5aa150 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Wed, 29 Jul 2020 09:43:37 -0700 Subject: [PATCH 10/14] Check tags existence to prevent runtime exception --- packages/block-library/src/post-tags/edit.js | 31 +++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index 0f965dda16c6b..c667a860a2ad3 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -17,17 +17,26 @@ function PostTagsDisplay( { context } ) { ( 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 } - - ); - } ); + let links; + + if ( tags ) { + links = tags.map( ( tagId ) => { + const tag = getEntityRecord( + 'taxonomy', + 'post_tag', + tagId + ); + if ( ! tag ) { + return ( loaded = false ); + } + return ( + + { tag.name } + + ); + } ); + } + return loaded && links; }, [ tags ] From ffad6fa927f9420cf4432bab745b1d694e0b30e3 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Wed, 29 Jul 2020 09:57:20 -0700 Subject: [PATCH 11/14] Add wp block post tags class to php render --- packages/block-library/src/post-tags/index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/post-tags/index.php b/packages/block-library/src/post-tags/index.php index 98730961b8d5f..c7b90fede2651 100644 --- a/packages/block-library/src/post-tags/index.php +++ b/packages/block-library/src/post-tags/index.php @@ -20,8 +20,8 @@ function render_block_core_post_tags( $attributes, $content, $block ) { $post_tags = get_the_tags( $block->context['postId'] ); if ( ! empty( $post_tags ) ) { - $output = ''; - $output .= '
'; + $classes = 'wp-block-post-tags'; + $output = sprintf( '
', esc_attr( $classes ) ); foreach ( $post_tags as $tag ) { $output .= '' . $tag->name . '' . ' | '; From e62e26a102e23b2ac8dff18198cfe0d5e9802686 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Wed, 29 Jul 2020 09:58:06 -0700 Subject: [PATCH 12/14] Remove unnecessary wrapping component --- packages/block-library/src/post-tags/edit.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index c667a860a2ad3..dc12a4da33068 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -6,7 +6,7 @@ import { Warning, __experimentalBlock as Block } from '@wordpress/block-editor'; import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; -function PostTagsDisplay( { context } ) { +export default function PostTagsEdit( { context } ) { const [ tags ] = useEntityProp( 'postType', context.postType, @@ -42,16 +42,11 @@ function PostTagsDisplay( { context } ) { [ tags ] ); - return ( + let display = tagLinks && ( tagLinks.length === 0 ? __( 'No tags.' ) - : tagLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr ] ) ) - ); -} - -export default function PostTagsEdit( { context } ) { - let display = ; + : tagLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr ] ) ); if ( ! context.postType || ! context.postId ) { display = ( From 9cdaa11c390280475d0f461201fefff0901952ba Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Thu, 30 Jul 2020 08:34:53 -0700 Subject: [PATCH 13/14] Refactor let declaration when generating tags --- packages/block-library/src/post-tags/edit.js | 29 ++++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index dc12a4da33068..40c3014028ad7 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -17,25 +17,18 @@ export default function PostTagsEdit( { context } ) { ( select ) => { const { getEntityRecord } = select( 'core' ); let loaded = true; - let links; - if ( tags ) { - links = tags.map( ( tagId ) => { - const tag = getEntityRecord( - 'taxonomy', - 'post_tag', - tagId - ); - if ( ! tag ) { - return ( loaded = false ); - } - return ( - - { tag.name } - - ); - } ); - } + const links = tags?.map( ( tagId ) => { + const tag = getEntityRecord( 'taxonomy', 'post_tag', tagId ); + if ( ! tag ) { + return ( loaded = false ); + } + return ( + + { tag.name } + + ); + } ); return loaded && links; }, From 788915375158d224d0ea1686651bc207f9f71d50 Mon Sep 17 00:00:00 2001 From: Jeremy Yip Date: Thu, 30 Jul 2020 08:45:35 -0700 Subject: [PATCH 14/14] Move the comment block --- packages/block-library/src/post-tags/edit.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/block-library/src/post-tags/edit.js b/packages/block-library/src/post-tags/edit.js index 40c3014028ad7..faa14e437da4a 100644 --- a/packages/block-library/src/post-tags/edit.js +++ b/packages/block-library/src/post-tags/edit.js @@ -47,7 +47,7 @@ export default function PostTagsEdit( { context } ) { { __( 'Post tags block: No post found for this block.' ) } ); - + } else if ( context.postType !== 'post' ) { /** * Do not render the block when viewing a page (as opposed to a post) * @@ -56,7 +56,6 @@ export default function PostTagsEdit( { context } ) { * In the future, instead of only evaluating posts, we should check whether the * post_tag taxonomy is registered for the current post type. */ - } else if ( context.postType !== 'post' ) { display = ( { __(