From 8de877837c13861073c9cb01a8bbf45e2084877a Mon Sep 17 00:00:00 2001 From: Austin Passy <367897+thefrosty@users.noreply.github.com> Date: Mon, 23 May 2022 11:31:21 -0700 Subject: [PATCH 1/2] Check for WP_Error in wp_remote_get; fixes #2386 --- src/blocks/gist/index.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/blocks/gist/index.php b/src/blocks/gist/index.php index 582eb9a0ccc..ecd581e284b 100644 --- a/src/blocks/gist/index.php +++ b/src/blocks/gist/index.php @@ -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. From 129e2aec3b313543c1042a1e151bcc4050192a11 Mon Sep 17 00:00:00 2001 From: Austin Passy <367897+thefrosty@users.noreply.github.com> Date: Tue, 31 May 2022 11:12:47 -0700 Subject: [PATCH 2/2] Update src/blocks/gist/index.php --- src/blocks/gist/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blocks/gist/index.php b/src/blocks/gist/index.php index ecd581e284b..6ad5d3f1d59 100644 --- a/src/blocks/gist/index.php +++ b/src/blocks/gist/index.php @@ -48,7 +48,7 @@ function coblocks_block_gist_handler( $matches ) { $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 ) { + if ( !is_array( $result ) || empty( $result['files'] ) || is_wp_error( $result ) ) { return ''; }