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 (WordPress#67136)


Co-authored-by: sarthaknagoshe2002 <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: jameskoster <[email protected]>
Co-authored-by: dhruvang21 <[email protected]>
Co-authored-by: groenroos <[email protected]>
  • 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

0 comments on commit e9bd750

Please sign in to comment.