Skip to content

Commit

Permalink
Patterns: Update manage pattern links to go to site editor if availab…
Browse files Browse the repository at this point in the history
…le (#52403)
  • Loading branch information
aaronrobertshaw authored and tellthemachines committed Jul 10, 2023
1 parent 3e5bc99 commit cfcf5ff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 32 deletions.
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

0 comments on commit cfcf5ff

Please sign in to comment.