Нет описания

class-jetpack-mapbox-helper.php 3.1KB

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