Attributes: Add support for site options #3112
Closed
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.
Implements #2759
The essence
This pull request aims to add support for site options as an attribute source:
The first commit corresponds to the initial exploration, which works, though for simplicity it resorted to the pre-Gutenberg Backbone WP-API interface—
wp.api.models.Settings
—with an instance living ineditor/effects
to handle reads (populating Redux state via action dispatching) and writes (Model#save
method).Next: withAPIData
Following that exploration, thought was given to putting
withAPIData
(WAD) to use, in order to interact with WP-API at/wp/v2/settings
. Why? To see how far WAD can help Gutenberg satisfy its data needs across the board, to see if we can identify bits of it that could be improved.One-off, isolated use of site options
However, WAD is a higher-order component, thus designed to enhance components by injecting its data (which incidentally gets its own cache, invisibly shared across WAD-enhanced instances). It arguably has more local data needs in mind (for a specific component needing a specific external resource), potentially at odds with a new kind of attribute that is global to a site.
WAD’s ideal scenario it to enhance a block directly and statically (cf. example). However, we don’t want this explicit enhancement to be a burden for block authors seeking to interact with site options. Suppose, then, an enhancer—for now named
connectSiteOptions
—that builds on WAD:This is straightforward to implement.
withInterceptedAttributes
would inject the requested site options intoattributes
, and wrapsetAttributes
so as to intercept and handle attempts to set site options. Nonetheless, it shows limitations as soon as more than one block needs site options. For instance, if a block allows the user to change a site's title, should other blocks that present the site title immediately reflect the local change? Should they only update once that change is pushed to the server?Stretching the mechanism too far?
It would possible to make this scale for multiple blocks the following way:
VisualEditorBlockList
would be enhanced with a HoC that would keep its own state on site options.getSiteOptions
,setSiteOptions
) to its descendants.if attribute.name in siteOptionAttributes; then setRealAttribute(attribute); else setSiteOption(attribute)
.I got this to work conceptually, but it doesn't seem like anything we'd want.
As a virtual data-requesting component
Instead of working against Gutenberg's architecture, and namely its approach of centralized state, we could have something similar to Calypso's query components sitting in Layout or VisualEditor:
This still feels, in some way, like a suboptimal solution when compared to the existing paradigm in Gutenberg: views can dispatch actions signalling intent (to have data, to set/save it), effects react to these actions by interacting with the network and dispatching further actions, reducers commit state changes based on said actions, state is fed back into views. Suboptimal in the sense that effects (incl. XHR) are generated within a virtual component, rather than in
editor/effects
, and that the component would have to look at prop changes ofstate.saving.requesting
to infer post saves.One benefit of data "injection". as seen in §§ One-off, isolated use of site options & Stretching the mechanism too far?, is that it means that the Gutenberg "core" doesn't need to worry about different attribute sources (aside from a pass-through in the serializer, as seen here in the first commit).
Perhaps, if we are to follow the route of § As a virtual data-requesting component, devising some abstraction that could accommodate these new sources—site options, post meta, and others to come—would be a prerequisite.