class-wpml-tm-rest-ams-clients.php
4.04 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
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
<?php
/**
* @author OnTheGo Systems
*/
use WPML\FP\Fns;
class WPML_TM_REST_AMS_Clients extends WPML_REST_Base {
/** @var WPML_TM_AMS_API */
private $api;
private $ams_user_records;
/** @var WPML_TM_AMS_Translator_Activation_Records $translator_activation_records */
private $translator_activation_records;
/**
* @var WPML_TM_MCS_ATE_Strings
*/
private $strings;
public function __construct(
WPML_TM_AMS_API $api,
WPML_TM_AMS_Users $ams_user_records,
WPML_TM_AMS_Translator_Activation_Records $translator_activation_records,
WPML_TM_MCS_ATE_Strings $strings
) {
parent::__construct( 'wpml/tm/v1' );
$this->api = $api;
$this->ams_user_records = $ams_user_records;
$this->translator_activation_records = $translator_activation_records;
$this->strings = $strings;
}
function add_hooks() {
$this->register_routes();
}
function register_routes() {
parent::register_route(
'/ams/register_manager',
array(
'methods' => 'POST',
'callback' => array( $this, 'register_manager' ),
)
);
parent::register_route(
'/ams/synchronize/translators',
array(
'methods' => 'GET',
'callback' => array( $this, 'synchronize_translators' ),
)
);
parent::register_route(
'/ams/synchronize/managers',
array(
'methods' => 'GET',
'callback' => array( $this, 'synchronize_managers' ),
)
);
parent::register_route(
'/ams/status',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_status' ),
)
);
parent::register_route(
'/ams/console',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_console' ),
)
);
parent::register_route(
'/ams/engines',
array(
'methods' => 'GET',
'callback' => array( $this, 'get_translation_engines' ),
)
);
parent::register_route(
'/ams/engines',
array(
'methods' => 'POST',
'callback' => array( $this, 'update_translation_engines' ),
)
);
}
/**
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function register_manager() {
$current_user = wp_get_current_user();
$translators = $this->ams_user_records->get_translators();
$managers = $this->ams_user_records->get_managers();
$handleError = function ( $error ) {
return [
'enabled' => false,
'error' => $error
];
};
return $this->api->register_manager( $current_user, $translators, $managers )
->coalesce( $handleError, Fns::always( [ 'enabled' => true ] ) )
->get();
}
/**
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function synchronize_translators() {
$translators = $this->ams_user_records->get_translators();
$result = $this->api->synchronize_translators( $translators );
if ( is_wp_error( $result ) ) {
return $result;
}
$this->translator_activation_records->update( $result['translators'] );
return array( 'result' => $result );
}
/**
* @return array|WP_Error
* @throws \InvalidArgumentException
*/
public function synchronize_managers() {
$managers = $this->ams_user_records->get_managers();
$result = $this->api->synchronize_managers( $managers );
if ( is_wp_error( $result ) ) {
return $result;
}
return array( 'result' => $result );
}
/**
* @return array|mixed|null|object|WP_Error
* @throws \InvalidArgumentException
*/
public function get_status() {
return $this->api->get_status();
}
public function get_console() {
return $this->strings->get_auto_login();
}
function get_allowed_capabilities( WP_REST_Request $request ) {
return array( 'manage_translations', 'manage_options' );
}
public function get_translation_engines() {
return $this->api->get_translation_engines();
}
public function update_translation_engines( WP_REST_Request $request ) {
$params = $request->get_json_params();
$result = $this->api->update_translation_engines( $params );
if ( ! is_wp_error( $result ) ) {
do_action( 'wpml_tm_ate_translation_engines_updated' );
}
return $result;
}
}