[WIP] Add ability to delete a reusable block #4031
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✨ What this is
This PR does a few things so as to close #3792 and close #4041.
🌱 Simplify
WP_REST_Blocks_Controller
At first, our API looked like this:
GET /gutenberg/v1/reusable-blocks
— fetch all blocksGET /gutenberg/v1/reusable-blocks/:uuid
— fetch a single blockPUT /gutenberg/v1/reusable-blocks/:uuid
— create or update a blockWe then switched to using IDs instead of UUIDs, which made our API look like this:
GET /gutenberg/v1/reusable-blocks
— fetch all blocksGET /gutenberg/v1/reusable-blocks/:id
— fetch a single blockPOST /gutenberg/v1/reusable-blocks
— create or update a blockPUT /gutenberg/v1/reusable-blocks/:id
— create or update a blockLook familiar? That's the same as our API for viewing and modifying regular posts!
In this PR, I switched the base class of
WP_REST_Blocks_Controller
fromWP_REST_Controller
to the less abstractWP_REST_Posts_Controller
. This lets us remove a lot of boilerplate, and gives us aDELETE
endpoint for free.I also took this opportunity to implement three smaller changes regarding the name of these routes:
reusable-block
to simplyblock
. This corresponds nicely to the name of our registered block type (core/block
) and the name of our custom post type (wp_block
).gutenberg/v1
towp/v2
. This means we can remove this annoying hack that I never liked, and should make things smoother for us when Gutenberg is eventually merged into Core.name
attribute in the resource totitle
. Blocks are custom post types: they have titles, not names.Our API now looks like a very regular REST endpoint:
GET /wp/v2/blocks
— fetch all blocksGET /wp/v2/blocks/:id
— fetch a single blockPOST /wp/v2/blocks
— create or update a blockPUT /wp/v2/blocks/:id
— create or update a blockDELETE /wp/v2/blocks/:id
— delete a block🗑 Add the ability to delete a Reusable Block
With the above changes, this is pretty straightforward:
DELETE_REUSABLE_BLOCK
effect which calls thedestroy()
method that the WP API Backbone library gives us for free.REMOVE_REUSABLE_BLOCK
which actually removes the block from local Redux state. If the delete fails,REMOVE_REUSABLE_BLOCK
is reverted.💆♀️ Improve the Reusable Block flow
I implemented the changes suggested by @karmatosed in #4041 by moving 'Detach Reusable Block' into a seperate settings menu section alongside our new 'Delete Reusable Block' button.
Here's how the settings menu now looks for a static block:
And here's how it now looks for a reusable block:
📋 How to test
🚧 What's left to do
core/block
blocks from the post when a reusable block is deletedwindow.confirm
with something better