-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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 Author block. #19576
Changes from all commits
16ac572
49eab59
225b79d
ae1861d
5ec16de
4fb15c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "core/post-author", | ||
"category": "layout" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEntityProp, useEntityId } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { sprintf, __ } from '@wordpress/i18n'; | ||
|
||
function PostAuthorDisplay() { | ||
const [ authorId ] = useEntityProp( 'postType', 'post', 'author' ); | ||
const author = useSelect( | ||
( select ) => select( 'core' ).getEntityRecord( 'root', 'user', authorId ), | ||
[ authorId ] | ||
); | ||
return author ? <address>{ sprintf( __( 'By %s' ), author.name ) }</address> : null; | ||
} | ||
|
||
export default function PostAuthorEdit() { | ||
if ( ! useEntityId( 'postType', 'post' ) ) { | ||
return 'Post Author Placeholder'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i18n? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
return <PostAuthorDisplay />; | ||
} |
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 Author' ), | ||
edit, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-author` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-author` block on the server. | ||
* | ||
* @return string Returns the filtered post author for the current post wrapped inside "h6" tags. | ||
*/ | ||
function render_block_core_post_author() { | ||
$post = gutenberg_get_post_from_context(); | ||
if ( ! $post ) { | ||
return ''; | ||
} | ||
// translators: %s: The author. | ||
return sprintf( __( '<address>By %s</address>' ), get_the_author() ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is causing a lint error on WP.com when attempting to update the plugin there:
which seems fair. @chriskmnds is working on a fix. If core contributors feel that a lint check this makes sense, is there any chance we can add one to CI? /cc @akirk There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this an issue? It would just make the code harder to read with the concatenations to what end? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion is to have translators only translate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It makes easier for translators because the html tag in that case will just be repeated. It doesn't make sense to allow translators to change the HTML tag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. Should I open a PR or are you already on it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a fix, but putting together the PR properly, testing, etc. will take a while (even for something simple as this). :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will test tomorrow, but feel free to review pls :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For posterity's sake, there is a follow-up task related to this conversation at: #19911 |
||
} | ||
|
||
/** | ||
* Registers the `core/post-author` block on the server. | ||
*/ | ||
function register_block_core_post_author() { | ||
register_block_type( | ||
'core/post-author', | ||
array( | ||
'render_callback' => 'render_block_core_post_author', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_post_author' ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-author /--> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-author", | ||
"isValid": true, | ||
"attributes": {}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-author", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n", | ||
"innerContent": [ | ||
"\n" | ||
] | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<!-- wp:post-author /--> |
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.
In slow connection, there's a jumpiness happening when loading the author. This should be solved with design somehow.
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.
We need loading block placeholders for these "async blocks."