WordPress.org

Make WordPress Core

Opened 6 weeks ago

Closed 6 weeks ago

Last modified 6 weeks ago

#53949 closed defect (bug) (fixed)

WP_Media_List_Table::prepare_items(): fix PHP warning

Reported by: jrf Owned by: SergeyBiryukov
Milestone: 5.9 Priority: normal
Severity: normal Version:
Component: Administration Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

The following warnings could, in very select circumstances, be shown when the WP_Media_List_Table::prepare_items() method is called:

// PHP 8.0 and higher:
Warning: foreach() argument must be of type array|object, bool given

// PHP 5.6 - 7.4
Warning: Invalid argument supplied for foreach()

In WP_Media_List_Table::prepare_items(), the cron info array is retrieved via a call to _get_cron_array(), but as the documentation for that function (correctly) states, the return type of that function is array|false, where false is returned for a site, where no cron jobs have been scheduled (yet).

However, no type check is done on the return value and the method just blindly continues by using the return value in a foreach.

Fixed by adding validation for the returned value from _get_cron_array() and only running the foreach when the returned value is an array.

Ref: https://developer.wordpress.org/reference/functions/_get_cron_array/

Change History (4)

This ticket was mentioned in PR #1600 on WordPress/wordpress-develop by jrfnl.


6 weeks ago

### Tests: add test for WP_Media_List_Table::prepare_items()

... to specifically test a particular PHP warning.

### WP_Media_List_Table::prepare_items(): fix PHP warning

The following warnings could, in very select circumstances, be shown:

// PHP 8.0 and higher:
Warning: foreach() argument must be of type array|object, bool given

// PHP 5.6 - 7.4
Warning: Invalid argument supplied for foreach()

In WP_Media_List_Table::prepare_items(), the cron info array is retrieved via a call to _get_cron_array(), but as the documentation for that function (correctly) states, the return type of that function is array|false, where false is returned for a virgin site, where no cron jobs have been scheduled yet.

However, no type check is done on the return value and the method just blindly continues by using the return value in a foreach.

Fixed by adding validation for the returned value from _get_cron_array() and only running the foreach when the returned value is an array.

Ref: https://developer.wordpress.org/reference/functions/_get_cron_array/

Trac ticket: https://core.trac.wordpress.org/ticket/53949

#2 @mukesh27
6 weeks ago

Hi there! thanks for ticket and PR.

It looks fine to me.

#3 @SergeyBiryukov
6 weeks ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 51638:

Media: Check the return type of _get_cron_array() in WP_Media_List_Table::prepare_items().

The following warnings could, in very select circumstances, be shown:

// PHP 8.0 and higher:
Warning: foreach() argument must be of type array|object, bool given

// PHP 5.6 – 7.4
Warning: Invalid argument supplied for foreach()

In WP_Media_List_Table::prepare_items(), the cron info array is retrieved via a call to _get_cron_array(), but as the documentation (correctly) states, the return type of that function is array|false, where false is returned for a virgin site, with no cron jobs scheduled yet.

However, no type check is done on the return value, and the method just blindly continues by using it in a foreach.

Fixed by adding validation for the returned value from _get_cron_array() and only running the foreach when the returned value is an array.

Reference: WordPress Developer Resources: _get_cron_array()

Follow-up to [48417].

Props jrf, hellofromTonya, mukesh27.
Fixes #53949.

#4 @prbot
6 weeks ago

jrfnl commented on PR #1600:

Closing as committed via 51638.

Note: See TracTickets for help on using tickets.