Make WordPress Core

Opened 4 years ago

Last modified 8 weeks ago

#53603 new enhancement

Expose block data directly through REST API endpoints

Reported by: gziolo's profile gziolo Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 5.8
Component: REST API Keywords: needs-patch dev-feedback
Focuses: Cc:

Description

Part of "PHP APIs Overview" in Gutenberg: https://github.com/WordPress/gutenberg/issues/8352.

Originally reported in https://github.com/WordPress/gutenberg/issues/4763 by @adamsilverstein.

Consider adding Gutenberg block data to the post endpoints - so when retrieving a post via the REST API you could get the block data as part of the content object.

This provides a way to get the data/content of each block of a Gutenberg-built page from the front end. This would be very useful for building component based front ends, because the components could map one-to-one with gutenberg blocks. With these endpoints, an App could easily get the data it needs to render each component. This might also provide a patch for a standard component library matching Gutenberg blocks.

POC PR for this: https://github.com/WordPress/gutenberg/pull/2649

Related ticket with changes proposed to get_posts function: #53602.

Change History (7)

#1 @gziolo
4 years ago

There is also a plugin from @spacedmonkey that adds probably exactly the same functionality:

https://github.com/spacedmonkey/wp-rest-blocks

#2 @spacedmonkey
4 years ago

The biggest issue with extracting data from blocks, is attributes that are css selectors for html tags contents or attributes. Image blocks are the prefect example, as src and alt attributes are stored in the html.

In my plugin, I used a library called pQuery, as a way to use css selectors in PHP. Whatever goes into core, may want to use a different approach.

#3 @gziolo
4 years ago

In my plugin, I used a library called pQuery, as a way to use css selectors in PHP. Whatever goes into core, may want to use a different approach.

So you went one step ahead and mirrored how the blocks are parsed from the inner HTML in JavaScript. That's cool.

The other question is whether we do a good job dynamically injecting attributes in PHP that are derived from the supports field in Block API - things like align, colors, class names, etc.

#4 @adamsilverstein
4 years ago

  • Keywords needs-patch dev-feedback added
  • Milestone changed from Awaiting Review to Future Release

#5 @gziolo
4 years ago

There is also a prototype build 2 years ago for server-side block attributes sourcing:
https://github.com/WordPress/gutenberg/pull/18414

#6 @gziolo
2 months ago

@jonsurrell opened a new ticket #62653, that tracks progress on adding CSS selectors support to HTML API. That is a step towards having an efficient way to extract on the server sourced attributes from HTML saved for blocks.

Last edited 2 months ago by gziolo (previous) (diff)

#7 @jonsurrell
8 weeks ago

I've done some sketching in PR 8000 to see what it would look like to include sourced attributes, building on top of CSS Selectors in PR 7857. Early results are promising, although everything depends on #62653.

Note: See TracTickets for help on using tickets.