PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.15.1
Translate and Go multilingual – Automatic AI translation – wpLingua v2.15.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.15.1, at inc/buffering.php

348 lines 7.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 * 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 $output_translated = $output;
190
191 if ( wplng_str_is_json( $output ) ) {
192
193 $output_translated = apply_filters( 'wplng_intercepted_json', $output_translated );
194 $output_translated = wplng_translate_json( $output_translated );
195 $output_translated = apply_filters( 'wplng_translated_json', $output_translated );
196
197 } elseif ( wplng_str_is_html( $output ) ) {
198
199 $output_translated = apply_filters( 'wplng_intercepted_html', $output_translated );
200 $output_translated = wplng_translate_html( $output_translated );
201 $output_translated = apply_filters( 'wplng_translated_html', $output_translated );
202
203 }
204
205 // Print debug data in debug.log file
206 if ( true === WPLNG_DEBUG_AJAX ) {
207
208 $action = 'UNKNOW';
209 if ( ! empty( $_POST['action'] ) && is_string( $_POST['action'] ) ) {
210 $action = $_POST['action'];
211 }
212
213 $debug = array(
214 'title' => 'wpLingua AJAX debug',
215 'action' => $action,
216 'request_uri' => $wplng_request_uri,
217 'value' => $output,
218 'translated' => $output_translated,
219 );
220
221 error_log(
222 var_export(
223 $debug,
224 true
225 )
226 );
227 }
228
229 return $output_translated;
230 }
231
232
233 /**
234 * Callback function for output buffering to process and modify sitemap XML content.
235 *
236 * @param string $content The original XML content.
237 * @return string The modified XML content with added <xhtml:link> tags or the original content if no changes are made.
238 */
239 function wplng_ob_callback_sitemap_xml( $content ) {
240 return wplng_sitemap_add_hreflang_links( $content );
241 }
242
243
244 /**
245 * wpLingua OB Callback function : Translate pages
246 *
247 * @param string $html
248 * @return string
249 */
250 function wplng_ob_callback_page( $content ) {
251
252 if ( wplng_str_is_json( $content ) ) {
253
254 $content = apply_filters( 'wplng_intercepted_json', $content );
255 $content = wplng_translate_json( $content );
256 $content = apply_filters( 'wplng_translated_json', $content );
257
258 } elseif ( wplng_str_is_html( $content ) ) {
259
260 $args = array();
261
262 if ( current_user_can( 'edit_posts' ) ) {
263
264 if ( ! empty( $_GET['wplng-mode'] ) ) {
265
266 switch ( $_GET['wplng-mode'] ) {
267
268 case 'editor':
269 $args['mode'] = 'editor';
270 break;
271
272 case 'list':
273 $args['mode'] = 'list';
274 break;
275 }
276 }
277
278 $load_in_progress_enabled = apply_filters(
279 'wplng_enable_in_progress_feature',
280 get_option( 'wplng_load_in_progress', false )
281 );
282
283 if ( $load_in_progress_enabled ) {
284
285 $args['load'] = 'enabled';
286
287 if ( ! empty( $_GET['wplng-load'] )
288 && (
289 $_GET['wplng-load'] === 'translated'
290 || $_GET['wplng-load'] === 'disabled'
291 )
292 ) {
293
294 $args['load'] = $_GET['wplng-load'];
295 wplng_do_not_cache_page();
296 }
297 }
298 }
299
300 $content = apply_filters( 'wplng_intercepted_html', $content );
301 $content = wplng_translate_html( $content, $args );
302 $content = apply_filters( 'wplng_translated_html', $content );
303
304 }
305
306 return $content;
307 }
308
309
310 /**
311 * wpLingua OB Callback function : /wp-json/
312 *
313 * @param string $output
314 * @return string
315 */
316 function wplng_ob_callback_wp_json( $output ) {
317
318 global $wplng_request_uri;
319
320 $output_translated = $output;
321
322 if ( wplng_str_is_json( $output ) ) {
323 $output_translated = apply_filters( 'wplng_intercepted_json', $output_translated );
324 $output_translated = wplng_translate_json( $output_translated );
325 $output_translated = apply_filters( 'wplng_translated_json', $output_translated );
326 }
327
328 // Print debug data in debug.log file
329 if ( true === WPLNG_DEBUG_REST ) {
330
331 $debug = array(
332 'title' => 'wpLingua /wp-json/ debug',
333 'request_uri' => $wplng_request_uri,
334 'value' => $output,
335 'translated' => $output_translated,
336 );
337
338 error_log(
339 var_export(
340 $debug,
341 true
342 )
343 );
344 }
345
346 return $output_translated;
347 }
348