-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Fix: Block manager doesn't respect allowed_block_types hook #16586
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext } from '@wordpress/element'; | ||
|
||
const EditPostSettings = createContext( {} ); | ||
export default EditPostSettings; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { without, map } from 'lodash'; | ||
import { includes, map, without } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useContext, useMemo } from '@wordpress/element'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { compose, withInstanceId } from '@wordpress/compose'; | ||
import { CheckboxControl } from '@wordpress/components'; | ||
|
@@ -14,6 +15,7 @@ import { CheckboxControl } from '@wordpress/components'; | |
* Internal dependencies | ||
*/ | ||
import BlockTypesChecklist from './checklist'; | ||
import EditPostSettings from '../edit-post-settings'; | ||
|
||
function BlockManagerCategory( { | ||
instanceId, | ||
|
@@ -23,18 +25,32 @@ function BlockManagerCategory( { | |
toggleVisible, | ||
toggleAllVisible, | ||
} ) { | ||
if ( ! blockTypes.length ) { | ||
const settings = useContext( EditPostSettings ); | ||
const { allowedBlockTypes } = settings; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are all available in the editor store why not just use that store instead of adding a new context provider? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @youknowriad, when a user disables a block in the block manager the block is removed from the editor store allowed blocks. Using just the editor store, it is not possible to differentiate between a block disabled by the user and a block restricted on the server using a plugin. In this case, we need to distinguish both cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's minor but It feels like something that can be solved in the store somehow instead of adding a new context for sharing data while we already have a context for that (the store). |
||
const filteredBlockTypes = useMemo( | ||
() => { | ||
if ( allowedBlockTypes === true ) { | ||
return blockTypes; | ||
} | ||
return blockTypes.filter( ( { name } ) => { | ||
return includes( allowedBlockTypes || [], name ); | ||
} ); | ||
}, | ||
[ allowedBlockTypes, blockTypes ] | ||
); | ||
|
||
if ( ! filteredBlockTypes.length ) { | ||
return null; | ||
} | ||
|
||
const checkedBlockNames = without( | ||
map( blockTypes, 'name' ), | ||
map( filteredBlockTypes, 'name' ), | ||
...hiddenBlockTypes | ||
); | ||
|
||
const titleId = 'edit-post-manage-blocks-modal__category-title-' + instanceId; | ||
|
||
const isAllChecked = checkedBlockNames.length === blockTypes.length; | ||
const isAllChecked = checkedBlockNames.length === filteredBlockTypes.length; | ||
|
||
let ariaChecked; | ||
if ( isAllChecked ) { | ||
|
@@ -59,7 +75,7 @@ function BlockManagerCategory( { | |
label={ <span id={ titleId }>{ category.title }</span> } | ||
/> | ||
<BlockTypesChecklist | ||
blockTypes={ blockTypes } | ||
blockTypes={ filteredBlockTypes } | ||
value={ checkedBlockNames } | ||
onItemChange={ toggleVisible } | ||
/> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is more for convenience in making the settings available to the modal component? I thought whether we had this tracked in state, but I guess that's only in the
editor
andblock-editor
modules. We could do the same in this module as well, but it might be a bit overkill for the one setting we're interested in (I also feel it's already become difficult to manage in the two modules it is present).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I used context as a convenience. Adding it to the edit-post store seemed overkill, and it seemed it would just bring more confusion, I guess we can reconsider later if we have other reasons to add it. While if we add now, I think we can not easily remove them because of o back-compatibility.