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