Skip to content

Commit

Permalink
Centralise all permissions lookup in Link UI and enable (#52166)
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave authored and tellthemachines committed Jul 3, 2023
1 parent 5b1f580 commit 14c54c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
16 changes: 1 addition & 15 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ import {
} from '@wordpress/dom';
import { decodeEntities } from '@wordpress/html-entities';
import { link as linkIcon, addSubmenu } from '@wordpress/icons';
import {
store as coreStore,
useResourcePermissions,
} from '@wordpress/core-data';
import { store as coreStore } from '@wordpress/core-data';
import { useMergeRefs } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -184,9 +181,6 @@ export default function NavigationLinkEdit( {
const itemLabelPlaceholder = __( 'Add label…' );
const ref = useRef();

const pagesPermissions = useResourcePermissions( 'pages' );
const postsPermissions = useResourcePermissions( 'posts' );

const {
innerBlocks,
isAtMaxNesting,
Expand Down Expand Up @@ -322,13 +316,6 @@ export default function NavigationLinkEdit( {
setIsLinkOpen( false );
}

let userCanCreate = false;
if ( ! type || type === 'page' ) {
userCanCreate = pagesPermissions.canCreate;
} else if ( type === 'post' ) {
userCanCreate = postsPermissions.canCreate;
}

const {
textColor,
customTextColor,
Expand Down Expand Up @@ -589,7 +576,6 @@ export default function NavigationLinkEdit( {
link={ attributes }
onClose={ () => setIsLinkOpen( false ) }
anchor={ popoverAnchor }
hasCreateSuggestion={ userCanCreate }
onRemove={ removeLink }
onChange={ ( updatedValue ) => {
updateAttributes(
Expand Down
16 changes: 14 additions & 2 deletions packages/block-library/src/navigation-link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
store as blockEditorStore,
} from '@wordpress/block-editor';
import { createInterpolateElement, useMemo } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import {
store as coreStore,
useResourcePermissions,
} from '@wordpress/core-data';
import { decodeEntities } from '@wordpress/html-entities';
import { switchToBlockType } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -125,6 +128,8 @@ function LinkControlTransforms( { clientId } ) {

export function LinkUI( props ) {
const { saveEntityRecord } = useDispatch( coreStore );
const pagesPermissions = useResourcePermissions( 'pages' );
const postsPermissions = useResourcePermissions( 'posts' );

async function handleCreate( pageTitle ) {
const postType = props.link.type || 'page';
Expand Down Expand Up @@ -155,6 +160,13 @@ export function LinkUI( props ) {

const { label, url, opensInNewTab, type, kind } = props.link;

let userCanCreate = false;
if ( ! type || type === 'page' ) {
userCanCreate = pagesPermissions.canCreate;
} else if ( type === 'post' ) {
userCanCreate = postsPermissions.canCreate;
}

// Memoize link value to avoid overriding the LinkControl's internal state.
// This is a temporary fix. See https://github.com/WordPress/gutenberg/issues/50976#issuecomment-1568226407.
const link = useMemo(
Expand All @@ -179,7 +191,7 @@ export function LinkUI( props ) {
className={ props.className }
value={ link }
showInitialSuggestions={ true }
withCreateSuggestion={ props.hasCreateSuggestion }
withCreateSuggestion={ userCanCreate }
createSuggestion={ handleCreate }
createSuggestionButtonText={ ( searchTerm ) => {
let format;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ function AdditionalBlockContent( { block, insertedBlock, setInsertedBlock } ) {
onClose={ () => {
setInsertedBlock( null );
} }
hasCreateSuggestion={ false }
onChange={ ( updatedValue ) => {
updateAttributes(
updatedValue,
Expand Down

0 comments on commit 14c54c5

Please sign in to comment.