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

Add ability to delete a Reusable Block to REST API #4137

Merged
merged 1 commit into from Dec 27, 2017

Conversation

Projects
None yet
2 participants
@noisysocks
Member

noisysocks commented Dec 22, 2017

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 blocks
  • GET /gutenberg/v1/reusable-blocks/:uuid — fetch a single block
  • PUT /gutenberg/v1/reusable-blocks/:uuid — create or update a block

We then switched to using IDs instead of UUIDs, which made our API look like this:

  • GET /gutenberg/v1/reusable-blocks — fetch all blocks
  • GET /gutenberg/v1/reusable-blocks/:id — fetch a single block
  • POST /gutenberg/v1/reusable-blocks — create or update a block
  • PUT /gutenberg/v1/reusable-blocks/:id — create or update a block

Look 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 from WP_REST_Controller to the less abstract WP_REST_Posts_Controller. This lets us remove a lot of boilerplate, and gives us the DELETE endpoint that we need for free.

I also took this opportunity to implement three smaller changes regarding the name of these routes:

  1. I've changed the name of the resource from reusable-block to simply block. This corresponds nicely to the name of our registered block type (core/block) and the name of our custom post type (wp_block).
  2. I've moved the namespace of these REST endpoints from gutenberg/v1 to wp/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.
  3. I've renamed the name attribute in the resource to title. 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 blocks
  • GET /wp/v2/blocks/:id — fetch a single block
  • POST /wp/v2/blocks — create or update a block
  • PUT /wp/v2/blocks/:id — create or update a block
  • DELETE /wp/v2/blocks/:id — delete a block

📋 How to test

Unit tests are included. No functionality has changed (yet), so regression testing should suffice:

  1. Create a block
  2. Convert it to a reusable block
  3. Rename the reusable block
  4. Edit the reusable block
Refactor Reusable Blocks controller to use WP_REST_Posts_Controller
We can remove a lot of code by subclassing WP_REST_Posts_Controller,
which gives us methods for creating, reading, updating and deleting
posts (in this case, blocks) for free.

This also adds support for DELETEing a block to the API, and renames the
'name' field to 'title'.
@noisysocks

This comment has been minimized.

Show comment
Hide comment
@noisysocks
Member

noisysocks commented Dec 22, 2017

@youknowriad

Awesome PR. I like the deleted lines here :)

A bit concerned about the wp/v2/blocks. Existing websites could have registered a blocks CPT with the same name. Maybe we should just document it as a new reserved CPT.

@noisysocks

This comment has been minimized.

Show comment
Hide comment
@noisysocks

noisysocks Dec 27, 2017

Member

Thanks @youknowriad! I created an issue (#4190) to remind us to look into documenting that this could be a breaking change.

Member

noisysocks commented Dec 27, 2017

Thanks @youknowriad! I created an issue (#4190) to remind us to look into documenting that this could be a breaking change.

@noisysocks noisysocks merged commit 1ff0f5f into master Dec 27, 2017

3 checks passed

codecov/project 39.08% (+0%) compared to 5aabd70
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@noisysocks noisysocks deleted the add/reusable-block-deletion-to-api branch Dec 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment