class-wpml-tm-rest-jobs.php
9.28 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* WPML_TM_REST_Jobs class file.
*
* @package wpml-translation-management
*/
use WPML\FP\Obj;
use WPML\FP\Fns;
use WPML\FP\Maybe;
use WPML\LIB\WP\User;
use WPML\TM\ATE\Review\Cancel;
use function WPML\FP\pipe;
use function WPML\FP\partial;
use function WPML\FP\invoke;
use function WPML\FP\curryN;
/**
* Class WPML_TM_REST_Jobs
*/
class WPML_TM_REST_Jobs extends WPML_REST_Base {
const CAPABILITY = 'translate';
/**
* Jobs repository
*
* @var WPML_TM_Jobs_Repository
*/
private $jobs_repository;
/**
* Rest jobs criteria parser
*
* @var WPML_TM_Rest_Jobs_Criteria_Parser
*/
private $criteria_parser;
/**
* View model
*
* @var WPML_TM_Rest_Jobs_View_Model
*/
private $view_model;
/**
* Update jobs synchronisation
*
* @var WPML_TP_Sync_Update_Job
*/
private $update_jobs;
/**
* Last picked up jobs
*
* @var WPML_TM_Last_Picked_Up $wpml_tm_last_picked_up
*/
private $wpml_tm_last_picked_up;
/**
* WPML_TM_REST_Jobs constructor.
*
* @param WPML_TM_Jobs_Repository $jobs_repository Jobs repository.
* @param WPML_TM_Rest_Jobs_Criteria_Parser $criteria_parser Rest jobs criteria parser.
* @param WPML_TM_Rest_Jobs_View_Model $view_model View model.
* @param WPML_TP_Sync_Update_Job $update_jobs Update jobs synchronisation.
* @param WPML_TM_Last_Picked_Up $wpml_tm_last_picked_up Last picked up jobs.
*/
public function __construct(
WPML_TM_Jobs_Repository $jobs_repository,
WPML_TM_Rest_Jobs_Criteria_Parser $criteria_parser,
WPML_TM_Rest_Jobs_View_Model $view_model,
WPML_TP_Sync_Update_Job $update_jobs,
WPML_TM_Last_Picked_Up $wpml_tm_last_picked_up
) {
parent::__construct( 'wpml/tm/v1' );
$this->jobs_repository = $jobs_repository;
$this->criteria_parser = $criteria_parser;
$this->view_model = $view_model;
$this->update_jobs = $update_jobs;
$this->wpml_tm_last_picked_up = $wpml_tm_last_picked_up;
}
/**
* Add hooks
*/
public function add_hooks() {
$this->register_routes();
}
/**
* Register routes
*/
public function register_routes() {
parent::register_route(
'/jobs',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_jobs' ),
'args' => array(
'local_job_ids' => array(
'type' => 'string',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'scope' => array(
'type' => 'string',
'validate_callback' => array( 'WPML_TM_Jobs_Search_Params', 'is_valid_scope' ),
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'id' => array(
'type' => 'integer',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'integer' ),
),
'title' => array(
'type' => 'string',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'source_language' => array(
'type' => 'string',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'target_language' => array(
'type' => 'string',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'status' => array(
'type' => 'string',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'needs_update' => array(
'type' => 'string',
'validate_callback' => array( 'WPML_TM_Jobs_Needs_Update_Param', 'is_valid' ),
),
'limit' => array(
'type' => 'integer',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'integer' ),
),
'offset' => array(
'type' => 'integer',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'integer' ),
),
'sorting' => array(
'validate_callback' => array( $this, 'validate_sorting' ),
),
'translated_by' => array(
'type' => 'string',
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'string' ),
),
'sent_from' => array(
'type' => 'string',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'date' ),
),
'sent_to' => array(
'type' => 'string',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'date' ),
),
'deadline_from' => array(
'type' => 'string',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'date' ),
),
'deadline_to' => array(
'type' => 'string',
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'date' ),
),
),
)
);
parent::register_route(
'/jobs/assign',
array(
'methods' => 'POST',
'callback' => array( $this, 'assign_job' ),
'args' => array(
'jobId' => array(
'required' => true,
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'integer' ),
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'integer' ),
),
'type' => array(
'required' => false,
'validate_callback' => [ WPML_TM_Job_Entity::class, 'is_type_valid' ],
),
'translatorId' => array(
'required' => true,
'validate_callback' => array( 'WPML_REST_Arguments_Validation', 'integer' ),
'sanitize_callback' => array( 'WPML_REST_Arguments_Sanitation', 'integer' ),
),
),
)
);
parent::register_route(
'/jobs/cancel',
array(
'methods' => 'POST',
'callback' => array( $this, 'cancel_jobs' ),
)
);
}
/**
* Get jobs
*
* @param WP_REST_Request $request REST request.
*
* @return array|WP_Error
*/
public function get_jobs( WP_REST_Request $request ) {
try {
$criteria = $this->criteria_parser->build_criteria( $request );
$model = $this->view_model->build(
$this->jobs_repository->get( $criteria ),
$this->jobs_repository->get_count( $criteria ),
$criteria
);
$model['last_picked_up_date'] = $this->wpml_tm_last_picked_up->get();
return $model;
} catch ( Exception $e ) {
return new WP_Error( 500, $e->getMessage() );
}
}
/**
* Assign job.
*
* @param WP_REST_Request $request REST request.
*
* @return array
* @throws \InvalidArgumentException Exception on error.
*/
public function assign_job( WP_REST_Request $request ) {
/**
* It can be job_id from icl_translate_job or id from icl_string_translations
*
* @var int $job_id
*/
$job_id = $request->get_param( 'jobId' );
$job_type = $request->get_param( 'type' ) ? $request->get_param( 'type' ) : WPML_TM_Job_Entity::POST_TYPE;
$assignJob = curryN( 4, 'wpml_tm_assign_translation_job');
return Maybe::of( $request->get_param( 'translatorId' ) )
->filter( User::get() )
->map( $assignJob( $job_id, Fns::__, 'local', $job_type ) )
->map( Obj::objOf( 'assigned' ) )
->getOrElse( null );
}
/**
* Cancel job
*
* @param WP_REST_Request $request REST request.
*
* @return array|WP_Error
*/
public function cancel_jobs( WP_REST_Request $request ) {
try {
// $validateParameter :: [id, type] -> bool
$validateParameter = pipe( Obj::prop( 'type' ), [ \WPML_TM_Job_Entity::class, 'is_type_valid' ] );
// $getJob :: [id, type] -> \WPML_TM_Job_Entity
$getJob = Fns::converge( [ $this->jobs_repository, 'get_job' ], [ Obj::prop( 'id' ), Obj::prop( 'type' ) ] );
// $jobEntityToArray :: \WPML_TM_Job_Entity -> [id, type]
$jobEntityToArray = function ( \WPML_TM_Job_Entity $job ) {
return [
'id' => $job->get_id(),
'type' => $job->get_type(),
];
};
$jobs = \wpml_collect( $request->get_json_params() )
->filter( $validateParameter )
->map( $getJob )
->filter()
->map( Fns::tap( invoke( 'set_status' )->with( ICL_TM_NOT_TRANSLATED ) ) )
->map( Fns::tap( [ $this->update_jobs, 'update_state' ] ) );
do_action( 'wpml_tm_jobs_cancelled', $jobs->toArray() );
return $jobs->map( $jobEntityToArray )->values()->toArray();
} catch ( Exception $e ) {
return new WP_Error( 500, $e->getMessage() );
}
}
/**
* Get allowed capabilities
*
* @param WP_REST_Request $request REST request.
*
* @return array|string
*/
public function get_allowed_capabilities( WP_REST_Request $request ) {
return [ User::CAP_ADMINISTRATOR, User::CAP_MANAGE_TRANSLATIONS, User::CAP_TRANSLATE ];
}
/**
* Validate sorting
*
* @param mixed $sorting Sorting parameters.
*
* @return bool
*/
public function validate_sorting( $sorting ) {
if ( ! is_array( $sorting ) ) {
return false;
}
try {
foreach ( $sorting as $column => $asc_or_desc ) {
new WPML_TM_Jobs_Sorting_Param( $column, $asc_or_desc );
}
} catch ( Exception $e ) {
return false;
}
return true;
}
/**
* Validate job
*
* @param mixed $job Job.
*
* @return bool
*/
private function validate_job( $job ) {
return is_array( $job ) && isset( $job['id'] ) && isset( $job['type'] ) && \WPML_TM_Job_Entity::is_type_valid( $job['type'] );
}
}