-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Directory: Uninstall unused block types #22918
Conversation
Size Change: +451 B (0%) Total Size: 1.13 MB
ℹ️ View Unchanged
|
Good catch, I'll update that. |
@StevenDufresne The pre-publish panel should work correctly now. You can even watch blocks get added & removed from the list by opening the panel, and adding/removing installed blocks to your post. Once saved, the blocks not on the list will get uninstalled. I've also added tests for all this logic (in selectors & for |
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.
@StevenDufresne The pre-publish panel should work correctly now. You can even watch blocks get added & removed from the list by opening the panel, and adding/removing installed blocks to your post. Once saved, the blocks not on the list will get uninstalled. I've also added tests for all this logic (in selectors & for
hasBlockType
).
Yep, it's functioning well. 👍
I think the uninstall error handling will need another pass.
I also want to bubble up this experience that I reference in #22149
Notice how the sidebar changes dramatically because the block is no longer installed.
packages/block-directory/src/components/auto-block-uninstaller/index.js
Outdated
Show resolved
Hide resolved
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.
681be29
to
081b48d
Compare
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.
Looks good! Good job 🏆. Nice to have this back in.
Description
This pulls the rest of #22452 out, which uninstalls any block plugins that were installed from the block directory but not used in this post. See conversation in #22307 for more robust ideas, but this gets the work started with a basic implementation.
The new
AutoBlockUninstaller
component looks through the list of installed blocks (saved in state,getInstalledBlockTypes
) and filters out any that are not used in the current post. On save, if the post is not published, it loops through this unused block list, and removes those plugins & unregisters the blocks.The uninstall action is run on every save (except autosaves). The original intent was to be run only on publish, but I think this works better — if you save a draft without using Block A, it gets uninstalled. If you want to use that block after all, you can reinstall it. This approach also avoids leaving plugins in a half-installed state, where abandoned drafts are keeping some plugins installed-but-invisible.
Unfortunately someone can still use the plugin in another post concurrently with this one, otherwise the whole persistence issue would be solved 😆
How has this been tested?
Install a few blocks from the block directory, and use some in your post. Before saving, open the plugins list in a new tab (or list via wp-cli). All those plugins are installed. Save your post, it should save correctly, no errors. Reload the plugins list, the blocks that weren't used should be removed.
If there was an error uninstalling, it will display a notice in the editor.
Types of changes
New feature (non-breaking change which adds functionality)
Checklist: