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 all commits
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
40 changes: 32 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,42 @@ 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 url = useSelect( ( select ) => {
const { canUser } = select( coreStore );
const { getEditorSettings } = select( editorStore );

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

// 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.
return canUser( 'read', 'templates' ) && 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,28 +18,41 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as reusableBlocksStore } from '../../store';

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

return {
canRemove: canRemoveBlock( clientId ),
isVisible:
!! reusableBlock &&
isReusableBlock( reusableBlock ) &&
!! canUser(
'update',
'blocks',
reusableBlock.attributes.ref
),
innerBlockCount: getBlockCount( clientId ),
};
},
[ clientId ]
);
return {
canRemove: canRemoveBlock( clientId ),
isVisible:
!! reusableBlock &&
isReusableBlock( reusableBlock ) &&
!! canUser(
'update',
'blocks',
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.
managePatternsUrl:
isBlockTheme && canUser( 'read', 'templates' )
? addQueryArgs( 'site-editor.php', {
path: '/patterns',
} )
: addQueryArgs( 'edit.php', {
post_type: 'wp_block',
} ),
};
},
[ clientId ]
);

const { __experimentalConvertBlockToStatic: convertBlockToStatic } =
useDispatch( reusableBlocksStore );
Expand All @@ -50,9 +63,7 @@ function ReusableBlocksManageButton( { clientId } ) {

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