class-wpml-tm-icl20-token.php
1.83 KB
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
<?php
/**
* @author OnTheGo Systems
*/
class WPML_TM_ICL20_Token {
private $end_point;
private $http;
/**
* WPML_TM_ICL20 constructor.
*
* @param WP_Http $http
* @param string $end_point
*/
public function __construct( WP_Http $http, $end_point ) {
$this->http = $http;
$this->end_point = $end_point;
}
/**
* @param string $ts_accesskey
* @param int $ts_id
*
* @return string|null
* @throws \WPML_TM_ICL20MigrationException
*
* Note: `ts_id` (aka `website_id`) = `website_id`
*
* @link https://onthegosystems.myjetbrains.com/youtrack/issue/icldev-2285
*/
public function get_token( $ts_id, $ts_accesskey ) {
if ( ! $ts_id ) {
throw new WPML_TM_ICL20MigrationException( 'Missing ICL Website ID ($ts_id)' );
}
if ( ! $ts_accesskey ) {
throw new WPML_TM_ICL20MigrationException( 'Missing ICL Access KEY ($ts_accesskey)' );
}
$url = $this->end_point . '/wpml/websites/' . $ts_id . '/token';
$url = add_query_arg( array(
'accesskey' => $ts_accesskey,
),
$url );
$response = $this->http->get( $url,
array(
'method' => 'GET',
'headers' => array(
'Accept: application/json'
),
) );
$code = (int) $response['response']['code'];
if ( 200 !== $code ) {
throw new WPML_TM_ICL20MigrationException( $response['response']['message'], $code );
}
if ( isset( $response['body'] ) ) {
$response_data = json_decode( $response['body'], true );
if ( array_key_exists( 'api_token', $response_data )
&& '' !== trim( $response_data['api_token'] ) ) {
return $response_data['api_token'];
}
}
return null;
}
}