-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add a post categories block #22471
Closed
Closed
Add a post categories block #22471
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f06f38
Add a basic post categories block
carolinan cfc1c1e
Merge pull request #2 from WordPress/master
carolinan 4c9eea5
trying to fix merge conflict, to sync with master
carolinan 199aaf4
Merge pull request #4 from WordPress/master
carolinan b9cecb8
Merge pull request #13 from WordPress/master
carolinan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 ( | ||
<a key={categoryId } href={ category.link }> | ||
{ category.name } | ||
</a> | ||
); | ||
} ); | ||
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 <PostCategoriesDisplay />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-categories` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-categories` block on the server. | ||
* | ||
* @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. | ||
*/ | ||
function render_block_core_post_categories( $attributes, $content, $block ) { | ||
if ( ! isset( $block->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 .= '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a>' . ', '; | ||
} | ||
return sprintf( | ||
'<div class="%1$s"><span class="screen-reader-text">' . __( 'Categories' ) . '</span> %2$s</div>', | ||
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-categories /--> |
10 changes: 10 additions & 0 deletions
10
packages/e2e-tests/fixtures/blocks/core__post-categories.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-categories", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
18 changes: 18 additions & 0 deletions
18
packages/e2e-tests/fixtures/blocks/core__post-categories.parsed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-categories", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
packages/e2e-tests/fixtures/blocks/core__post-categories.serialized.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-categories /--> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the introduction of block context, the client-side implementation should also reference the context values as well, rather than via
useEntityId
anduseEntityProp
. At least in getting thepostId
andpostType
, it should largely amount to usingcontext
as a property of the edit function.(
postType
will need to be listed inblock.json
context
alongside the existingpostId
)To get the categories, it's a little more work using
core-data
entities to get the post's categories:See also for reference example: #22359 (specifically, #22359 (comment))
Related: #21665
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not been able to figure out how to get all taxonomies from core-data, or a named custom taxonomy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a list of all hierarchical taxonomies, but I don't know if I can use this core-data to check if a post uses that taxonomy.