Skip to content
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

Expose rendered blocks in REST API endpoint for Reusable Blocks #8625

Closed
westonruter opened this issue Aug 6, 2018 · 4 comments
Closed

Expose rendered blocks in REST API endpoint for Reusable Blocks #8625

westonruter opened this issue Aug 6, 2018 · 4 comments
Labels
[Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Status] Needs More Info Follow-up required in order to be actionable.

Comments

@westonruter
Copy link
Member

westonruter commented Aug 6, 2018

I noticed an API discrepancy between REST API post endpoints and the blocks endpoint: the latter lacks the raw and rendered properties of the former. Instead, it contains a flat title and content:

'title' => array(
'description' => __( 'The block’s title.', 'gutenberg' ),
'type' => 'string',
'required' => true,
),
'content' => array(
'description' => __( 'The block’s HTML content.', 'gutenberg' ),
'type' => 'string',
'required' => true,
),

Compare with other endpoints in core for where title and content are defined with raw and rendered. (Granted, the title is less key to have rendered for Reusable Blocks.)

For parity with the other core endpoints, it would seem logical to include raw and rendered. But also this would be very useful. Consider case of creating a reusable block and then wanting to be able to share the URL of the block for embedding on other site such as via an iframe. If there was a rendered value that used the block's render_callback for dynamic blocks and which applied the_content filters, then you could reuse the Reusable Blocks outside of your WordPress install.

Allowing the rendered output of a block to be exposed in the REST API could also be very useful in a site as well. Consider a case of a site that has a 10 minute TTL cache in Varnish, but there is a Latest Posts block that they want updated every minute. If the Recent Posts block were a Reusable Block and could be accessed from the REST API, then an ESI could be used to incorporate the block's contents on the edge without requiring WordPress to re-generate the entire page. This approach could also be used on the client when a service worker is installed, as it could compose a page with composite streams. It could be used to compose pages client-side using structured block templates in #3588.

We'd need to be careful about permissions. We wouldn't want someone to create a Reusable Block with sensitive data in it and then have it unexpectedly exposed.

To illustrate further, if I create a Reusable Block for the Latest Posts block, there is a request to https://example.com/wp-json/wp/v2/blocks/8145 when I open the editor, and the response is:

{"id":8145,"title":"Untitled Reusable Block","content":"<!-- wp:latest-posts \/-->"}

However, I'd love to see a response instead like:

{
	"id": 8145,
	"title": {
		"raw": "Untitled Reusable Block",
		"rendered": "Untitled Reusable Block"
	},
	"content": {
		"raw": "<!-- wp:latest-posts \/-->",
		"rendered": "<ul class=\"wp-block-latest-posts aligncenter\"><li><a href..."
	}
}

REST API endpoints originally introduced in #2503.

/cc @noisysocks

@westonruter westonruter added the [Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) label Aug 6, 2018
@aduth
Copy link
Member

aduth commented Aug 6, 2018

Related: #7453 (removes custom endpoints, promotes on base posts controller)

@noisysocks
Copy link
Member

Good catch, @westonruter. Thanks for opening an issue.

As @aduth mentioned, it's likely we'll eliminate the custom controller altogether which means we'll get parity between the two REST endpoints for free.

@noisysocks noisysocks added this to the Merge Proposal: Editor milestone Aug 7, 2018
@mtias mtias modified the milestones: Merge: Editor, WordPress 5.0 Oct 12, 2018
@mtias
Copy link
Member

mtias commented Nov 1, 2018

@noisysocks what's the status of this? Is it obsolete now that it's using a regular post controller?

@mtias mtias added the [Status] Needs More Info Follow-up required in order to be actionable. label Nov 1, 2018
@aduth
Copy link
Member

aduth commented Nov 1, 2018

This should be considered as fixed by #10751, which drops the custom endpoint in favor of the existing posts endpoints.

@aduth aduth closed this as completed Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Synced Patterns Related to synced patterns (formerly reusable blocks) [Status] Needs More Info Follow-up required in order to be actionable.
Projects
None yet
Development

No branches or pull requests

4 participants