Skip to content

Commit

Permalink
Extract code for making a single api request into a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Nov 12, 2018
1 parent a6a6b46 commit 5d0d024
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,29 +888,20 @@ function gutenberg_register_scripts_and_styles() {
add_action( 'admin_enqueue_scripts', 'gutenberg_register_scripts_and_styles', 5 );

/**
* Append result of internal request to REST API for purpose of preloading
* data to be attached to the page. Expected to be called in the context of
* `array_reduce`.
* Return result of internal request to REST API for purpose of preloading
* data to be attached to the page.
*
* @param array $memo Reduce accumulator.
* @param string $path REST API path to preload.
* @return array Modified reduce accumulator.
* @return array An associative array encapsulating data from the response.
*/
function gutenberg_preload_api_request( $memo, $path ) {

// array_reduce() doesn't support passing an array in PHP 5.2
// so we need to make sure we start with one.
if ( ! is_array( $memo ) ) {
$memo = array();
}

function gutenberg_api_request( $path ) {
if ( empty( $path ) ) {
return $memo;
return;
}

$path_parts = parse_url( $path );
if ( false === $path_parts ) {
return $memo;
return;
}

$request = new WP_REST_Request( 'GET', $path_parts['path'] );
Expand All @@ -928,11 +919,39 @@ function gutenberg_preload_api_request( $memo, $path ) {
$data['_links'] = $links;
}

$memo[ $path ] = array(
return array(
'body' => $data,
'headers' => $response->headers,
);
}
}

/**
* Append result of internal request to REST API for purpose of preloading
* data to be attached to the page. Expected to be called in the context of
* `array_reduce`.
*
* @param array $memo Reduce accumulator.
* @param string $path REST API path to preload.
* @return array Modified reduce accumulator.
*/
function gutenberg_preload_api_request( $memo, $path ) {

// array_reduce() doesn't support passing an array in PHP 5.2
// so we need to make sure we start with one.
if ( ! is_array( $memo ) ) {
$memo = array();
}

if ( empty( $path ) ) {
return $memo;
}

$reponse = gutenberg_api_request( $path );

if ( isset( $response ) ) {
$memo[ $path ] = $reponse;
}

return $memo;
}
Expand Down

0 comments on commit 5d0d024

Please sign in to comment.