class-wpml-tp-sync-ajax-handler.php
1.73 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
<?php
class WPML_TP_Sync_Ajax_Handler {
const AJAX_ACTION = 'wpml-tp-sync-job-states';
/** @var WPML_TP_Sync_Jobs */
private $tp_sync;
/** @var WPML_TM_Last_Picked_Up $wpml_tm_last_picked_up */
private $wpml_tm_last_picked_up;
/**
* WPML_TP_Sync_Jobs constructor.
*
* @param WPML_TP_Sync_Jobs $tp_sync
* @param WPML_TM_Last_Picked_Up $wpml_tm_last_picked_up
*/
public function __construct( WPML_TP_Sync_Jobs $tp_sync, WPML_TM_Last_Picked_Up $wpml_tm_last_picked_up ) {
$this->tp_sync = $tp_sync;
$this->wpml_tm_last_picked_up = $wpml_tm_last_picked_up;
}
public function add_hooks() {
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'handle' ) );
}
public function handle() {
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
if ( ! wp_verify_nonce( $nonce, 'sync-job-states' ) ) {
wp_send_json_error( esc_html__( 'Invalid request!' ) );
}
try {
if ( isset( $_REQUEST['update_last_picked_up'] ) ) {
$this->wpml_tm_last_picked_up->set();
}
$jobs = $this->tp_sync->sync();
do_action( 'wpml_tm_empty_mail_queue' );
wp_send_json_success( $jobs->map( array( $this, 'map_job_to_result' ) ) );
return true;
} catch ( Exception $e ) {
wp_send_json_error( $e->getMessage(), 503 );
return false;
}
}
/**
* @param WPML_TM_Job_Entity $job
*
* @return array
*/
public function map_job_to_result( WPML_TM_Job_Entity $job ) {
return array(
'id' => $job->get_id(),
'type' => $job->get_type(),
'status' => $job->get_status(),
'hasCompletedTranslation' => $job->has_completed_translation(),
'needsUpdate' => $job->does_need_update(),
);
}
}