From 76cf67852218d0b8e627d4497026bb9ec269c670 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 1 May 2023 18:50:44 +0100 Subject: [PATCH] Do not show dynamic add new post, add new page commands (#50221) --- packages/commands/src/hooks/use-command.js | 2 + .../src/add-post-type-commands.js | 72 +++++-------------- 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/packages/commands/src/hooks/use-command.js b/packages/commands/src/hooks/use-command.js index 6da87e645a2585..4ed8f1bcd930e5 100644 --- a/packages/commands/src/hooks/use-command.js +++ b/packages/commands/src/hooks/use-command.js @@ -26,6 +26,7 @@ export default function useCommand( command ) { name: command.name, group: command.group, label: command.label, + icon: command.icon, callback: currentCallback.current, } ); return () => { @@ -35,6 +36,7 @@ export default function useCommand( command ) { command.name, command.label, command.group, + command.icon, registerCommand, unregisterCommand, ] ); diff --git a/packages/core-commands/src/add-post-type-commands.js b/packages/core-commands/src/add-post-type-commands.js index 516686dea03944..afd783c7c0cc35 100644 --- a/packages/core-commands/src/add-post-type-commands.js +++ b/packages/core-commands/src/add-post-type-commands.js @@ -2,9 +2,7 @@ * WordPress dependencies */ import { privateApis } from '@wordpress/commands'; -import { __, sprintf } from '@wordpress/i18n'; -import { addQueryArgs } from '@wordpress/url'; -import { useMemo } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; import { plus } from '@wordpress/icons'; /** @@ -12,61 +10,23 @@ import { plus } from '@wordpress/icons'; */ import { unlock } from './lock-unlock'; -const { useCommandLoader } = unlock( privateApis ); - -const getAddPostTypeCommandLoader = ( postType ) => - function useAddCommandLoader( { search } ) { - let label; - if ( postType === 'post' ) { - label = __( 'Add a new post' ); - } else if ( postType === 'page' ) { - label = __( 'Add a new page' ); - } else { - throw 'unsupported post type ' + postType; - } - const hasRecordTitle = - !! search && ! label.toLowerCase().includes( search.toLowerCase() ); - if ( postType === 'post' && hasRecordTitle ) { - /* translators: %s: Post title placeholder */ - label = sprintf( __( 'Add a new post "%s"' ), search ); - } else if ( postType === 'page' && hasRecordTitle ) { - /* translators: %s: Page title placeholder */ - label = sprintf( __( 'Add a new page "%s"' ), search ); - } - - const commands = useMemo( - () => [ - { - name: 'core/wp-admin/add-' + postType + '-' + search, - label, - icon: plus, - callback: () => { - document.location.href = addQueryArgs( 'post-new.php', { - post_type: postType, - post_title: hasRecordTitle ? search : undefined, - } ); - }, - }, - ], - [ hasRecordTitle, search, label ] - ); - - return { - isLoading: false, - commands, - }; - }; - -const useAddPostLoader = getAddPostTypeCommandLoader( 'post' ); -const useAddPageLoader = getAddPostTypeCommandLoader( 'page' ); +const { useCommand } = unlock( privateApis ); export function useAddPostTypeCommands() { - useCommandLoader( { - name: 'core/wp-admin/add-post-loader', - hook: useAddPostLoader, + useCommand( { + name: 'add new post', + label: __( 'Add new post' ), + icon: plus, + callback: () => { + document.location.href = 'post-new.php'; + }, } ); - useCommandLoader( { - name: 'core/wp-admin/add-page-loader', - hook: useAddPageLoader, + useCommand( { + name: 'add new page', + label: __( 'Add new page' ), + icon: plus, + callback: () => { + document.location.href = 'post-new.php?post_type=page'; + }, } ); }