WordPress.org

Make WordPress Core

Opened 15 months ago

Closed 13 months ago

Last modified 13 months ago

#51390 closed defect (bug) (fixed)

PHP error: Trying to access array offset on value of type bool in /wp-admin/includes/meta-boxes.php on line 482

Reported by: sproutchris Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version: 5.5.1
Component: Post Formats Keywords: has-unit-tests needs-testing
Focuses: Cc:

Description

I got an error in my debug log (running PHP 7.4):

Trying to access array offset on value of type bool in .../wp-admin/includes/meta-boxes.php on line 482

The code in question is:

...
$post_formats = get_theme_support( 'post-formats' );
if ( is_array( $post_formats[0] ) ) :
...

$post_formats equals false in my scenario. I haven't added support for "post-formats" in my theme (because I don't want it in my theme) so is this is expected behavior or is this maybe in itself what's going wrong here?

When WordPress then next asks PHP if $post_formats[0] is an array (is_array( $post_formats[0] )) when $post_formats is a boolean (therefore having no array items/indices), obviously PHP is going to throw this error. If get_theme_support() returning a boolean instead of an array is expected behavior, then I guess the easy fix would be:

if ( isset( $post_formats[0] ) && is_array( $post_formats[0] ) ) :

Attachments (5)

51390.1.patch (525 bytes) - added by Mista-Flo 14 months ago.
Patch without unit tests
51390.2.patch (1.1 KB) - added by Mista-Flo 14 months ago.
Patch with unit test
51390.3.patch (1.6 KB) - added by Mista-Flo 14 months ago.
Improve doc
51390.4.diff (1.5 KB) - added by garrett-eclipse 14 months ago.
Minor refresh to make the comment a sentence.
51390.5.diff (617 bytes) - added by garrett-eclipse 13 months ago.
Patch to fix issue in unit test suite.

Download all attachments as: .zip

Change History (26)

#1 @sproutchris
15 months ago

  • Summary changed from Trying to access array offset on value of type bool in /var/www/realestate.columbian.com/htdocs/wp-admin/includes/meta-boxes.php on line 482 to PHP error: Trying to access array offset on value of type bool in /wp-admin/includes/meta-boxes.php on line 482

#2 @sabernhardt
14 months ago

  • Component changed from General to Post Formats

#3 @Mista-Flo
14 months ago

  • Keywords reporter-feedback added

Hi there, thanks for your report.

It's a bit bizarre because you said your theme does not support post formats, but the line just before line 142 is:

	if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) :

So it should not go further in this if state if you have not enabled theme_supports for post-formats.

It seems to be an edge-case with thee theme, can you give more information about your theme or how to reproduce the issue easily?

#4 follow-up: @sproutchris
14 months ago

I'm assuming by line 142 that you're actually referring to line 479.

So I apologize, my theme does in fact support post formats. I missed the line ( add_theme_support('post-formats') ) when reading through my functions.php file. So this issue is occurring when the post formats are enabled for the theme.

I switched to twenty seventeen theme (that theme has post-formats theme support enabled) and the post formats meta box works fine. It is just in my custom theme that this error is occurring.

Instead of an array, get_theme_support('post-formats') on line 480 is returning true. Any idea what could cause that to happen?

#5 in reply to: ↑ 4 ; follow-up: @SergeyBiryukov
14 months ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to 5.6
  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

Replying to sproutchris:

Instead of an array, get_theme_support('post-formats') on line 480 is returning true. Any idea what could cause that to happen?

As noted in the HelpHub and the Theme Developer Handbook, adding post formats support is supposed to include an array of supported formats:

add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );

So this usage is incorrect (previously discussed in #15443 and decided against):

add_theme_support( 'post-formats' );

What happens is that the second argument of add_theme_support() defaults to true if not specified.

We should probably add a _doing_it_wrong() notice similar to the one displayed for 'html5' support:

_doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );

#6 @SergeyBiryukov
14 months ago

  • Keywords needs-unit-tests added

@Mista-Flo
14 months ago

Patch without unit tests

#7 @Mista-Flo
14 months ago

  • Keywords has-patch added; needs-patch removed

Hi, I have uploaded a patch despite I'm unable to have working unit tests for it, any idea how to do it?

@Mista-Flo
14 months ago

Patch with unit test

#8 @Mista-Flo
14 months ago

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

