PluginProbe
Translate and boost SEO – Automatic AI translation – wpLingua / 3.0.0
Translate and boost SEO – Automatic AI translation – wpLingua v3.0.0
3.0.0 2.16.8 2.16.7 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 All 114 releases
wplingua / inc / api / validate-api-key.php

validate-api-key.php in Translate and boost SEO – Automatic AI translation – wpLingua 3.0.0, at inc/api/validate-api-key.php

240 lines 5.3 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 $request = wp_remote_post(
68 WPLNG_API_URL . '/app/',
69 array(
70 'method' => 'POST',
71 'timeout' => 5,
72 'sslverify' => WPLNG_API_SSLVERIFY,
73 'body' => array(
74 'request' => 'api_key',
75 'version' => WPLNG_API_VERSION,
76 'context' => wplng_get_context(),
77 'api_key' => $api_key,
78 ),
79 )
80 );
81
82 /**
83 * Check if the API call worked
84 */
85
86 if ( is_wp_error( $request )
87 || wp_remote_retrieve_response_code( $request ) !== 200
88 ) {
89 return array();
90 }
91
92 /**
93 * Check and sanitize the API response
94 */
95
96 $response_checked = array();
97 $response = json_decode(
98 wp_remote_retrieve_body( $request ),
99 true
100 );
101
102 if ( ! empty( $response['language_original'] )
103 && (
104 wplng_is_valid_language_id( $response['language_original'] )
105 || 'all' === $response['language_original']
106 )
107 && ! empty( $response['languages_target'] )
108 && is_array( $response['languages_target'] )
109 && isset( $response['features'] )
110 && is_array( $response['features'] )
111 ) {
112
113 /**
114 * API returned valid key informations
115 */
116
117 // Sanitize languages target
118
119 $languages_target = array();
120
121 foreach ( $response['languages_target'] as $id ) {
122
123 if ( ! wplng_is_valid_language_id( $id ) ) {
124 continue;
125 }
126
127 $languages_target[] = sanitize_key( $id );
128 }
129
130 // Sanitize features list
131
132 $features = array();
133
134 foreach ( $response['features'] as $key => $allow ) {
135
136 if ( ! is_string( $key ) || ! is_bool( $allow ) ) {
137 continue;
138 }
139
140 $key = sanitize_key( $key );
141 $allow = ( true === $allow );
142
143 $features[ $key ] = $allow;
144 }
145
146 // Sanitize status
147
148 $status = 'FREE';
149
150 if ( ! empty( $response['status'] )
151 && (
152 'PREMIUM' === $response['status']
153 || 'VIP' === $response['status']
154 )
155 ) {
156 $status = $response['status'];
157 }
158
159 // Sanitize plan
160
161 $plan = 'FREE';
162
163 if ( ! empty( $response['plan'] )
164 && in_array(
165 $response['plan'],
166 array( 'STARTER', 'PREMIUM', 'VIP STARTER', 'VIP PREMIUM' ),
167 true
168 )
169 ) {
170 $plan = $response['plan'];
171 }
172
173 // Make the checked response
174
175 $response_checked = array(
176 'language_original' => sanitize_key( $response['language_original'] ),
177 'languages_target' => $languages_target,
178 'features' => $features,
179 'status' => $status,
180 'plan' => $plan,
181 );
182
183 // Add expiration
184
185 if ( ! empty( $response['expiration'] )
186 && is_string( $response['expiration'] )
187 ) {
188 $response_checked['expiration'] = sanitize_text_field( $response['expiration'] );
189 }
190
191 if ( ! empty( $response['expired'] ) && $response['expired'] === true ) {
192 $response_checked['expired'] = true;
193 }
194 } elseif ( isset( $response['error'] )
195 && ( true === $response['error'] )
196 && isset( $response['code'] )
197 && is_int( $response['code'] )
198 && isset( $response['message'] )
199 && is_string( $response['message'] )
200 ) {
201
202 /**
203 * API returning a valid error
204 */
205
206 $error_message = __( 'Code', 'wplingua' ) . ' ';
207 $error_message .= $response['code'] . ' - ';
208 $error_message .= sanitize_text_field( $response['message'] );
209
210 set_transient(
211 'wplng_api_key_error',
212 $error_message,
213 MINUTE_IN_SECONDS * 5
214 );
215
216 if ( isset( $response['disconnect'] )
217 && true === $response['disconnect']
218 ) {
219 delete_option( 'wplng_api_key_data' );
220 delete_option( 'wplng_api_key' );
221 wplng_clear_translations_cache();
222 wplng_clear_slugs_cache();
223 }
224 } else {
225
226 /**
227 * API returned an unexpected response
228 */
229
230 set_transient(
231 'wplng_api_key_error',
232 __( 'API returned an unexpected response.', 'wplingua' ),
233 MINUTE_IN_SECONDS * 5
234 );
235
236 }
237
238 return $response_checked;
239 }
240