diff --git a/api/rest-api-filters/hook.php b/api/rest-api-filters/hook.php index d82985b1852..fc9113c6d84 100644 --- a/api/rest-api-filters/hook.php +++ b/api/rest-api-filters/hook.php @@ -1,17 +1,17 @@ 1 ) { - $key = array_shift( $keys ); - if (! isset( $array[$key] ) || ! is_array( $array[$key]) ) { + while ( count($keys) > 1 ) { + $key = array_shift($keys); + if (! isset($array[$key]) || ! is_array($array[$key]) ) { $array[$key] = []; } $array = &$array[$key]; @@ -23,27 +23,29 @@ function array_dot_set( &$array, $key, $value ) { add_filter( 'rest_request_after_callbacks', - function( $response, array $handler, \WP_REST_Request $request ) use ( $filters ) { - if ( ! $response instanceof \WP_REST_Response ) { - return $response; - } - $route = $request->get_route(); - $filters = array_filter( $filters, function( $filter ) use ( $request, $route ) { - if ( $filter['enabled'] && $filter['endpoint'] == $route ) { - return true; - } - return false; - }); + function ( $response, array $handler, \WP_REST_Request $request ) use ( $filters ) { + if (! $response instanceof \WP_REST_Response ) { + return $response; + } + $route = $request->get_route(); + $filters = array_filter( + $filters, function ( $filter ) use ( $request, $route ) { + if ($filter['enabled'] && $filter['endpoint'] == $route ) { + return true; + } + return false; + } + ); - $data = $response->get_data(); + $data = $response->get_data(); - foreach ( $filters as $filter ) { - array_dot_set( $data, $filter['dot_notation'], $filter['replacement'] ); - } + foreach ( $filters as $filter ) { + array_dot_set($data, $filter['dot_notation'], $filter['replacement']); + } - $response->set_data( $data ); + $response->set_data($data); - return $response; + return $response; }, 10, 3