Skip to content

Commit

Permalink
Check for WP_Error in wp_remote_get; fixes #2386 (#2387)
Browse files Browse the repository at this point in the history
* Check for WP_Error in wp_remote_get; fixes #2386

* Update src/blocks/gist/index.php
  • Loading branch information
thefrosty authored Jun 8, 2022
1 parent c22faea commit f591823
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/blocks/gist/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ function coblocks_block_gist_handler( $matches ) {
)
);

// JSON decode the response body.
// Then grab files from JSON object.
$body = json_decode( $api_resp['body'], true );
$files = empty( $body['files'] ) ? array() : $body['files'];
$resp_body = wp_remote_retrieve_body( $api_resp );
$result = json_decode( $resp_body, true );

if ( !is_array( $result ) || empty( $result['files'] ) || is_wp_error( $result ) ) {
return '';
}

// Map files object into filenames array.
$file_list = array_map(
$file_list = array_filter( array_map(
function( $file ) {
return $file['filename'];
return !empty( $file['filename'] ) ? $file['filename'] : null;
},
$files
);
$result['files']
) );

$script_src = untrailingslashit( $gist_path );
// .js suffix is needed after gist path for proper embed.
Expand Down

0 comments on commit f591823

Please sign in to comment.