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

Use useResourcePermissions in block-library and the widgets editor #43305

Merged
merged 5 commits into from
Aug 19, 2022
Merged
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
20 changes: 9 additions & 11 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
@@ -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 ]
20 changes: 9 additions & 11 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
@@ -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,15 +297,18 @@ export default function NavigationSubmenuEdit( {
const itemLabelPlaceholder = __( 'Add text…' );
const ref = useRef();

const [ , { canCreate: userCanCreatePages } ] =
useResourcePermissions( 'pages' );
adamziel marked this conversation as resolved.
Show resolved Hide resolved
const [ , { canCreate: userCanCreatePosts } ] =
useResourcePermissions( 'posts' );

const {
isAtMaxNesting,
isTopLevelItem,
isParentOfSelectedBlock,
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,
};
},
Original file line number Diff line number Diff line change
@@ -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( () => {