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
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
Loading