Skip to content

Commit

Permalink
Author blocks: Display nothing when a post type doesn't support the a…
Browse files Browse the repository at this point in the history
…uthor (#67136)


Co-authored-by: sarthaknagoshe2002 <sarthaknagoshe2002@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: fabiankaegy <fabiankaegy@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
Co-authored-by: dhruvang21 <dhruvang21@git.wordpress.org>
Co-authored-by: groenroos <groenroos@git.wordpress.org>
  • Loading branch information
7 people authored Dec 18, 2024
1 parent b71da13 commit e9bd750
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
21 changes: 17 additions & 4 deletions packages/block-library/src/post-author-name/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { PanelBody, ToggleControl } from '@wordpress/components';

Expand All @@ -22,9 +22,10 @@ function PostAuthorNameEdit( {
attributes: { textAlign, isLink, linkTarget },
setAttributes,
} ) {
const { authorName } = useSelect(
const { authorName, supportsAuthor } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser } = select( coreStore );
const { getEditedEntityRecord, getUser, getPostType } =
select( coreStore );
const _authorId = getEditedEntityRecord(
'postType',
postType,
Expand All @@ -33,6 +34,8 @@ function PostAuthorNameEdit( {

return {
authorName: _authorId ? getUser( _authorId ) : null,
supportsAuthor:
getPostType( postType )?.supports?.author ?? false,
};
},
[ postType, postId ]
Expand Down Expand Up @@ -90,7 +93,17 @@ function PostAuthorNameEdit( {
) }
</PanelBody>
</InspectorControls>
<div { ...blockProps }> { displayAuthor } </div>
<div { ...blockProps }>
{ supportsAuthor
? displayAuthor
: sprintf(
// translators: %s: Name of the post type e.g: "post".
__(
'This post type (%s) does not support the author.'
),
postType
) }
</div>
</>
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-author-name/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function render_block_core_post_author_name( $attributes, $content, $block ) {
return '';
}

if ( ! post_type_supports( $block->context['postType'], 'author' ) ) {
return '';
}

$author_name = get_the_author_meta( 'display_name', $author_id );
if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$author_name = sprintf( '<a href="%1$s" target="%2$s" class="wp-block-post-author-name__link">%3$s</a>', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $author_name );
Expand Down
20 changes: 17 additions & 3 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
__experimentalVStack as VStack,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';

const minimumUsersForCombobox = 25;
Expand All @@ -38,9 +38,9 @@ function PostAuthorEdit( {
setAttributes,
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const { authorId, authorDetails, authors } = useSelect(
const { authorId, authorDetails, authors, supportsAuthor } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser, getUsers } =
const { getEditedEntityRecord, getUser, getUsers, getPostType } =
select( coreStore );
const _authorId = getEditedEntityRecord(
'postType',
Expand All @@ -52,6 +52,8 @@ function PostAuthorEdit( {
authorId: _authorId,
authorDetails: _authorId ? getUser( _authorId ) : null,
authors: getUsers( AUTHORS_QUERY ),
supportsAuthor:
getPostType( postType )?.supports?.author ?? false,
};
},
[ postType, postId ]
Expand Down Expand Up @@ -97,6 +99,18 @@ function PostAuthorEdit( {
const showAuthorControl =
!! postId && ! isDescendentOfQueryLoop && authorOptions.length > 0;

if ( ! supportsAuthor ) {
return (
<div { ...blockProps }>
{ sprintf(
// translators: %s: Name of the post type e.g: "post".
__( 'This post type (%s) does not support the author.' ),
postType
) }
</div>
);
}

return (
<>
<InspectorControls>
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/post-author/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function render_block_core_post_author( $attributes, $content, $block ) {
return '';
}

if ( ! post_type_supports( $block->context['postType'], 'author' ) ) {
return '';
}

$avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar(
$author_id,
$attributes['avatarSize']
Expand Down

1 comment on commit e9bd750

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in e9bd750.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12398284128
📝 Reported issues:

Please sign in to comment.