-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Directory: Uninstall unused block types (#22918)
* Add unused block uninstall code into its own component. * Tweak saving logic * Fix mocked API response in test * Add new selectors to get used & unused block types This pulls `hasBlockType` out of the block uninstaller, and moves the logic into selectors so we can re-use it for the "blocks to install" list in pre-publish panel. * Update variable name to reflect nested structure Co-authored-by: dufresnesteven <[email protected]>
- Loading branch information
1 parent
a63375c
commit a0112fd
Showing
11 changed files
with
354 additions
and
9 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/block-directory/src/components/auto-block-uninstaller/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { unregisterBlockType } from '@wordpress/blocks'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { useEffect } from '@wordpress/element'; | ||
|
||
export default function AutoBlockUninstaller() { | ||
const { uninstallBlockType } = useDispatch( 'core/block-directory' ); | ||
|
||
const shouldRemoveBlockTypes = useSelect( ( select ) => { | ||
const { isAutosavingPost, isSavingPost } = select( 'core/editor' ); | ||
return isSavingPost() && ! isAutosavingPost(); | ||
} ); | ||
|
||
const unusedBlockTypes = useSelect( ( select ) => | ||
select( 'core/block-directory' ).getUnusedBlockTypes() | ||
); | ||
|
||
useEffect( () => { | ||
if ( shouldRemoveBlockTypes && unusedBlockTypes.length ) { | ||
unusedBlockTypes.forEach( ( blockType ) => { | ||
uninstallBlockType( blockType ); | ||
unregisterBlockType( blockType.name ); | ||
} ); | ||
} | ||
}, [ shouldRemoveBlockTypes ] ); | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.