Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add author name block * linting and fixtures * Update packages/block-library/src/author-name/index.php * change order... * Add author archive link option * Delete style.scss remove the stylesheet * update fixtures with attributes * fix spacing * Rename the block to post author name * Update fixtures * try to add the block to the docs
- Loading branch information
Showing
11 changed files
with
241 additions
and
0 deletions.
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
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,43 @@ | ||
{ | ||
"$schema": "https://schemas.wp.org/trunk/block.json", | ||
"apiVersion": 2, | ||
"name": "core/post-author-name", | ||
"title": "Post 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 | ||
} | ||
} | ||
} |
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,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 PostAuthorNameEdit( { | ||
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 PostAuthorNameEdit; |
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 @@ | ||
/** | ||
* 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, | ||
}; |
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,49 @@ | ||
<?php | ||
/** | ||
* Server-side rendering of the `core/post-author-name` block. | ||
* | ||
* @package WordPress | ||
*/ | ||
|
||
/** | ||
* Renders the `core/post-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 post author name block. | ||
*/ | ||
function render_block_core_post_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/post-author-name` block on the server. | ||
*/ | ||
function register_block_core_post_author_name() { | ||
register_block_type_from_metadata( | ||
__DIR__ . '/post-author-name', | ||
array( | ||
'render_callback' => 'render_block_core_post_author_name', | ||
) | ||
); | ||
} | ||
add_action( 'init', 'register_block_core_post_author_name' ); |
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-author-name /--> |
13 changes: 13 additions & 0 deletions
13
test/integration/fixtures/blocks/core__post-author-name.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,13 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "core/post-author-name", | ||
"isValid": true, | ||
"attributes": { | ||
"isLink": false, | ||
"linkTarget": "_self" | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "" | ||
} | ||
] |
9 changes: 9 additions & 0 deletions
9
test/integration/fixtures/blocks/core__post-author-name.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,9 @@ | ||
[ | ||
{ | ||
"blockName": "core/post-author-name", | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "", | ||
"innerContent": [] | ||
} | ||
] |
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/blocks/core__post-author-name.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-author-name /--> |