From 7555d97033212a9d94f2d70fb7d6ba2b3b9e5fa8 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Wed, 20 Nov 2024 01:15:02 +0530 Subject: [PATCH 1/6] fix: prevent author information from displaying author block for unsupported Custom Post Types & show notice to user --- .../src/post-author-name/edit.js | 28 +++++++++++++++++-- .../src/post-author-name/index.php | 4 +++ .../block-library/src/post-author/edit.js | 25 ++++++++++++++++- .../block-library/src/post-author/index.php | 4 +++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-author-name/edit.js b/packages/block-library/src/post-author-name/edit.js index b4afb9a9799498..82ca505cb28318 100644 --- a/packages/block-library/src/post-author-name/edit.js +++ b/packages/block-library/src/post-author-name/edit.js @@ -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( @@ -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, diff --git a/packages/block-library/src/post-author-name/index.php b/packages/block-library/src/post-author-name/index.php index effc83962a3547..1228d7efda6044 100644 --- a/packages/block-library/src/post-author-name/index.php +++ b/packages/block-library/src/post-author-name/index.php @@ -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 ''; + } + $author_name = get_the_author_meta( 'display_name', $author_id ); if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) { $author_name = sprintf( '%3$s', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $author_name ); diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js index 6186b0d052e8aa..5e96046970429e 100644 --- a/packages/block-library/src/post-author/edit.js +++ b/packages/block-library/src/post-author/edit.js @@ -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; @@ -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 ); @@ -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 ] @@ -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, diff --git a/packages/block-library/src/post-author/index.php b/packages/block-library/src/post-author/index.php index faf894d997d732..d6b240da50209c 100644 --- a/packages/block-library/src/post-author/index.php +++ b/packages/block-library/src/post-author/index.php @@ -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'] From dc2e0671aef7e28c07641ce82605882c6f7ab644 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Wed, 20 Nov 2024 01:44:37 +0530 Subject: [PATCH 2/6] fix: PHP coding standards --- packages/block-library/src/post-author-name/index.php | 2 +- packages/block-library/src/post-author/index.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/post-author-name/index.php b/packages/block-library/src/post-author-name/index.php index 1228d7efda6044..367ba2da28eac4 100644 --- a/packages/block-library/src/post-author-name/index.php +++ b/packages/block-library/src/post-author-name/index.php @@ -27,7 +27,7 @@ function render_block_core_post_author_name( $attributes, $content, $block ) { } if ( ! post_type_supports( get_post_type( $block->context['postId'] ), 'author' ) ) { - return ''; + return ''; } $author_name = get_the_author_meta( 'display_name', $author_id ); diff --git a/packages/block-library/src/post-author/index.php b/packages/block-library/src/post-author/index.php index d6b240da50209c..ae902e8a02c49d 100644 --- a/packages/block-library/src/post-author/index.php +++ b/packages/block-library/src/post-author/index.php @@ -27,8 +27,8 @@ function render_block_core_post_author( $attributes, $content, $block ) { } if ( ! post_type_supports( get_post_type( $block->context['postId'] ), 'author' ) ) { - return ''; - } + return ''; + } $avatar = ! empty( $attributes['avatarSize'] ) ? get_avatar( $author_id, From c506175371a0e4eb6a2245e66152a9c214410794 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Thu, 21 Nov 2024 00:35:29 +0530 Subject: [PATCH 3/6] fix: render static text warning instead of editor notice --- .../src/post-author-name/edit.js | 33 ++--- .../block-library/src/post-author/edit.js | 120 ++++++++---------- 2 files changed, 65 insertions(+), 88 deletions(-) diff --git a/packages/block-library/src/post-author-name/edit.js b/packages/block-library/src/post-author-name/edit.js index 82ca505cb28318..cab923a4cb2c9f 100644 --- a/packages/block-library/src/post-author-name/edit.js +++ b/packages/block-library/src/post-author-name/edit.js @@ -12,21 +12,16 @@ import { InspectorControls, useBlockProps, } from '@wordpress/block-editor'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useSelect } 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 { createNotice } = useDispatch( noticesStore ); - const noticeDisplayedRef = useRef( false ); const { authorName, supportsAuthor } = useSelect( ( select ) => { const { getEditedEntityRecord, getUser } = select( coreStore ); @@ -46,22 +41,6 @@ function PostAuthorNameEdit( { [ 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, @@ -114,7 +93,15 @@ function PostAuthorNameEdit( { ) } -
{ displayAuthor }
+ { supportsAuthor ? ( +
{ displayAuthor }
+ ) : ( +
+ { __( + 'The current post type does not support authors. The Post Author Name block will not be displayed.' + ) } +
+ ) } ); } diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js index 5e96046970429e..41addc2b17adea 100644 --- a/packages/block-library/src/post-author/edit.js +++ b/packages/block-library/src/post-author/edit.js @@ -23,8 +23,6 @@ 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; @@ -39,8 +37,6 @@ function PostAuthorEdit( { attributes, setAttributes, } ) { - const { createNotice } = useDispatch( noticesStore ); - const noticeDisplayedRef = useRef( false ); const isDescendentOfQueryLoop = Number.isFinite( queryId ); const { authorId, authorDetails, authors, supportsAuthor } = useSelect( ( select ) => { @@ -79,22 +75,6 @@ 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, @@ -212,55 +192,65 @@ function PostAuthorEdit( { /> -
- { showAvatar && authorDetails?.avatar_urls && ( -
- { -
- ) } -
- { ( ! RichText.isEmpty( byline ) || isSelected ) && ( - - setAttributes( { byline: value } ) - } - /> + { supportsAuthor ? ( +
+ { showAvatar && authorDetails?.avatar_urls && ( +
+ { +
) } -

- { isLink ? ( - event.preventDefault() } - > - { authorName } - - ) : ( - authorName +

+ { ( ! RichText.isEmpty( byline ) || isSelected ) && ( + + setAttributes( { byline: value } ) + } + /> ) } -

- { showBio && ( -

+

+ { isLink ? ( + + event.preventDefault() + } + > + { authorName } + + ) : ( + authorName + ) } +

+ { showBio && ( +

+ ) } +

+
+ ) : ( +
+ { __( + 'The current post type does not support authors. The Post Author block will not be displayed.' ) }
-
+ ) } ); } From 4123cb07690e618dc01af2bb1495aae3039a1182 Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Wed, 18 Dec 2024 02:15:14 +0530 Subject: [PATCH 4/6] Fix: optimize conditional rendering & address feedbacks --- .../src/post-author-name/edit.js | 28 +++-- .../src/post-author-name/index.php | 2 +- .../block-library/src/post-author/edit.js | 119 +++++++++--------- .../block-library/src/post-author/index.php | 2 +- 4 files changed, 77 insertions(+), 74 deletions(-) diff --git a/packages/block-library/src/post-author-name/edit.js b/packages/block-library/src/post-author-name/edit.js index cab923a4cb2c9f..1bd88d51f07539 100644 --- a/packages/block-library/src/post-author-name/edit.js +++ b/packages/block-library/src/post-author-name/edit.js @@ -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'; @@ -24,7 +24,8 @@ function PostAuthorNameEdit( { } ) { const { authorName, supportsAuthor } = useSelect( ( select ) => { - const { getEditedEntityRecord, getUser } = select( coreStore ); + const { getEditedEntityRecord, getUser, getPostType } = + select( coreStore ); const _authorId = getEditedEntityRecord( 'postType', postType, @@ -34,8 +35,7 @@ function PostAuthorNameEdit( { return { authorName: _authorId ? getUser( _authorId ) : null, supportsAuthor: - select( coreStore ).getPostType( postType )?.supports - ?.author ?? false, + getPostType( postType )?.supports?.author ?? false, }; }, [ postType, postId ] @@ -93,15 +93,17 @@ function PostAuthorNameEdit( { ) } - { supportsAuthor ? ( -
{ displayAuthor }
- ) : ( -
- { __( - 'The current post type does not support authors. The Post Author Name block will not be displayed.' - ) } -
- ) } +
+ { supportsAuthor + ? displayAuthor + : sprintf( + // Translators: %s is replaced with the name of the post type. + __( + 'This post type (%s) does not support Authors.' + ), + postType + ) } +
); } diff --git a/packages/block-library/src/post-author-name/index.php b/packages/block-library/src/post-author-name/index.php index 367ba2da28eac4..243d78ca70129e 100644 --- a/packages/block-library/src/post-author-name/index.php +++ b/packages/block-library/src/post-author-name/index.php @@ -26,7 +26,7 @@ function render_block_core_post_author_name( $attributes, $content, $block ) { return ''; } - if ( ! post_type_supports( get_post_type( $block->context['postId'] ), 'author' ) ) { + if ( ! post_type_supports( $block->context['postType'], 'author' ) ) { return ''; } diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js index 41addc2b17adea..26d568c99b5c0f 100644 --- a/packages/block-library/src/post-author/edit.js +++ b/packages/block-library/src/post-author/edit.js @@ -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; @@ -40,7 +40,7 @@ function PostAuthorEdit( { const isDescendentOfQueryLoop = Number.isFinite( queryId ); const { authorId, authorDetails, authors, supportsAuthor } = useSelect( ( select ) => { - const { getEditedEntityRecord, getUser, getUsers } = + const { getEditedEntityRecord, getUser, getUsers, getPostType } = select( coreStore ); const _authorId = getEditedEntityRecord( 'postType', @@ -53,8 +53,7 @@ function PostAuthorEdit( { authorDetails: _authorId ? getUser( _authorId ) : null, authors: getUsers( AUTHORS_QUERY ), supportsAuthor: - select( coreStore ).getPostType( postType )?.supports - ?.author ?? false, + getPostType( postType )?.supports?.author ?? false, }; }, [ postType, postId ] @@ -100,6 +99,18 @@ function PostAuthorEdit( { const showAuthorControl = !! postId && ! isDescendentOfQueryLoop && authorOptions.length > 0; + if ( ! supportsAuthor ) { + return ( +
+ { sprintf( + // Translators: %s is replaced with the name of the post type. + __( 'This post type (%s) does not support Authors.' ), + postType + ) } +
+ ); + } + return ( <> @@ -192,65 +203,55 @@ function PostAuthorEdit( { /> - { supportsAuthor ? ( -
- { showAvatar && authorDetails?.avatar_urls && ( -
- { -
+
+ { showAvatar && authorDetails?.avatar_urls && ( +
+ { +
+ ) } +
+ { ( ! RichText.isEmpty( byline ) || isSelected ) && ( + + setAttributes( { byline: value } ) + } + /> ) } -
- { ( ! RichText.isEmpty( byline ) || isSelected ) && ( - - setAttributes( { byline: value } ) - } - /> +

+ { isLink ? ( + event.preventDefault() } + > + { authorName } + + ) : ( + authorName ) } -

- { isLink ? ( - - event.preventDefault() - } - > - { authorName } - - ) : ( - authorName - ) } -

- { showBio && ( -

- ) } -

-
- ) : ( -
- { __( - 'The current post type does not support authors. The Post Author block will not be displayed.' +

+ { showBio && ( +

) }

- ) } +
); } diff --git a/packages/block-library/src/post-author/index.php b/packages/block-library/src/post-author/index.php index ae902e8a02c49d..2d01de508b94af 100644 --- a/packages/block-library/src/post-author/index.php +++ b/packages/block-library/src/post-author/index.php @@ -26,7 +26,7 @@ function render_block_core_post_author( $attributes, $content, $block ) { return ''; } - if ( ! post_type_supports( get_post_type( $block->context['postId'] ), 'author' ) ) { + if ( ! post_type_supports( $block->context['postType'], 'author' ) ) { return ''; } From 512b702f670b541b415e29f636853e7515a8766d Mon Sep 17 00:00:00 2001 From: sarthaknagoshe2002 Date: Wed, 18 Dec 2024 22:15:41 +0530 Subject: [PATCH 5/6] Fix: update the warning message & translator comment --- packages/block-library/src/post-author-name/edit.js | 4 ++-- packages/block-library/src/post-author/edit.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/post-author-name/edit.js b/packages/block-library/src/post-author-name/edit.js index 1bd88d51f07539..65a0e4b8414cb0 100644 --- a/packages/block-library/src/post-author-name/edit.js +++ b/packages/block-library/src/post-author-name/edit.js @@ -97,9 +97,9 @@ function PostAuthorNameEdit( { { supportsAuthor ? displayAuthor : sprintf( - // Translators: %s is replaced with the name of the post type. + // translators: %s: Name of the post type e.g: "post". __( - 'This post type (%s) does not support Authors.' + 'This post type (%s) does not support the author' ), postType ) } diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js index 26d568c99b5c0f..dd2b3aa617548d 100644 --- a/packages/block-library/src/post-author/edit.js +++ b/packages/block-library/src/post-author/edit.js @@ -103,8 +103,8 @@ function PostAuthorEdit( { return (
{ sprintf( - // Translators: %s is replaced with the name of the post type. - __( 'This post type (%s) does not support Authors.' ), + // translators: %s: Name of the post type e.g: "post". + __( 'This post type (%s) does not support the author.' ), postType ) }
From e179899c9cdf84805e3d5d3955ab3b3d9cbc9510 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 18 Dec 2024 20:51:40 +0400 Subject: [PATCH 6/6] Missing period --- packages/block-library/src/post-author-name/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/post-author-name/edit.js b/packages/block-library/src/post-author-name/edit.js index 65a0e4b8414cb0..2b4bb0709356b0 100644 --- a/packages/block-library/src/post-author-name/edit.js +++ b/packages/block-library/src/post-author-name/edit.js @@ -99,7 +99,7 @@ function PostAuthorNameEdit( { : sprintf( // translators: %s: Name of the post type e.g: "post". __( - 'This post type (%s) does not support the author' + 'This post type (%s) does not support the author.' ), postType ) }