PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.12.2
Translate and Go multilingual – Automatic AI translation – wpLingua v2.12.2
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 / dictionary.php

dictionary.php in Translate and Go multilingual – Automatic AI translation – wpLingua 2.12.2, at inc/dictionary.php

324 lines 6.9 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 dictionary entries
11 *
12 * @return array
13 */
14 function wplng_dictionary_get_entries() {
15
16 $entries_clear = array();
17 $entries_json = get_option( 'wplng_dictionary_entries' );
18 $entries = json_decode( $entries_json, true );
19
20 if ( empty( $entries ) || ! is_array( $entries ) ) {
21 return array();
22 }
23
24 foreach ( $entries as $entry ) {
25
26 /**
27 * Get and check the source
28 */
29
30 if ( ! isset( $entry['source'] )
31 || ! is_string( $entry['source'] )
32 || strlen( $entry['source'] ) >= 256
33 ) {
34 continue;
35 }
36
37 $source_clear = wplng_text_esc( $entry['source'] );
38 $source_clear = str_replace( '', '', $source_clear );
39 $source_clear = str_replace( '', '', $source_clear );
40 $source_clear = preg_replace( '#\[wplng_dictionary.*\]#', '', $source_clear );
41 $source_clear = preg_replace( '#\[\/wplng_dictionary\]#', '', $source_clear );
42
43 /**
44 * Check if rule already exist
45 */
46
47 $already_in = false;
48 foreach ( $entries_clear as $entry_clear ) {
49 if ( $source_clear === $entry_clear['source'] ) {
50 $already_in = true;
51 break;
52 }
53 }
54
55 if ( $already_in ) {
56 continue;
57 }
58
59 /**
60 * Get and check the rules
61 */
62
63 if ( ! isset( $entry['rules'] )
64 || ! is_array( $entry['rules'] )
65 ) {
66 /**
67 * Create the clear entries
68 */
69
70 $entries_clear[] = array(
71 'source' => $source_clear,
72 );
73 continue;
74 }
75
76 $rules_clear = array();
77
78 foreach ( $entry['rules'] as $language_id => $rule ) {
79 if ( ! wplng_is_valid_language_id( $language_id )
80 || ! is_string( $rule )
81 || '' === trim( $rule )
82 || $rule === $source_clear
83 || strlen( $rule ) >= 256
84 ) {
85 continue;
86 }
87
88 $rules_clear[ $language_id ] = wplng_text_esc( $rule );
89 }
90
91 /**
92 * Create the clear entries
93 */
94
95 if ( empty( $rules_clear ) ) {
96 $entries_clear[] = array(
97 'source' => $source_clear,
98 );
99 } else {
100 $entries_clear[] = array(
101 'source' => $source_clear,
102 'rules' => $rules_clear,
103 );
104 }
105 }
106
107 /**
108 * Sort dictionary entries by sources length
109 */
110
111 usort(
112 $entries_clear,
113 function ( $a, $b ) {
114 return strlen( $b['source'] ) - strlen( $a['source'] );
115 }
116 );
117
118 /**
119 * Apply wplng_dictionary_entries filter
120 */
121
122 $entries_clear = apply_filters(
123 'wplng_dictionary_entries',
124 $entries_clear
125 );
126
127 return $entries_clear;
128 }
129
130
131 /**
132 * Get dictionary entries in JSON format
133 *
134 * @return string JSON
135 */
136 function wplng_dictionary_get_entries_json() {
137 return wp_json_encode(
138 wplng_dictionary_get_entries(),
139 JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
140 );
141 }
142
143
144 /**
145 * Add dictionary tag on a texts list
146 *
147 * @param array $texts
148 * @param array $dictionary_entries
149 * @return array Texts tagged
150 */
151 function wplng_dictionary_add_tags( $texts, $language_target_id, $dictionary_entries = false ) {
152
153 if ( false === $dictionary_entries ) {
154 $dictionary_entries = wplng_dictionary_get_entries();
155 }
156
157 /**
158 * Preg quote sources texts
159 */
160
161 foreach ( $dictionary_entries as $entry_key => $entry ) {
162 $dictionary_entries[ $entry_key ]['source'] = preg_quote( $entry['source'] );
163 }
164
165 foreach ( $texts as $text_key => $text ) {
166
167 /**
168 * Get used dictionary entry in current text
169 */
170
171 $entries_used = array();
172
173 foreach ( $dictionary_entries as $entry_key => $entry ) {
174
175 $has_rules = isset( $entry['rules'] )
176 && is_array( $entry['rules'] )
177 && ! empty( $entry['rules'] );
178
179 if ( $has_rules && empty( $entry['rules'][ $language_target_id ] ) ) {
180 continue;
181 }
182
183 $preg_match = array();
184
185 preg_match_all(
186 '#' . $entry['source'] . '#i',
187 $text,
188 $preg_match
189 );
190
191 $preg_match = $preg_match[0];
192
193 foreach ( $preg_match as $key => $match ) {
194
195 $upper = 'none';
196 if ( $match === strtoupper( $match ) ) {
197 // Check uppercase
198 $upper = 'all';
199 } elseif ( $match === ucfirst( $match ) ) {
200 // Check capitalize
201 $upper = 'first';
202 }
203
204 $entry_current = $entry;
205 $entry_current['source'] = $match;
206 $entry_current['key'] = $entry_key;
207 $entry_current['upper'] = $upper;
208 $entries_used[] = $entry_current;
209 }
210 }
211
212 /**
213 * Remove dupicate in used entries
214 */
215
216 $entries_used = array_map( 'serialize', $entries_used );
217 $entries_used = array_unique( $entries_used );
218 $entries_used = array_map( 'unserialize', $entries_used );
219
220 /**
221 * Put tempory tag in current text
222 */
223
224 foreach ( $entries_used as $entry_key => $entry_used ) {
225 $text = preg_replace(
226 '#\b' . $entry_used['source'] . '\b#',
227 '' . str_repeat( '', $entry_key + 1 ) . '',
228 $text
229 );
230 }
231
232 /**
233 * Repace tempory tag by final tag
234 */
235
236 foreach ( $entries_used as $entry_key => $entry ) {
237
238 $search = '' . str_repeat( '', $entry_key + 1 ) . '';
239
240 $replace = '[wplng_dictionary ';
241 $replace .= 'key="' . $entry['key'] . '" ';
242 $replace .= 'upper="' . $entry['upper'] . '"]';
243 $replace .= $entry['source'];
244 $replace .= '[/wplng_dictionary]';
245
246 $text = str_replace(
247 $search,
248 $replace,
249 $text
250 );
251
252 }
253
254 /**
255 * Set updated text in text array
256 */
257
258 $texts[ $text_key ] = $text;
259 }
260
261 return $texts;
262 }
263
264
265 /**
266 * Transform dictionary tags to translated text
267 *
268 * @param array $texts
269 * @param array $dictionary_entries
270 * @return array Texts untagged
271 */
272 function wplng_dictionary_replace_tags( $texts, $language_target_id, $dictionary_entries = false ) {
273
274 if ( false === $dictionary_entries ) {
275 $dictionary_entries = wplng_dictionary_get_entries();
276 }
277
278 foreach ( $texts as $text_key => $text ) {
279 foreach ( $dictionary_entries as $key => $entry ) {
280
281 // For ruls "Do not translate
282 $replacement = $entry['source'];
283
284 // For specific rule by language
285 if ( ! empty( $entry['rules'][ $language_target_id ] ) ) {
286 $replacement = $entry['rules'][ $language_target_id ];
287 }
288
289 // Replacement for text in uppercase
290 $text = preg_replace(
291 '#\[wplng_dictionary key="' . $key . '" upper="all"\].+\[\/wplng_dictionary\]#U',
292 strtoupper( $replacement ),
293 $text
294 );
295
296 // Replacement for text when only the first letter is uppercase
297 $text = preg_replace(
298 '#\[wplng_dictionary key="' . $key . '" upper="first"\].+\[\/wplng_dictionary\]#U',
299 ucfirst( $replacement ),
300 $text
301 );
302
303 // Replacement for other case
304 $text = preg_replace(
305 '#\[wplng_dictionary key="' . $key . '" upper="none"\].+\[\/wplng_dictionary\]#U',
306 $replacement,
307 $text
308 );
309
310 }
311
312 // Cleaning of any residual rules
313 $text = preg_replace(
314 '#\[wplng_dictionary key=".*" upper=".*"\](.+)\[\/wplng_dictionary\]#U',
315 '${1}',
316 $text
317 );
318
319 $texts[ $text_key ] = $text;
320 }
321
322 return $texts;
323 }
324