Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patterns: update the Manage all my patterns command to redirect to site editor patterns list #52817

Merged
merged 8 commits into from
Jul 25, 2023
57 changes: 52 additions & 5 deletions packages/core-commands/src/admin-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,40 @@
*/
import { useCommand } from '@wordpress/commands';
import { __ } from '@wordpress/i18n';
import { external, plus } from '@wordpress/icons';
import { external, plus, symbol } from '@wordpress/icons';
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { addQueryArgs, getPath } from '@wordpress/url';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { unlock } from './lock-unlock';

const { useHistory } = unlock( routerPrivateApis );

export function useAdminNavigationCommands() {
const history = useHistory();

const { isBlockTheme, canAccessSiteEditor } = useSelect( ( select ) => {
return {
isBlockTheme:
// To avoid making core-commands dependent on block-editor using store string literal name.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
select( 'core/block-editor' )?.getSettings()
.__unstableIsBlockBasedTheme,
canAccessSiteEditor: select( coreStore ).canUser(
'read',
'templates'
),
};
}, [] );

const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);

useCommand( {
name: 'core/add-new-post',
label: __( 'Add new post' ),
Expand All @@ -24,10 +55,26 @@ export function useAdminNavigationCommands() {
} );
useCommand( {
name: 'core/manage-reusable-blocks',
label: __( 'Manage all of my patterns' ),
callback: () => {
document.location.href = 'edit.php?post_type=wp_block';
label: __( 'Open patterns' ),
callback: ( { close } ) => {
if (
( ! isSiteEditor && ! isBlockTheme ) ||
! canAccessSiteEditor
) {
document.location.href = 'edit.php?post_type=wp_block';
} else {
const args = {
path: '/patterns',
};
const targetUrl = addQueryArgs( 'site-editor.php', args );
if ( isSiteEditor ) {
history.push( args );
} else {
document.location = targetUrl;
}
close();
}
},
icon: external,
icon: isSiteEditor ? symbol : external,
} );
}
18 changes: 0 additions & 18 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
symbolFilled,
styles,
navigation,
symbol,
} from '@wordpress/icons';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { getQueryArg, addQueryArgs, getPath } from '@wordpress/url';
Expand Down Expand Up @@ -198,23 +197,6 @@ function useSiteEditorBasicNavigationCommands() {
},
} );

result.push( {
name: 'core/edit-site/open-template-parts',
label: __( 'Open patterns' ),
icon: symbol,
callback: ( { close } ) => {
const args = {
path: '/patterns',
};
const targetUrl = addQueryArgs( 'site-editor.php', args );
if ( isSiteEditor ) {
history.push( args );
} else {
document.location = targetUrl;
}
close();
},
} );
return result;
}, [ history, isSiteEditor ] );

Expand Down