From 29736998623b503071adca91c15b4a42207c73a1 Mon Sep 17 00:00:00 2001 From: Copons Date: Tue, 21 Jul 2020 12:53:58 +0100 Subject: [PATCH 1/9] Add new Post Categories block --- lib/blocks.php | 1 + packages/block-library/src/index.js | 2 + .../src/post-categories/block.json | 23 +++++ .../block-library/src/post-categories/edit.js | 85 +++++++++++++++++++ .../src/post-categories/index.js | 18 ++++ .../src/post-categories/index.php | 56 ++++++++++++ 6 files changed, 185 insertions(+) create mode 100644 packages/block-library/src/post-categories/block.json create mode 100644 packages/block-library/src/post-categories/edit.js create mode 100644 packages/block-library/src/post-categories/index.js create mode 100644 packages/block-library/src/post-categories/index.php diff --git a/lib/blocks.php b/lib/blocks.php index 2d0b969cc6b19..46dff73a2b9d7 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -74,6 +74,7 @@ function gutenberg_reregister_core_block_types() { $block_names, array( 'post-author.php' => 'core/post-author', + 'post-categories.php' => 'core/post-categories', 'post-comment.php' => 'core/post-comment', 'post-comment-content.php' => 'core/post-comment-content', 'post-comments.php' => 'core/post-comments', diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index e0733e8dc1231..da1e24ade5940 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -75,6 +75,7 @@ import * as queryPagination from './query-pagination'; import * as postTitle from './post-title'; import * as postContent from './post-content'; import * as postAuthor from './post-author'; +import * as postCategories from './post-categories'; import * as postComment from './post-comment'; import * as postCommentContent from './post-comment-content'; import * as postComments from './post-comments'; @@ -210,6 +211,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = postTitle, postContent, postAuthor, + postCategories, postComment, postCommentContent, postComments, diff --git a/packages/block-library/src/post-categories/block.json b/packages/block-library/src/post-categories/block.json new file mode 100644 index 0000000000000..fe4e218c92f95 --- /dev/null +++ b/packages/block-library/src/post-categories/block.json @@ -0,0 +1,23 @@ +{ + "name": "core/post-categories", + "category": "design", + "attributes": { + "textAlign": { + "type": "string" + } + }, + "usesContext": [ + "postId", + "postType" + ], + "supports": { + "html": false, + "lightBlockWrapper": true, + "__experimentalFontSize": true, + "__experimentalColor": { + "gradients": true, + "linkColor": true + }, + "__experimentalLineHeight": true + } +} diff --git a/packages/block-library/src/post-categories/edit.js b/packages/block-library/src/post-categories/edit.js new file mode 100644 index 0000000000000..b58b7d3067111 --- /dev/null +++ b/packages/block-library/src/post-categories/edit.js @@ -0,0 +1,85 @@ +/** + * External dependencies + */ +import classnames from 'classnames'; +import { map } from 'lodash'; + +/** + * WordPress dependencies + */ +import { useEntityProp } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; +import { + AlignmentToolbar, + BlockControls, + __experimentalBlock as Block, +} from '@wordpress/block-editor'; +import { __ } from '@wordpress/i18n'; + +export default function PostCategoriesEdit( { + attributes, + context, + setAttributes, +} ) { + const { textAlign } = attributes; + const { postId, postType } = context; + + const [ categories ] = useEntityProp( + 'postType', + postType, + 'categories', + postId + ); + + const categoryLinks = useSelect( + ( select ) => { + const { getEntityRecord } = select( 'core' ); + let loaded = true; + const links = map( categories, ( categoryId ) => { + const category = getEntityRecord( + 'taxonomy', + 'category', + categoryId + ); + if ( ! category ) { + return ( loaded = false ); + } + return ( + + { category.name } + + ); + } ); + return loaded && links; + }, + [ categories ] + ); + + const hasCategories = categoryLinks && categoryLinks.length > 0; + + return ( + <> + + { + setAttributes( { textAlign: nextAlign } ); + } } + /> + + + { hasCategories && + categoryLinks.reduce( ( prev, curr ) => [ + prev, + ' | ', + curr, + ] ) } + { ! hasCategories && __( 'No categories.' ) } + + + ); +} diff --git a/packages/block-library/src/post-categories/index.js b/packages/block-library/src/post-categories/index.js new file mode 100644 index 0000000000000..a4dc7beb6021f --- /dev/null +++ b/packages/block-library/src/post-categories/index.js @@ -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 Categories' ), + edit, +}; diff --git a/packages/block-library/src/post-categories/index.php b/packages/block-library/src/post-categories/index.php new file mode 100644 index 0000000000000..f50b57312307b --- /dev/null +++ b/packages/block-library/src/post-categories/index.php @@ -0,0 +1,56 @@ +context['postId'] ) ) { + return ''; + } + + $post_categories = get_the_category( $block->context['postId'] ); + if ( empty( $post_categories ) ) { + return ''; + } + + $align_class_name = empty( $attributes['textAlign'] ) ? '' : ' ' . "has-text-align-{$attributes['textAlign']}"; + + $categoryLinks = ''; + foreach ( $post_categories as $category ) { + $categoryLinks .= sprintf( + '%2$s | ', + get_category_link( $category->term_id ), + esc_html( $category->name ) + ); + } + $categoryLinks = trim( $categoryLinks, ' | ' ); + + return sprintf( + '
%2$s
', + 'wp-block-post-categories' . esc_attr( $align_class_name ), + $categoryLinks + ); +} + +/** + * Registers the `core/post-categories` block on the server. + */ +function register_block_core_post_categories() { + register_block_type_from_metadata( + __DIR__ . '/post-categories', + array( + 'render_callback' => 'render_block_core_post_categories', + ) + ); +} +add_action( 'init', 'register_block_core_post_categories' ); From 17c11d57903c5ab1d1f1c632b8cce937fb3041cc Mon Sep 17 00:00:00 2001 From: Copons Date: Tue, 21 Jul 2020 12:56:25 +0100 Subject: [PATCH 2/9] Add tests --- .../fixtures/blocks/core__post-categories.html | 1 + .../fixtures/blocks/core__post-categories.json | 10 ++++++++++ .../blocks/core__post-categories.parsed.json | 18 ++++++++++++++++++ .../core__post-categories.serialized.html | 1 + 4 files changed, 30 insertions(+) create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-categories.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-categories.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.html b/packages/e2e-tests/fixtures/blocks/core__post-categories.html new file mode 100644 index 0000000000000..b4bbf67e7b61a --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-categories.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.json b/packages/e2e-tests/fixtures/blocks/core__post-categories.json new file mode 100644 index 0000000000000..502199eb8a3d9 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-categories.json @@ -0,0 +1,10 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/post-categories", + "isValid": true, + "attributes": {}, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json b/packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json new file mode 100644 index 0000000000000..d284c12916466 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json @@ -0,0 +1,18 @@ +[ + { + "blockName": "core/post-categories", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + }, + { + "blockName": null, + "attrs": {}, + "innerBlocks": [], + "innerHTML": "\n", + "innerContent": [ + "\n" + ] + } +] diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html b/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html new file mode 100644 index 0000000000000..b4bbf67e7b61a --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html @@ -0,0 +1 @@ + From 79435595f366179e0d15f70dab1e8e34db5a690a Mon Sep 17 00:00:00 2001 From: Copons Date: Tue, 21 Jul 2020 13:16:53 +0100 Subject: [PATCH 3/9] Fix incorrect case format --- packages/block-library/src/post-categories/index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/post-categories/index.php b/packages/block-library/src/post-categories/index.php index f50b57312307b..25715acae7e32 100644 --- a/packages/block-library/src/post-categories/index.php +++ b/packages/block-library/src/post-categories/index.php @@ -25,20 +25,20 @@ function render_block_core_post_categories( $attributes, $content, $block ) { $align_class_name = empty( $attributes['textAlign'] ) ? '' : ' ' . "has-text-align-{$attributes['textAlign']}"; - $categoryLinks = ''; + $category_links = ''; foreach ( $post_categories as $category ) { - $categoryLinks .= sprintf( + $category_links .= sprintf( '%2$s | ', get_category_link( $category->term_id ), esc_html( $category->name ) ); } - $categoryLinks = trim( $categoryLinks, ' | ' ); + $category_links = trim( $category_links, ' | ' ); return sprintf( '
%2$s
', 'wp-block-post-categories' . esc_attr( $align_class_name ), - $categoryLinks + $category_links ); } From bb5eea3907c9f47a25a3d6faa78c0b14896099a7 Mon Sep 17 00:00:00 2001 From: Copons Date: Mon, 27 Jul 2020 11:59:55 +0100 Subject: [PATCH 4/9] Add a warning for when there is no post in context --- packages/block-library/src/post-categories/edit.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/post-categories/edit.js b/packages/block-library/src/post-categories/edit.js index b58b7d3067111..85f8ff99d811b 100644 --- a/packages/block-library/src/post-categories/edit.js +++ b/packages/block-library/src/post-categories/edit.js @@ -12,6 +12,7 @@ import { useSelect } from '@wordpress/data'; import { AlignmentToolbar, BlockControls, + Warning, __experimentalBlock as Block, } from '@wordpress/block-editor'; import { __ } from '@wordpress/i18n'; @@ -55,6 +56,7 @@ export default function PostCategoriesEdit( { [ categories ] ); + const hasPost = postId && postType; const hasCategories = categoryLinks && categoryLinks.length > 0; return ( @@ -78,7 +80,14 @@ export default function PostCategoriesEdit( { ' | ', curr, ] ) } - { ! hasCategories && __( 'No categories.' ) } + + { hasPost && ! hasCategories && __( 'No categories.' ) } + + { ! hasPost && ( + + { __( 'Post Categories block: post not found.' ) } + + ) } ); From 80c568e363a3bc68f982547b4f3b2600af0d4ee4 Mon Sep 17 00:00:00 2001 From: Copons Date: Tue, 11 Aug 2020 16:31:33 +0100 Subject: [PATCH 5/9] Start refactoring Post Categories Block as Hierarcical Terms Block --- .../src/post-categories/block.json | 3 + .../block-library/src/post-categories/edit.js | 124 +++++++++++++----- .../use-hierarchical-term-links.js | 49 +++++++ .../post-categories/use-hierarchical-terms.js | 41 ++++++ 4 files changed, 181 insertions(+), 36 deletions(-) create mode 100644 packages/block-library/src/post-categories/use-hierarchical-term-links.js create mode 100644 packages/block-library/src/post-categories/use-hierarchical-terms.js diff --git a/packages/block-library/src/post-categories/block.json b/packages/block-library/src/post-categories/block.json index fe4e218c92f95..93d283f592fe3 100644 --- a/packages/block-library/src/post-categories/block.json +++ b/packages/block-library/src/post-categories/block.json @@ -2,6 +2,9 @@ "name": "core/post-categories", "category": "design", "attributes": { + "term": { + "type": "object" + }, "textAlign": { "type": "string" } diff --git a/packages/block-library/src/post-categories/edit.js b/packages/block-library/src/post-categories/edit.js index 85f8ff99d811b..9e615188561ae 100644 --- a/packages/block-library/src/post-categories/edit.js +++ b/packages/block-library/src/post-categories/edit.js @@ -2,62 +2,94 @@ * External dependencies */ import classnames from 'classnames'; -import { map } from 'lodash'; +import { find, map } from 'lodash'; /** * WordPress dependencies */ -import { useEntityProp } from '@wordpress/core-data'; -import { useSelect } from '@wordpress/data'; import { AlignmentToolbar, BlockControls, Warning, __experimentalBlock as Block, + __experimentalBlockVariationPicker as BlockVariationPicker, } from '@wordpress/block-editor'; +import { Spinner } from '@wordpress/components'; +import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; +/** + * Internal dependencies + */ +import useHierarchicalTerms from './use-hierarchical-terms'; +import useHierarchicalTermLinks from './use-hierarchical-term-links'; + +function HierarchicalTermPicker( { + hierarchicalTerms, + isLoadingHierarchicalTerms, + setAttributes, +} ) { + if ( isLoadingHierarchicalTerms ) { + return ; + } + + const variations = map( hierarchicalTerms, ( term ) => ( { + /* eslint-disable camelcase */ + name: term?.slug, + title: term?.name, + description: term?.description, + is_default: 'category' === term?.slug, + attributes: { + term: { restBase: term?.rest_base, slug: term?.slug }, + }, + /* eslint-enable camelcase */ + } ) ); + + return ( + { + setAttributes( variation.attributes ); + } } + /> + ); +} + export default function PostCategoriesEdit( { attributes, context, setAttributes, } ) { - const { textAlign } = attributes; + const { term, textAlign } = attributes; const { postId, postType } = context; - const [ categories ] = useEntityProp( - 'postType', + const [ selectedTerm, setSelectedTerm ] = useState(); + + const { + hierarchicalTerms, + isLoadingHierarchicalTerms, + } = useHierarchicalTerms(); + + const { + hierarchicalTermLinks, + isLoadingHierarchicalTermLinks, + } = useHierarchicalTermLinks( { + postId, postType, - 'categories', - postId - ); + term, + } ); - const categoryLinks = useSelect( - ( select ) => { - const { getEntityRecord } = select( 'core' ); - let loaded = true; - const links = map( categories, ( categoryId ) => { - const category = getEntityRecord( - 'taxonomy', - 'category', - categoryId - ); - if ( ! category ) { - return ( loaded = false ); - } - return ( - - { category.name } - - ); - } ); - return loaded && links; - }, - [ categories ] - ); + useEffect( () => { + if ( selectedTerm || ! term?.slug ) { + return; + } + + setSelectedTerm( find( hierarchicalTerms, { slug: term.slug } ) ); + } ); const hasPost = postId && postType; - const hasCategories = categoryLinks && categoryLinks.length > 0; + const hasHierarchicalTermLinks = + hierarchicalTermLinks && hierarchicalTermLinks.length > 0; return ( <> @@ -74,14 +106,34 @@ export default function PostCategoriesEdit( { [ `has-text-align-${ textAlign }` ]: textAlign, } ) } > - { hasCategories && - categoryLinks.reduce( ( prev, curr ) => [ + { hasPost && ! term && ( + + ) } + + { hasPost && isLoadingHierarchicalTermLinks && } + + { hasPost && + hasHierarchicalTermLinks && + ! isLoadingHierarchicalTermLinks && + hierarchicalTermLinks.reduce( ( prev, curr ) => [ prev, ' | ', curr, ] ) } - { hasPost && ! hasCategories && __( 'No categories.' ) } + { hasPost && + !! term && + ! isLoadingHierarchicalTermLinks && + ! hasHierarchicalTermLinks && + // eslint-disable-next-line camelcase + ( selectedTerm?.labels?.no_terms || + __( 'Term items not found.' ) ) } { ! hasPost && ( diff --git a/packages/block-library/src/post-categories/use-hierarchical-term-links.js b/packages/block-library/src/post-categories/use-hierarchical-term-links.js new file mode 100644 index 0000000000000..fb539d0eff79a --- /dev/null +++ b/packages/block-library/src/post-categories/use-hierarchical-term-links.js @@ -0,0 +1,49 @@ +/** + * External dependencies + */ +import { map } from 'lodash'; + +/** + * WordPress dependencies + */ +import { useEntityProp } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; + +export default function useHierarchicalTermLinks( { postId, postType, term } ) { + const [ hierarchicalTermItems ] = useEntityProp( + 'postType', + postType, + term?.restBase, + postId + ); + + const { hierarchicalTermLinks, isLoadingHierarchicalTermLinks } = useSelect( + ( select ) => { + const { getEntityRecord } = select( 'core' ); + + let loaded = true; + + const links = map( hierarchicalTermItems, ( itemId ) => { + const item = getEntityRecord( 'taxonomy', term?.slug, itemId ); + + if ( ! item ) { + return ( loaded = false ); + } + + return ( + + { item.name } + + ); + } ); + + return { + hierarchicalTermLinks: links, + isLoadingHierarchicalTermLinks: ! loaded, + }; + }, + [ hierarchicalTermItems ] + ); + + return { hierarchicalTermLinks, isLoadingHierarchicalTermLinks }; +} diff --git a/packages/block-library/src/post-categories/use-hierarchical-terms.js b/packages/block-library/src/post-categories/use-hierarchical-terms.js new file mode 100644 index 0000000000000..b47f54c30d8e7 --- /dev/null +++ b/packages/block-library/src/post-categories/use-hierarchical-terms.js @@ -0,0 +1,41 @@ +/** + * External dependencies + */ +import { filter } from 'lodash'; + +/** + * WordPress dependencies + */ +import { useSelect } from '@wordpress/data'; +import { useEffect, useState } from '@wordpress/element'; + +export default function useHierarchicalTerms() { + const taxonomies = useSelect( ( select ) => + select( 'core' ).getTaxonomies( { per_page: -1 } ) + ); + + const [ hierarchicalTerms, setHierarchicalTerms ] = useState( [] ); + + const [ + isLoadingHierarchicalTerms, + setIsLoadingHierarchicalTerms, + ] = useState( false ); + + useEffect( () => { + if ( ! taxonomies ) { + setIsLoadingHierarchicalTerms( true ); + return; + } + + setHierarchicalTerms( + filter( + taxonomies, + ( taxonomy ) => + taxonomy.hierarchical && taxonomy.visibility.show_ui + ) + ); + setIsLoadingHierarchicalTerms( false ); + }, [ setIsLoadingHierarchicalTerms, taxonomies ] ); + + return { hierarchicalTerms, isLoadingHierarchicalTerms }; +} From 67c13e6856f791b09d4fd9387ce3ad80b79832df Mon Sep 17 00:00:00 2001 From: Copons Date: Wed, 12 Aug 2020 11:59:59 +0100 Subject: [PATCH 6/9] Hardcode the variations --- .../src/post-categories/block.json | 2 +- .../block-library/src/post-categories/edit.js | 137 +++++++++--------- .../src/post-categories/index.js | 2 + .../use-hierarchical-term-links.js | 6 +- .../post-categories/use-hierarchical-terms.js | 41 ------ .../src/post-categories/variations.js | 16 ++ 6 files changed, 90 insertions(+), 114 deletions(-) delete mode 100644 packages/block-library/src/post-categories/use-hierarchical-terms.js create mode 100644 packages/block-library/src/post-categories/variations.js diff --git a/packages/block-library/src/post-categories/block.json b/packages/block-library/src/post-categories/block.json index 93d283f592fe3..3cbe508413688 100644 --- a/packages/block-library/src/post-categories/block.json +++ b/packages/block-library/src/post-categories/block.json @@ -3,7 +3,7 @@ "category": "design", "attributes": { "term": { - "type": "object" + "type": "string" }, "textAlign": { "type": "string" diff --git a/packages/block-library/src/post-categories/edit.js b/packages/block-library/src/post-categories/edit.js index 9e615188561ae..bc8399b6efd0b 100644 --- a/packages/block-library/src/post-categories/edit.js +++ b/packages/block-library/src/post-categories/edit.js @@ -2,7 +2,7 @@ * External dependencies */ import classnames from 'classnames'; -import { find, map } from 'lodash'; +import { find } from 'lodash'; /** * WordPress dependencies @@ -15,60 +15,59 @@ import { __experimentalBlockVariationPicker as BlockVariationPicker, } from '@wordpress/block-editor'; import { Spinner } from '@wordpress/components'; -import { useEffect, useState } from '@wordpress/element'; +import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import useHierarchicalTerms from './use-hierarchical-terms'; import useHierarchicalTermLinks from './use-hierarchical-term-links'; -function HierarchicalTermPicker( { - hierarchicalTerms, - isLoadingHierarchicalTerms, - setAttributes, -} ) { - if ( isLoadingHierarchicalTerms ) { - return ; - } - - const variations = map( hierarchicalTerms, ( term ) => ( { - /* eslint-disable camelcase */ - name: term?.slug, - title: term?.name, - description: term?.description, - is_default: 'category' === term?.slug, - attributes: { - term: { restBase: term?.rest_base, slug: term?.slug }, - }, - /* eslint-enable camelcase */ - } ) ); - - return ( - { - setAttributes( variation.attributes ); - } } - /> - ); -} - export default function PostCategoriesEdit( { attributes, + clientId, context, + name, setAttributes, } ) { const { term, textAlign } = attributes; const { postId, postType } = context; - const [ selectedTerm, setSelectedTerm ] = useState(); + const { blockType, defaultVariation, variations } = useSelect( + ( select ) => { + const { + getBlockVariations, + getBlockType, + getDefaultBlockVariation, + } = select( 'core/blocks' ); + + return { + blockType: getBlockType( name ), + defaultVariation: getDefaultBlockVariation( name, 'block' ), + variations: getBlockVariations( name, 'block' ), + }; + }, + [ clientId, name ] + ); - const { - hierarchicalTerms, - isLoadingHierarchicalTerms, - } = useHierarchicalTerms(); + const selectedTerm = useSelect( + ( select ) => { + if ( ! term ) return {}; + const taxonomies = select( 'core' ).getTaxonomies( { + per_page: -1, + } ); + return ( + find( + taxonomies, + ( taxonomy ) => + taxonomy.slug === term && + taxonomy.hierarchical && + taxonomy.visibility.show_ui + ) || {} + ); + }, + [ term ] + ); const { hierarchicalTermLinks, @@ -76,21 +75,38 @@ export default function PostCategoriesEdit( { } = useHierarchicalTermLinks( { postId, postType, - term, - } ); - - useEffect( () => { - if ( selectedTerm || ! term?.slug ) { - return; - } - - setSelectedTerm( find( hierarchicalTerms, { slug: term.slug } ) ); + term: selectedTerm, } ); const hasPost = postId && postType; const hasHierarchicalTermLinks = hierarchicalTermLinks && hierarchicalTermLinks.length > 0; + if ( ! hasPost ) { + return ( + + + { __( 'Post Categories block: post not found.' ) } + + + ); + } + + if ( ! term ) { + return ( + + { + setAttributes( variation.attributes ); + } } + variations={ variations } + /> + + ); + } + return ( <> @@ -106,20 +122,9 @@ export default function PostCategoriesEdit( { [ `has-text-align-${ textAlign }` ]: textAlign, } ) } > - { hasPost && ! term && ( - - ) } + { isLoadingHierarchicalTermLinks && } - { hasPost && isLoadingHierarchicalTermLinks && } - - { hasPost && - hasHierarchicalTermLinks && + { hasHierarchicalTermLinks && ! isLoadingHierarchicalTermLinks && hierarchicalTermLinks.reduce( ( prev, curr ) => [ prev, @@ -127,19 +132,11 @@ export default function PostCategoriesEdit( { curr, ] ) } - { hasPost && - !! term && - ! isLoadingHierarchicalTermLinks && + { ! isLoadingHierarchicalTermLinks && ! hasHierarchicalTermLinks && // eslint-disable-next-line camelcase ( selectedTerm?.labels?.no_terms || __( 'Term items not found.' ) ) } - - { ! hasPost && ( - - { __( 'Post Categories block: post not found.' ) } - - ) } ); diff --git a/packages/block-library/src/post-categories/index.js b/packages/block-library/src/post-categories/index.js index a4dc7beb6021f..90adace6718e8 100644 --- a/packages/block-library/src/post-categories/index.js +++ b/packages/block-library/src/post-categories/index.js @@ -8,11 +8,13 @@ import { __ } from '@wordpress/i18n'; */ import metadata from './block.json'; import edit from './edit'; +import variations from './variations'; const { name } = metadata; export { metadata, name }; export const settings = { title: __( 'Post Categories' ), + variations, edit, }; diff --git a/packages/block-library/src/post-categories/use-hierarchical-term-links.js b/packages/block-library/src/post-categories/use-hierarchical-term-links.js index fb539d0eff79a..abf2af7426cd3 100644 --- a/packages/block-library/src/post-categories/use-hierarchical-term-links.js +++ b/packages/block-library/src/post-categories/use-hierarchical-term-links.js @@ -10,10 +10,12 @@ import { useEntityProp } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; export default function useHierarchicalTermLinks( { postId, postType, term } ) { + const { rest_base: restBase, slug } = term; + const [ hierarchicalTermItems ] = useEntityProp( 'postType', postType, - term?.restBase, + restBase, postId ); @@ -24,7 +26,7 @@ export default function useHierarchicalTermLinks( { postId, postType, term } ) { let loaded = true; const links = map( hierarchicalTermItems, ( itemId ) => { - const item = getEntityRecord( 'taxonomy', term?.slug, itemId ); + const item = getEntityRecord( 'taxonomy', slug, itemId ); if ( ! item ) { return ( loaded = false ); diff --git a/packages/block-library/src/post-categories/use-hierarchical-terms.js b/packages/block-library/src/post-categories/use-hierarchical-terms.js deleted file mode 100644 index b47f54c30d8e7..0000000000000 --- a/packages/block-library/src/post-categories/use-hierarchical-terms.js +++ /dev/null @@ -1,41 +0,0 @@ -/** - * External dependencies - */ -import { filter } from 'lodash'; - -/** - * WordPress dependencies - */ -import { useSelect } from '@wordpress/data'; -import { useEffect, useState } from '@wordpress/element'; - -export default function useHierarchicalTerms() { - const taxonomies = useSelect( ( select ) => - select( 'core' ).getTaxonomies( { per_page: -1 } ) - ); - - const [ hierarchicalTerms, setHierarchicalTerms ] = useState( [] ); - - const [ - isLoadingHierarchicalTerms, - setIsLoadingHierarchicalTerms, - ] = useState( false ); - - useEffect( () => { - if ( ! taxonomies ) { - setIsLoadingHierarchicalTerms( true ); - return; - } - - setHierarchicalTerms( - filter( - taxonomies, - ( taxonomy ) => - taxonomy.hierarchical && taxonomy.visibility.show_ui - ) - ); - setIsLoadingHierarchicalTerms( false ); - }, [ setIsLoadingHierarchicalTerms, taxonomies ] ); - - return { hierarchicalTerms, isLoadingHierarchicalTerms }; -} diff --git a/packages/block-library/src/post-categories/variations.js b/packages/block-library/src/post-categories/variations.js new file mode 100644 index 0000000000000..6f755877dce3e --- /dev/null +++ b/packages/block-library/src/post-categories/variations.js @@ -0,0 +1,16 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; + +const variations = [ + { + name: 'category', + title: __( 'Categories' ), + icon: 'category', + is_default: true, + attributes: { term: 'category' }, + }, +]; + +export default variations; From e455435759e574e1082b48822fbad139ed8c9c35 Mon Sep 17 00:00:00 2001 From: Copons Date: Wed, 12 Aug 2020 12:16:16 +0100 Subject: [PATCH 7/9] Rename to Post Hierarchical Terms --- lib/blocks.php | 40 +++++++++---------- packages/block-library/src/index.js | 4 +- .../block.json | 2 +- .../edit.js | 2 +- .../index.js | 2 +- .../index.php | 16 ++++---- .../use-hierarchical-term-links.js | 0 .../variations.js | 2 +- .../blocks/core__post-categories.html | 1 - .../core__post-categories.serialized.html | 1 - .../blocks/core__post-hierarchical-terms.html | 1 + ...son => core__post-hierarchical-terms.json} | 2 +- ...core__post-hierarchical-terms.parsed.json} | 2 +- ...e__post-hierarchical-terms.serialized.html | 1 + 14 files changed, 38 insertions(+), 38 deletions(-) rename packages/block-library/src/{post-categories => post-hierarchical-terms}/block.json (90%) rename packages/block-library/src/{post-categories => post-hierarchical-terms}/edit.js (98%) rename packages/block-library/src/{post-categories => post-hierarchical-terms}/index.js (88%) rename packages/block-library/src/{post-categories => post-hierarchical-terms}/index.php (66%) rename packages/block-library/src/{post-categories => post-hierarchical-terms}/use-hierarchical-term-links.js (100%) rename packages/block-library/src/{post-categories => post-hierarchical-terms}/variations.js (87%) delete mode 100644 packages/e2e-tests/fixtures/blocks/core__post-categories.html delete mode 100644 packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.html rename packages/e2e-tests/fixtures/blocks/{core__post-categories.json => core__post-hierarchical-terms.json} (76%) rename packages/e2e-tests/fixtures/blocks/{core__post-categories.parsed.json => core__post-hierarchical-terms.parsed.json} (84%) create mode 100644 packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.serialized.html diff --git a/lib/blocks.php b/lib/blocks.php index 46dff73a2b9d7..7bd7763e7b497 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -73,26 +73,26 @@ function gutenberg_reregister_core_block_types() { $block_names = array_merge( $block_names, array( - 'post-author.php' => 'core/post-author', - 'post-categories.php' => 'core/post-categories', - 'post-comment.php' => 'core/post-comment', - 'post-comment-content.php' => 'core/post-comment-content', - 'post-comments.php' => 'core/post-comments', - 'post-comments-count.php' => 'core/post-comments-count', - 'post-comments-form.php' => 'core/post-comments-form', - 'post-content.php' => 'core/post-content', - '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', - 'post-title.php' => 'core/post-title', - 'query.php' => 'core/query', - 'query-loop.php' => 'core/query-loop', - 'query-pagination.php' => 'core/query-pagination', - 'site-logo.php' => 'core/site-logo', - 'site-tagline.php' => 'core/site-tagline', - 'site-title.php' => 'core/site-title', - 'template-part.php' => 'core/template-part', + 'post-author.php' => 'core/post-author', + 'post-comment.php' => 'core/post-comment', + 'post-comment-content.php' => 'core/post-comment-content', + 'post-comments.php' => 'core/post-comments', + 'post-comments-count.php' => 'core/post-comments-count', + 'post-comments-form.php' => 'core/post-comments-form', + 'post-content.php' => 'core/post-content', + 'post-date.php' => 'core/post-date', + 'post-excerpt.php' => 'core/post-excerpt', + 'post-featured-image.php' => 'core/post-featured-image', + 'post-hierarchical-terms.php' => 'core/post-hierarchical-terms', + 'post-tags.php' => 'core/post-tags', + 'post-title.php' => 'core/post-title', + 'query.php' => 'core/query', + 'query-loop.php' => 'core/query-loop', + 'query-pagination.php' => 'core/query-pagination', + 'site-logo.php' => 'core/site-logo', + 'site-tagline.php' => 'core/site-tagline', + 'site-title.php' => 'core/site-title', + 'template-part.php' => 'core/template-part', ) ); } diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index da1e24ade5940..ac2ae4d759d56 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -75,7 +75,6 @@ import * as queryPagination from './query-pagination'; import * as postTitle from './post-title'; import * as postContent from './post-content'; import * as postAuthor from './post-author'; -import * as postCategories from './post-categories'; import * as postComment from './post-comment'; import * as postCommentContent from './post-comment-content'; import * as postComments from './post-comments'; @@ -84,6 +83,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 postHierarchicalTerms from './post-hierarchical-terms'; import * as postTags from './post-tags'; /** @@ -211,7 +211,6 @@ export const __experimentalRegisterExperimentalCoreBlocks = postTitle, postContent, postAuthor, - postCategories, postComment, postCommentContent, postComments, @@ -220,6 +219,7 @@ export const __experimentalRegisterExperimentalCoreBlocks = postDate, postExcerpt, postFeaturedImage, + postHierarchicalTerms, postTags, ] : [] ), diff --git a/packages/block-library/src/post-categories/block.json b/packages/block-library/src/post-hierarchical-terms/block.json similarity index 90% rename from packages/block-library/src/post-categories/block.json rename to packages/block-library/src/post-hierarchical-terms/block.json index 3cbe508413688..0afec0b02857a 100644 --- a/packages/block-library/src/post-categories/block.json +++ b/packages/block-library/src/post-hierarchical-terms/block.json @@ -1,5 +1,5 @@ { - "name": "core/post-categories", + "name": "core/post-hierarchical-terms", "category": "design", "attributes": { "term": { diff --git a/packages/block-library/src/post-categories/edit.js b/packages/block-library/src/post-hierarchical-terms/edit.js similarity index 98% rename from packages/block-library/src/post-categories/edit.js rename to packages/block-library/src/post-hierarchical-terms/edit.js index bc8399b6efd0b..ce0df119b1979 100644 --- a/packages/block-library/src/post-categories/edit.js +++ b/packages/block-library/src/post-hierarchical-terms/edit.js @@ -23,7 +23,7 @@ import { __ } from '@wordpress/i18n'; */ import useHierarchicalTermLinks from './use-hierarchical-term-links'; -export default function PostCategoriesEdit( { +export default function PostHierarchicalTermsEdit( { attributes, clientId, context, diff --git a/packages/block-library/src/post-categories/index.js b/packages/block-library/src/post-hierarchical-terms/index.js similarity index 88% rename from packages/block-library/src/post-categories/index.js rename to packages/block-library/src/post-hierarchical-terms/index.js index 90adace6718e8..b244ad54ff287 100644 --- a/packages/block-library/src/post-categories/index.js +++ b/packages/block-library/src/post-hierarchical-terms/index.js @@ -14,7 +14,7 @@ const { name } = metadata; export { metadata, name }; export const settings = { - title: __( 'Post Categories' ), + title: __( 'Post Hierarchical Terms' ), variations, edit, }; diff --git a/packages/block-library/src/post-categories/index.php b/packages/block-library/src/post-hierarchical-terms/index.php similarity index 66% rename from packages/block-library/src/post-categories/index.php rename to packages/block-library/src/post-hierarchical-terms/index.php index 25715acae7e32..73e23b5889ade 100644 --- a/packages/block-library/src/post-categories/index.php +++ b/packages/block-library/src/post-hierarchical-terms/index.php @@ -1,19 +1,19 @@ context['postId'] ) ) { return ''; } @@ -37,7 +37,7 @@ function render_block_core_post_categories( $attributes, $content, $block ) { return sprintf( '
%2$s
', - 'wp-block-post-categories' . esc_attr( $align_class_name ), + 'wp-block-post-hierarchical-terms' . esc_attr( $align_class_name ), $category_links ); } @@ -45,12 +45,12 @@ function render_block_core_post_categories( $attributes, $content, $block ) { /** * Registers the `core/post-categories` block on the server. */ -function register_block_core_post_categories() { +function register_block_core_post_hierarchical_terms() { register_block_type_from_metadata( - __DIR__ . '/post-categories', + __DIR__ . '/post-hierarchical-terms', array( - 'render_callback' => 'render_block_core_post_categories', + 'render_callback' => 'render_block_core_post_hierarchical_terms', ) ); } -add_action( 'init', 'register_block_core_post_categories' ); +add_action( 'init', 'register_block_core_post_hierarchical_terms' ); diff --git a/packages/block-library/src/post-categories/use-hierarchical-term-links.js b/packages/block-library/src/post-hierarchical-terms/use-hierarchical-term-links.js similarity index 100% rename from packages/block-library/src/post-categories/use-hierarchical-term-links.js rename to packages/block-library/src/post-hierarchical-terms/use-hierarchical-term-links.js diff --git a/packages/block-library/src/post-categories/variations.js b/packages/block-library/src/post-hierarchical-terms/variations.js similarity index 87% rename from packages/block-library/src/post-categories/variations.js rename to packages/block-library/src/post-hierarchical-terms/variations.js index 6f755877dce3e..8ff6dcc07f910 100644 --- a/packages/block-library/src/post-categories/variations.js +++ b/packages/block-library/src/post-hierarchical-terms/variations.js @@ -6,7 +6,7 @@ import { __ } from '@wordpress/i18n'; const variations = [ { name: 'category', - title: __( 'Categories' ), + title: __( 'Post Categories' ), icon: 'category', is_default: true, attributes: { term: 'category' }, diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.html b/packages/e2e-tests/fixtures/blocks/core__post-categories.html deleted file mode 100644 index b4bbf67e7b61a..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__post-categories.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html b/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html deleted file mode 100644 index b4bbf67e7b61a..0000000000000 --- a/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.html b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.html new file mode 100644 index 0000000000000..7614fa26add90 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.html @@ -0,0 +1 @@ + diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.json b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.json similarity index 76% rename from packages/e2e-tests/fixtures/blocks/core__post-categories.json rename to packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.json index 502199eb8a3d9..fd864d95242e3 100644 --- a/packages/e2e-tests/fixtures/blocks/core__post-categories.json +++ b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.json @@ -1,7 +1,7 @@ [ { "clientId": "_clientId_0", - "name": "core/post-categories", + "name": "core/post-hierarchical-terms", "isValid": true, "attributes": {}, "innerBlocks": [], diff --git a/packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.parsed.json similarity index 84% rename from packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json rename to packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.parsed.json index d284c12916466..468496fa95bc6 100644 --- a/packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json +++ b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.parsed.json @@ -1,6 +1,6 @@ [ { - "blockName": "core/post-categories", + "blockName": "core/post-hierarchical-terms", "attrs": {}, "innerBlocks": [], "innerHTML": "", diff --git a/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.serialized.html b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.serialized.html new file mode 100644 index 0000000000000..7614fa26add90 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-hierarchical-terms.serialized.html @@ -0,0 +1 @@ + From ead8eb823348cba3a86fefbbd2be17cd3e40938a Mon Sep 17 00:00:00 2001 From: Copons Date: Wed, 12 Aug 2020 12:25:34 +0100 Subject: [PATCH 8/9] Update the SSR --- .../src/post-hierarchical-terms/edit.js | 2 +- .../src/post-hierarchical-terms/index.php | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/post-hierarchical-terms/edit.js b/packages/block-library/src/post-hierarchical-terms/edit.js index ce0df119b1979..dbc4da3060844 100644 --- a/packages/block-library/src/post-hierarchical-terms/edit.js +++ b/packages/block-library/src/post-hierarchical-terms/edit.js @@ -86,7 +86,7 @@ export default function PostHierarchicalTermsEdit( { return ( - { __( 'Post Categories block: post not found.' ) } + { __( 'Post Hierarchical Terms block: post not found.' ) } ); diff --git a/packages/block-library/src/post-hierarchical-terms/index.php b/packages/block-library/src/post-hierarchical-terms/index.php index 73e23b5889ade..0a153b7958e0a 100644 --- a/packages/block-library/src/post-hierarchical-terms/index.php +++ b/packages/block-library/src/post-hierarchical-terms/index.php @@ -11,39 +11,39 @@ * @param array $attributes Block attributes. * @param string $content Block default content. * @param WP_Block $block Block instance. - * @return string Returns the filtered post categories for the current post wrapped inside "a" tags. + * @return string Returns the filtered post hierarchical terms for the current post wrapped inside "a" tags. */ function render_block_core_post_hierarchical_terms( $attributes, $content, $block ) { - if ( ! isset( $block->context['postId'] ) ) { + if ( ! isset( $block->context['postId'] ) || ! isset( $attributes['term'] ) ) { return ''; } - $post_categories = get_the_category( $block->context['postId'] ); - if ( empty( $post_categories ) ) { + $post_hierarchical_terms = get_the_terms( $block->context['postId'], $attributes['term'] ); + if ( empty( $post_hierarchical_terms ) ) { return ''; } $align_class_name = empty( $attributes['textAlign'] ) ? '' : ' ' . "has-text-align-{$attributes['textAlign']}"; - $category_links = ''; - foreach ( $post_categories as $category ) { - $category_links .= sprintf( + $terms_links = ''; + foreach ( $post_hierarchical_terms as $term ) { + $terms_links .= sprintf( '%2$s | ', - get_category_link( $category->term_id ), - esc_html( $category->name ) + get_term_link( $term->term_id ), + esc_html( $term->name ) ); } - $category_links = trim( $category_links, ' | ' ); + $terms_links = trim( $terms_links, ' | ' ); return sprintf( '
%2$s
', 'wp-block-post-hierarchical-terms' . esc_attr( $align_class_name ), - $category_links + $terms_links ); } /** - * Registers the `core/post-categories` block on the server. + * Registers the `core/post-hierarchical-terms` block on the server. */ function register_block_core_post_hierarchical_terms() { register_block_type_from_metadata( From 11a42f7a96acbbb62f27998772fa090459b7f5d1 Mon Sep 17 00:00:00 2001 From: Copons Date: Tue, 18 Aug 2020 13:59:11 +0100 Subject: [PATCH 9/9] Remove unnecessary block's CSS class --- packages/block-library/src/post-hierarchical-terms/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-hierarchical-terms/index.php b/packages/block-library/src/post-hierarchical-terms/index.php index 0a153b7958e0a..7f04a8da3a87b 100644 --- a/packages/block-library/src/post-hierarchical-terms/index.php +++ b/packages/block-library/src/post-hierarchical-terms/index.php @@ -37,7 +37,7 @@ function render_block_core_post_hierarchical_terms( $attributes, $content, $bloc return sprintf( '
%2$s
', - 'wp-block-post-hierarchical-terms' . esc_attr( $align_class_name ), + esc_attr( $align_class_name ), $terms_links ); }