From acbfacf8a261ea5cbc85b4e57b8169c32d51a408 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Sat, 8 Jun 2024 12:04:59 +0200 Subject: [PATCH] Replace "Add new post" link text with more meaningful Label (v2) (#62277) * Replace "Add new post" link text with more meaningful 'add_new_item' label of current post type. * Avoid rendering the CreateNewPostLink component itself if postType is falsy * Follow recommendation to use useSelect hook, instead of using select() directly in React components Co-authored-by: carstingaxion Co-authored-by: t-hamano --- .../create-new-post-link.js | 20 +++++++++++-------- .../query/edit/inspector-controls/index.js | 8 +++++--- 2 files changed, 17 insertions(+), 11 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..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 @@ -1,23 +1,27 @@ /** * 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 { useSelect } from '@wordpress/data'; -const CreateNewPostLink = ( { - attributes: { query: { postType } = {} } = {}, -} ) => { - if ( ! postType ) { - return null; - } +const CreateNewPostLink = ( { postType } ) => { const newPostUrl = addQueryArgs( 'post-new.php', { post_type: postType, } ); + + const addNewItemLabel = useSelect( + ( select ) => { + const { getPostType } = select( coreStore ); + return getPostType( postType )?.labels?.add_new_item; + }, + [ postType ] + ); return (
{ createInterpolateElement( - __( 'Add new post' ), + '' + addNewItemLabel + '', // eslint-disable-next-line jsx-a11y/anchor-has-content { a: } ) } 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 && (