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

Fix author information leakage by author blocks for Custom Post Types without author support & display notice to user #67136

Merged
merged 7 commits into from
Dec 18, 2024
15 changes: 13 additions & 2 deletions packages/block-library/src/post-author-name/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function PostAuthorNameEdit( {
attributes: { textAlign, isLink, linkTarget },
setAttributes,
} ) {
const { authorName } = useSelect(
const { authorName, supportsAuthor } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser } = select( coreStore );
const _authorId = getEditedEntityRecord(
Expand All @@ -33,6 +33,9 @@ function PostAuthorNameEdit( {

return {
authorName: _authorId ? getUser( _authorId ) : null,
supportsAuthor:
select( coreStore ).getPostType( postType )?.supports
?.author ?? false,
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
};
},
[ postType, postId ]
Expand Down Expand Up @@ -90,7 +93,15 @@ function PostAuthorNameEdit( {
) }
</PanelBody>
</InspectorControls>
<div { ...blockProps }> { displayAuthor } </div>
{ supportsAuthor ? (
<div { ...blockProps }> { displayAuthor } </div>
) : (
<div { ...blockProps }>
{ __(
'The current post type does not support authors. The Post Author Name block will not be displayed.'
) }
</div>
) }
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
</>
);
}
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( get_post_type( $block->context['postId'] ), 'author' ) ) {
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
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
105 changes: 59 additions & 46 deletions packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function PostAuthorEdit( {
setAttributes,
} ) {
const isDescendentOfQueryLoop = Number.isFinite( queryId );
const { authorId, authorDetails, authors } = useSelect(
const { authorId, authorDetails, authors, supportsAuthor } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser, getUsers } =
select( coreStore );
Expand All @@ -52,6 +52,9 @@ function PostAuthorEdit( {
authorId: _authorId,
authorDetails: _authorId ? getUser( _authorId ) : null,
authors: getUsers( AUTHORS_QUERY ),
supportsAuthor:
select( coreStore ).getPostType( postType )?.supports
?.author ?? false,
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
};
},
[ postType, postId ]
Expand Down Expand Up @@ -189,55 +192,65 @@ function PostAuthorEdit( {
/>
</BlockControls>

<div { ...blockProps }>
{ showAvatar && authorDetails?.avatar_urls && (
<div className="wp-block-post-author__avatar">
<img
width={ attributes.avatarSize }
src={
authorDetails.avatar_urls[
attributes.avatarSize
]
}
alt={ authorDetails.name }
/>
</div>
) }
<div className="wp-block-post-author__content">
{ ( ! RichText.isEmpty( byline ) || isSelected ) && (
<RichText
identifier="byline"
className="wp-block-post-author__byline"
aria-label={ __( 'Post author byline text' ) }
placeholder={ __( 'Write byline…' ) }
value={ byline }
onChange={ ( value ) =>
setAttributes( { byline: value } )
}
/>
{ supportsAuthor ? (
<div { ...blockProps }>
{ showAvatar && authorDetails?.avatar_urls && (
<div className="wp-block-post-author__avatar">
<img
width={ attributes.avatarSize }
src={
authorDetails.avatar_urls[
attributes.avatarSize
]
}
alt={ authorDetails.name }
/>
</div>
) }
<p className="wp-block-post-author__name">
{ isLink ? (
<a
href="#post-author-pseudo-link"
onClick={ ( event ) => event.preventDefault() }
>
{ authorName }
</a>
) : (
authorName
<div className="wp-block-post-author__content">
{ ( ! RichText.isEmpty( byline ) || isSelected ) && (
<RichText
identifier="byline"
className="wp-block-post-author__byline"
aria-label={ __( 'Post author byline text' ) }
placeholder={ __( 'Write byline…' ) }
value={ byline }
onChange={ ( value ) =>
setAttributes( { byline: value } )
}
/>
) }
</p>
{ showBio && (
<p
className="wp-block-post-author__bio"
dangerouslySetInnerHTML={ {
__html: authorDetails?.description,
} }
/>
<p className="wp-block-post-author__name">
{ isLink ? (
<a
href="#post-author-pseudo-link"
onClick={ ( event ) =>
event.preventDefault()
}
>
{ authorName }
</a>
) : (
authorName
) }
</p>
{ showBio && (
<p
className="wp-block-post-author__bio"
dangerouslySetInnerHTML={ {
__html: authorDetails?.description,
} }
/>
) }
</div>
</div>
) : (
<div { ...blockProps }>
{ __(
'The current post type does not support authors. The Post Author block will not be displayed.'
sarthaknagoshe2002 marked this conversation as resolved.
Show resolved Hide resolved
) }
</div>
</div>
) }
</>
);
}
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( get_post_type( $block->context['postId'] ), 'author' ) ) {
return '';
}

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