From 7f06f38c870b21ba43a7ac6d0847bcda8c296409 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Tue, 19 May 2020 18:49:13 +0200 Subject: [PATCH 1/2] Add a basic post categories block --- lib/blocks.php | 1 + packages/block-library/src/index.js | 1 + .../src/post-categories/block.json | 20 +++++++ .../block-library/src/post-categories/edit.js | 42 ++++++++++++++ .../src/post-categories/index.js | 25 ++++++++ .../src/post-categories/index.php | 57 +++++++++++++++++++ .../blocks/core__post-categories.html | 1 + .../blocks/core__post-categories.json | 10 ++++ .../blocks/core__post-categories.parsed.json | 18 ++++++ .../core__post-categories.serialized.html | 1 + 10 files changed, 176 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 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/lib/blocks.php b/lib/blocks.php index cdbc41fcdf0e91..54846e513c58be 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -52,6 +52,7 @@ function gutenberg_reregister_core_block_types() { 'query.php' => 'core/query', 'query-loop.php' => 'core/query-loop', 'query-pagination.php' => 'core/query-pagination', + 'post-categories.php' => 'core/post-categories', ) ); } diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 0851b0384db1ed..a73ffd3e669951 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -79,6 +79,7 @@ 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'; +import * as postCategories from './post-categories'; /** * Function to register an individual block. 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 00000000000000..731b2e370a1804 --- /dev/null +++ b/packages/block-library/src/post-categories/block.json @@ -0,0 +1,20 @@ +{ + "name": "core/post-categories", + "category": "layout", + "context": [ "postId" ], + "attributes": { + "align": { + "type": "string", + "enum": [ + "left", + "center", + "right", + "wide", + "full" + ] + }, + "className": { + "type": "string" + } + } +} 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 00000000000000..1c59552a0aa39e --- /dev/null +++ b/packages/block-library/src/post-categories/edit.js @@ -0,0 +1,42 @@ +/** + * WordPress dependencies + */ +import { useEntityProp, useEntityId } from '@wordpress/core-data'; +import { useSelect } from '@wordpress/data'; +import { __ } from '@wordpress/i18n'; + +function PostCategoriesDisplay() { + const [ categories ] = useEntityProp( 'postType', 'post', 'categories' ); + const categoryLinks = useSelect( + ( select ) => { + const { getEntityRecord } = select( 'core' ); + let loaded = true; + const links = categories.map( ( categoryId ) => { + const category = getEntityRecord('taxonomy', 'category', categoryId ); + if ( ! category ) { + return ( loaded = false ); + } + return ( + + { category.name } + + ); + } ); + return loaded && links; + }, + [ categories ] + ); + return ( + categoryLinks && + ( categoryLinks.length === 0 + ? __( 'No categories.' ) + : categoryLinks.reduce( ( prev, curr ) => [ prev, ', ', curr ] ) ) + ); +} + +export default function PostCategoriesEdit() { + if ( ! useEntityId( 'postType', 'post' ) ) { + return 'Post Categories Placeholder'; + } + return ; +} 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 00000000000000..4ed37d3394ddd0 --- /dev/null +++ b/packages/block-library/src/post-categories/index.js @@ -0,0 +1,25 @@ +/** + * WordPress dependencies + */ +import { category as icon } from '@wordpress/icons'; +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' ), + description: __('Display a list of categories for this post.'), + icon, + supports: { + align: true, + }, + 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 00000000000000..07698fe86d8439 --- /dev/null +++ b/packages/block-library/src/post-categories/index.php @@ -0,0 +1,57 @@ +context['postId'] ) ) { + return ''; + } + + $class = 'wp-block-post-categories'; + + if ( isset( $attributes['align'] ) ) { + $class .= " align{$attributes['align']}"; + } + + if ( isset( $attributes['className'] ) ) { + $class .= " {$attributes['className']}"; + } + + $post_categories = get_the_category( $block->context['postId'] ); + if ( ! empty( $post_categories ) ) { + $output = ''; + foreach ( $post_categories as $category ) { + $output .= '' . $category->name . '' . ', '; + } + return sprintf( + '
' . __( 'Categories' ) . ' %2$s
', + esc_attr( $class ), + trim( $output, ', ' ) + ); + + } +} + +/** + * 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' ); 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 00000000000000..b4bbf67e7b61a5 --- /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 00000000000000..502199eb8a3d9d --- /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 00000000000000..d284c129164663 --- /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 00000000000000..b4bbf67e7b61a5 --- /dev/null +++ b/packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html @@ -0,0 +1 @@ + From 4c9eea5a96b7e7286b2fb14bcffcf5daef60d132 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Sat, 30 May 2020 04:59:06 +0200 Subject: [PATCH 2/2] trying to fix merge conflict, to sync with master --- lib/blocks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/blocks.php b/lib/blocks.php index 54846e513c58be..cdbc41fcdf0e91 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -52,7 +52,6 @@ function gutenberg_reregister_core_block_types() { 'query.php' => 'core/query', 'query-loop.php' => 'core/query-loop', 'query-pagination.php' => 'core/query-pagination', - 'post-categories.php' => 'core/post-categories', ) ); }