Skip to content

Commit

Permalink
Do not show dynamic add new post, add new page commands (#50221)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored May 1, 2023
1 parent a770dfd commit 76cf678
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 56 deletions.
2 changes: 2 additions & 0 deletions packages/commands/src/hooks/use-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -35,6 +36,7 @@ export default function useCommand( command ) {
command.name,
command.label,
command.group,
command.icon,
registerCommand,
unregisterCommand,
] );
Expand Down
72 changes: 16 additions & 56 deletions packages/core-commands/src/add-post-type-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,31 @@
* 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';

/**
* Internal dependencies
*/
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';
},
} );
}

0 comments on commit 76cf678

Please sign in to comment.