[wp-trac] [WordPress Trac] #51986: PHP Warning: array_intersect_key(): Expected parameter 1 to be an array, string given in class-wp-rest-server.php

WordPress Trac noreply at wordpress.org
Tue Aug 24 13:15:06 UTC 2021


#51986: PHP Warning: array_intersect_key(): Expected parameter 1 to be an array,
string given in class-wp-rest-server.php
---------------------------------------------+-----------------------------
 Reporter:  slaFFik                          |       Owner:  (none)
     Type:  defect (bug)                     |      Status:  new
 Priority:  normal                           |   Milestone:  Future Release
Component:  Editor                           |     Version:  5.6
 Severity:  critical                         |  Resolution:
 Keywords:  php8 has-patch needs-unit-tests  |     Focuses:  rest-api
---------------------------------------------+-----------------------------
Changes (by Hinjiriyo):

 * severity:  normal => critical


Comment:

 The fatal error still appears in WP 5.8. When I try to call a post edit
 page, PHP throws in debug mode the following lines:

 {{{
 Fatal error: Uncaught TypeError: array_intersect_key(): Argument
 #1 ($array) must be of type array, string given in /wp-includes/rest-api
 /class-wp-rest-server.php:1436
 Stack trace:
 #0 /wp-includes/rest-api/class-wp-rest-server.php(1436):
 array_intersect_key('footer', Array)
 #1 /wp-includes/rest-api/class-wp-rest-server.php(1358):
 WP_REST_Server->get_data_for_route('/gridd/v1/parti...', Array, 'view')
 #2 /wp-includes/rest-api/class-wp-rest-server.php(1224):
 WP_REST_Server->get_data_for_routes(Array, 'view')
 #3 /wp-includes/rest-api/class-wp-rest-server.php(1140):
 WP_REST_Server->get_index(Object(WP_REST_Request))
 #4 /wp-includes/rest-api/class-wp-rest-server.php(987):
 WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/', Array,
 NULL)
 #5 /wp-includes/rest-api.php(495):
 WP_REST_Server->dispatch(Object(WP_REST_Request))
 #6 /wp-includes/rest-api.php(2832):
 rest_do_request(Object(WP_REST_Request))
 #7 [internal function]: rest_preload_api_request(Array, '/')
 #8 /wp-includes/block-editor.php(444): array_reduce(Array,
 'rest_preload_ap...', Array)
 #9 /wp-admin/edit-form-blocks.php(68):
 block_editor_rest_api_preload(Array, Object(WP_Block_Editor_Context))
 #10 /wp-admin/post.php(187): require('/www/htdocs/...')
 #11 {main} thrown in /wp-includes/rest-api/class-wp-rest-server.php on
 line 1436
 }}}

 The error appears even no plugin is active. So I'm supposing a core bug.

 In the file **/wp-includes/rest-api/class-wp-rest-server.php** in line
 143****6 the variable **$opts** sometimes contains a string. Since it is
 used in **array_intersect_key()** which expects an array, an error is
 raised.

 With a little debugging **$opts** yields on the test installation in the
 loop sth. like:
 {{{#!php
 $opts = 'footer';
 $opts = 'sidebar_1';
 $opts = 'sidebar_2';
 $opts = 'sidebar_3';
 }}}

 I inserted a quick workaround before line 1436 to avoid the error:
 {{{#!php
 if ( ! is_array( $opts ) ) {
         continue;
 }
 }}}

 Now editing a post and page is possible.

 That is just a quick approach without any tests for side effects. So I'm
 warning about using that fix. I'm glad if there would be a better
 solution.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51986#comment:27>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list