wpml-tm-jobs-deadline-estimate-ajax-action.php
1.93 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
<?php
class WPML_TM_Jobs_Deadline_Estimate_AJAX_Action implements IWPML_Action {
/** @var WPML_TM_Jobs_Deadline_Estimate $deadline_estimate */
private $deadline_estimate;
/** @var array $translation_basket */
private $translation_basket;
/** @var array $post_data */
private $post_data;
public function __construct(
WPML_TM_Jobs_Deadline_Estimate $deadline_estimate,
array $translation_basket,
array $post_data
) {
$this->deadline_estimate = $deadline_estimate;
$this->translation_basket = $translation_basket;
$this->post_data = $post_data;
}
public function add_hooks() {
add_action(
'wp_ajax_' . 'wpml-tm-jobs-deadline-estimate-ajax-action',
array( $this, 'refresh' )
);
}
public function refresh() {
if ( isset( $this->post_data['translators'] ) ) {
$deadline_per_lang = array();
foreach ( $this->post_data['translators'] as $lang_to => $translator_data ) {
list( $translator_id, $service ) = $this->parse_translator_data( $translator_data );
$translator_args = array(
'translator_id' => $translator_id,
'service' => $service,
'to' => $lang_to,
);
$deadline_per_lang[ $lang_to ] = $this->deadline_estimate->get( $this->translation_basket, $translator_args );
}
$response_data = array(
'deadline' => max( $deadline_per_lang ),
);
wp_send_json_success( $response_data );
}
}
/**
* The translator data for a remote service will be like "ts-7" with 7 the ID of the remote service
* For a local translator, the translation data will be the ID
*
* @param string $translator_data
*
* @return array
*/
private function parse_translator_data( $translator_data ) {
$translator_id = $translator_data;
$service = 'local';
if ( false !== strpos( $translator_data, 'ts-' ) ) {
$translator_id = 0;
$service = (int) preg_replace( '/^ts-/', '', $translator_data );
}
return array( $translator_id, $service );
}
}