PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.12.1
Translate and Go multilingual – Automatic AI translation – wpLingua v2.12.1
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 / buffering.php

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

344 lines 6.8 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 * Redirect page if is called wiht an untranslate slug to the translated URL
11 *
12 * @return void
13 */
14 function wplng_redirect_translated_slug() {
15
16 if ( is_404() ) {
17 return;
18 }
19
20 $language_website_id = wplng_get_language_website_id();
21 $language_current_id = wplng_get_language_current_id();
22
23 if ( $language_website_id === $language_current_id ) {
24 return;
25 }
26
27 if ( empty( $_COOKIE['wplingua'] )
28 && apply_filters( 'wplng_cookie_check', true )
29 ) {
30 wplng_do_not_cache_page();
31 }
32
33 $url_current = wplng_get_url_current();
34
35 if ( ! is_string( $url_current )
36 || '' === $url_current
37 || '/' === $url_current
38 ) {
39 return;
40 }
41
42 $url_translated = wplng_get_url_current_for_language(
43 $language_current_id
44 );
45
46 if ( $url_current === $url_translated ) {
47 return;
48 }
49
50 wp_safe_redirect(
51 wp_make_link_relative(
52 $url_translated
53 )
54 );
55
56 exit;
57 }
58
59
60 /**
61 * wpLingua output buffering starting function
62 *
63 * @return void
64 */
65 function wplng_ob_start() {
66
67 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
68
69 /**
70 * Is an AJAX call
71 */
72
73 if ( empty( $_SERVER['HTTP_REFERER'] )
74 || ! is_string( $_SERVER['HTTP_REFERER'] )
75 ) {
76 return;
77 }
78
79 $referer = sanitize_url( $_SERVER['HTTP_REFERER'] );
80
81 // Check if the referer is clean
82 if ( strtolower( esc_url_raw( $referer ) ) !== strtolower( $referer ) ) {
83 return;
84 }
85
86 // Check AJAX action
87 if ( ! empty( $_POST['action'] )
88 && in_array( $_POST['action'], wplng_data_excluded_ajax_action() )
89 ) {
90 return;
91 }
92
93 global $wplng_request_uri;
94 $wplng_request_uri = wplng_normalize_request_path(
95 wp_make_link_relative( $referer )
96 );
97
98 if ( ! wplng_url_is_translatable( $wplng_request_uri )
99 || wplng_get_language_website_id() === wplng_get_language_current_id()
100 ) {
101 return;
102 }
103
104 ob_start( 'wplng_ob_callback_ajax' );
105
106 } elseif ( wp_is_json_request() ) {
107
108 /**
109 * Is a REST API call
110 */
111
112 if ( empty( $_SERVER['HTTP_REFERER'] ) ) {
113 return;
114 }
115
116 $referer = sanitize_url( $_SERVER['HTTP_REFERER'] );
117
118 // Check if the referer is clean
119 if ( strtolower( esc_url_raw( $referer ) ) !== strtolower( $referer ) ) {
120 return;
121 }
122
123 global $wplng_request_uri;
124 $wplng_request_uri = wplng_normalize_request_path(
125 wp_make_link_relative( $referer )
126 );
127
128 if ( ! wplng_url_is_translatable( $wplng_request_uri )
129 || wplng_get_language_website_id() === wplng_get_language_current_id()
130 ) {
131 return;
132 }
133
134 ob_start( 'wplng_ob_callback_wp_json' );
135
136 } elseif ( wplng_url_is_sitemap_xml() ) {
137
138 /**
139 * Is an XML sitemap
140 */
141
142 if ( ! get_option( 'wplng_sitemap_xml', true ) ) {
143 return;
144 }
145
146 ob_start( 'wplng_ob_callback_sitemap_xml' );
147
148 } else {
149
150 /**
151 * Is a front call
152 */
153
154 if ( wplng_get_language_website_id() === wplng_get_language_current_id() ) {
155 return;
156 }
157
158 global $wplng_request_uri;
159 $current_path = $wplng_request_uri;
160
161 if ( ! is_string( $current_path ) ) {
162 return;
163 }
164
165 $origin_path = wplng_get_url_original( $current_path );
166
167 if ( ! wplng_url_is_translatable( $origin_path ) ) {
168 wp_safe_redirect( $origin_path );
169 exit;
170 }
171
172 $_SERVER['REQUEST_URI'] = wplng_request_path_for_wp( $origin_path );
173
174 ob_start( 'wplng_ob_callback_page' );
175 }
176 }
177
178
179 /**
180 * wpLingua OB Callback function : AJAX call
181 *
182 * @param string $output
183 * @return string
184 */
185 function wplng_ob_callback_ajax( $output ) {
186
187 global $wplng_request_uri;
188
189 if ( wplng_str_is_json( $output ) ) {
190
191 $output_translated = wplng_translate_json( $output );
192
193 } elseif ( wplng_str_is_html( $output ) ) {
194
195 $output_translated = wplng_translate_html( $output );
196
197 } else {
198
199 $output_translated = $output;
200
201 }
202
203 // Print debug data in debug.log file
204 if ( true === WPLNG_DEBUG_AJAX ) {
205
206 $action = 'UNKNOW';
207 if ( ! empty( $_POST['action'] ) && is_string( $_POST['action'] ) ) {
208 $action = $_POST['action'];
209 }
210
211 $debug = array(
212 'title' => 'wpLingua AJAX debug',
213 'action' => $action,
214 'request_uri' => $wplng_request_uri,
215 'value' => $output,
216 'translated' => $output_translated,
217 );
218
219 error_log(
220 var_export(
221 $debug,
222 true
223 )
224 );
225 }
226
227 return $output_translated;
228 }
229
230
231 /**
232 * Callback function for output buffering to process and modify sitemap XML content.
233 *
234 * @param string $content The original XML content.
235 * @return string The modified XML content with added <xhtml:link> tags or the original content if no changes are made.
236 */
237 function wplng_ob_callback_sitemap_xml( $content ) {
238 return wplng_sitemap_add_hreflang_links( $content );
239 }
240
241
242 /**
243 * wpLingua OB Callback function : Translate pages
244 *
245 * @param string $html
246 * @return string
247 */
248 function wplng_ob_callback_page( $content ) {
249
250 if ( wplng_str_is_json( $content ) ) {
251
252 $content = apply_filters( 'wplng_intercepted_json', $content );
253 $content = wplng_translate_json( $content );
254 $content = apply_filters( 'wplng_translated_json', $content );
255
256 } elseif ( wplng_str_is_html( $content ) ) {
257
258 $args = array();
259
260 if ( current_user_can( 'edit_posts' ) ) {
261
262 if ( ! empty( $_GET['wplng-mode'] ) ) {
263
264 switch ( $_GET['wplng-mode'] ) {
265
266 case 'editor':
267 $args['mode'] = 'editor';
268 break;
269
270 case 'list':
271 $args['mode'] = 'list';
272 break;
273 }
274 }
275
276 $load_in_progress_enabled = apply_filters(
277 'wplng_enable_in_progress_feature',
278 get_option( 'wplng_load_in_progress', false )
279 );
280
281 if ( $load_in_progress_enabled ) {
282
283 $args['load'] = 'enabled';
284
285 if ( ! empty( $_GET['wplng-load'] )
286 && (
287 $_GET['wplng-load'] === 'translated'
288 || $_GET['wplng-load'] === 'disabled'
289 )
290 ) {
291
292 $args['load'] = $_GET['wplng-load'];
293 wplng_do_not_cache_page();
294 }
295 }
296 }
297
298 $content = apply_filters( 'wplng_intercepted_html', $content );
299 $content = wplng_translate_html( $content, $args );
300 $content = apply_filters( 'wplng_translated_html', $content );
301
302 }
303
304 return $content;
305 }
306
307
308 /**
309 * wpLingua OB Callback function : /wp-json/
310 *
311 * @param string $output
312 * @return string
313 */
314 function wplng_ob_callback_wp_json( $output ) {
315
316 global $wplng_request_uri;
317
318 if ( wplng_str_is_json( $output ) ) {
319 $output_translated = wplng_translate_json( $output );
320 } else {
321 $output_translated = $output;
322 }
323
324 // Print debug data in debug.log file
325 if ( true === WPLNG_DEBUG_REST ) {
326
327 $debug = array(
328 'title' => 'wpLingua /wp-json/ debug',
329 'request_uri' => $wplng_request_uri,
330 'value' => $output,
331 'translated' => $output_translated,
332 );
333
334 error_log(
335 var_export(
336 $debug,
337 true
338 )
339 );
340 }
341
342 return $output_translated;
343 }
344