PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / _inc / lib / class-jetpack-mapbox-helper.php

class-jetpack-mapbox-helper.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at _inc/lib/class-jetpack-mapbox-helper.php

112 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
112