From b9cde6baac4f975eebb623d6a6008a9a536c677b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 17 Aug 2022 09:09:53 +0200 Subject: [PATCH 1/5] Use useResourcePermissions in block-library and the widgets editor --- .../block-library/src/navigation-link/edit.js | 20 +++---- .../src/navigation-submenu/edit.js | 20 +++---- .../index.js | 55 ++++++++++--------- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index befe613e32fc8..ebc58c1c914dd 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -42,7 +42,10 @@ import { } from '@wordpress/element'; import { placeCaretAtHorizontalEdge } from '@wordpress/dom'; import { link as linkIcon, addSubmenu } from '@wordpress/icons'; -import { store as coreStore } from '@wordpress/core-data'; +import { + store as coreStore, + __experimentalUseResourcePermissions as useResourcePermissions, +} from '@wordpress/core-data'; import { decodeEntities } from '@wordpress/html-entities'; /** @@ -463,14 +466,17 @@ export default function NavigationLinkEdit( { const itemLabelPlaceholder = __( 'Add link…' ); const ref = useRef(); + const [ , { canCreate: userCanCreatePages } ] = + useResourcePermissions( 'pages' ); + const [ , { canCreate: userCanCreatePosts } ] = + useResourcePermissions( 'posts' ); + const { innerBlocks, isAtMaxNesting, isTopLevelLink, isParentOfSelectedBlock, hasChildren, - userCanCreatePages, - userCanCreatePosts, } = useSelect( ( select ) => { const { @@ -497,14 +503,6 @@ export default function NavigationLinkEdit( { true ), hasChildren: !! getBlockCount( clientId ), - userCanCreatePages: select( coreStore ).canUser( - 'create', - 'pages' - ), - userCanCreatePosts: select( coreStore ).canUser( - 'create', - 'posts' - ), }; }, [ clientId ] diff --git a/packages/block-library/src/navigation-submenu/edit.js b/packages/block-library/src/navigation-submenu/edit.js index 8b4d3d1b833b1..b6fefbb4d070a 100644 --- a/packages/block-library/src/navigation-submenu/edit.js +++ b/packages/block-library/src/navigation-submenu/edit.js @@ -39,7 +39,10 @@ import { } from '@wordpress/element'; import { placeCaretAtHorizontalEdge } from '@wordpress/dom'; import { link as linkIcon, removeSubmenu } from '@wordpress/icons'; -import { store as coreStore } from '@wordpress/core-data'; +import { + __experimentalUseResourcePermissions as useResourcePermissions, + store as coreStore, +} from '@wordpress/core-data'; import { speak } from '@wordpress/a11y'; import { createBlock } from '@wordpress/blocks'; @@ -294,6 +297,11 @@ export default function NavigationSubmenuEdit( { const itemLabelPlaceholder = __( 'Add text…' ); const ref = useRef(); + const [ , { canCreate: userCanCreatePages } ] = + useResourcePermissions( 'pages' ); + const [ , { canCreate: userCanCreatePosts } ] = + useResourcePermissions( 'posts' ); + const { isAtMaxNesting, isTopLevelItem, @@ -301,8 +309,6 @@ export default function NavigationSubmenuEdit( { isImmediateParentOfSelectedBlock, hasChildren, selectedBlockHasChildren, - userCanCreatePages, - userCanCreatePosts, onlyDescendantIsEmptyLink, } = useSelect( ( select ) => { @@ -348,14 +354,6 @@ export default function NavigationSubmenuEdit( { ), hasChildren: !! getBlockCount( clientId ), selectedBlockHasChildren: !! selectedBlockChildren?.length, - userCanCreatePages: select( coreStore ).canUser( - 'create', - 'pages' - ), - userCanCreatePosts: select( coreStore ).canUser( - 'create', - 'posts' - ), onlyDescendantIsEmptyLink: _onlyDescendantIsEmptyLink, }; }, diff --git a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js index 86ebf3d8c1a81..30fccd1eb807a 100644 --- a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js +++ b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js @@ -4,6 +4,11 @@ import { SlotFillProvider } from '@wordpress/components'; import { uploadMedia } from '@wordpress/media-utils'; import { useDispatch, useSelect } from '@wordpress/data'; +import { + useEntityBlockEditor, + store as coreStore, + __experimentalUseResourcePermissions as useResourcePermissions, +} from '@wordpress/core-data'; import { useMemo } from '@wordpress/element'; import { BlockEditorProvider, @@ -18,7 +23,6 @@ import { store as preferencesStore } from '@wordpress/preferences'; * Internal dependencies */ import KeyboardShortcuts from '../keyboard-shortcuts'; -import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data'; import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../../store/utils'; import useLastSelectedWidgetArea from '../../hooks/use-last-selected-widget-area'; import { store as editWidgetsStore } from '../../store'; @@ -29,31 +33,30 @@ export default function WidgetAreasBlockEditorProvider( { children, ...props } ) { - const { - hasUploadPermissions, - reusableBlocks, - isFixedToolbarActive, - keepCaretInsideBlock, - } = useSelect( - ( select ) => ( { - hasUploadPermissions: - select( coreStore ).canUser( 'create', 'media' ) ?? true, - widgetAreas: select( editWidgetsStore ).getWidgetAreas(), - widgets: select( editWidgetsStore ).getWidgets(), - reusableBlocks: ALLOW_REUSABLE_BLOCKS - ? select( coreStore ).getEntityRecords( 'postType', 'wp_block' ) - : [], - isFixedToolbarActive: !! select( preferencesStore ).get( - 'core/edit-widgets', - 'fixedToolbar' - ), - keepCaretInsideBlock: !! select( preferencesStore ).get( - 'core/edit-widgets', - 'keepCaretInsideBlock' - ), - } ), - [] - ); + const [ , { canCreate: hasUploadPermissions } ] = + useResourcePermissions( 'media' ); + const { reusableBlocks, isFixedToolbarActive, keepCaretInsideBlock } = + useSelect( + ( select ) => ( { + widgetAreas: select( editWidgetsStore ).getWidgetAreas(), + widgets: select( editWidgetsStore ).getWidgets(), + reusableBlocks: ALLOW_REUSABLE_BLOCKS + ? select( coreStore ).getEntityRecords( + 'postType', + 'wp_block' + ) + : [], + isFixedToolbarActive: !! select( preferencesStore ).get( + 'core/edit-widgets', + 'fixedToolbar' + ), + keepCaretInsideBlock: !! select( preferencesStore ).get( + 'core/edit-widgets', + 'keepCaretInsideBlock' + ), + } ), + [] + ); const { setIsInserterOpened } = useDispatch( editWidgetsStore ); const settings = useMemo( () => { From 8405b173c1ca26858800523ab19f19f3a1f0ac15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 17 Aug 2022 11:23:06 +0200 Subject: [PATCH 2/5] Return an object from useResourcePermissions instead of an array --- .../block-library/src/navigation-link/edit.js | 6 +- .../src/navigation-submenu/edit.js | 6 +- .../hooks/test/use-resource-permissions.js | 64 ++++++++----------- .../src/hooks/use-resource-permissions.ts | 30 ++++----- 4 files changed, 45 insertions(+), 61 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index ebc58c1c914dd..0966ed75620e8 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -466,10 +466,8 @@ export default function NavigationLinkEdit( { const itemLabelPlaceholder = __( 'Add link…' ); const ref = useRef(); - const [ , { canCreate: userCanCreatePages } ] = - useResourcePermissions( 'pages' ); - const [ , { canCreate: userCanCreatePosts } ] = - useResourcePermissions( 'posts' ); + const { canCreate: userCanCreatePages } = useResourcePermissions( 'pages' ); + const { canCreate: userCanCreatePosts } = useResourcePermissions( 'posts' ); const { innerBlocks, diff --git a/packages/block-library/src/navigation-submenu/edit.js b/packages/block-library/src/navigation-submenu/edit.js index b6fefbb4d070a..942c991acc38a 100644 --- a/packages/block-library/src/navigation-submenu/edit.js +++ b/packages/block-library/src/navigation-submenu/edit.js @@ -297,10 +297,8 @@ export default function NavigationSubmenuEdit( { const itemLabelPlaceholder = __( 'Add text…' ); const ref = useRef(); - const [ , { canCreate: userCanCreatePages } ] = - useResourcePermissions( 'pages' ); - const [ , { canCreate: userCanCreatePosts } ] = - useResourcePermissions( 'posts' ); + const { canCreate: userCanCreatePages } = useResourcePermissions( 'pages' ); + const { canCreate: userCanCreatePosts } = useResourcePermissions( 'posts' ); const { isAtMaxNesting, diff --git a/packages/core-data/src/hooks/test/use-resource-permissions.js b/packages/core-data/src/hooks/test/use-resource-permissions.js index f62ea0df738dd..55b17f03d4f8d 100644 --- a/packages/core-data/src/hooks/test/use-resource-permissions.js +++ b/packages/core-data/src/hooks/test/use-resource-permissions.js @@ -50,28 +50,24 @@ describe( 'useResourcePermissions', () => { ); - expect( data ).toEqual( [ - false, - { - status: 'IDLE', - isResolving: false, - canCreate: false, - }, - ] ); + expect( data ).toEqual( { + status: 'IDLE', + isResolving: false, + hasResolved: false, + canCreate: false, + } ); // Required to make sure no updates happen outside of act() await act( async () => { jest.advanceTimersByTime( 1 ); } ); - expect( data ).toEqual( [ - true, - { - status: 'SUCCESS', - isResolving: false, - canCreate: true, - }, - ] ); + expect( data ).toEqual( { + status: 'SUCCESS', + isResolving: false, + hasResolved: true, + canCreate: true, + } ); } ); it( 'retrieves the relevant permissions for a resource with a key', async () => { @@ -85,31 +81,27 @@ describe( 'useResourcePermissions', () => { ); - expect( data ).toEqual( [ - false, - { - status: 'IDLE', - isResolving: false, - canCreate: false, - canUpdate: false, - canDelete: false, - }, - ] ); + expect( data ).toEqual( { + status: 'IDLE', + isResolving: false, + hasResolved: false, + canCreate: false, + canUpdate: false, + canDelete: false, + } ); // Required to make sure no updates happen outside of act() await act( async () => { jest.advanceTimersByTime( 1 ); } ); - expect( data ).toEqual( [ - true, - { - status: 'SUCCESS', - isResolving: false, - canCreate: true, - canUpdate: false, - canDelete: false, - }, - ] ); + expect( data ).toEqual( { + status: 'SUCCESS', + isResolving: false, + hasResolved: true, + canCreate: true, + canUpdate: false, + canDelete: false, + } ); } ); } ); diff --git a/packages/core-data/src/hooks/use-resource-permissions.ts b/packages/core-data/src/hooks/use-resource-permissions.ts index d9b1c7a35a500..a6b5ca8d1506b 100644 --- a/packages/core-data/src/hooks/use-resource-permissions.ts +++ b/packages/core-data/src/hooks/use-resource-permissions.ts @@ -81,14 +81,12 @@ export default function __experimentalUseResourcePermissions< IdType = void >( const { canUser } = resolve( coreStore ); const create = canUser( 'create', resource ); if ( ! id ) { - return [ - create.hasResolved, - { - status: create.status, - isResolving: create.isResolving, - canCreate: create.hasResolved && create.data, - }, - ]; + return { + status: create.status, + isResolving: create.isResolving, + hasResolved: create.hasResolved, + canCreate: create.hasResolved && create.data, + }; } const update = canUser( 'update', resource, id ); @@ -104,16 +102,14 @@ export default function __experimentalUseResourcePermissions< IdType = void >( } else if ( hasResolved ) { status = Status.Success; } - return [ + return { + status, + isResolving, hasResolved, - { - status, - isResolving, - canCreate: hasResolved && create.data, - canUpdate: hasResolved && update.data, - canDelete: hasResolved && _delete.data, - }, - ]; + canCreate: hasResolved && create.data, + canUpdate: hasResolved && update.data, + canDelete: hasResolved && _delete.data, + }; }, [ resource, id ] ); From e96f2410df973f803c45b4056e83fcf0d5b45cd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 17 Aug 2022 11:27:05 +0200 Subject: [PATCH 3/5] Update the usage in useNavigationMenu --- .../src/navigation/use-navigation-menu.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index e6ccad1d017c8..f4f0cd0568647 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -12,10 +12,13 @@ export default function useNavigationMenu( ref ) { return useSelect( ( select ) => { - const [ - hasResolvedPermissions, - { canCreate, canUpdate, canDelete, isResolving }, - ] = permissions; + const { + canCreate, + canUpdate, + canDelete, + isResolving, + hasResolved, + } = permissions; const { navigationMenus, @@ -44,16 +47,16 @@ export default function useNavigationMenu( ref ) { canUserCreateNavigationMenu: canCreate, isResolvingCanUserCreateNavigationMenu: isResolving, - hasResolvedCanUserCreateNavigationMenu: hasResolvedPermissions, + hasResolvedCanUserCreateNavigationMenu: hasResolved, canUserUpdateNavigationMenu: canUpdate, hasResolvedCanUserUpdateNavigationMenu: ref - ? hasResolvedPermissions + ? hasResolved : undefined, canUserDeleteNavigationMenu: canDelete, hasResolvedCanUserDeleteNavigationMenu: ref - ? hasResolvedPermissions + ? hasResolved : undefined, }; }, From f379aea6dfb3482f5354df246e4f865c777e35af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 17 Aug 2022 11:55:08 +0200 Subject: [PATCH 4/5] Update the usage in the widgets editor --- .../src/components/widget-areas-block-editor-provider/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js index 30fccd1eb807a..8970f3bf020ed 100644 --- a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js +++ b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js @@ -33,7 +33,7 @@ export default function WidgetAreasBlockEditorProvider( { children, ...props } ) { - const [ , { canCreate: hasUploadPermissions } ] = + const { canCreate: hasUploadPermissions } = useResourcePermissions( 'media' ); const { reusableBlocks, isFixedToolbarActive, keepCaretInsideBlock } = useSelect( From e9f4c4021d91e372d8c6eb06e2ee2deaff2a9265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Thu, 18 Aug 2022 16:23:05 +0200 Subject: [PATCH 5/5] Assign useResourcePermissions results to named variables --- packages/block-library/src/navigation-link/edit.js | 8 ++++---- packages/block-library/src/navigation-submenu/edit.js | 8 ++++---- .../widget-areas-block-editor-provider/index.js | 7 +++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index 0966ed75620e8..afaf146ae5479 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -466,8 +466,8 @@ export default function NavigationLinkEdit( { const itemLabelPlaceholder = __( 'Add link…' ); const ref = useRef(); - const { canCreate: userCanCreatePages } = useResourcePermissions( 'pages' ); - const { canCreate: userCanCreatePosts } = useResourcePermissions( 'posts' ); + const pagesPermissions = useResourcePermissions( 'pages' ); + const postsPermissions = useResourcePermissions( 'posts' ); const { innerBlocks, @@ -602,9 +602,9 @@ export default function NavigationLinkEdit( { let userCanCreate = false; if ( ! type || type === 'page' ) { - userCanCreate = userCanCreatePages; + userCanCreate = pagesPermissions.canCreate; } else if ( type === 'post' ) { - userCanCreate = userCanCreatePosts; + userCanCreate = postsPermissions.canCreate; } async function handleCreate( pageTitle ) { diff --git a/packages/block-library/src/navigation-submenu/edit.js b/packages/block-library/src/navigation-submenu/edit.js index 942c991acc38a..9f1901613d064 100644 --- a/packages/block-library/src/navigation-submenu/edit.js +++ b/packages/block-library/src/navigation-submenu/edit.js @@ -297,8 +297,8 @@ export default function NavigationSubmenuEdit( { const itemLabelPlaceholder = __( 'Add text…' ); const ref = useRef(); - const { canCreate: userCanCreatePages } = useResourcePermissions( 'pages' ); - const { canCreate: userCanCreatePosts } = useResourcePermissions( 'posts' ); + const pagesPermissions = useResourcePermissions( 'pages' ); + const postsPermissions = useResourcePermissions( 'posts' ); const { isAtMaxNesting, @@ -422,9 +422,9 @@ export default function NavigationSubmenuEdit( { let userCanCreate = false; if ( ! type || type === 'page' ) { - userCanCreate = userCanCreatePages; + userCanCreate = pagesPermissions.canCreate; } else if ( type === 'post' ) { - userCanCreate = userCanCreatePosts; + userCanCreate = postsPermissions.canCreate; } async function handleCreate( pageTitle ) { diff --git a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js index 8970f3bf020ed..7ccf54fb8c445 100644 --- a/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js +++ b/packages/edit-widgets/src/components/widget-areas-block-editor-provider/index.js @@ -33,8 +33,7 @@ export default function WidgetAreasBlockEditorProvider( { children, ...props } ) { - const { canCreate: hasUploadPermissions } = - useResourcePermissions( 'media' ); + const mediaPermissions = useResourcePermissions( 'media' ); const { reusableBlocks, isFixedToolbarActive, keepCaretInsideBlock } = useSelect( ( select ) => ( { @@ -61,7 +60,7 @@ export default function WidgetAreasBlockEditorProvider( { const settings = useMemo( () => { let mediaUploadBlockEditor; - if ( hasUploadPermissions ) { + if ( mediaPermissions.canCreate ) { mediaUploadBlockEditor = ( { onError, ...argumentsObject } ) => { uploadMedia( { wpAllowedMimeTypes: blockEditorSettings.allowedMimeTypes, @@ -83,7 +82,7 @@ export default function WidgetAreasBlockEditorProvider( { blockEditorSettings, isFixedToolbarActive, keepCaretInsideBlock, - hasUploadPermissions, + mediaPermissions.canCreate, reusableBlocks, setIsInserterOpened, ] );