Add reusable blocks REST API#2503
Add reusable blocks REST API#2503youknowriad merged 11 commits intoWordPress:masterfrom noisysocks:add/reusable-blocks-api
Conversation
|
For context, I'm working on building out the flow that @jasmussen describes in this comment. The goal is that users can:
This is a good place to start with and iterate on as it isn't too technically difficult, it offers a lot of value to users (e.g. one could create a custom HTML block to reuse throughout their blog), and it moves us a step closer to a world where Gutenberg can handle theme customisation. I'm splitting the overall work into roughly three PRs:
|
|
Thanks for working on this, this is a killer feature. I'm adding some REST API people as reviewers for a better review. I don't think the type and the attributes are being saved yet, are they? |
|
Thanks @youknowriad!
Ah, I forgot to update this PR's description! I decided to omit {
"id": "358b59ee-bab3-4d6f-8445-e8c6971a5605",
"name": "My cool block",
"content": "<!-- wp:core/paragraph -->\n<p>Hello <strong>World</strong>! How are you?</p>\n<!-- /wp:core/paragraph -->"
}This simplifies the client code that works with the API (here and here), since we can just use Gutenberg's |
|
@noisysocks Oh great! in that case, this is looking good to me. Might be better to get a REST folk Review and merge. |
|
@noisysocks I think it should include the block in the |
I personally don't like "duplication" because it means, something could go out of sync easily, and it seems weird to me to store the serialization and a half parsed block in the same structure. I see two options:
Unless we have the PHP implementations of the attributes matchers #2751 the first option is the achievable one. |
|
@youknowriad That's why I suggest that the The |
|
@westonruter The frontend code I wrote didn't use those properties, so I figured we ain't gonna need it. Happy to add them back in as |
Post types are immutable because each type has their own specific handling, and changing it would require two controllers working together to handle that. I'm not sure if that's something that needs to be considered for Gutenberg blocks? |
|
It just seemed that having a |
Adds a GET route for reading reusable blocks, and a PUT route for creating and editing reusable blocks.
The WP_Error wasn't being returned, which meant that a 400 response would not occur for invalid block PUT requests.
Adds a GET route for browsing reusable blocks.
We don't yet need this functionality, so best to leave it out for now (YAGNI). On the backend, this lets us not have to use post meta. On the frontend, it lets us not have to import private parts of the block serializer and parser.
PHPCS mistakenly thinks that our calls to $this->namespace are us using the namespace keyword, which is not available in PHP 5.2. The fix is to explicitly ignore the lint error.
Baby's first PR! 👶
This adds the REST API for creating, viewing and editing reusable blocks that @westonruter described in #2081. This API will let us add the ability to turn a static block into a dynamic reusable block (see #1516).
What I did
The TL;DR of it all is:
gb_reusable_blockGET /wp-json/gutenberg/v1/reusable-blocks- lists all reusable blocksGET /wp-json/gutenberg/v1/reusable-blocks/:uuid- fetches a single reusable blockPUT /wp-json/gutenberg/v1/reusable-blocks/:uuid- creates or edits a reusable block{ "id": "358b59ee-bab3-4d6f-8445-e8c6971a5605", "name": "My cool block", "content": "<!-- wp:core/paragraph -->\n<p>Hello <strong>World</strong>! How are you?</p>\n<!-- /wp:core/paragraph -->" }post_typeset togb_reusable_block.idis stored inpost_namenameis stored inpost_titlecontentis stored inpost_contentHow to test
OK. First, make sure you have the JSON Basic Authentication plugin installed, and that your test WordPress environment has pretty URLs enabled.
Now, you can create a block by hitting the
PUTendpoint:You'll likely want to change
admin:passwordandhttp://vagrant.localto match your test WordPress environment.Now that our block is created, we can fetch it:
We can also list all blocks:
Play around with creating, editing and reading a few different valid and invalid reusable blocks.
cc. @nb @mtias @westonruter