From aa6cb5f053300ce87ceb25a60390807ba6faef14 Mon Sep 17 00:00:00 2001 From: Copons Date: Thu, 23 Jan 2020 15:44:25 +0000 Subject: [PATCH] Fix missing Mapbox access token on WPCOM --- extensions/blocks/map/map.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/extensions/blocks/map/map.php b/extensions/blocks/map/map.php index 0faed156695a4..a1cbd8f338439 100644 --- a/extensions/blocks/map/map.php +++ b/extensions/blocks/map/map.php @@ -20,11 +20,17 @@ * @return string */ function jetpack_get_mapbox_api_key() { - if ( ! class_exists( 'WPCOM_REST_API_V2_Endpoint_Service_API_Keys' ) || ! Jetpack::is_active() ) { - return Jetpack_Options::get_option( 'mapbox_api_key' ); + $site_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? get_current_blog_id() : Jetpack_Options::get_option( 'id' ); + + $request_url = 'https://public-api.wordpress.com/wpcom/v2/sites/' . $site_id . '/service-api-keys/mapbox'; + $response = wp_remote_get( esc_url_raw( $request_url ) ); + + if ( 200 === wp_remote_retrieve_response_code( $response ) ) { + $response_body = json_decode( wp_remote_retrieve_body( $response ) ); + return $response_body->service_api_key; } - $response = WPCOM_REST_API_V2_Endpoint_Service_API_Keys::get_service_api_key( array( 'service' => 'mapbox' ) ); - return $response['service_api_key']; + + return Jetpack_Options::get_option( 'mapbox_api_key' ); } /**