Actually, I have found a way to write the unit test.

#9 @sproutchris
14 months ago

Thanks a lot, guys.

@Mista-Flo
14 months ago

Improve doc

@garrett-eclipse
14 months ago

Minor refresh to make the comment a sentence.

#10 @garrett-eclipse
14 months ago

  • Keywords commit added

Thanks @Mista-Flo this tests well and unit tests are passing, moving forward for a committer review.

In 51390.4.diff the only change was adding the missing period to the comment sentence in test_post_format_doing_it_wrong.

This ticket was mentioned in Slack in #core by helen. View the logs.


13 months ago

#12 @helen
13 months ago

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

In 49344:

Post Formats: You have to pass an array of supported post formats.

This shows a _doing_it_wrong() message and also returns false instead of true if no array of formats is passed in add_theme_support(), avoiding a PHP error.

Post formats maintainership comes full circle. 🙃

Props Mista-Flo, sproutchris, garrett-eclipse.
Fixes #51390.

#13 @SergeyBiryukov
13 months ago

In 49354:

Themes: Move the test for post-formats theme support to a more appropriate place.

Follow-up to [49344].

See #51390.

#14 follow-up: @SergeyBiryukov
13 months ago

  • Keywords has-patch commit removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

After [49344], there is now some unexpected output in the unit test suite, as seen in this build for example:

Notice: add_theme_support( 'post-formats' ) was called <strong>incorrectly</strong>. You need to pass an array of types. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 5.6.0.) in /var/www/build/wp-includes/functions.php on line 5289

Cannot reproduce locally so far. Reopening for investigation.

Last edited 13 months ago by SergeyBiryukov (previous) (diff)

#15 in reply to: ↑ 5 @SergeyBiryukov
13 months ago

Replying to SergeyBiryukov:

We should probably add a _doing_it_wrong() notice similar to the one displayed for 'html5' support:

_doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );

Looks like there is actually a bug with that notice not being displayed as expected, see #51657.

This ticket was mentioned in Slack in #core by helen. View the logs.


13 months ago

#17 in reply to: ↑ 14 @garrett-eclipse
13 months ago

Replying to SergeyBiryukov:

After [49344], there is now some unexpected output in the unit test suite, as seen in this build for example:

Notice: add_theme_support( 'post-formats' ) was called <strong>incorrectly</strong>. You need to pass an array of types. Please see <a href="https://wordpress.org/support/article/debugging-in-wordpress/">Debugging in WordPress</a> for more information. (This message was added in version 5.6.0.) in /var/www/build/wp-includes/functions.php on line 5289

Cannot reproduce locally so far. Reopening for investigation.

I'm currently seeing this on my local MAMP install of trunk when running the full suite via grunt test. Mine is in the same location;
https://wordpress.slack.com/archives/C02RQBWTW/p1603919117252900

@garrett-eclipse
13 months ago

Patch to fix issue in unit test suite.

#18 follow-up: @garrett-eclipse
13 months ago

  • Keywords needs-testing added

After investigation @SergeyBiryukov I found it was the add_theme_support( 'post-formats', array( 'post', 'page' ) ); call in the rest-search-controller.php wpSetUpBeforeClass. Uploaded patch 51390.5.diff that fixed for my local by introducing post and page as an array to the function call.

#19 in reply to: ↑ 18 @SergeyBiryukov
13 months ago

Replying to garrett-eclipse:

After investigation @SergeyBiryukov I found it was the add_theme_support( 'post-formats', array( 'post', 'page' ) ); call in the rest-search-controller.php wpSetUpBeforeClass. Uploaded patch 51390.5.diff that fixed for my local by introducing post and page as an array to the function call.

Ah, that explains why the notice was only displayed when running the full suite and not when running the theme tests individually. Thanks for the patch!

#20 @SergeyBiryukov
13 months ago

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

In 49365:

Tests: Correct post formats registration in WP_REST_Search_Controller tests.

When declaring theme support for the post-formats feature, an array of supported post formats needs to be specified.

Follow-up to [49344], [49354].

Props garrett-eclipse.
Fixes #51390.

#21 @SergeyBiryukov
13 months ago

In 49367:

Themes: Clarify the _doing_it_wrong() message for post formats in add_theme_support().

Follow-up to [49344], [49354], [49365].

See #51390.

Note: See TracTickets for help on using tickets.