Skip to content
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 post author biography block #38269

Merged
merged 3 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,15 @@ Display post author details such as name, avatar, and bio. ([Source](https://git
- **Supports:** color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** avatarSize, byline, showAvatar, showBio, textAlign

## Post Author Biography

The author biography. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/post-author-biography))

- **Name:** core/post-author-biography
- **Category:** theme
- **Supports:** color (background, gradients, link, text), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** textAlign

## Post Author Name

The author name. ([Source](https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/post-author-name))
Expand Down
1 change: 1 addition & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function gutenberg_reregister_core_block_types() {
'pattern.php' => 'core/pattern',
'post-author.php' => 'core/post-author',
'post-author-name.php' => 'core/post-author-name',
'post-author-biography.php' => 'core/post-author-biography',
'post-comment.php' => 'core/post-comment',
'post-comments.php' => 'core/post-comments',
'post-comments-count.php' => 'core/post-comments-count',
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import * as pageList from './page-list';
import * as paragraph from './paragraph';
import * as postAuthor from './post-author';
import * as postAuthorName from './post-author-name';
import * as postAuthorBiography from './post-author-biography';
import * as postComment from './post-comment';
import * as postComments from './post-comments';
import * as postCommentsCount from './post-comments-count';
Expand Down Expand Up @@ -204,6 +205,7 @@ export const __experimentalGetCoreBlocks = () => [
termDescription,
queryTitle,
postAuthorName,
postAuthorBiography,
];

/**
Expand Down
37 changes: 37 additions & 0 deletions packages/block-library/src/post-author-biography/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "core/post-author-biography",
"title": "Post Author Biography",
"category": "theme",
"description": "The author biography.",
"textdomain": "default",
"attributes": {
"textAlign": {
"type": "string"
}
},
"usesContext": [ "postType", "postId" ],
"supports": {
"spacing": {
"margin": true,
"padding": true
},
"color": {
"gradients": true,
"link": true
},
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
}
}
63 changes: 63 additions & 0 deletions packages/block-library/src/post-author-biography/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';

function PostAuthorBiographyEdit( {
context: { postType, postId },
attributes: { textAlign },
setAttributes,
} ) {
const { authorDetails } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser } = select( coreStore );
const _authorId = getEditedEntityRecord(
'postType',
postType,
postId
)?.author;

return {
authorDetails: _authorId ? getUser( _authorId ) : null,
};
},
[ postType, postId ]
);

const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

const displayAuthorBiography =
authorDetails?.description || __( 'Author Biography' );

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<div { ...blockProps }> { displayAuthorBiography } </div>
</>
);
}

export default PostAuthorBiographyEdit;
18 changes: 18 additions & 0 deletions packages/block-library/src/post-author-biography/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

/**
* WordPress dependencies
*/
import { postAuthor as icon } from '@wordpress/icons';

const { name } = metadata;
export { metadata, name };

export const settings = {
icon,
edit,
};
48 changes: 48 additions & 0 deletions packages/block-library/src/post-author-biography/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Server-side rendering of the `core/post-author-biography` block.
*
* @package WordPress
*/

/**
* Renders the `core/post-author-biography` 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 rendered post author biography block.
*/
function render_block_core_post_author_biography( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
}

$author_id = get_post_field( 'post_author', $block->context['postId'] );
if ( empty( $author_id ) ) {
return '';
}

$author_biography = get_the_author_meta( 'description', $author_id );
if ( empty( $author_biography ) ) {
return '';
}

$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );

return sprintf( '<div %1$s>', $wrapper_attributes ) . $author_biography . '</div>';
}

/**
* Registers the `core/post-author-biography` block on the server.
*/
function register_block_core_post_author_biography() {
register_block_type_from_metadata(
__DIR__ . '/post-author-biography',
array(
'render_callback' => 'render_block_core_post_author_biography',
)
);
}
add_action( 'init', 'register_block_core_post_author_biography' );
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-author-biography /-->
10 changes: 10 additions & 0 deletions test/integration/fixtures/blocks/core__post-author-biography.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"clientId": "_clientId_0",
"name": "core/post-author-biography",
"isValid": true,
"attributes": {},
"innerBlocks": [],
"originalContent": ""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"blockName": "core/post-author-biography",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- wp:post-author-biography /-->