Skip to content

Experimental widgets editor - API and data model #24267

Closed
@adamziel

Description

The current version of the experimental widgets screen is able to store blocks in widget areas using the following trick: In core, all the widgets are stored in the sidebars_widgets like this:

[
"sidebar-1" => ["custom-html-3", "categories-2"],
"sidebar-2" => ["recent-posts-2", "recent-comments-1"],
]

Let's say you add a paragraph block to the sidebar-1 using the new screen. The stored option now becomes:

[
"sidebar-1" => 12,
"sidebar-2" => ["recent-posts-2", "recent-comments-1"],
]

Where 12 is a post ID holding a content similar to:

<!-- wp:paragraph --><p>My fine paragraph</p><!-- /wp:paragraph -->
<!-- wp:legacy-widget {"widgetClass":"WP_Widget_Custom_HTML","id":"custom_html-2","idBase":"custom_html","number":3,"instance":{"title":"","content":""}} /-->
<!-- wp:legacy-widget {"widgetClass":"WP_Widget_Categories","id":"categories-4","idBase":"categories","number":2,"instance":{"title":"","count":0,"hierarchical":0,"dropdown":0}} /-->

And whenever a consumer of that option attempts to read it through e.g. get_option, there are filters in place to transform it back to the array format:

[
"sidebar-1" => ["blocks-widget-03192b1a8bae97c12a9f94129a8326ac"],
"sidebar-2" => ["recent-posts-2", "recent-comments-1"],
]

The result is that the existing/legacy widgets screen says there is only one widget called Block area:

Zrzut ekranu 2020-07-29 o 12 45 57

And making any changes on that screen (e.g. adding a calendar widget) breaks the stored data: the post ID is lost and the updated option looks like this:

[
"sidebar-1" => ["calendar-4", "blocks-widget-03192b1a8bae97c12a9f94129a8326ac"],
"sidebar-2" => ["recent-posts-2", "recent-comments-1"],
]

My big question is: Do we need to work around the widgets data model like that?

The idea of using a post great if it wasn't for the BC - it is possible to go from the post ID to the array of widgets, but this operation is not easily invertible (if at all). This means that once a user saves the widgets on the new screen, the legacy screen is no longer able to correctly handle any writes. Was that intended? If yes, then we could likely close this issue and proceed as is. If we want to retain BC, then let's consider an alternative way forward. CC @noisysocks @mtias @draganescu

An alternative could look like this:

[
"sidebar-1" => ["blocks-widget-2", "calendar-4", "blocks-widget-4"],
]

In this scenario, we would say that a "block area" is just a regular widget. There would be class called WP_Widget_Blocks (tbd) which would handle rendering (probably not editing) of the widget on the existing/legacy screen. The experimental widgets screen would be based on the data model above. Block content related to each "block area" could be stored as an option just like all the other widgets store their settings. It could also diverge from that standard a bit and use a post if it would play better with some larger vision of block areas. The main point here is not changing the format of the sidebars_widgets option and using a regular WP_Widget class to support.

Activity

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

Metadata

Assignees

Labels

[Block] Legacy WidgetAffects the Legacy Widget Block - used for displaying Classic Widgets[Feature] Widgets ScreenThe block-based screen that replaced widgets.php.[Package] Edit Widgets/packages/edit-widgets

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions