Add ability to delete a Reusable Block to REST API #4137
Merged
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 simplifies the Reusable Block REST API and adds a
DELETE
endpoint needed for #3792.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 theDELETE
endpoint that we need 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📋 How to test
Unit tests are included. No functionality has changed (yet), so regression testing should suffice: