wpml-tp-abstract-api.php
2.65 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
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
<?php
/**
* Class WPML_TP_Abstract_API
*
* @author OnTheGoSystems
*/
abstract class WPML_TP_Abstract_API {
/** @var WPML_TP_Client $tp_client */
protected $tp_client;
/** @var null|Exception $exception */
protected $exception;
/** @var null|string $error_message */
protected $error_message;
public function __construct( WPML_TP_Client $tp_client ) {
$this->tp_client = $tp_client;
}
/** @return string */
abstract protected function get_endpoint_uri();
/** @return bool */
abstract protected function is_authenticated();
/**
* @param array $params
*
* @return mixed
*/
protected function get( array $params = array() ) {
return $this->remote_call( $params, 'GET' );
}
/**
* @param array $params
*
* @return mixed
*/
protected function post( array $params = array() ) {
return $this->remote_call( $params, 'POST' );
}
protected function put( array $params = array() ) {
// @todo: Implement put
}
protected function delete( array $params = array() ) {
// @todo: Implement delete
}
/**
* @param array $params
* @param string $method
*
* @return mixed
*/
private function remote_call( array $params, $method ) {
$response = false;
try {
$params = $this->pre_process_params( $params );
$response = TranslationProxy_Api::proxy_request( $this->get_endpoint_uri(), $params, $method );
} catch ( Exception $e ) {
$this->exception = $e;
}
return $response;
}
/**
* @param array $params
*
* @return array
*/
private function pre_process_params( array $params ) {
if ( $this->is_authenticated() ) {
$params['accesskey'] = $this->tp_client->get_project()->get_access_key();
}
return $params;
}
/**
* WPML does not store the Translation Proxy Job ID
* We have to identify the job somehow.
* This is why we are using `original_file_id`.
* It is the same as used in the XLIFF file as a value of `original` attribute.
* The combination of `original_file_id` and `batch_id` will be always unique.
* Translation Proxy provides this call, with these arguments, for this specific reason.
*
* @see https://git.onthegosystems.com/tp/translation-proxy/wikis/rate_translation
* @see https://git.onthegosystems.com/tp/translation-proxy/wikis/send_feedback
*
* @param int $job_id
* @param int $document_source_id
*
* @return string
*/
protected function get_original_file_id( $job_id, $document_source_id ) {
return $job_id . '-' . md5( $job_id . $document_source_id );
}
/** @return null|Exception */
public function get_exception() {
return $this->exception;
}
/** @return null|string */
public function get_error_message() {
return $this->exception->getMessage();
}
}