Skip to content

Commit

Permalink
Use registry instead of select in canUserEditValue (WordPress#6…
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot authored Sep 25, 2024
1 parent 0c097a3 commit c6025de
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export function RichTextWrapper(

const _disableBoundBlock =
! blockBindingsSource?.canUserEditValue?.( {
select,
registry,
context: blockBindingsContext,
args: relatedBinding.args,
} );
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
store as blocksStore,
} from '@wordpress/blocks';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';

const LINK_SETTINGS = [
...LinkControl.DEFAULT_LINK_SETTINGS,
Expand Down Expand Up @@ -190,6 +190,7 @@ function ButtonEdit( props ) {
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
const shadowProps = useShadowProps( attributes );
const registry = useRegistry();
const ref = useRef();
const richTextRef = useRef();
const blockProps = useBlockProps( {
Expand Down Expand Up @@ -248,7 +249,7 @@ function ButtonEdit( props ) {
lockUrlControls:
!! metadata?.bindings?.url &&
! blockBindingsSource?.canUserEditValue?.( {
select,
registry,
context,
args: metadata?.bindings?.url?.args,
} ),
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import clsx from 'clsx';
import { isBlobURL, createBlobURL } from '@wordpress/blob';
import { store as blocksStore, createBlock } from '@wordpress/blocks';
import { Placeholder } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import {
BlockIcon,
useBlockProps,
Expand Down Expand Up @@ -113,6 +113,7 @@ export function ImageEdit( {

const [ temporaryURL, setTemporaryURL ] = useState( attributes.blob );

const registry = useRegistry();
const containerRef = useRef();
// Only observe the max width from the parent container when the parent layout is not flex nor grid.
// This won't work for them because the container width changes with the image.
Expand Down Expand Up @@ -380,7 +381,7 @@ export function ImageEdit( {
lockUrlControls:
!! metadata?.bindings?.url &&
! blockBindingsSource?.canUserEditValue?.( {
select,
registry,
context,
args: metadata?.bindings?.url?.args,
} ),
Expand Down
9 changes: 5 additions & 4 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
Placeholder,
} from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import {
BlockControls,
InspectorControls,
Expand Down Expand Up @@ -134,6 +134,7 @@ export default function Image( {
const numericWidth = width ? parseInt( width, 10 ) : undefined;
const numericHeight = height ? parseInt( height, 10 ) : undefined;

const registry = useRegistry();
const imageRef = useRef();
const { allowResize = true } = context;
const { getBlock, getSettings } = useSelect( blockEditorStore );
Expand Down Expand Up @@ -496,7 +497,7 @@ export default function Image( {
lockUrlControls:
!! urlBinding &&
! urlBindingSource?.canUserEditValue?.( {
select,
registry,
context,
args: urlBinding?.args,
} ),
Expand All @@ -511,7 +512,7 @@ export default function Image( {
lockAltControls:
!! altBinding &&
! altBindingSource?.canUserEditValue?.( {
select,
registry,
context,
args: altBinding?.args,
} ),
Expand All @@ -525,7 +526,7 @@ export default function Image( {
lockTitleControls:
!! titleBinding &&
! titleBindingSource?.canUserEditValue?.( {
select,
registry,
context,
args: titleBinding?.args,
} ),
Expand Down
32 changes: 18 additions & 14 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ export default {
meta: newMeta,
} );
},
canUserEditValue( { select, context, args } ) {
canUserEditValue( { registry, context, args } ) {
// Lock editing in query loop.
if ( context?.query || context?.queryId ) {
return false;
}

const postType =
context?.postType || select( editorStore ).getCurrentPostType();
context?.postType ||
registry.select( editorStore ).getCurrentPostType();

// Check that editing is happening in the post editor and not a template.
if ( postType === 'wp_template' ) {
Expand All @@ -115,28 +116,31 @@ export default {

// Check that the custom field is not protected and available in the REST API.
// Empty string or `false` could be a valid value, so we need to check if the field value is undefined.
const fieldValue = select( coreDataStore ).getEntityRecord(
'postType',
postType,
context?.postId
)?.meta?.[ args.key ];
const fieldValue = registry
.select( coreDataStore )
.getEntityRecord( 'postType', postType, context?.postId )?.meta?.[
args.key
];

if ( fieldValue === undefined ) {
return false;
}
// Check that custom fields metabox is not enabled.
const areCustomFieldsEnabled =
select( editorStore ).getEditorSettings().enableCustomFields;
const areCustomFieldsEnabled = registry
.select( editorStore )
.getEditorSettings().enableCustomFields;
if ( areCustomFieldsEnabled ) {
return false;
}

// Check that the user has the capability to edit post meta.
const canUserEdit = select( coreDataStore ).canUser( 'update', {
kind: 'postType',
name: context?.postType,
id: context?.postId,
} );
const canUserEdit = registry
.select( coreDataStore )
.canUser( 'update', {
kind: 'postType',
name: context?.postType,
id: context?.postId,
} );
if ( ! canUserEdit ) {
return false;
}
Expand Down

0 comments on commit c6025de

Please sign in to comment.