Skip to content

Commit

Permalink
fix: prevent author information from displaying author block for unsu…
Browse files Browse the repository at this point in the history
…pported Custom Post Types & show notice to user
  • Loading branch information
sarthaknagoshe2002 committed Nov 19, 2024
1 parent f6e6e45 commit 7555d97
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
28 changes: 26 additions & 2 deletions packages/block-library/src/post-author-name/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ import {
InspectorControls,
useBlockProps,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { PanelBody, ToggleControl } from '@wordpress/components';
import { store as noticesStore } from '@wordpress/notices';
import { useEffect, useRef } from '@wordpress/element';

function PostAuthorNameEdit( {
isSelected,
context: { postType, postId },
attributes: { textAlign, isLink, linkTarget },
setAttributes,
} ) {
const { authorName } = useSelect(
const { createNotice } = useDispatch( noticesStore );
const noticeDisplayedRef = useRef( false );
const { authorName, supportsAuthor } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser } = select( coreStore );
const _authorId = getEditedEntityRecord(
Expand All @@ -33,11 +38,30 @@ function PostAuthorNameEdit( {

return {
authorName: _authorId ? getUser( _authorId ) : null,
supportsAuthor:
select( coreStore ).getPostType( postType )?.supports
?.author ?? false,
};
},
[ postType, postId ]
);

useEffect( () => {
// The extra `! noticeDisplayedRef.current` check avoids duplicate notices in development mode (React.StrictMode).
if ( ! supportsAuthor && ! noticeDisplayedRef.current && isSelected ) {
createNotice(
'warning',
__(
'The current post type does not support authors. The Post Author Name block will not be displayed.'
),
{
isDismissible: true,
}
);
noticeDisplayedRef.current = true;
}
}, [ supportsAuthor, createNotice, isSelected ] );

const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
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' ) ) {
return '';

Check failure on line 30 in packages/block-library/src/post-author-name/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
}

$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
25 changes: 24 additions & 1 deletion packages/block-library/src/post-author/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { useEffect, useRef } from '@wordpress/element';

const minimumUsersForCombobox = 25;

Expand All @@ -37,8 +39,10 @@ function PostAuthorEdit( {
attributes,
setAttributes,
} ) {
const { createNotice } = useDispatch( noticesStore );
const noticeDisplayedRef = useRef( false );
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 +56,9 @@ function PostAuthorEdit( {
authorId: _authorId,
authorDetails: _authorId ? getUser( _authorId ) : null,
authors: getUsers( AUTHORS_QUERY ),
supportsAuthor:
select( coreStore ).getPostType( postType )?.supports
?.author ?? false,
};
},
[ postType, postId ]
Expand All @@ -72,6 +79,22 @@ function PostAuthorEdit( {
} );
}

useEffect( () => {
// The extra `! noticeDisplayedRef.current` check avoids duplicate notices in development mode (React.StrictMode).
if ( ! supportsAuthor && ! noticeDisplayedRef.current && isSelected ) {
createNotice(
'warning',
__(
'The current post type does not support authors. The Post Author block will not be displayed.'
),
{
isDismissible: true,
}
);
noticeDisplayedRef.current = true;
}
}, [ supportsAuthor, createNotice, isSelected ] );

const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
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 '';

Check failure on line 30 in packages/block-library/src/post-author/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
}

Check failure on line 31 in packages/block-library/src/post-author/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Spaces must be used for mid-line alignment; tabs are not allowed

Check failure on line 31 in packages/block-library/src/post-author/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Whitespace found at end of line

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

0 comments on commit 7555d97

Please sign in to comment.