PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.0.4
Translate and Go multilingual – Automatic AI translation – wpLingua v1.0.4
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 1.0.5 1.1.0 All 112 releases
wplingua / inc / api-call / validate-api-key.php

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

192 lines 4.0 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 * - api_key : the API key of website
22 * - version : API compatile version
23 *
24 * ---------------------------------------------------
25 * Data received if successful :
26 * ---------------------------------------------------
27 * - language_original : A language ID
28 * - languages_target : An array of languages ID
29 * - features : Array of allowed API features
30 *
31 * ---------------------------------------------------
32 * Data received in case of failure
33 * ---------------------------------------------------
34 * - error : true
35 * - code : An integer
36 * - message : Error description
37 *
38 * @param string $api_key
39 * @return array
40 */
41 function wplng_api_call_validate_api_key( $api_key = '' ) {
42
43 /**
44 * Get and check the API key
45 */
46
47 if ( empty( $api_key ) ) {
48
49 $api_key = wplng_get_api_key();
50
51 if ( empty( $api_key ) ) {
52 return array();
53 }
54 }
55
56 if ( ! wplng_is_valid_api_key_format( $api_key ) ) {
57 return array();
58 }
59
60 /**
61 * Get the API call
62 */
63
64 $body = array(
65 'request' => 'api_key',
66 'version' => WPLNG_API_VERSION,
67 'api_key' => $api_key,
68 );
69
70 $args = array(
71 'method' => 'POST',
72 'timeout' => 5,
73 'sslverify' => WPLNG_API_SSLVERIFY,
74 'body' => $body,
75 );
76
77 $request = wp_remote_post(
78 WPLNG_API_URL . '/app/',
79 $args
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 // Make the checked response
147
148 $response_checked = array(
149 'language_original' => sanitize_key( $response['language_original'] ),
150 'languages_target' => $languages_target,
151 'features' => $features,
152 );
153
154 } elseif ( isset( $response['error'] )
155 && ( true === $response['error'] )
156 && isset( $response['code'] )
157 && is_int( $response['code'] )
158 && isset( $response['message'] )
159 && is_string( $response['message'] )
160 ) {
161
162 /**
163 * API returning a valid error
164 */
165
166 $error_message = __( 'Code', 'wplingua' ) . ' ';
167 $error_message = $response['code'] . ' - ';
168 $error_message .= $response['message'];
169
170 set_transient(
171 'wplng_api_key_error',
172 $error_message,
173 60 * 5
174 );
175
176 } else {
177
178 /**
179 * API returned an unexpected response
180 */
181
182 set_transient(
183 'wplng_api_key_error',
184 __( 'API returned an unexpected response.', 'wplingua' ),
185 60 * 5
186 );
187
188 }
189
190 return $response_checked;
191 }
192