AuthenticationAjax.php
3.47 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace WPML\TM\Menu\TranslationServices;
use WPML\TM\TranslationProxy\Services\AuthorizationFactory;
class AuthenticationAjax {
const AJAX_ACTION = 'translation_service_authentication';
/** @var AuthorizationFactory */
protected $authorize_factory;
/**
* @param AuthorizationFactory $authorize_factory
*/
public function __construct( AuthorizationFactory $authorize_factory ) {
$this->authorize_factory = $authorize_factory;
}
public function add_hooks() {
add_action( 'wp_ajax_translation_service_authentication', [ $this, 'authenticate_service' ] );
add_action( 'wp_ajax_translation_service_update_credentials', [ $this, 'update_credentials' ] );
add_action( 'wp_ajax_translation_service_invalidation', [ $this, 'invalidate_service' ] );
}
/**
* @return void
*/
public function authenticate_service() {
$this->handle_action(
function () {
$this->authorize_factory->create()->authorize(
json_decode( stripslashes( $_POST['custom_fields'] ) )
);
},
[ $this, 'is_valid_request_with_params' ],
__( 'Service activated.', 'wpml-translation-management' ),
__(
'The authentication didn\'t work. Please make sure you entered your details correctly and try again.',
'wpml-translation-management'
)
);
}
/**
* @return void
*/
public function update_credentials() {
$this->handle_action(
function () {
$this->authorize_factory->create()->updateCredentials(
json_decode( stripslashes( $_POST['custom_fields'] ) )
);
},
[ $this, 'is_valid_request_with_params' ],
__( 'Service credentials updated.', 'wpml-translation-management' ),
__(
'The authentication didn\'t work. Please make sure you entered your details correctly and try again.',
'wpml-translation-management'
)
);
}
/**
* @return void
*/
public function invalidate_service() {
$this->handle_action(
function () {
$this->authorize_factory->create()->deauthorize();
},
[ $this, 'is_valid_request' ],
__( 'Service invalidated.', 'wpml-translation-management' ),
__( 'Unable to invalidate this service. Please contact WPML support.', 'wpml-translation-management' )
);
}
/**
* @param callable $action
* @param callable $request_validation
* @param string $success_message
* @param string $failure_message
*
* @return void
*/
private function handle_action(
callable $action,
callable $request_validation,
$success_message,
$failure_message
) {
if ( $request_validation() ) {
try {
$action();
$this->send_success_response( $success_message );
} catch ( \Exception $e ) {
return $this->send_error_message( $failure_message );
}
} else {
$this->send_error_message( __( 'Invalid Request', 'wpml-translation-management' ) );
}
}
/**
* @param string $msg
*
* @return void
*/
private function send_success_response( $msg ) {
wp_send_json_success(
[
'errors' => 0,
'message' => $msg,
'reload' => 1,
]
);
}
/**
* @param string $msg
*
* @return bool
*/
private function send_error_message( $msg ) {
wp_send_json_error(
[
'errors' => 1,
'message' => $msg,
'reload' => 0,
]
);
}
/**
* @return bool
*/
public function is_valid_request() {
return isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], self::AJAX_ACTION );
}
/**
* @return bool
*/
public function is_valid_request_with_params() {
return isset( $_POST['service_id'], $_POST['custom_fields'] ) && $this->is_valid_request();
}
}