PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.10.0
Translate and Go multilingual – Automatic AI translation – wpLingua v2.10.0
2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 All 111 releases
wplingua / inc / api-call / validate-api-key.php

validate-api-key.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.10.0, at inc/api-call/validate-api-key.php

225 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Get data from wpLingua API : API key validation
11 *
12 * Return an error message with error code
13 * or return the basic information of the API key
14 *
15 * API terms : https://wplingua.com/terms/
16 *
17 * ---------------------------------------------------
18 * Data sent :
19 * ---------------------------------------------------
20 * - request : 'api_key'
21 * - version : API compatile version
22 * - context : Sends page calling URL
23 * - api_key : the API key of website
24 *
25 * ---------------------------------------------------
26 * Data received if successful :
27 * ---------------------------------------------------
28 * - language_original : A language ID
29 * - languages_target : An array of languages ID
30 * - features : Array of allowed API features
31 * - status : FREE | PREMIUM | VIP
32 * - expiration : A date dd/mm/yyyy
33 *
34 * ---------------------------------------------------
35 * Data received in case of failure
36 * ---------------------------------------------------
37 * - error : true
38 * - code : An integer
39 * - message : Error description
40 *
41 * @param string $api_key
42 * @return array
43 */
44 function wplng_api_call_validate_api_key( $api_key = '' ) {
45
46 /**
47 * Get and check the API key
48 */
49
50 if ( empty( $api_key ) ) {
51
52 $api_key = wplng_get_api_key();
53
54 if ( empty( $api_key ) ) {
55 return array();
56 }
57 }
58
59 if ( ! wplng_is_valid_api_key_format( $api_key ) ) {
60 return array();
61 }
62
63 /**
64 * Get the API call
65 */
66
67 $body = array(
68 'request' => 'api_key',
69 'version' => WPLNG_API_VERSION,
70 'context' => wplng_get_context(),
71 'api_key' => $api_key,
72 );
73
74 $args = array(
75 'method' => 'POST',
76 'timeout' => 5,
77 'sslverify' => WPLNG_API_SSLVERIFY,
78 'body' => $body,
79 );
80
81 $request = wp_remote_post(
82 WPLNG_API_URL . '/app/',
83 $args
84 );
85
86 /**
87 * Check if the API call worked
88 */
89
90 if ( is_wp_error( $request )
91 || wp_remote_retrieve_response_code( $request ) != 200
92 ) {
93 return array();
94 }
95
96 /**
97 * Check and sanitize the API response
98 */
99
100 $response_checked = array();
101 $response = json_decode(
102 wp_remote_retrieve_body( $request ),
103 true
104 );
105
106 if ( ! empty( $response['language_original'] )
107 && (
108 wplng_is_valid_language_id( $response['language_original'] )
109 || 'all' === $response['language_original']
110 )
111 && ! empty( $response['languages_target'] )
112 && is_array( $response['languages_target'] )
113 && isset( $response['features'] )
114 && is_array( $response['features'] )
115 ) {
116
117 /**
118 * API returned valid key informations
119 */
120
121 // Sanitize languages target
122
123 $languages_target = array();
124
125 foreach ( $response['languages_target'] as $id ) {
126
127 if ( ! wplng_is_valid_language_id( $id ) ) {
128 continue;
129 }
130
131 $languages_target[] = sanitize_key( $id );
132 }
133
134 // Sanitize features list
135
136 $features = array();
137
138 foreach ( $response['features'] as $key => $allow ) {
139
140 if ( ! is_string( $key ) || ! is_bool( $allow ) ) {
141 continue;
142 }
143
144 $key = sanitize_key( $key );
145 $allow = ( true === $allow );
146
147 $features[ $key ] = $allow;
148 }
149
150 // Sanitize status
151
152 $status = 'FREE';
153
154 if ( ! empty( $response['status'] )
155 && (
156 'PREMIUM' === $response['status']
157 || 'VIP' === $response['status']
158 )
159 ) {
160 $status = $response['status'];
161 }
162
163 // Make the checked response
164
165 $response_checked = array(
166 'language_original' => sanitize_key( $response['language_original'] ),
167 'languages_target' => $languages_target,
168 'features' => $features,
169 'status' => $status,
170 );
171
172 // Add expiration
173
174 if ( ! empty( $response['expiration'] )
175 && is_string( $response['expiration'] )
176 ) {
177 $response_checked['expiration'] = $response['expiration'];
178 }
179 } elseif ( isset( $response['error'] )
180 && ( true === $response['error'] )
181 && isset( $response['code'] )
182 && is_int( $response['code'] )
183 && isset( $response['message'] )
184 && is_string( $response['message'] )
185 ) {
186
187 /**
188 * API returning a valid error
189 */
190
191 $error_message = __( 'Code', 'wplingua' ) . ' ';
192 $error_message .= $response['code'] . ' - ';
193 $error_message .= $response['message'];
194
195 set_transient(
196 'wplng_api_key_error',
197 $error_message,
198 MINUTE_IN_SECONDS * 5
199 );
200
201 if ( isset( $response['disconnect'] )
202 && true === $response['disconnect']
203 ) {
204 delete_option( 'wplng_api_key_data' );
205 delete_option( 'wplng_api_key' );
206 wplng_clear_translations_cache();
207 wplng_clear_slugs_cache();
208 }
209 } else {
210
211 /**
212 * API returned an unexpected response
213 */
214
215 set_transient(
216 'wplng_api_key_error',
217 __( 'API returned an unexpected response.', 'wplingua' ),
218 MINUTE_IN_SECONDS * 5
219 );
220
221 }
222
223 return $response_checked;
224 }
225