Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
46.88% |
15 / 32 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Mapbox_Helper | |
46.88% |
15 / 32 |
|
50.00% |
2 / 4 |
60.33 | |
0.00% |
0 / 1 |
| get_access_token | |
31.82% |
7 / 22 |
|
0.00% |
0 / 1 |
41.70 | |||
| is_wpcom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| get_wpcom_site_id | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
5.02 | |||
| format_access_token | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Mapbox API helper. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Status\Host; |
| 9 | |
| 10 | /** |
| 11 | * Class Jetpack_Mapbox_Helper |
| 12 | */ |
| 13 | class Jetpack_Mapbox_Helper { |
| 14 | /** |
| 15 | * Site option key for the Mapbox service. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | private static $site_option_key = 'mapbox_api_key'; |
| 20 | |
| 21 | /** |
| 22 | * Transient key for the WordPress.com Mapbox access token. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private static $transient_key = 'wpcom_mapbox_access_token'; |
| 27 | |
| 28 | /** |
| 29 | * Get the site's own Mapbox access token if set, or the WordPress.com's one otherwise. |
| 30 | * |
| 31 | * @return array An array containing the key (if any) and its source ("site" or "wpcom"). |
| 32 | */ |
| 33 | public static function get_access_token() { |
| 34 | // If the site provides its own Mapbox access token, return it. |
| 35 | $service_api_key = Jetpack_Options::get_option( self::$site_option_key ); |
| 36 | if ( $service_api_key ) { |
| 37 | return self::format_access_token( $service_api_key ); |
| 38 | } |
| 39 | |
| 40 | $site_id = self::get_wpcom_site_id(); |
| 41 | |
| 42 | // If on WordPress.com, try to return the access token straight away. |
| 43 | if ( self::is_wpcom() && defined( 'WPCOM_MAPBOX_ACCESS_TOKEN' ) ) { |
| 44 | require_lib( 'mapbox-blocklist' ); |
| 45 | return wpcom_is_site_blocked_from_map_block( $site_id ) |
| 46 | ? self::format_access_token() |
| 47 | : self::format_access_token( WPCOM_MAPBOX_ACCESS_TOKEN, 'wpcom' ); |
| 48 | } |
| 49 | |
| 50 | // If not on WordPress.com or Atomic, return an empty access token. |
| 51 | if ( ! $site_id || ( ! self::is_wpcom() && ! ( new Host() )->is_woa_site() ) ) { |
| 52 | return self::format_access_token(); |
| 53 | } |
| 54 | |
| 55 | // If there is a cached token, return it. |
| 56 | $cached_token = get_transient( self::$transient_key ); |
| 57 | if ( $cached_token ) { |
| 58 | return self::format_access_token( $cached_token, 'wpcom' ); |
| 59 | } |
| 60 | |
| 61 | // Otherwise get it from the WordPress.com endpoint. |
| 62 | $request_url = 'https://public-api.wordpress.com/wpcom/v2/sites/' . $site_id . '/mapbox'; |
| 63 | $response = wp_remote_get( esc_url_raw( $request_url ) ); |
| 64 | if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 65 | return self::format_access_token(); |
| 66 | } |
| 67 | |
| 68 | $response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 69 | $wpcom_mapbox_access_token = $response_body->wpcom_mapbox_access_token; |
| 70 | |
| 71 | set_transient( self::$transient_key, $wpcom_mapbox_access_token, HOUR_IN_SECONDS ); |
| 72 | return self::format_access_token( $wpcom_mapbox_access_token, 'wpcom' ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check if we're in WordPress.com. |
| 77 | * |
| 78 | * @return bool |
| 79 | */ |
| 80 | private static function is_wpcom() { |
| 81 | return defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the current site's WordPress.com ID. |
| 86 | * |
| 87 | * @return mixed The site's WordPress.com ID. |
| 88 | */ |
| 89 | private static function get_wpcom_site_id() { |
| 90 | if ( self::is_wpcom() ) { |
| 91 | return get_current_blog_id(); |
| 92 | } elseif ( method_exists( 'Jetpack', 'is_connection_ready' ) && Jetpack::is_connection_ready() ) { |
| 93 | return Jetpack_Options::get_option( 'id' ); |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Format an access token and its source into an array. |
| 100 | * |
| 101 | * @param string $key The API key. |
| 102 | * @param string $source The key's source ("site" or "wpcom"). |
| 103 | * @return array |
| 104 | */ |
| 105 | private static function format_access_token( $key = '', $source = 'site' ) { |
| 106 | return array( |
| 107 | 'key' => $key, |
| 108 | 'source' => $source, |
| 109 | ); |
| 110 | } |
| 111 | } |