From 3c9efa89b24062b0d892ddcdd62c69652b87a7be Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Thu, 27 Jan 2022 08:19:26 +0100 Subject: [PATCH 1/3] Add post author biography block --- docs/reference-guides/core-blocks.md | 9 +++ lib/blocks.php | 1 + packages/block-library/src/index.js | 2 + .../src/post-author-biography/block.json | 37 +++++++++++ .../src/post-author-biography/edit.js | 63 +++++++++++++++++++ .../src/post-author-biography/index.js | 18 ++++++ .../src/post-author-biography/index.php | 44 +++++++++++++ .../blocks/core__post-author-biography.html | 1 + .../blocks/core__post-author-biography.json | 10 +++ .../core__post-author-biography.parsed.json | 9 +++ ...ore__post-author-biography.serialized.html | 1 + 11 files changed, 195 insertions(+) create mode 100644 packages/block-library/src/post-author-biography/block.json create mode 100644 packages/block-library/src/post-author-biography/edit.js create mode 100644 packages/block-library/src/post-author-biography/index.js create mode 100644 packages/block-library/src/post-author-biography/index.php create mode 100644 test/integration/fixtures/blocks/core__post-author-biography.html create mode 100644 test/integration/fixtures/blocks/core__post-author-biography.json create mode 100644 test/integration/fixtures/blocks/core__post-author-biography.parsed.json create mode 100644 test/integration/fixtures/blocks/core__post-author-biography.serialized.html diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index f121f20204fd1..fa583509f6210 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -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)) diff --git a/lib/blocks.php b/lib/blocks.php index 0094ef32230b9..ea81083f1ae03 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -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', diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index 037eaa0f3df70..032e77d426d30 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -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'; @@ -204,6 +205,7 @@ export const __experimentalGetCoreBlocks = () => [ termDescription, queryTitle, postAuthorName, + postAuthorBiography, ]; /** diff --git a/packages/block-library/src/post-author-biography/block.json b/packages/block-library/src/post-author-biography/block.json new file mode 100644 index 0000000000000..fe288f9e7828b --- /dev/null +++ b/packages/block-library/src/post-author-biography/block.json @@ -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 + } + } + } +} diff --git a/packages/block-library/src/post-author-biography/edit.js b/packages/block-library/src/post-author-biography/edit.js new file mode 100644 index 0000000000000..a2a96423faa06 --- /dev/null +++ b/packages/block-library/src/post-author-biography/edit.js @@ -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 ( + <> + + { + setAttributes( { textAlign: nextAlign } ); + } } + /> + +
{ displayAuthorBiography }
+ + ); +} + +export default PostAuthorBiographyEdit; diff --git a/packages/block-library/src/post-author-biography/index.js b/packages/block-library/src/post-author-biography/index.js new file mode 100644 index 0000000000000..68fb2c2f737c2 --- /dev/null +++ b/packages/block-library/src/post-author-biography/index.js @@ -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, +}; diff --git a/packages/block-library/src/post-author-biography/index.php b/packages/block-library/src/post-author-biography/index.php new file mode 100644 index 0000000000000..3733fe6b1d99c --- /dev/null +++ b/packages/block-library/src/post-author-biography/index.php @@ -0,0 +1,44 @@ +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 ); + $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; + $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); + + return sprintf( '
', $wrapper_attributes ) . $author_biography . '
'; +} + +/** + * 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' ); diff --git a/test/integration/fixtures/blocks/core__post-author-biography.html b/test/integration/fixtures/blocks/core__post-author-biography.html new file mode 100644 index 0000000000000..d30c301e44267 --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-author-biography.html @@ -0,0 +1 @@ + diff --git a/test/integration/fixtures/blocks/core__post-author-biography.json b/test/integration/fixtures/blocks/core__post-author-biography.json new file mode 100644 index 0000000000000..a24d4ca091e4a --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-author-biography.json @@ -0,0 +1,10 @@ +[ + { + "clientId": "_clientId_0", + "name": "core/post-author-biography", + "isValid": true, + "attributes": {}, + "innerBlocks": [], + "originalContent": "" + } +] diff --git a/test/integration/fixtures/blocks/core__post-author-biography.parsed.json b/test/integration/fixtures/blocks/core__post-author-biography.parsed.json new file mode 100644 index 0000000000000..3b6680a3ce95e --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-author-biography.parsed.json @@ -0,0 +1,9 @@ +[ + { + "blockName": "core/post-author-biography", + "attrs": {}, + "innerBlocks": [], + "innerHTML": "", + "innerContent": [] + } +] diff --git a/test/integration/fixtures/blocks/core__post-author-biography.serialized.html b/test/integration/fixtures/blocks/core__post-author-biography.serialized.html new file mode 100644 index 0000000000000..d30c301e44267 --- /dev/null +++ b/test/integration/fixtures/blocks/core__post-author-biography.serialized.html @@ -0,0 +1 @@ + From 4ff5225dba9222bc1b770a2a22c2ef2bae6166c8 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Thu, 27 Jan 2022 08:40:53 +0100 Subject: [PATCH 2/3] Don't print markup if there is no user biography. --- packages/block-library/src/post-author-biography/index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/block-library/src/post-author-biography/index.php b/packages/block-library/src/post-author-biography/index.php index 3733fe6b1d99c..d72df64217992 100644 --- a/packages/block-library/src/post-author-biography/index.php +++ b/packages/block-library/src/post-author-biography/index.php @@ -24,6 +24,10 @@ function render_block_core_post_author_biography( $attributes, $content, $block } $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 ) ); From 18b9810e767d56fc12065738ecb995069fed2718 Mon Sep 17 00:00:00 2001 From: Carolina Nymark Date: Thu, 27 Jan 2022 08:59:44 +0100 Subject: [PATCH 3/3] Linting --- packages/block-library/src/post-author-biography/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-author-biography/index.php b/packages/block-library/src/post-author-biography/index.php index d72df64217992..3ea6ecbd9b46a 100644 --- a/packages/block-library/src/post-author-biography/index.php +++ b/packages/block-library/src/post-author-biography/index.php @@ -23,7 +23,7 @@ function render_block_core_post_author_biography( $attributes, $content, $block return ''; } - $author_biography = get_the_author_meta( 'description', $author_id ); + $author_biography = get_the_author_meta( 'description', $author_id ); if ( empty( $author_biography ) ) { return ''; }