RefreshServices.php
2.64 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
<?php
namespace WPML\TM\Menu\TranslationServices\Troubleshooting;
class RefreshServices {
const TEMPLATE = 'refresh-services.twig';
const AJAX_ACTION = 'wpml_tm_refresh_services';
/**
* @var \IWPML_Template_Service
*/
private $template;
/**
* @var \WPML_TP_API_Services
*/
private $tp_services;
public function __construct( \IWPML_Template_Service $template, \WPML_TP_API_Services $tp_services ) {
$this->template = $template;
$this->tp_services = $tp_services;
}
public function add_hooks() {
add_action( 'after_setup_complete_troubleshooting_functions', array( $this, 'render' ), 1 );
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'refresh_services_ajax_handler' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
public function render() {
echo $this->template->show( $this->get_model(), self::TEMPLATE );
}
/**
* @return array
*/
private function get_model() {
return array(
'button_text' => __( 'Refresh Translation Services', 'wpml-translation-management' ),
'nonce' => wp_create_nonce( self::AJAX_ACTION ),
);
}
public function refresh_services_ajax_handler() {
if ( $this->is_valid_request() ) {
if ( $this->refresh_services() ) {
wp_send_json_success(
array(
'message' => __( 'Services Refreshed.', 'wpml-translation-management' ),
)
);
} else {
wp_send_json_error(
array(
'message' => __(
'WPML cannot load the list of translation services. This can be a connection problem. Please wait a minute and reload this page.
If the problem continues, please contact WPML support.',
'wpml-translation-management'
),
)
);
}
} else {
wp_send_json_error(
array(
'message' => __( 'Invalid Request.', 'wpml-translation-management' ),
)
);
}
}
/**
* @return bool
*/
public function refresh_services() {
return $this->tp_services->refresh_cache() && $this->refresh_active_service();
}
private function refresh_active_service() {
$active_service = $this->tp_services->get_active();
if ( $active_service ) {
$active_service = (object) (array) $active_service; // Cast to stdClass
\TranslationProxy::build_and_store_active_translation_service( $active_service, $active_service->custom_fields_data );
}
return true;
}
/**
* @return bool
*/
private function is_valid_request() {
return isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], self::AJAX_ACTION );
}
public function enqueue_scripts() {
wp_enqueue_script(
'wpml-tm-refresh-services',
WPML_TM_URL . '/res/js/refresh-services.js',
array(),
ICL_SITEPRESS_VERSION
);
}
}