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 / admin / Helper.php

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

202 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Linguise\WordPress\Admin;
4
5 defined('ABSPATH') || die('');
6
7
8 /**
9 * Class Helper
10 */
11 class Helper
12 {
13 /**
14 * Retrieve the latest errors from Linguise php script
15 *
16 * @return array
17 */
18 public static function getLastErrors()
19 {
20 if (defined('LINGUISE_IS_TESTING') && LINGUISE_IS_TESTING) {
21 /**
22 * If we are testing, we use custom errors data
23 * Also ignore type check error
24 *
25 * @disregard
26 */
27 $errors = defined('LINGUISE_TESTING_ERRORS') ? LINGUISE_TESTING_ERRORS : '';
28 } else {
29 // @codeCoverageIgnoreStart
30 $errorsFile = LINGUISE_PLUGIN_PATH . DIRECTORY_SEPARATOR. 'vendor' . DIRECTORY_SEPARATOR . 'linguise' . DIRECTORY_SEPARATOR . 'script-php' . DIRECTORY_SEPARATOR . 'errors.php';
31 if (file_exists($errorsFile)) {
32 $errors = file_get_contents($errorsFile);
33 } else {
34 $errors = '';
35 }
36 // @codeCoverageIgnoreEnd
37 }
38
39 $errorsList = [];
40 if (!preg_match_all('/^\[([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})\] (?:([0-9]{3}): )?(.*)$/m', $errors, $matches, PREG_SET_ORDER)) {
41 return $errorsList; // @codeCoverageIgnore
42 }
43
44 foreach ($matches as $error) {
45 $message_extract = self::extractMoreFromMessage($error[3]);
46
47 $errorsList[] = self::mergeErrorList($error, $message_extract);
48 }
49
50 return $errorsList;
51 }
52
53 /**
54 * Extract more data from JSON message data
55 *
56 * @param string $message The message to extract data from
57 *
58 * @return array|null The extracted data
59 */
60 private static function extractMoreFromMessage($message)
61 {
62 if (empty($message)) {
63 return null;
64 }
65
66 // parse as JSON
67 $json = json_decode($message, true);
68 if (json_last_error() !== JSON_ERROR_NONE) {
69 return null;
70 }
71
72 $data = [];
73 if (isset($json['error_code'])) {
74 $data['code'] = $json['error_code'];
75 } elseif (isset($json['code'])) {
76 $data['code'] = $json['code'];
77 }
78
79 if (isset($json['message'])) {
80 $data['message'] = $json['message'];
81 }
82
83 return $data;
84 }
85
86 /**
87 * Merge error data from the main and the extracted data
88 *
89 * @param array $error The error data from the main message
90 * @param array|null $extracted The extracted data from the JSON message
91 *
92 * @return array|null The extracted data
93 */
94 private static function mergeErrorList($error, $extracted)
95 {
96 $current = [
97 'time' => $error[1],
98 'code' => $error[2],
99 'message' => $error[3],
100 ];
101
102 if ($extracted !== null && empty($current['code']) && !empty($extracted['code'])) {
103 $current['code'] = $extracted['code'];
104 $current['message'] = $extracted['message'];
105 }
106
107 return $current;
108 }
109
110 /**
111 * Render an admonition box
112 *
113 * @param string $content The content to display inside the admonition
114 * @param string $mode The mode of the admonition (info, warning, error, success, primary)
115 * @param array $options Options for the admonition
116 *
117 * @return string The HTML for the admonition box
118 */
119 public static function renderAdmonition($content, $mode = 'info', $options = [])
120 {
121 $merged_options = array_merge([
122 'toggle' => false,
123 'id' => '',
124 'hide' => false,
125 'class' => '',
126 ], $options);
127 $admonition_icons = [
128 'info' => 'info_outline',
129 'warning' => 'warning_amber',
130 'error' => 'error_outline',
131 'success' => 'check_circle',
132 'primary' => 'info_outline',
133 ];
134
135 return (
136 '<div ' . (!empty($merged_options['id']) ? 'id="' . esc_attr($merged_options['id']) . '" ' : '') . 'class="linguise-admonition mode-' . esc_attr($mode) . ' ' . ($merged_options['toggle'] ? 'with-close' : '') . (!empty($merged_options['class']) ? ' ' . esc_attr($merged_options['class']) : '') . '"' . ($merged_options['hide'] ? 'style="display: none;">' : '>') .
137 '<div class="admonition-icon">' .
138 '<span class="material-icons">' . $admonition_icons[$mode] . '</span>' .
139 '</div>' .
140 '<div class="admonition-content">' .
141 $content .
142 '</div>' .
143 ($merged_options['toggle'] ? '<span class="close-icon"><span class="material-icons">close</span></span>' : '') .
144 '</div>'
145 );
146 }
147
148 /**
149 * Get the native name of a language code
150 *
151 * @param string $lang_code The language code to get the native name for
152 *
153 * @return string
154 */
155 public static function getWPLangNativeName($lang_code)
156 {
157 if (defined('LINGUISE_IS_TESTING') && LINGUISE_IS_TESTING) {
158 return 'Testing (' . $lang_code . ')'; // In testing mode, we return the code as is
159 }
160 // @codeCoverageIgnoreStart
161 require_once ABSPATH . 'wp-admin/includes/translation-install.php';
162 $translations = wp_get_available_translations();
163 if (isset($translations[$lang_code])) {
164 return $translations[$lang_code]['native_name'];
165 }
166
167 return 'Unknown';
168 // @codeCoverageIgnoreEnd
169 }
170
171 /**
172 * Get multisite data information
173 *
174 * @param string $mode The mode to check for multisite
175 *
176 * @codeCoverageIgnore
177 *
178 * @return array the multisite information
179 */
180 public static function getMultisiteInfo($mode = 'main-page')
181 {
182 $is_multisite_and_not_main = false;
183 if (linguiseIsMultisiteFolder()) {
184 $current_network_id = get_current_network_id();
185 $main_site_id = get_main_site_id($current_network_id);
186 $current_blog_id = get_current_blog_id();
187 $is_multisite_and_not_main = $main_site_id !== $current_blog_id;
188 }
189
190 $actual_mode = $mode !== 'main-page' ? '&ling_mode=' . $mode : '';
191
192 linguiseSwitchMainSite();
193 $main_site_url = admin_url('admin.php?page=linguise' . $actual_mode);
194 linguiseRestoreMultisite();
195
196 return [
197 'multisite' => $is_multisite_and_not_main,
198 'main_site' => $main_site_url,
199 ];
200 }
201 }
202