Make WordPress Core

Opened 9 years ago

Closed 7 years ago

#42875 closed defect (bug) (fixed)

Registering an "array" setting for the REST API causes a "Undefined index: items" warning

Reported by: perrywagle Owned by: TimothyBlynJacobs
Priority: normal Milestone: 5.4
Component: REST API Version: 4.9
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: docs

Description

Getting the error:

Undefined index: items in /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 328

The context of that line is that the variable is determined to be an 'array', but then gets indexed by the string 'items', which is not in the apparently associative array of associative array (?).

Apparently, some people trigger this, and some don't.

Attachments (3)

42875.diff​ (4.5 KB ) - added by TimothyBlynJacobs 7 years ago.
42875.2.diff​ (4.5 KB ) - added by TimothyBlynJacobs 7 years ago.
42875.3.diff​ (4.6 KB ) - added by TimothyBlynJacobs 7 years ago.

Download all attachments as: .zip

Change History (13)

#1 @perrywagle
9 years ago

Actually, with debugging on, it doesn't work.

#2 @perrywagle
9 years ago

AH. Its leaving out the

define( 'WP_DEBUG_DISPLAY', false );

that dies it (default true).

Seems seems to be a problem, just a different one.

Version 0, edited 9 years ago by perrywagle (next)

#3 @dshanske
9 years ago

I traced the issue, in discussions with @perrywagle to a plugin using register_setting to declare a type of 'array' and noting show_in_rest.

It says that valid types are 'string', 'boolean', 'integer', and 'number'. I don't recall seeing this when the feature was first extended. Either way, the addition of a function in 4.9 created this error if the type is not set to one of the above 4.

The simple solution is, if it is an unsupported type, it should generate an error about that.

#4 @TimothyBlynJacobs
7 years ago

  • Focuses docs added
  • Keywords has-patch has-unit-tests added
  • Milestone Awaiting Review → 5.4
  • Owner set to TimothyBlynJacobs
  • Status new → assigned
  • Summary /var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 328 → Registering an "array" setting for the REST API causes a "Undefined index: items" warning
  • Version 4.9.1 → 4.9

See also https://core.trac.wordpress.org/ticket/43392#comment:45 which had the same issue.

To summarize, when registering an array setting, a defined "items" schema is required. I've uploaded a patch that mirrors the fix in #43392, except that it won't prevent the registration of the setting. Since this code is already in the wild, I don't think we can prevent the registration. This means you'll still get the PHP warning as well.

This ticket was mentioned in ​Slack in #core-restapi by timothybjacobs. ​View the logs.


7 years ago

#6 @kadamwhite
7 years ago

We say "meta" when we mean setting, in one comment on the latest patch; and for clarity it might be good to have a test that explicitly asserts we DON'T get a doing_it_wrong when we DO provide the items schema, though that may be covered implicitly by the other tests.

#7 @TimothyBlynJacobs
7 years ago

I uploaded a new patch that fixes the documentation error.

Opted not to add an additional test since we do have the test_get_item_with_custom_array_setting test and it isn't clear what the assertion would be for just testing the doing it wrong.

This ticket was mentioned in ​Slack in #core-restapi by kadamwhite. ​View the logs.


7 years ago

This ticket was mentioned in ​PR #144 on ​WordPress/wordpress-develop by ​TimothyBJacobs.


7 years ago
#9

#10 @TimothyBlynJacobs
7 years ago

  • Resolution → fixed
  • Status assigned → closed

In 47325:

REST API: Warn if registering array settings without an items schema.

The documentation for register_setting has also been clarified to include all of the supported types and the flexibility of the show_in_rest parameter.

Fixes #42875.
Props perrywagle, dshanske, kadamwhite.

Note: See TracTickets for help on using tickets.