[wp-trac] [WordPress Trac] #51393: REST API: Inconsistent type for 'media_details' and 'sizes' in attachments controller

WordPress Trac noreply at wordpress.org
Thu Sep 24 23:53:18 UTC 2020


#51393: REST API: Inconsistent type for 'media_details' and 'sizes' in attachments
controller
----------------------------+----------------------------
 Reporter:  SergeyBiryukov  |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  5.6
Component:  REST API        |    Version:
 Severity:  normal          |   Keywords:  has-patch php8
  Focuses:                  |
----------------------------+----------------------------
 Background: #50913, #50640.

 While working on the tickets above, I've discovered an inconsistency in
 `WP_REST_Attachments_Controller`.

 In the `::prepare_item_for_response()` method, `$data['media_details']` is
 treated as an `array` if not empty, but is initialized as an `stdClass`
 object if empty. Same for `$data['media_details']['sizes']`.

 This was introduced along with the class itself in [38832].

 This is currently causing [https://travis-ci.com/github/WordPress
 /wordpress-develop/jobs/390243311 two errors on PHP 8]:
 {{{
 1) WP_Test_REST_Attachments_Controller::test_get_item_sizes
 Error: Cannot use object of type stdClass as array

 /var/www/tests/phpunit/tests/rest-api/rest-attachments-controller.php:607

 2) WP_Test_REST_Attachments_Controller::test_get_item_sizes_with_no_url
 Error: Cannot use object of type stdClass as array

 /var/www/tests/phpunit/tests/rest-api/rest-attachments-controller.php:635
 }}}
 The errors are caused by `$data['media_details']['sizes']` being
 initialized as an empty object, due to missing GD library in the PHP 8
 build in the Docker image used for the tests. See comment:10:ticket:50640
 for some more context.

 The same errors can also be reproduced on PHP 7 or 5.6 if the GD library
 is not loaded.

 The tests should probably be updated to check if
 `$data['media_details']['sizes']` is not empty before proceeding, or to
 require the GD library, but the main issue remains.

 Could we switch these properties to be initialized as an empty array
 instead of an object? See the attached patch.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51393>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list