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 author name block #36001

Merged
merged 13 commits into from Dec 23, 2021
1 change: 1 addition & 0 deletions lib/blocks.php
Expand Up @@ -51,6 +51,7 @@ function gutenberg_reregister_core_block_types() {
),
'block_names' => array(
'archives.php' => 'core/archives',
'author-name.php' => 'core/author-name',
'block.php' => 'core/block',
'calendar.php' => 'core/calendar',
'categories.php' => 'core/categories',
Expand Down
42 changes: 42 additions & 0 deletions packages/block-library/src/author-name/block.json
@@ -0,0 +1,42 @@
{
"apiVersion": 2,
"name": "core/author-name",
carolinan marked this conversation as resolved.
Show resolved Hide resolved
"title": "Author Name",
"category": "theme",
"description": "The author name.",
"textdomain": "default",
"attributes": {
"textAlign": {
"type": "string"
},
"isLink": {
"type": "boolean",
"default": false
},
"linkTarget": {
"type": "string",
"default": "_self"
}
},
"usesContext": [ "postType", "postId" ],
"supports": {
"html": false,
"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
}
}
}
95 changes: 95 additions & 0 deletions packages/block-library/src/author-name/edit.js
@@ -0,0 +1,95 @@
/**
* External dependencies
*/
import classnames from 'classnames';

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

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

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

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

const displayName = authorName?.name || __( 'Author Name' );

const displayAuthor = isLink ? (
<a
href="#author-pseudo-link"
onClick={ ( event ) => event.preventDefault() }
>
{ displayName }
</a>
) : (
displayName
);

return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Link settings' ) }>
<ToggleControl
label={ __( 'Link to author archive' ) }
onChange={ () => setAttributes( { isLink: ! isLink } ) }
checked={ isLink }
/>
{ isLink && (
<ToggleControl
label={ __( 'Open in new tab' ) }
onChange={ ( value ) =>
setAttributes( {
linkTarget: value ? '_blank' : '_self',
} )
}
checked={ linkTarget === '_blank' }
/>
) }
</PanelBody>
</InspectorControls>
<div { ...blockProps }> { displayAuthor } </div>
</>
);
}

export default AuthorNameEdit;
18 changes: 18 additions & 0 deletions packages/block-library/src/author-name/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,
};
49 changes: 49 additions & 0 deletions packages/block-library/src/author-name/index.php
@@ -0,0 +1,49 @@
<?php
/**
* Server-side rendering of the `core/author-name` block.
*
* @package WordPress
*/

/**
* Renders the `core/author-name` 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 author name block.
*/
function render_block_core_author_name( $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_name = get_the_author_meta( 'display_name', $author_id );
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";

if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$author_name = sprintf( '<a href="%1$s" target="%2$s" class="wp-block-author-name__link">%3$s</a>', get_author_posts_url( $author_id ), $attributes['linkTarget'], $author_name );
}

$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );

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

/**
* Registers the `core/author-name` block on the server.
*/
function register_block_core_author_name() {
register_block_type_from_metadata(
__DIR__ . '/author-name',
array(
'render_callback' => 'render_block_core_author_name',
)
);
}
add_action( 'init', 'register_block_core_author_name' );
2 changes: 2 additions & 0 deletions packages/block-library/src/index.js
Expand Up @@ -61,6 +61,7 @@ import * as tagCloud from './tag-cloud';
import * as classic from './freeform';
import * as socialLinks from './social-links';
import * as socialLink from './social-link';
import * as authorName from './author-name';

// Full Site Editing Blocks
import * as siteLogo from './site-logo';
Expand Down Expand Up @@ -188,6 +189,7 @@ export const __experimentalGetCoreBlocks = () => [
postExcerpt,
postFeaturedImage,
postTerms,
authorName,

logInOut,
];
Expand Down
1 change: 1 addition & 0 deletions test/integration/fixtures/blocks/core__author-name.html
@@ -0,0 +1 @@
<!-- wp:author-name /-->
13 changes: 13 additions & 0 deletions test/integration/fixtures/blocks/core__author-name.json
@@ -0,0 +1,13 @@
[
{
"clientId": "_clientId_0",
"name": "core/author-name",
"isValid": true,
"attributes": {
"isLink": false,
"linkTarget": "_self"
},
"innerBlocks": [],
"originalContent": ""
}
]
@@ -0,0 +1,9 @@
[
{
"blockName": "core/author-name",
"attrs": {},
"innerBlocks": [],
"innerHTML": "",
"innerContent": []
}
]
@@ -0,0 +1 @@
<!-- wp:author-name /-->