class-wpml-tp-refresh-language-pairs.php
1.27 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
<?php
class WPML_TP_Refresh_Language_Pairs {
const AJAX_ACTION = 'wpml-tp-refresh-language-pairs';
/**
* @var WPML_TP_Project_API
*/
private $tp_api;
/**
* WPML_TP_AJAX constructor.
*
* @param WPML_TP_Project_API $wpml_tp_api
*/
public function __construct( WPML_TP_Project_API $wpml_tp_api ) {
$this->tp_api = $wpml_tp_api;
}
public function add_hooks() {
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'refresh_language_pairs' ) );
}
public function refresh_language_pairs() {
if ( $this->is_valid_request() ) {
try {
$this->tp_api->refresh_language_pairs();
wp_send_json_success(
array(
'msg' => __( 'Language pairs refreshed', 'wpml-translation-management' ),
)
);
} catch ( Exception $e ) {
wp_send_json_error(
array(
'msg' => __( 'Language pairs not refreshed, please try again', 'wpml-translation-management' ),
)
);
}
} else {
wp_send_json_error(
array(
'msg' => __( 'Invalid Request', 'wpml-translation-management' ),
)
);
}
}
/**
* @return bool
*/
private function is_valid_request() {
return array_key_exists( 'nonce', $_POST ) &&
wp_verify_nonce( filter_var( $_POST['nonce'], FILTER_SANITIZE_FULL_SPECIAL_CHARS ), self::AJAX_ACTION );
}
}