is_woa_site() ) ) { return self::format_access_token(); } // If there is a cached token, return it. $cached_token = get_transient( self::$transient_key ); if ( $cached_token ) { return self::format_access_token( $cached_token, 'wpcom' ); } // Otherwise get it from the WordPress.com endpoint. $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_access_token(); } $response_body = json_decode( wp_remote_retrieve_body( $response ) ); $wpcom_mapbox_access_token = $response_body->wpcom_mapbox_access_token; set_transient( self::$transient_key, $wpcom_mapbox_access_token, HOUR_IN_SECONDS ); return self::format_access_token( $wpcom_mapbox_access_token, 'wpcom' ); } /** * 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. */ private static function get_wpcom_site_id() { if ( self::is_wpcom() ) { return get_current_blog_id(); } elseif ( method_exists( 'Jetpack', 'is_connection_ready' ) && Jetpack::is_connection_ready() ) { return Jetpack_Options::get_option( 'id' ); } return false; } /** * Format an access token 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_access_token( $key = '', $source = 'site' ) { return array( 'key' => $key, 'source' => $source, ); } }