From 4096f10942d91193031aeb2b8c93a81ec940bd67 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Mon, 11 Mar 2024 03:30:01 +0100 Subject: [PATCH 1/3] Replace "Add new post" link text with more meaningful 'add_new_item' label of current post type. --- .../edit/inspector-controls/create-new-post-link.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js b/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js index a8be9ed1c8d4fa..d9de276b1cb223 100644 --- a/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js +++ b/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js @@ -1,9 +1,10 @@ /** * WordPress dependencies */ -import { __ } from '@wordpress/i18n'; import { createInterpolateElement } from '@wordpress/element'; import { addQueryArgs } from '@wordpress/url'; +import { store as coreStore } from '@wordpress/core-data'; +import { select } from '@wordpress/data'; const CreateNewPostLink = ( { attributes: { query: { postType } = {} } = {}, @@ -14,10 +15,14 @@ const CreateNewPostLink = ( { const newPostUrl = addQueryArgs( 'post-new.php', { post_type: postType, } ); + + const addNewItemLabel = + select( coreStore ).getPostType( postType )?.labels?.add_new_item; + return (
{ createInterpolateElement( - __( 'Add new post' ), + '' + addNewItemLabel + '', // eslint-disable-next-line jsx-a11y/anchor-has-content { a: } ) } From 1632d84f7395c845c26aa73a3f397515cf0d24eb Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Tue, 4 Jun 2024 13:22:51 +0200 Subject: [PATCH 2/3] Avoid rendering the CreateNewPostLink component itself if postType is falsy --- .../query/edit/inspector-controls/create-new-post-link.js | 7 +------ .../src/query/edit/inspector-controls/index.js | 8 +++++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js b/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js index d9de276b1cb223..c6020985bda380 100644 --- a/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js +++ b/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js @@ -6,12 +6,7 @@ import { addQueryArgs } from '@wordpress/url'; import { store as coreStore } from '@wordpress/core-data'; import { select } from '@wordpress/data'; -const CreateNewPostLink = ( { - attributes: { query: { postType } = {} } = {}, -} ) => { - if ( ! postType ) { - return null; - } +const CreateNewPostLink = ( postType ) => { const newPostUrl = addQueryArgs( 'post-new.php', { post_type: postType, } ); diff --git a/packages/block-library/src/query/edit/inspector-controls/index.js b/packages/block-library/src/query/edit/inspector-controls/index.js index eb5abe9c24d98f..a8b3330f5b4b54 100644 --- a/packages/block-library/src/query/edit/inspector-controls/index.js +++ b/packages/block-library/src/query/edit/inspector-controls/index.js @@ -131,9 +131,11 @@ export default function QueryInspectorControls( props ) { return ( <> - - - + { !! postType && ( + + + + ) } { showSettingsPanel && ( { showInheritControl && ( From 0c1cf66dd2c5d01c8411957c7dd2931cec437987 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Tue, 4 Jun 2024 13:30:23 +0200 Subject: [PATCH 3/3] Follow recommendation to use useSelect hook, instead of using select() directly in React components --- .../inspector-controls/create-new-post-link.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js b/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js index c6020985bda380..58d58a6089aacd 100644 --- a/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js +++ b/packages/block-library/src/query/edit/inspector-controls/create-new-post-link.js @@ -4,16 +4,20 @@ import { createInterpolateElement } from '@wordpress/element'; import { addQueryArgs } from '@wordpress/url'; import { store as coreStore } from '@wordpress/core-data'; -import { select } from '@wordpress/data'; +import { useSelect } from '@wordpress/data'; -const CreateNewPostLink = ( postType ) => { +const CreateNewPostLink = ( { postType } ) => { const newPostUrl = addQueryArgs( 'post-new.php', { post_type: postType, } ); - const addNewItemLabel = - select( coreStore ).getPostType( postType )?.labels?.add_new_item; - + const addNewItemLabel = useSelect( + ( select ) => { + const { getPostType } = select( coreStore ); + return getPostType( postType )?.labels?.add_new_item; + }, + [ postType ] + ); return (
{ createInterpolateElement(