WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 6 months ago

#39293 closed defect (bug) (fixed)

WordPress REST API warnings

Reported by: dreamon11 Owned by: jnylen0
Milestone: 4.7.1 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests commit fixed-major
Focuses: rest-api Cc:

Description (last modified by jnylen0)

Hello everybody,

A simple get request to wp-json/wp/v2/posts (or anything get request) give me these warnings:

( ! ) Warning: array_values() expects parameter 1 to be array, null given in /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php on line 1925

( ! ) Warning: array_merge(): Argument #2 is not an array in /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php on line 1925

( ! ) Warning: array_values() expects parameter 1 to be array, null given in /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php on line 1925

( ! ) Warning: array_merge(): Argument #2 is not an array in /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php on line 1925

( ! ) Warning: array_values() expects parameter 1 to be array, null given in /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php on line 1925

( ! ) Warning: Cannot modify header information - headers already sent by (output started at /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:1925) in /srv/www/xxx.fr/current/web/wp/wp-includes/rest-api/class-wp-rest-server.php on line 1244

At the end, expected result is here but warnings prevent software whichi make get request to retrieve it.

Attachments (1)

39293.diff (2.3 KB) - added by ChopinBach 6 months ago.
39293 Adds a test to handle edge case uses of add_theme_support. When add_theme_support( 'post-formats' ) is called, it resolves to true. This caused PHP to throw warnings expecting an array. This patch resolves this problem for any themes/plugins accidentally triggering this.

Download all attachments as: .zip

Change History (16)

#1 @dreamon11
6 months ago

Hummm... I cannot edit my first comment. If anyone can, please edit first line warning to delete my website domain. Thanks :)

#2 @ChopinBach
6 months ago

Hi @dreamon11,

Thank you for using the WordPress REST API! I am sorry you came across these warnings. From a quick look at the code, this is most likely occurring due to the active theme not supporting any post formats. There should be some safeguards put in place to prevent these PHP warnings. Thank you for reporting your issue. I will be double checking to verify if that is the problem.

#3 @ChopinBach
6 months ago

I can't duplicate the warnings what do get as a return value for get_theme_support( 'post_formats' )?

#4 @dhanendran
6 months ago

Hi @dreamon11 I couldn't reproduce this warnings. I also tried removing all post formats manually and checked it, still can't reproduce this issue. Can you give us more information on this ticket as requested by @ChopinBach.

#5 @dreamon11
6 months ago

Problem fixed.

In my theme functions.php file, I have the following code:

add_theme_support('post-formats');

Replace with:

add_theme_support('post-formats', []);

Or simply remove post-formats theme support.

And no more warnings ;)

#6 @dreamon11
6 months ago

@ChopinBach @dhanendran Thanks for help.

Do you know how can I edit the first comment? Or contact an admin to edit it?

#7 @jnylen0
6 months ago

  • Keywords needs-patch added

It's valid to call add_theme_support( 'post-formats' ) with no additional arguments (docs), though I'm not sure how useful that is.

Per the `get_theme_support` documentation the function will return true in this situation (and it could also return false in other situations). So we should add is_array checks for both $supports_formats and $supports_formats[0] here.

#8 @jnylen0
6 months ago

  • Description modified (diff)

#9 @ChopinBach
6 months ago

Okay, I will write a patch for this later today then, at the very least it will prevent this from happening to anyone else.

@ChopinBach
6 months ago

39293 Adds a test to handle edge case uses of add_theme_support. When add_theme_support( 'post-formats' ) is called, it resolves to true. This caused PHP to throw warnings expecting an array. This patch resolves this problem for any themes/plugins accidentally triggering this.

#10 @ChopinBach
6 months ago

  • Keywords has-patch has-unit-tests added; needs-patch removed

#11 @jnylen0
6 months ago

  • Milestone changed from Awaiting Review to 4.7.1
  • Owner set to jnylen0
  • Status changed from new to accepted

#12 @pento
6 months ago

  • Keywords commit added

#13 @jnylen0
6 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 39620:

REST API: Fix PHP warnings when get_theme_support( 'post-formats' ) is not an array.

If add_theme_support( 'post-formats' ) is called with no additional
arguments, then get_theme_support( 'post-formats' ) returns true rather
than an array of supported formats. Avoid generating PHP warnings in this
situation.

Props dreamon11, ChopinBach.
Fixes #39293.

#14 @jnylen0
6 months ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Needs to be ported to the 4.7 branch.

#15 @pento
6 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 39630:

REST API: Fix PHP warnings when get_theme_support( 'post-formats' ) is not an array.

If add_theme_support( 'post-formats' ) is called with no additional arguments, then get_theme_support( 'post-formats' ) returns true rather than an array of supported formats. Avoid generating PHP warnings in this situation.

Merge of [39620] to the 4.7 branch.

Props dreamon11, ChopinBach.
Fixes #39293.

Note: See TracTickets for help on using tickets.