WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 3 years ago

#35574 new enhancement

Add schema information to WP_Widget — at Version 6

Reported by: westonruter Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 2.8
Component: Widgets Keywords: needs-patch needs-unit-tests
Focuses: rest-api Cc:

Description (last modified by westonruter)

Widgets often have duplicated logic between the WP_Widget::widget(), WP_Widget::update(), and WP_Widget::form() methods for checking if a given $instance property has been set, and if not, supplying a default value. In some cases, these isset checks are not performed resulting in PHP notices if the methods are programmatically invoked with an empty array. I suggest that we introduce a WP_Widget::$default_instance property or WP_Widget::get_default_instance() method that will return the $instance defaults that can be merged on top of with the current $instance.

With the REST API, there is an emerging-established way in WordPress for describing a data structure, such as a widget instance. One aspect of the REST API endpoint schema is the default values for given fields on a property.

Widgets in WordPress are assumed to be arrays, with the applied filters and function return values. With a schema defined, the data type of a widget instance would be guaranteed. Meta in the REST API is a challenge given that it may or may not contain serialized PHP. Widgets are stored in serialized PHP arrays in WP options (though it is possible to store them in posts, per #32474). Additionally, the instance arrays may also contain PHP objects for classes that cannot be cleanly serialized into JSON, and having a REST API JSON schema defined for a widget would guarantee that a widget instance can be represented in JSON. This would, in turn, allow widgets to be exposed as endpoints in the REST API, and it would allow widget instances to be completely manipulated with JavaScript (such as in the Customizer, as described in #33507).

By adding schema information to WP_Widget, we get a lot more than having default $instance data available. For one, the widget form can be automatically generated based on the schema if one is not defined (i.e. if noform is returned from the WP_Widget::form() method). (This may actually be focus of the Fields API.)

Change History (7)

@westonruter
4 years ago

WIP

#1 @westonruter
4 years ago

  • Keywords needs-patch dev-feedback needs-unit-tests added

35574.wip.diff is a first stab. If this ticket has support from other devs, we can flush it out.

#2 @sebastian.pisula
4 years ago

Oo good idea ;)

#3 @jdgrimes
4 years ago

+1. I do something like this already for widgets in one of my plugins.

#4 @boonebgorges
4 years ago

+1 to something like this. If this came up for @westonruter in the context of adding new widgets in the Customizer, then I have run into the very problem before :)

#5 @westonruter
4 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Awaiting Review to Future Release
  • Version set to 2.8

#6 @westonruter
4 years ago

  • Description modified (diff)
  • Summary changed from Add default instance information to WP_Widget to Add schema information to WP_Widget
Note: See TracTickets for help on using tickets.