Skip to content

Commit

Permalink
Minor code cleanliness fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Sep 3, 2018
1 parent 047b952 commit 78bb1ef
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
);
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit 78bb1ef

Please sign in to comment.