1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_Google_Maps_Api_Key { |
4
|
|
|
static $option_key = 'jetpack_google_maps_api_data'; |
|
|
|
|
5
|
|
|
|
6
|
|
|
// return back the api key |
7
|
|
|
function get( $key ) { |
8
|
|
|
if ( ! empty( $key ) ) { |
9
|
|
|
return $key; |
10
|
|
|
} |
11
|
|
|
$api_key = get_option( self::$option_key, false ); |
12
|
|
|
if ( $api_key ) { |
13
|
|
|
if ( isset( $api_key->expires ) && $api_key->expires < time() ) { |
14
|
|
|
$this->maybe_refresh_on_shutdown(); |
15
|
|
|
} |
16
|
|
|
return $api_key->key; |
17
|
|
|
} |
18
|
|
|
if ( false === $api_key ) { |
19
|
|
|
$this->maybe_refresh_on_shutdown(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return $key; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function maybe_refresh_on_shutdown() { |
26
|
|
|
if ( ! has_action('shutdown', array( $this, 'refresh' ) ) ){ |
27
|
|
|
add_action( 'shutdown', array( $this, 'refresh' ) ); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
function refresh() { |
32
|
|
|
$data = $this->remote_get(); |
33
|
|
|
if ( $data ) { |
34
|
|
|
update_option( self::$option_key, $data ); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function set_transient() { |
39
|
|
|
set_transient( self::$option_key . '_temp', time(), HOUR_IN_SECONDS ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
function remote_get() { |
43
|
|
|
if ( get_transient( self::$option_key . '_temp' ) ) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Make the API request |
48
|
|
|
$request = sprintf( '/sites/%d/google-maps-api', Jetpack_Options::get_option( 'id' ) ); |
49
|
|
|
$response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '2', array(), null, 'wpcom' ); |
50
|
|
|
// Bail if there was an error or malformed response |
51
|
|
View Code Duplication |
if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { |
52
|
|
|
$this->set_transient(); |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$response = json_decode( $response['body'] ); |
57
|
|
|
if ( isset( $response->code ) ) { |
58
|
|
|
$this->set_transient(); |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $response; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function init() { |
66
|
|
|
add_filter( 'jetpack_google_maps_api_key', array( $this, 'get' ) ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.