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 manage pattern links to go to site editor if available #52403

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions packages/edit-post/src/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* WordPress dependencies
*/
import { MenuItem, VisuallyHidden } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { store as editorStore } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';
import { external } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';
Expand All @@ -15,21 +18,46 @@ import KeyboardShortcutsHelpMenuItem from './keyboard-shortcuts-help-menu-item';
import ToolsMoreMenuGroup from '../components/header/tools-more-menu-group';
import WelcomeGuideMenuItem from './welcome-guide-menu-item';

function ManagePatternsMenuItem() {
const { isBlockTheme, isVisible } = useSelect( ( select ) => {
const { canUser } = select( coreStore );
const { getEditorSettings } = select( editorStore );

return {
// The site editor and templates both check whether the user has
// edit_theme_options capabilities. We can leverage that here and not
// display the manage patterns link if the user can't access it.
isVisible: canUser( 'read', 'templates' ),
isBlockTheme: getEditorSettings().__unstableIsBlockBasedTheme,
};
}, [] );

if ( ! isVisible ) {
return null;
}
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved

const defaultUrl = addQueryArgs( 'edit.php', { post_type: 'wp_block' } );
const patternsUrl = addQueryArgs( 'site-editor.php', {
path: '/patterns',
} );

const url = isBlockTheme ? patternsUrl : defaultUrl;

return (
<MenuItem role="menuitem" href={ url }>
{ __( 'Manage Patterns' ) }
</MenuItem>
);
}

registerPlugin( 'edit-post', {
render() {
return (
<>
<ToolsMoreMenuGroup>
{ ( { onClose } ) => (
<>
<MenuItem
role="menuitem"
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_block',
} ) }
>
{ __( 'Manage Patterns' ) }
</MenuItem>
<ManagePatternsMenuItem />
<KeyboardShortcutsHelpMenuItem
onSelect={ onClose }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as reusableBlocksStore } from '../../store';

function ReusableBlocksManageButton( { clientId } ) {
const { canRemove, isVisible, innerBlockCount } = useSelect(
const {
canRemove,
isVisible,
innerBlockCount,
canManagePatterns,
managePatternsUrl,
} = useSelect(
( select ) => {
const { getBlock, canRemoveBlock, getBlockCount } =
const { getBlock, canRemoveBlock, getBlockCount, getSettings } =
select( blockEditorStore );
const { canUser } = select( coreStore );
const reusableBlock = getBlock( clientId );
const isBlockTheme = getSettings().__unstableIsBlockBasedTheme;

return {
canRemove: canRemoveBlock( clientId ),
Expand All @@ -36,6 +43,17 @@ function ReusableBlocksManageButton( { clientId } ) {
reusableBlock.attributes.ref
),
innerBlockCount: getBlockCount( clientId ),
// The site editor and templates both check whether the user
// has edit_theme_options capabilities. We can leverage that here
// and omit the manage patterns link if the user can't access it.
canManagePatterns: canUser( 'read', 'templates' ),
managePatternsUrl: isBlockTheme
? addQueryArgs( 'site-editor.php', {
path: '/patterns',
} )
: addQueryArgs( 'edit.php', {
post_type: 'wp_block',
} ),
};
},
[ clientId ]
Expand All @@ -50,11 +68,11 @@ function ReusableBlocksManageButton( { clientId } ) {

return (
<BlockSettingsMenuControls>
<MenuItem
href={ addQueryArgs( 'edit.php', { post_type: 'wp_block' } ) }
>
{ __( 'Manage Patterns' ) }
</MenuItem>
{ canManagePatterns && (
<MenuItem href={ managePatternsUrl }>
{ __( 'Manage Patterns' ) }
</MenuItem>
) }
{ canRemove && (
<MenuItem onClick={ () => convertBlockToStatic( clientId ) }>
{ innerBlockCount > 1
Expand Down