PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.1.0
Translate and Go multilingual – Automatic AI translation – wpLingua v1.1.0
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 1.1.0, at inc/dictionary.php

304 lines 6.3 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, function( $a, $b ) {
113 return strlen( $b['source'] ) - strlen( $a['source'] );
114 }
115 );
116
117 /**
118 * Apply wplng_dictionary_entries filter
119 */
120
121 $entries_clear = apply_filters(
122 'wplng_dictionary_entries',
123 $entries_clear
124 );
125
126 return $entries_clear;
127 }
128
129
130 /**
131 * Get dictionary entries in JSON format
132 *
133 * @return string JSON
134 */
135 function wplng_dictionary_get_entries_json() {
136 return wp_json_encode(
137 wplng_dictionary_get_entries(),
138 JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
139 );
140 }
141
142
143 /**
144 * Add dictionary tag on a texts list
145 *
146 * @param array $texts
147 * @param array $dictionary_entries
148 * @return array Texts tagged
149 */
150 function wplng_dictionary_add_tags( $texts, $dictionary_entries = false ) {
151
152 if ( false === $dictionary_entries ) {
153 $dictionary_entries = wplng_dictionary_get_entries();
154 }
155
156 /**
157 * Preg quote sources texts
158 */
159
160 foreach ( $dictionary_entries as $entry_key => $entry ) {
161 $dictionary_entries[ $entry_key ]['source'] = preg_quote( $entry['source'] );
162 }
163
164 foreach ( $texts as $text_key => $text ) {
165
166 /**
167 * Get used dictionary entry in current text
168 */
169
170 $entries_used = array();
171
172 foreach ( $dictionary_entries as $entry_key => $entry ) {
173
174 $preg_match = array();
175
176 preg_match_all(
177 '#' . $entry['source'] . '#i',
178 $text,
179 $preg_match
180 );
181
182 $preg_match = $preg_match[0];
183
184 foreach ( $preg_match as $key => $match ) {
185
186 $upper = 'none';
187 if ( $match === strtoupper( $match ) ) {
188 // Check uppercase
189 $upper = 'all';
190 } elseif ( $match === ucfirst( $match ) ) {
191 // Check capitalize
192 $upper = 'first';
193 }
194
195 $entry_current = $entry;
196 $entry_current['source'] = $match;
197 $entry_current['key'] = $entry_key;
198 $entry_current['upper'] = $upper;
199 $entries_used[] = $entry_current;
200 }
201 }
202
203 /**
204 * Remove dupicate in used entries
205 */
206
207 $entries_used = array_map( 'serialize', $entries_used );
208 $entries_used = array_unique( $entries_used );
209 $entries_used = array_map( 'unserialize', $entries_used );
210
211 /**
212 * Put tempory tag in current text
213 */
214
215 foreach ( $entries_used as $entry_key => $entry_used ) {
216 $text = preg_replace(
217 '#' . $entry_used['source'] . '#',
218 '' . str_repeat( '', $entry_key + 1 ) . '',
219 $text
220 );
221 }
222
223 /**
224 * Repace tempory tag by final tag
225 */
226
227 foreach ( $entries_used as $entry_key => $entry ) {
228
229 $search = '' . str_repeat( '', $entry_key + 1 ) . '';
230
231 $replace = '[wplng_dictionary ';
232 $replace .= 'key="' . $entry['key'] . '" ';
233 $replace .= 'upper="' . $entry['upper'] . '"]';
234 $replace .= $entry['source'];
235 $replace .= '[/wplng_dictionary]';
236
237 $text = str_replace(
238 $search,
239 $replace,
240 $text
241 );
242
243 }
244
245 /**
246 * Set updated text in text array
247 */
248
249 $texts[ $text_key ] = $text;
250 }
251
252 return $texts;
253 }
254
255
256 /**
257 * Transform dictionary tags to translated text
258 *
259 * @param array $texts
260 * @param array $dictionary_entries
261 * @return array Texts untagged
262 */
263 function wplng_dictionary_replace_tags( $texts, $dictionary_entries = false ) {
264
265 $current_language_id = wplng_get_language_current_id();
266
267 if ( false === $dictionary_entries ) {
268 $dictionary_entries = wplng_dictionary_get_entries();
269 }
270
271 foreach ( $texts as $text_key => $text ) {
272 foreach ( $dictionary_entries as $key => $entry ) {
273
274 $replacement = $entry['source'];
275
276 if ( ! empty( $entry['rules'][ $current_language_id ] ) ) {
277 $replacement = $entry['rules'][ $current_language_id ];
278 }
279
280 $text = preg_replace(
281 '#\[wplng_dictionary key="' . $key . '" upper="all"\].+\[\/wplng_dictionary\]#U',
282 strtoupper( $replacement ),
283 $text
284 );
285
286 $text = preg_replace(
287 '#\[wplng_dictionary key="' . $key . '" upper="first"\].+\[\/wplng_dictionary\]#U',
288 ucfirst( $replacement ),
289 $text
290 );
291
292 $text = preg_replace(
293 '#\[wplng_dictionary key="' . $key . '" upper="none"\].+\[\/wplng_dictionary\]#U',
294 $replacement,
295 $text
296 );
297
298 $texts[ $text_key ] = $text;
299 }
300 }
301
302 return $texts;
303 }
304