class-wpml-tm-pickup-mode-ajax.php
1.87 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
<?php
class WPML_TM_Pickup_Mode_Ajax {
const NONCE_PICKUP_MODE = 'wpml_save_translation_pickup_mode';
/**
* @var SitePress
*/
private $sitepress;
/**
* @var WPML_Update_PickUp_Method
*/
private $update_pickup_mode;
/**
* @var WPML_Pro_Translation
*/
private $icl_pro_translation;
public function __construct( SitePress $sitepress, WPML_Pro_Translation $icl_pro_translation ) {
$this->sitepress = $sitepress;
$this->icl_pro_translation = $icl_pro_translation;
$this->update_pickup_mode = new WPML_Update_PickUp_Method( $this->sitepress );
}
public function ajax_hooks() {
add_action( 'wp_ajax_wpml_save_translation_pickup_mode', array( $this, 'wpml_save_translation_pickup_mode' ) );
}
public function wpml_save_translation_pickup_mode() {
try {
if ( ! $this->is_valid_request() ) {
throw new InvalidArgumentException('Request is not valid');
}
if ( ! array_key_exists('pickup_mode', $_POST ) ) {
throw new InvalidArgumentException();
}
$available_pickup_modes = array( 0, 1 );
$pickup_mode = filter_var( $_POST['pickup_mode'], FILTER_SANITIZE_NUMBER_INT, FILTER_NULL_ON_FAILURE );
if ( ! in_array( (int) $pickup_mode, $available_pickup_modes, true ) ) {
throw new InvalidArgumentException();
}
$data['icl_translation_pickup_method'] = $pickup_mode;
$this->update_pickup_mode->update_pickup_method( $data, $this->icl_pro_translation->get_current_project() );
wp_send_json_success();
} catch ( InvalidArgumentException $e ) {
wp_send_json_error();
}
}
private function is_valid_request() {
$valid_request = true;
if ( ! array_key_exists( 'nonce', $_POST ) ) {
$valid_request = false;
}
if ( $valid_request ) {
$nonce = $_POST['nonce'];
$nonce_is_valid = wp_verify_nonce( $nonce, self::NONCE_PICKUP_MODE );
if ( ! $nonce_is_valid ) {
$valid_request = false;
}
}
return $valid_request;
}
}