Skip to content

Commit

Permalink
Replace "Add new post" link text with more meaningful Label (v2) (#62277
Browse files Browse the repository at this point in the history
)

* 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 <carstenbach@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
  • Loading branch information
3 people authored Jun 8, 2024
1 parent 7b4d386 commit acbfacf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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 (
<div className="wp-block-query__create-new-link">
{ createInterpolateElement(
__( '<a>Add new post</a>' ),
'<a>' + addNewItemLabel + '</a>',
// eslint-disable-next-line jsx-a11y/anchor-has-content
{ a: <a href={ newPostUrl } /> }
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ export default function QueryInspectorControls( props ) {

return (
<>
<BlockInfo>
<CreateNewPostLink { ...props } />
</BlockInfo>
{ !! postType && (
<BlockInfo>
<CreateNewPostLink postType={ postType } />
</BlockInfo>
) }
{ showSettingsPanel && (
<PanelBody title={ __( 'Settings' ) }>
{ showInheritControl && (
Expand Down

0 comments on commit acbfacf

Please sign in to comment.