diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php')
-rw-r--r-- | plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php | 118 |
1 files changed, 32 insertions, 86 deletions
diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php index edf174cf..8f279ea9 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php @@ -32,6 +32,7 @@ class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( __CLASS__, 'get_service_api_key' ), + 'permission_callback' => '__return_true', ), array( 'methods' => WP_REST_Server::EDITABLE, @@ -40,7 +41,7 @@ class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { 'args' => array( 'service_api_key' => array( 'required' => true, - 'type' => 'text', + 'type' => 'string', ), ), ), @@ -121,7 +122,10 @@ class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { switch ( $service ) { case 'mapbox': - $mapbox = self::get_service_api_key_mapbox(); + if ( ! class_exists( 'Jetpack_Mapbox_Helper' ) ) { + jetpack_require_lib( 'class-jetpack-mapbox-helper' ); + } + $mapbox = Jetpack_Mapbox_Helper::get_access_token(); $service_api_key = $mapbox['key']; $service_api_key_source = $mapbox['source']; break; @@ -168,10 +172,11 @@ class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { $message = esc_html__( 'API key updated successfully.', 'jetpack' ); Jetpack_Options::update_option( $option, $service_api_key ); return array( - 'code' => 'success', - 'service' => $service, - 'service_api_key' => Jetpack_Options::get_option( $option, '' ), - 'message' => $message, + 'code' => 'success', + 'service' => $service, + 'service_api_key' => Jetpack_Options::get_option( $option, '' ), + 'service_api_key_source' => 'site', + 'message' => $message, ); } @@ -192,11 +197,28 @@ class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { $option = self::key_for_api_service( $service ); Jetpack_Options::delete_option( $option ); $message = esc_html__( 'API key deleted successfully.', 'jetpack' ); + + switch ( $service ) { + case 'mapbox': + // After deleting a custom Mapbox key, try to revert to the WordPress.com one if available. + if ( ! class_exists( 'Jetpack_Mapbox_Helper' ) ) { + jetpack_require_lib( 'class-jetpack-mapbox-helper' ); + } + $mapbox = Jetpack_Mapbox_Helper::get_access_token(); + $service_api_key = $mapbox['key']; + $service_api_key_source = $mapbox['source']; + break; + default: + $service_api_key = Jetpack_Options::get_option( $option, '' ); + $service_api_key_source = 'site'; + }; + return array( - 'code' => 'success', - 'service' => $service, - 'service_api_key' => Jetpack_Options::get_option( $option, '' ), - 'message' => $message, + 'code' => 'success', + 'service' => $service, + 'service_api_key' => $service_api_key, + 'service_api_key_source' => $service_api_key_source, + 'message' => $message, ); } @@ -287,82 +309,6 @@ class WPCOM_REST_API_V2_Endpoint_Service_API_Keys extends WP_REST_Controller { } /** - * Get the site's own Mapbox API key if set, or the WordPress.com's one otherwise. - * - * @return array An array containing the key (if any) and its source ("site" or "wpcom"). - */ - public static function get_service_api_key_mapbox() { - // If the site provides its own Mapbox API key, return it. - $service_api_key = Jetpack_Options::get_option( self::key_for_api_service( 'mapbox' ) ); - if ( $service_api_key ) { - return self::format_api_key( $service_api_key ); - } - - // If the site is not WordPress.com, return an empty API key. - $site_id = self::get_wpcom_site_id(); - if ( ( ! self::is_wpcom() && ! jetpack_is_atomic_site() ) || ! $site_id ) { - return self::format_api_key(); - } - - // If there is a cached token, return it. - $transient_key = 'wpcom_mapbox_access_token'; - $cached_token = get_transient( $transient_key ); - if ( $cached_token ) { - return self::format_api_key( $cached_token, 'wpcom' ); - } - - // Otherwise retrieve a WordPress.com token. - $request_url = 'https://public-api.wordpress.com/wpcom/v2/sites/' . $site_id . '/mapbox'; - $response = wp_remote_get( esc_url_raw( $request_url ) ); - if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { - return self::format_api_key(); - } - - $response_body = json_decode( wp_remote_retrieve_body( $response ) ); - $wpcom_mapbox_access_token = $response_body->wpcom_mapbox_access_token; - - set_transient( $transient_key, $wpcom_mapbox_access_token, HOUR_IN_SECONDS ); - return self::format_api_key( $wpcom_mapbox_access_token, 'wpcom' ); - } - - /** - * Format an API key and its source into an array. - * - * @param string $key The API key. - * @param string $source The key's source ("site" or "wpcom"). - * @return array - */ - private static function format_api_key( $key = '', $source = 'site' ) { - return array( - 'key' => $key, - 'source' => $source, - ); - } - - /** - * Check if we're in WordPress.com. - * - * @return bool - */ - private static function is_wpcom() { - return defined( 'IS_WPCOM' ) && IS_WPCOM; - } - - /** - * Get the current site's WordPress.com ID. - * - * @return mixed The site's WordPress.com ID or an empty string. - */ - private static function get_wpcom_site_id() { - if ( self::is_wpcom() ) { - return get_current_blog_id(); - } elseif ( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) { - return Jetpack_Options::get_option( 'id' ); - } - return false; - } - - /** * Create site option key for service * * @param string $service The service to create key for. |