From 78bb1ef482b3743152de6bf53fda46ce8afb3d0e Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 23 Jul 2018 16:10:01 +0800 Subject: [PATCH] Minor code cleanliness fixes --- lib/client-assets.php | 60 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 536b2a6fb92b00..53614f323d3be8 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -746,18 +746,12 @@ function gutenberg_register_scripts_and_styles() { */ function gutenberg_api_request( $path ) { if ( empty( $path ) ) { - return; + return null; } $path_parts = parse_url( $path ); if ( false === $path_parts ) { - return; - } - - $request = new WP_REST_Request( 'GET', $path_parts['path'] ); - if ( ! empty( $path_parts['query'] ) ) { - parse_str( $path_parts['query'], $query_params ); - $request->set_query_params( $query_params ); + return null; } // Ensure the global $post remains the same after the API request is made. @@ -767,28 +761,38 @@ function gutenberg_api_request( $path ) { global $post; $backup_global_post = $post; + $request = new WP_REST_Request( 'GET', $path_parts['path'] ); + if ( ! empty( $path_parts['query'] ) ) { + parse_str( $path_parts['query'], $query_params ); + $request->set_query_params( $query_params ); + } + $response = rest_do_request( $request ); // restore the global post. $post = $backup_global_post; - if ( 200 === $response->status ) { - $server = rest_get_server(); - $data = (array) $response->get_data(); - if ( method_exists( $server, 'get_compact_response_links' ) ) { - $links = call_user_func( array( $server, 'get_compact_response_links' ), $response ); - } else { - $links = call_user_func( array( $server, 'get_response_links' ), $response ); - } - if ( ! empty( $links ) ) { - $data['_links'] = $links; - } + if ( 200 !== $response->status ) { + return null; + } - return array( - 'body' => $data, - 'headers' => $response->headers, - ); + $server = rest_get_server(); + $data = (array) $response->get_data(); + + if ( method_exists( $server, 'get_compact_response_links' ) ) { + $links = call_user_func( array( $server, 'get_compact_response_links' ), $response ); + } else { + $links = call_user_func( array( $server, 'get_response_links' ), $response ); } + + if ( ! empty( $links ) ) { + $data['_links'] = $links; + } + + return array( + 'body' => $data, + 'headers' => $response->headers, + ); } /** @@ -807,14 +811,10 @@ function gutenberg_preload_api_request( $memo, $path ) { $memo = array(); } - if ( empty( $path ) ) { - return $memo; - } - - $response = gutenberg_api_request( $path ); + $api_response = gutenberg_api_request( $path ); - if ( isset( $response ) ) { - $memo[ $path ] = $response; + if ( isset( $api_response ) ) { + $memo[ $path ] = $api_response; } return $memo;