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 / synchronization.php

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

126 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Linguise\WordPress;
3
4 use Linguise\Vendor\Linguise\Script\Core\Configuration;
5
6 defined('ABSPATH') || die('');
7
8 /**
9 * Syncronization class
10 */
11 class Synchronization
12 {
13 /**
14 * Synchronization class.
15 *
16 * @var Synchronization
17 */
18 private static $instance = null;
19
20 /**
21 * Get the singleton instance of the Synchronization class
22 *
23 * @return Synchronization
24 */
25 public static function getInstance()
26 {
27 if (is_null(self::$instance)) {
28 // Create a new instance if it doesn't exist
29 self::$instance = new Synchronization();
30 }
31
32 return self::$instance;
33 }
34
35 /**
36 * Builds the payload to be sent to Linguise API for synchronization.
37 *
38 * @param array $options The plugin options as an associative array.
39 *
40 * @return array The payload as an associative array.
41 */
42 public function buildPayload($options)
43 {
44 $dynamic_translations = $options['dynamic_translations'];
45
46 $payload = [
47 'language' => $options['default_language'],
48 'allowed_languages' => $options['enabled_languages'],
49 'dynamic_translations' => $this->intBool($dynamic_translations['enabled'])
50 ];
51
52 return $payload;
53 }
54
55 /**
56 * Converts the params coming from the Linguise API to the format understood by WordPress options.
57 *
58 * @param array $params The params coming from the Linguise API as an associative array.
59 *
60 * @return array The converted params as an associative array.
61 */
62 public function convertparamsToWPOptions($params)
63 {
64 $options = [
65 'default_language' => $params['language'],
66 'enabled_languages' => $params['allowed_languages'],
67 'dynamic_translations' => [
68 'enabled' => $this->intBool($params['dynamic_translations']),
69 ]
70 ];
71
72 return $options;
73 }
74
75 /**
76 * Convert an integer (0 or 1) to a boolean value
77 *
78 * @param mixed $value The value to convert
79 *
80 * @return mixed The converted value (true or false), or original
81 */
82 public function intBool($value)
83 {
84 if (is_bool($value)) {
85 return $value;
86 }
87 if (is_int($value)) {
88 return $value === 1;
89 }
90
91 return $value;
92 }
93
94 /**
95 * Constructs the API root URL for synchronization.
96 *
97 * @param array $options An associative array containing configuration options
98 * @param string|null $path An optional path to append to the API root URL
99 *
100 * @return string The constructed API root URL for domain synchronization.
101 */
102 public static function getApiRoot($options, $path = null)
103 {
104 $api_port_base = [443, 80];
105 if (isset($options['expert_mode'])) {
106 $api_host = isset($options['expert_mode']['api_host']) ? $options['expert_mode']['api_host'] : null;
107 $api_port = isset($options['expert_mode']['api_port']) ? (int)$options['expert_mode']['api_port'] : 443;
108 $protocol = $api_port === 443 ? 'https' : 'http';
109
110 if (!empty($api_host)) {
111 return $protocol . '://' . $api_host . (in_array($api_port, $api_port_base) ? '' : ':' . $api_port) . $path;
112 }
113 }
114
115 $api_host = Configuration::getInstance()->get('api_host') ?? 'api.linguise.com';
116 $api_port_raw = Configuration::getInstance()->get('api_port');
117 if ($api_port_raw === null || $api_port_raw === '') {
118 $api_port = 443;
119 } else {
120 $api_port = (int)$api_port_raw;
121 }
122 $protocol = $api_port === 443 ? 'https' : 'http';
123 return $protocol . '://' . $api_host . (in_array($api_port, $api_port_base) ? '' : ':' . $api_port) . $path;
124 }
125 }
126