-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpsstm-core-api.php
229 lines (166 loc) · 6.49 KB
/
wpsstm-core-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
//define('WPSSTM_API_URL','http://localhost:3000/');
define('WPSSTM_API_URL','https://wpsstmapi.herokuapp.com/');
define('WPSSTM_API_REGISTER_URL','https://github.com/gordielachance/wp-soundsystem/wiki/SoundSystem-API');
class WPSSTM_Core_API {
public static $token_transient_name = 'wpsstmapi_token';
public static $premium_userdata_transient_name = 'wpsstmapi_premium_expiry';
function __construct(){
add_filter( 'wpsstm_autolink_input',array($this,'api_track_autolink'), 5, 2 );
}
public static function get_api_userdatas(){
if ( false === ( $datas = get_transient(self::$premium_userdata_transient_name ) ) ) {
WP_SoundSystem::debug_log('get api user datas...');
$datas = WPSSTM_Core_API::api_request('v2/auth/userdata');
if ( is_wp_error($datas) ) return $datas;
set_transient( self::$premium_userdata_transient_name, $datas, 1 * DAY_IN_SECONDS );
}
return $datas;
}
public static function is_premium(){
$membership = WPSSTM_Core_API::get_api_userdatas();
if ( is_wp_error($membership) ){
return false;
}
return isset($membership['is_premium']) ? (bool)$membership['is_premium'] : false;
}
public static function get_token(){
$response = null;
$valid = false;
if ( !$api_key = wpsstm()->get_options('wpsstmapi_key') ){
return false;
}
if ( false === ( $token = get_transient(self::$token_transient_name ) ) ) {
$url = WPSSTM_API_URL . 'v2/auth/token';
//build headers
$args = array(
'body' => array(
'api_key' => $api_key
)
);
$request = wp_remote_post($url,$args);
if ( is_wp_error($request) ) return $request;
$response_code = wp_remote_retrieve_response_code($request);
if ( $response_code == 503 ){
$message = __('API unavailable','wpsstm');
WP_SoundSystem::debug_log($message);
return new WP_Error('api_error',$message );
}
$response = wp_remote_retrieve_body( $request );
if ( is_wp_error($response) ) return $response;
$response = json_decode($response, true);
//check error in JSON response
//TOUCHECK TOUFIX
if ( $error = wpsstm_get_array_value(array('error'),$response) ){
$first_value = reset($error);
$first_key = key($error);
return new WP_Error($first_key,$first_value);
}
if ( !$token = wpsstm_get_array_value(array('token'),$response) ){
return new WP_Error('no_token','Missing token');
}
//API token is 25 hours
set_transient( self::$token_transient_name, $token, 1 * DAY_IN_SECONDS );
}
return $token;
}
static function api_request($endpoint, $params=null,$method = 'GET'){
if (!$endpoint){
return new WP_Error('wpsstmapi_no_api_url',__("Missing API endpoint",'wpsstm'));
}
//TOUFIX URGENT we don't need this premium check : request should be (or not) accepted at the API url.
$premium_endpoints = array(
'v2/playlist/import',
'v2/track/links'
);
if ( in_array($endpoint,$premium_endpoints) && !self::is_premium() ){
return new WP_Error('api_key_required',__("A SoundSystem API key is required.",'wpsstm'));
}
$rest_url = WPSSTM_API_URL . $endpoint;
//build headers
$api_args = array(
'method' => $method,
'timeout' => wpsstm()->get_options('wpsstmapi_timeout'),
'headers'=> array(
'Accept' => 'application/json',
)
);
//parameters
if ($params){
$params = wpsstm_clean_array($params);
if ($method == 'GET'){
$url_params = http_build_query($params);
$rest_url .= '?' . $url_params;
}elseif ($method == 'POST' || $method == 'PUT') {
$api_args['body'] = $params;
}
}
//token
if ( ( $token = self::get_token() ) && !is_wp_error($token) ){
$api_args['headers']['Authorization'] = sprintf('Bearer %s',$token);
}
WP_SoundSystem::debug_log(array('endpoint'=>$endpoint,'params'=>$params,'args'=>$api_args,'method'=>$method,'url'=>$rest_url),'remote API query...');
$request = wp_remote_request($rest_url,$api_args);
if ( is_wp_error($request) ){
WP_SoundSystem::debug_log($request->get_error_message());
return $request;
}
$headers = wp_remote_retrieve_headers($request);
$response_code = wp_remote_retrieve_response_code($request);
if ( $response_code == 503 ){
$message = __('API unavailable','wpsstm');
WP_SoundSystem::debug_log($message);
return new WP_Error('api_error',$message );
}
//invalid token, redo.
if ( in_array($response_code,array(401,422,498)) ){
WP_SoundSystem::debug_log('invalid token, force clear');
WPSSTM_Settings::clear_premium_transients();
}
$response = wp_remote_retrieve_body( $request );
$response = json_decode($response, true);
//api error
if ( $error_msg = wpsstm_get_array_value('error',$response) ){
$error = sprintf(__('Error %s: %s','wpsstm'),$response_code,$error_msg);
WP_SoundSystem::debug_log($error);
return new WP_Error('api_error',$error );
}
if ( is_wp_error($response) ){
WP_SoundSystem::debug_log($response->get_error_message());
return $response;
}
return $response;
}
function api_track_autolink($links,$track){
if (!$track->post_id){
return new WP_Error( 'missing_track_id',__( 'Missing Track ID.', 'wpsstm' ));
}
$spotify_engine = new WPSSTM_Spotify_Data();
if ( !$music_id = $spotify_engine->get_post_music_id($track->post_id) ){
$music_id = $spotify_engine->auto_music_id($track->post_id); //we need a Spotify ID here
if ( is_wp_error($music_id) ) $music_id = null;
}
if ( !$music_id ){
return new WP_Error( 'missing_spotify_id',__( 'Missing Spotify ID.', 'wpsstmapi' ));
}
$args = [
'track' => [ //jspf track
'creator'=>$track->artist,
'title'=>$track->title,
'identifier'=>(array)sprintf('https://open.spotify.com/track/%s',$music_id)
]
];
$response = self::api_request('v2/track/links',$args,'POST');
if ( is_wp_error($response) ){
WP_SoundSystem::debug_log('Error while filtering autolinks');
}else{
$new_links = wpsstm_get_array_value('links',$response);
$links = array_merge((array)$links,(array)$new_links);
}
return $links;
}
}
function wpsstm_api_init(){
new WPSSTM_Core_API();
}
add_action('plugins_loaded','wpsstm_api_init');