PluginProbe
Linguise – AI Automatic Multilingual Translation / trunk
Linguise – AI Automatic Multilingual Translation vtrunk
2.2.64 2.2.63 2.2.62 2.2.61 2.2.60 2.2.59 2.2.58 2.2.57 2.2.56 2.2.55 2.2.54 2.2.53 2.2.52 2.2.51 2.2.50 2.2.49 2.2.47 2.2.48 2.2.46 2.2.45 2.2.44 2.2.43 2.2.42 1.9.7 1.9.8 All 255 releases
linguise / src / configuration.php

configuration.php in Linguise – AI Automatic Multilingual Translation trunk, at src/configuration.php

531 lines 26.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Linguise\WordPress\Helper;
4 use Linguise\WordPress\ThirdPartyLoader;
5
6 defined('ABSPATH') || die('');
7
8 /**
9 * Class Linguise Configuration
10 */
11 class LinguiseConfiguration
12 {
13 /**
14 * Languages names
15 *
16 * @var array
17 */
18 public $languages_names = array();
19
20 /**
21 * LinguiseConfiguration constructor.
22 *
23 * @param array $languages_names Languages names
24 */
25 public function __construct($languages_names)
26 {
27 if (!empty($languages_names)) {
28 $this->languages_names = $languages_names;
29 }
30 add_action('admin_menu', array($this, 'registerMenuPage'));
31 add_action('admin_head', array($this, 'adminHead'));
32 add_action('admin_enqueue_scripts', array($this, 'enqueueScripts'));
33 }
34
35 /**
36 * Function custom menu icon
37 *
38 * @return void
39 */
40 public function adminHead()
41 {
42 echo '<style>
43 #toplevel_page_linguise .dashicons-before img {
44 width: 25px;
45 padding-top: 7px;
46 }
47 </style>';
48 }
49
50 /**
51 * Register menu page
52 *
53 * @return void
54 */
55 public function registerMenuPage()
56 {
57 add_menu_page(
58 'Linguise',
59 'Linguise',
60 'manage_options',
61 'linguise',
62 array($this, 'renderSettings'),
63 LINGUISE_PLUGIN_URL . 'assets/images/linguise-logo.svg'
64 );
65 }
66
67 /**
68 * Render settings
69 *
70 * @return void
71 */
72 public function renderSettings()
73 {
74 // check user capabilities
75 if (!current_user_can('manage_options')) {
76 return;
77 }
78
79 $api_web_errors = [];
80
81 if (isset($_POST['linguise_options'])) {
82 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified inside the function
83 $this->updatePluginOptions($_POST['linguise_options'], isset($_POST['linguise_nonce']) ? $_POST['linguise_nonce'] : '', $api_web_errors);
84 }
85
86 if (isset($_POST['expert_linguise'])) {
87 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce is verified inside the function
88 $this->updateExpertOptions($_POST['expert_linguise'], isset($_POST['linguise_nonce']) ? $_POST['linguise_nonce'] : '');
89 };
90
91 if (isset($_POST['linguise_debug_disable']) && wp_verify_nonce($_POST['debug_banner_nonce'], 'linguise-settings')) {
92 $options = linguiseGetOptions();
93 $options['debug'] = false;
94
95 linguiseSwitchMainSite();
96 update_option('linguise_options', $options);
97 linguiseRestoreMultisite();
98
99 echo '<div class="linguise-notification-popup"><span class="material-icons"> done </span> ' . esc_html__('Disabled debug!', 'linguise') . '</div>';
100 }
101
102 // get URL query
103 $view_mode = isset($_GET['ling_mode']) ? $_GET['ling_mode'] : 'standard';
104
105 if (!extension_loaded('xml')) {
106 // @codeCoverageIgnoreStart
107 $api_web_errors[] = [
108 'type' => 'warning',
109 'message' => __('`xml` extension is not loaded, some Linguise features might not works properly!', 'linguise'),
110 ];
111 // @codeCoverageIgnoreEnd
112 }
113 if (!class_exists('\\DOMDocument')) {
114 // @codeCoverageIgnoreStart
115 $api_web_errors[] = [
116 'type' => 'warning',
117 'message' => __('`DOMDocument` class is not available, some Linguise features might not works properly!', 'linguise'),
118 ];
119 // @codeCoverageIgnoreEnd
120 }
121
122 // Check if not running php7 or higher
123 if (version_compare(PHP_VERSION, '7.0.0', '<')) {
124 // @codeCoverageIgnoreStart
125 $api_web_errors[] = [
126 'type' => 'error',
127 'message' => __('PHP version is lower than 7.0.0, some Linguise features might not works properly!', 'linguise'),
128 ];
129 // @codeCoverageIgnoreEnd
130 }
131
132 // Show expert mode
133 if ($view_mode === 'expert') {
134 require_once(LINGUISE_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'src/admin/views' . DIRECTORY_SEPARATOR . 'expert-view.php');
135 } else {
136 require_once(LINGUISE_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'src/admin/views' . DIRECTORY_SEPARATOR . 'view.php');
137 }
138 }
139
140 /**
141 * Patch htaccess file
142 *
143 * @throws Exception With custom error message
144 *
145 * @return void
146 */
147 protected function patchHtaccess()
148 {
149 global $wp_filesystem;
150 if (empty($wp_filesystem)) {
151 // @codeCoverageIgnoreStart
152 require_once(ABSPATH . 'wp-admin/includes/file.php');
153 WP_Filesystem();
154 // @codeCoverageIgnoreEnd
155 }
156
157 // Save htaccess content
158 $htaccess_path = ABSPATH . DIRECTORY_SEPARATOR . '.htaccess';
159 if (!$wp_filesystem->exists($htaccess_path)) {
160 throw new Exception(__('Htaccess file doesn\'t exist. This may be a problem is you are under an Apache server. Please check the documentation to finish the installation manually: <a href="https://www.linguise.com/documentation/linguise-installation/install-linguise-on-wordpress/" target="_blank">how to configure Linguise</a>', 'linguise'), 0);
161 }
162
163 $script_path = LINGUISE_PLUGIN_PATH . 'script.php';
164 $script_path_parts = explode('/', trim($script_path, '/'));
165 $abspath_parts = explode('/', trim(ABSPATH, '/'));
166
167 $script_relative_path = array_slice($script_path_parts, count($abspath_parts));
168 $script_relative_path = implode('/', $script_relative_path);
169
170 $htaccess_content = $wp_filesystem->get_contents($htaccess_path);
171 $htaccess_content_original = $htaccess_content;
172 $htaccess_patched = false;
173 if (strpos($htaccess_content, '#### LINGUISE DO NOT EDIT ####') !== false) {
174 $htaccess_patched = true;
175 }
176
177 $content =
178 '#### LINGUISE DO NOT EDIT ####' . PHP_EOL .
179 '<IfModule mod_rewrite.c>' . PHP_EOL .
180 ' RewriteEngine On' . PHP_EOL .
181 ' RewriteRule ^(af|sq|am|ar|hy|az|bn|bs|bg|ca|zh-cn|zh-tw|hr|cs|da|nl|en|eo|et|fi|fr|de|el|gu|ht|ha|iw|hi|hmn|hu|is|ig|id|ga|it|ja|kn|kk|km|ko|ku|lo|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|sr|sd|sk|sl|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|vi|cy|zz-zz)(?:$|/)(.*)$ ' . $script_relative_path . '?linguise_language=$1&original_url=$2 [L,QSA]' . PHP_EOL .
182 '</IfModule>' . PHP_EOL .
183 '#### LINGUISE DO NOT EDIT END ####' . PHP_EOL;
184
185 if ($htaccess_patched) {
186 // Replace previous version
187 $htaccess_content = preg_replace('/#### LINGUISE DO NOT EDIT ####.*?#### LINGUISE DO NOT EDIT END ####/', $content, $htaccess_content);
188 } else {
189 // Add it at the beginning of the file
190 $htaccess_content = $content . PHP_EOL . $htaccess_content;
191 }
192
193 if ($htaccess_content_original === $htaccess_content) {
194 return;
195 }
196
197 if (!is_writable($htaccess_path)) {
198 throw new Exception(__('Htaccess file is not writable, please make sure to allow the current script to update the .htaccess file to make linguise work as expected. You can also check our online documentation to read <a href="https://www.linguise.com/documentation/linguise-installation/install-linguise-on-wordpress/" target="_blank">how to configure Linguise</a>.', 'linguise'), 1);
199 }
200
201 // Only write if necessary
202 if (!$wp_filesystem->put_contents($htaccess_path, $htaccess_content)) {
203 throw new Exception(__('Failed to write to htaccess file, please make sure to allow the current script to update the .htaccess file to make linguise work as expected. You can also check our online documentation to read <a href="https://www.linguise.com/documentation/linguise-installation/install-linguise-on-wordpress/" target="_blank">how to configure Linguise</a>.', 'linguise'), 2);
204 }
205 }
206
207 /**
208 * Update plugin options
209 *
210 * @param array $linguise_options Linguise options
211 * @param string $nonce Nonce value
212 * @param array $api_web_errors Reference to API web errors array
213 *
214 * @return void
215 */
216 protected function updatePluginOptions($linguise_options, $nonce, &$api_web_errors)
217 {
218 if (!wp_verify_nonce($nonce, 'linguise-settings')) {
219 wp_die(esc_html__('Nonce verification failed', 'linguise'), esc_html__('Unauthorized', 'linguise'), array(
220 'response' => 403,
221 'back_link' => true,
222 ));
223 return;
224 }
225
226 $old_options = linguiseGetOptions();
227 $default_language = sanitize_key($_POST['linguise_options']['default_language']);
228
229 $expert_mode_conf = isset($old_options['expert_mode']) ? $old_options['expert_mode'] : [];
230
231 $api_host = isset($expert_mode_conf['api_host']) ? $expert_mode_conf['api_host'] : 'api.linguise.com';
232 $api_port = isset($expert_mode_conf['api_port']) ? $expert_mode_conf['api_port'] : '443';
233 $api_portless = ['80', '443'];
234
235 $api_base_url = 'http' . ($api_port === '443' ? 's' : '') . '://' . $api_host . (!in_array($api_port, $api_portless) ? ':' . $api_port : '');
236
237 $translate_languages = [];
238
239 $token = sanitize_text_field($_POST['linguise_options']['token']);
240 $dynamic_translations = [
241 'enabled' => isset($_POST['linguise_options']['dynamic_translations']) && $_POST['linguise_options']['dynamic_translations'] === '1' ? 1 : 0,
242 'public_key' => '',
243 ];
244
245 $token_changed = false;
246 $config_api_url = $api_base_url . '/api/config';
247 if ($old_options['token'] !== $token && $token !== '') {
248 $result = $this->verifyRemoteToken($token, $config_api_url, $api_web_errors);
249 if ($result !== false) {
250 $default_language = sanitize_key($result->language);
251 $translation_languages = $result->languages;
252 if (!empty($translation_languages)) {
253 foreach ($translation_languages as $translation_language) {
254 $translate_languages[] = sanitize_key($translation_language->code);
255 }
256 }
257 $dynamic_translations['public_key'] = $result->public_key;
258
259 if (isset($result->dynamic_translations) &&
260 isset($result->dynamic_translations->enabled)
261 ) {
262 $dynamic_translations['enabled'] = (int)$result->dynamic_translations->enabled;
263 }
264
265 $token_changed = true;
266 } else {
267 if (!empty($old_options['enabled_languages'])) {
268 $translate_languages = $old_options['enabled_languages'];
269 }
270 }
271 } else {
272 if (!empty($_POST['enabled_languages_sortable'])) {
273 $lang_lists = explode(',', $_POST['enabled_languages_sortable']);
274 } else {
275 $lang_lists = (!empty($_POST['linguise_options']['enabled_languages'])) ? $_POST['linguise_options']['enabled_languages'] : array();
276 }
277
278 if (!empty($lang_lists)) {
279 foreach ($lang_lists as $language) {
280 $translate_languages[] = sanitize_key($language);
281 }
282 }
283 }
284
285 if ($dynamic_translations['enabled'] === 1 && empty($dynamic_translations['public_key']) && $token !== '') {
286 $result = $this->verifyRemoteToken($token, $config_api_url, $api_web_errors);
287 if ($result !== false) {
288 $dynamic_translations['public_key'] = $result->public_key;
289
290 if (isset($result->dynamic_translations) &&
291 isset($result->dynamic_translations->enabled) &&
292 $token_changed // only update if token has changed
293 ) {
294 $dynamic_translations['enabled'] = (int)$result->dynamic_translations->enabled;
295 }
296 }
297 };
298
299 $pre_text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', stripslashes($_POST['linguise_options']['pre_text']));
300 $post_text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', stripslashes($_POST['linguise_options']['post_text']));
301 $add_flag_automatically = isset($_POST['linguise_options']['add_flag_automatically']) && $_POST['linguise_options']['add_flag_automatically'] === '1' ? 1 : 0;
302 $alternate_link = isset($_POST['linguise_options']['alternate_link']) && $_POST['linguise_options']['alternate_link'] === '1' ? 1 : 0;
303 $enable_flag = isset($_POST['linguise_options']['enable_flag']) && $_POST['linguise_options']['enable_flag'] === '1' ? 1 : 0;
304 $enable_language_name = isset($_POST['linguise_options']['enable_language_name']) && $_POST['linguise_options']['enable_language_name'] === '1' ? 1 : 0;
305 $enable_language_name_popup = isset($_POST['linguise_options']['enable_language_name_popup']) && $_POST['linguise_options']['enable_language_name_popup'] === '1' ? 1 : 0;
306 $short_name_input = isset($_POST['linguise_options']['enable_language_short_name']) ? $_POST['linguise_options']['enable_language_short_name'] : '0';
307 $enable_language_short_name = ($short_name_input === '1' || $short_name_input === 'short') ? 1 : 0;
308 $browser_redirect = isset($_POST['linguise_options']['browser_redirect']) && $_POST['linguise_options']['browser_redirect'] === '1' ? 1 : 0;
309 $cookies_redirect = isset($_POST['linguise_options']['cookies_redirect']) && $_POST['linguise_options']['cookies_redirect'] === '1' ? 1 : 0;
310 $ukraine_redirect = isset($_POST['linguise_options']['ukraine_redirect']) && $_POST['linguise_options']['ukraine_redirect'] === '1' ? 1 : 0;
311 $cache_enabled = isset($_POST['linguise_options']['cache_enabled']) && $_POST['linguise_options']['cache_enabled'] === '1' ? 1 : 0;
312 $cache_max_size = isset($_POST['linguise_options']['cache_max_size']) ? (int)$_POST['linguise_options']['cache_max_size'] : 200;
313 $cache_ignore_parameters = isset($_POST['linguise_options']['cache_ignore_parameters']) && $_POST['linguise_options']['cache_ignore_parameters'] === '1' ? 1 : 0;
314 $cache_params_always_included = isset($_POST['linguise_options']['cache_params_always_included']) ? trim($_POST['linguise_options']['cache_params_always_included']) : 's,utm_source,utm_medium,utm_campaign,utm_term,utm_content';
315 $search_translation = isset($_POST['linguise_options']['search_translation']) && $_POST['linguise_options']['search_translation'] === '1' ? 1 : 0;
316 $woocommerce_emails_translation = isset($_POST['linguise_options']['woocommerce_emails_translation']) && $_POST['linguise_options']['woocommerce_emails_translation'] === '1' ? 1 : 0;
317 $debug = isset($_POST['linguise_options']['debug']) && $_POST['linguise_options']['debug'] === '1' ? 1 : 0;
318
319 $linguise_options = array(
320 'token' => $token,
321 'default_language' => $default_language,
322 'enabled_languages' => $translate_languages,
323 'pre_text' => $pre_text,
324 'post_text' => $post_text,
325 'alternate_link' => $alternate_link,
326 'enable_flag' => $enable_flag,
327 'enable_language_name' => $enable_language_name,
328 'enable_language_name_popup' => $enable_language_name_popup,
329 'enable_language_short_name' => $enable_language_short_name,
330 'add_flag_automatically' => $add_flag_automatically,
331 'custom_css' => isset($_POST['linguise_options']['custom_css']) ? $_POST['linguise_options']['custom_css'] : '',
332 'flag_display_type' => isset($_POST['linguise_options']['flag_display_type']) ? $_POST['linguise_options']['flag_display_type'] : 'popup',
333 'display_position' => isset($_POST['linguise_options']['display_position']) ? $_POST['linguise_options']['display_position'] : 'no',
334 'language_name_display' => isset($_POST['linguise_options']['language_name_display']) ? $_POST['linguise_options']['language_name_display'] : 'en',
335 'flag_shape' => isset($_POST['linguise_options']['flag_shape']) ? $_POST['linguise_options']['flag_shape'] : 'rounded',
336 'flag_en_type' => isset($_POST['linguise_options']['flag_en_type']) ? $_POST['linguise_options']['flag_en_type'] : 'en-us',
337 'flag_de_type' => isset($_POST['linguise_options']['flag_de_type']) ? $_POST['linguise_options']['flag_de_type'] : 'de',
338 'flag_es_type' => isset($_POST['linguise_options']['flag_es_type']) ? $_POST['linguise_options']['flag_es_type'] : 'es',
339 'flag_pt_type' => isset($_POST['linguise_options']['flag_pt_type']) ? $_POST['linguise_options']['flag_pt_type'] : 'pt',
340 'flag_tw_type' => isset($_POST['linguise_options']['flag_tw_type']) ? $_POST['linguise_options']['flag_tw_type'] : 'zh-tw',
341 'flag_border_radius' => isset($_POST['linguise_options']['flag_border_radius']) ? (int)$_POST['linguise_options']['flag_border_radius'] : 0,
342 'flag_width' => isset($_POST['linguise_options']['flag_width']) ? (int)$_POST['linguise_options']['flag_width'] : 24,
343 'language_name_color' => isset($_POST['linguise_options']['language_name_color']) ? $_POST['linguise_options']['language_name_color'] : '#222',
344 'language_name_hover_color' => isset($_POST['linguise_options']['language_name_hover_color']) ? $_POST['linguise_options']['language_name_hover_color'] : '#222',
345 'popup_language_name_color' => isset($_POST['linguise_options']['popup_language_name_color']) ? $_POST['linguise_options']['popup_language_name_color'] : '#222',
346 'popup_language_name_hover_color' => isset($_POST['linguise_options']['popup_language_name_hover_color']) ? $_POST['linguise_options']['popup_language_name_hover_color'] : '#222',
347 'flag_shadow_h' => isset($_POST['linguise_options']['flag_shadow_h']) ? (int)$_POST['linguise_options']['flag_shadow_h'] : 2,
348 'flag_shadow_v' => isset($_POST['linguise_options']['flag_shadow_v']) ? (int)$_POST['linguise_options']['flag_shadow_v'] : 2,
349 'flag_shadow_blur' => isset($_POST['linguise_options']['flag_shadow_blur']) ? (int)$_POST['linguise_options']['flag_shadow_blur'] : 12,
350 'flag_shadow_spread' => isset($_POST['linguise_options']['flag_shadow_spread']) ? (int)$_POST['linguise_options']['flag_shadow_spread'] : 0,
351 'flag_shadow_color' => isset($_POST['linguise_options']['flag_shadow_color']) ? $_POST['linguise_options']['flag_shadow_color'] : '#eee',
352 'flag_shadow_color_alpha' => isset($_POST['linguise_options']['flag_shadow_color_alpha']) ? (float)$_POST['linguise_options']['flag_shadow_color_alpha'] : 1.0,
353 'flag_hover_shadow_h' => isset($_POST['linguise_options']['flag_hover_shadow_h']) ? (int)$_POST['linguise_options']['flag_hover_shadow_h'] : 3,
354 'flag_hover_shadow_v' => isset($_POST['linguise_options']['flag_hover_shadow_v']) ? (int)$_POST['linguise_options']['flag_hover_shadow_v'] : 3,
355 'flag_hover_shadow_blur' => isset($_POST['linguise_options']['flag_hover_shadow_blur']) ? (int)$_POST['linguise_options']['flag_hover_shadow_blur'] : 6,
356 'flag_hover_shadow_spread' => isset($_POST['linguise_options']['flag_hover_shadow_spread']) ? (int)$_POST['linguise_options']['flag_hover_shadow_spread'] : 0,
357 'flag_hover_shadow_color' => isset($_POST['linguise_options']['flag_hover_shadow_color']) ? $_POST['linguise_options']['flag_hover_shadow_color'] : '#bfbfbf',
358 'flag_hover_shadow_color_alpha' => isset($_POST['linguise_options']['flag_hover_shadow_color_alpha']) ? (float)$_POST['linguise_options']['flag_hover_shadow_color_alpha'] : 1.0,
359 'browser_redirect' => $browser_redirect,
360 'cookies_redirect' => $cookies_redirect,
361 'ukraine_redirect' => $ukraine_redirect,
362 'cache_enabled' => $cache_enabled,
363 'cache_max_size' => $cache_max_size,
364 'cache_ignore_parameters' => $cache_ignore_parameters,
365 'cache_params_always_included' => $cache_params_always_included,
366 'search_translation' => $search_translation,
367 'woocommerce_emails_translation' => $woocommerce_emails_translation,
368 'debug' => $debug,
369 'dynamic_translations' => $dynamic_translations,
370 'expert_mode' => $expert_mode_conf,
371 );
372
373 linguiseSwitchMainSite();
374 update_option('linguise_options', $linguise_options);
375 linguiseRestoreMultisite();
376
377 // Reload the third party loader
378 ThirdPartyLoader::getInstance()->reload();
379
380 echo '<div class="linguise-notification-popup"><span class="material-icons"> done </span> ' . esc_html__('Linguise settings saved!', 'linguise') . '</div>';
381 }
382
383 /**
384 * Update plugin expert options
385 *
386 * @param array $expert_config Expert configuration options
387 * @param string $nonce Nonce value
388 *
389 * @return void
390 */
391 protected function updateExpertOptions($expert_config, $nonce)
392 {
393 if (!wp_verify_nonce($nonce, 'linguise-expert-settings')) {
394 wp_die(esc_html__('Nonce verification failed', 'linguise'), esc_html__('Unauthorized', 'linguise'), array(
395 'response' => 403,
396 'back_link' => true,
397 ));
398 return;
399 }
400
401 $original_config = linguiseGetConfiguration();
402 $patched_options = linguiseGetOptions();
403
404 foreach ($expert_config as $key => $value) {
405 // check if $key exists in original config
406 if (!isset($original_config[$key])) {
407 // apply directly if not exists
408 $patched_options['expert_mode'][$key] = $value;
409 continue;
410 }
411
412 $original = $original_config[$key];
413
414 if (is_bool($original['value'])) {
415 $value = $value === '1' ? true : false;
416 }
417 if (is_numeric($original['value'])) {
418 $value = (int)$value;
419 }
420
421 if ($original['value'] === $value) {
422 // Skip if value is the same as original
423 continue;
424 }
425
426 if ($original['value'] === null && empty($value)) {
427 // If original is null and value is empty, we don't need to save it
428 continue;
429 }
430
431 $patched_options['expert_mode'][$key] = $value;
432 }
433
434 linguiseSwitchMainSite();
435 update_option('linguise_options', $patched_options);
436 linguiseRestoreMultisite();
437
438 echo '<div class="linguise-notification-popup"><span class="material-icons"> done </span> ' . esc_html__('Linguise settings saved!', 'linguise') . '</div>';
439 }
440
441 /**
442 * Verify remote token
443 *
444 * @param string $new_token New token value
445 * @param string $api_url API URL
446 * @param array $api_web_errors Reference to API web errors array
447 *
448 * @return object|false Returns API data object if successful, false otherwise
449 */
450 protected function verifyRemoteToken($new_token, $api_url, &$api_web_errors)
451 {
452 $args = array(
453 'method' => 'GET',
454 'headers' => array('Referer' => linguiseGetSite(), 'authorization' => $new_token)
455 );
456
457 $result = wp_remote_get($api_url, $args);
458 if (!is_wp_error($result) && isset($result['response']['code'])
459 && ($result['response']['code'] === 200) && !empty($result['body'])
460 ) {
461 $apiResponse = json_decode($result['body']);
462 if (!empty($apiResponse) && is_object($apiResponse) && isset($apiResponse->data) && is_object($apiResponse->data)) {
463 return $apiResponse->data;
464 } else {
465 $api_web_errors[] = [
466 'type' => 'error',
467 'message' => __('API returns empty data when querying configuration. Please try again later or contact our support team if the problem persist.', 'linguise'),
468 ];
469 }
470 } else {
471 if (!is_wp_error($result) && !empty($result['response']['code']) && $result['response']['code'] === 404) {
472 $api_web_errors[] = [
473 'type' => 'error',
474 'message' => sprintf(
475 /* translators: %s: Site domain name */
476 __('The API Key provided has been rejected, please make sure you use the right key associated with the domain %s', 'linguise'),
477 linguiseGetSite()
478 ),
479 ];
480 } else {
481 $api_web_errors[] = [
482 'type' => 'error',
483 'message' => __('Configuration has not been loaded from Linguise website. Please try again later or contact our support team if the problem persist.', 'linguise'),
484 ];
485 }
486 }
487
488 return false;
489 }
490
491 /**
492 * Enqueue scripts
493 *
494 * @return void
495 */
496 public function enqueueScripts()
497 {
498 global $current_screen;
499 if (!empty($current_screen) && $current_screen->id === 'toplevel_page_linguise') {
500 // Required script
501 wp_enqueue_script('jquery-ui-sortable');
502 wp_enqueue_script('iris');
503 wp_enqueue_style('linguise_switcher', Helper::getScriptUrl('/assets/css/front.bundle.css'), array(), LINGUISE_VERSION);
504
505 wp_enqueue_script(
506 'linguise_admin_script_v2',
507 Helper::getScriptUrl('/assets/js/admin-v2.bundle.js'),
508 array('jquery'),
509 filemtime(LINGUISE_PLUGIN_PATH . '/assets/js/admin-v2.bundle.js'),
510 true
511 );
512 wp_enqueue_style(
513 'linguise_admin_script_v2',
514 Helper::getScriptUrl('/assets/css/admin-v2.bundle.css'),
515 array(),
516 filemtime(LINGUISE_PLUGIN_PATH . '/assets/css/admin-v2.bundle.css')
517 );
518 wp_localize_script('linguise_admin_script_v2', 'linguise_admin_iframe', array(
519 'nonce' => wp_create_nonce('linguise_update_config_iframe'),
520 'ajax_url' => admin_url('admin-ajax.php'),
521 'action' => 'linguise_update_config_iframe',
522 'action_get_headers' => 'linguise_get_headers',
523 // get the admin page for linguise
524 'root_page' => admin_url('admin.php?page=linguise'),
525 ));
526 }
527 }
528 }
529
530 new LinguiseConfiguration($languages_names);
531