| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* File: /helper/e2pdf-translator.php |
| 5 |
* |
| 6 |
* @package E2Pdf |
| 7 |
* @license GPLv3 |
| 8 |
* @link https://e2pdf.com |
| 9 |
*/ |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
die('Access denied.'); |
| 12 |
} |
| 13 |
|
| 14 |
class Helper_E2pdf_Translator { |
| 15 |
|
| 16 |
private $translator; |
| 17 |
private $translation; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
|
| 21 |
$this->translation = get_option('e2pdf_pdf_translation', '2'); |
| 22 |
|
| 23 |
if ($this->translation !== '0') { |
| 24 |
if (class_exists('E2pdf_Polylang_Translator')) { |
| 25 |
/** |
| 26 |
* Polylang |
| 27 |
* https://wordpress.org/plugins/polylang/ |
| 28 |
*/ |
| 29 |
$this->translator = new E2pdf_Polylang_Translator($this->translation); |
| 30 |
} elseif (class_exists('E2pdf_WPML_Translator')) { |
| 31 |
/** |
| 32 |
* The WordPress Multilingual Plugin |
| 33 |
* https://wpml.org/ |
| 34 |
*/ |
| 35 |
$this->translator = new E2pdf_WPML_Translator($this->translation); |
| 36 |
} elseif (class_exists('E2pdf_Weglot_Translator')) { |
| 37 |
/** |
| 38 |
* Weglot Translate – Translate your WordPress website and go multilingual |
| 39 |
* https://wordpress.org/plugins/weglot/ |
| 40 |
*/ |
| 41 |
$this->translator = new E2pdf_Weglot_Translator($this->translation); |
| 42 |
} elseif (class_exists('E2pdf_TRP_Translator')) { |
| 43 |
/** |
| 44 |
* Translate Multilingual sites – TranslatePress |
| 45 |
* https://wordpress.org/plugins/translatepress-multilingual/ |
| 46 |
*/ |
| 47 |
$this->translator = new E2pdf_TRP_Translator($this->translation); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
public function translator() { |
| 53 |
return $this->translator; |
| 54 |
} |
| 55 |
|
| 56 |
public function isWPML() { |
| 57 |
return ($this->translator && is_a($this->translator, 'E2pdf_WPML_Translator')) ? true : false; |
| 58 |
} |
| 59 |
|
| 60 |
public function isTRP() { |
| 61 |
return ($this->translator && is_a($this->translator, 'E2pdf_TRP_Translator')) ? true : false; |
| 62 |
} |
| 63 |
|
| 64 |
public function isPolylang() { |
| 65 |
return ($this->translator && is_a($this->translator, 'E2pdf_Polylang_Translator')) ? true : false; |
| 66 |
} |
| 67 |
|
| 68 |
public function pre_translate($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 69 |
if ($string && $this->translator && method_exists($this->translator, 'pre_translate')) { |
| 70 |
$string = $this->translator->pre_translate($string, $template_id, $element_id, $type); |
| 71 |
} |
| 72 |
return $string; |
| 73 |
} |
| 74 |
|
| 75 |
public function translate($string = '', $type = 'default', $post_id = 0) { |
| 76 |
if ($string && $this->translator && method_exists($this->translator, 'translate')) { |
| 77 |
$translation = false; |
| 78 |
switch ($type) { |
| 79 |
case 'full': |
| 80 |
if ($this->translation === '2') { |
| 81 |
$translation = true; |
| 82 |
} |
| 83 |
break; |
| 84 |
case 'partial': |
| 85 |
if ( |
| 86 |
$this->translation === '1' || |
| 87 |
(is_a($this->translator, 'E2pdf_Polylang_Translator') && $this->translation === '2') || |
| 88 |
(is_a($this->translator, 'E2pdf_TRP_Translator') && $this->translation === '2') |
| 89 |
) { |
| 90 |
$translation = true; |
| 91 |
} |
| 92 |
break; |
| 93 |
default: |
| 94 |
$translation = true; |
| 95 |
break; |
| 96 |
} |
| 97 |
if ($translation) { |
| 98 |
if (is_a($this->translator, 'E2pdf_Polylang_Translator')) { |
| 99 |
/** |
| 100 |
* Polylang |
| 101 |
* https://wordpress.org/plugins/polylang/ |
| 102 |
*/ |
| 103 |
$string = $this->translator->translate($string, $post_id); |
| 104 |
} elseif (is_a($this->translator, 'E2pdf_WPML_Translator')) { |
| 105 |
/** |
| 106 |
* Multilingual CMS (WPML) |
| 107 |
* https://wpml.org/ |
| 108 |
*/ |
| 109 |
} elseif (is_a($this->translator, 'E2pdf_Weglot_Translator')) { |
| 110 |
/** |
| 111 |
* Weglot Translate – Translate your WordPress website and go multilingual |
| 112 |
* https://wordpress.org/plugins/weglot/ |
| 113 |
*/ |
| 114 |
$string = $this->translator->translate($string); |
| 115 |
} elseif (is_a($this->translator, 'E2pdf_TRP_Translator')) { |
| 116 |
/** |
| 117 |
* Translate Multilingual sites – TranslatePress |
| 118 |
* https://wordpress.org/plugins/translatepress-multilingual/ |
| 119 |
*/ |
| 120 |
$string = $this->translator->translate($string); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (is_string($string)) { |
| 126 |
if (false !== strpos($string, 'e2pdf-no-translation')) { |
| 127 |
$string = str_replace(['[e2pdf-no-translation]', '[/e2pdf-no-translation]'], ['', ''], $string); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $string; |
| 132 |
} |
| 133 |
|
| 134 |
public function lang($post_id = 0) { |
| 135 |
if (!$this->translator) { |
| 136 |
return ''; |
| 137 |
} |
| 138 |
$lang = ''; |
| 139 |
if (method_exists($this->translator, 'lang')) { |
| 140 |
$lang = $this->translator->lang($post_id); |
| 141 |
} |
| 142 |
return !empty($lang) ? $lang : ''; |
| 143 |
} |
| 144 |
|
| 145 |
public function translate_url($url = '') { |
| 146 |
if ($url && $this->translator && method_exists($this->translator, 'flush')) { |
| 147 |
$url = $this->translator->translate_url($url); |
| 148 |
} |
| 149 |
return $url; |
| 150 |
} |
| 151 |
|
| 152 |
public function flush($template_id = 0) { |
| 153 |
if ($this->translator && method_exists($this->translator, 'flush')) { |
| 154 |
$this->translator->flush($template_id); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
public function update_translation($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 159 |
if ($this->translator && method_exists($this->translator, 'update_translation')) { |
| 160 |
$this->translator->update_translation($string, $template_id, $element_id, $type); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (class_exists('WeglotWP\Services\Translate_Service_Weglot') && function_exists('weglot_get_current_language') && function_exists('weglot_get_original_language')) { |
| 166 |
|
| 167 |
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 168 |
class E2pdf_Weglot_Translator { |
| 169 |
|
| 170 |
private $helper; |
| 171 |
private $translation; |
| 172 |
private $weglot; |
| 173 |
|
| 174 |
public function __construct($translation) { |
| 175 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 176 |
$this->translation = $translation; |
| 177 |
if (weglot_get_current_language() != weglot_get_original_language()) { |
| 178 |
$this->weglot = new WeglotWP\Services\Translate_Service_Weglot(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public function translate($string) { |
| 183 |
if ($this->weglot) { |
| 184 |
$placeholders = []; |
| 185 |
$string = $this->encode($string, $placeholders); |
| 186 |
$string = $this->weglot->weglot_treat_page($string); |
| 187 |
$string = $this->decode($string, $placeholders); |
| 188 |
} |
| 189 |
return $string; |
| 190 |
} |
| 191 |
|
| 192 |
private function encode($string, &$placeholders) { |
| 193 |
$index = 0; |
| 194 |
|
| 195 |
$string = preg_replace_callback( |
| 196 |
'/\[e2pdf-no-translation\](.*?)\[\/e2pdf-no-translation\]/s', |
| 197 |
function ($m) use (&$placeholders, &$index) { |
| 198 |
$key = "__E2PDF_NT_{$index}__"; |
| 199 |
$placeholders[$key] = $m[1]; |
| 200 |
$index++; |
| 201 |
return $key; |
| 202 |
}, |
| 203 |
$string |
| 204 |
); |
| 205 |
|
| 206 |
$tokens = array('e2pdf-page-number', 'e2pdf-page-total'); |
| 207 |
foreach ($tokens as $token) { |
| 208 |
if (strpos($string, $token) !== false) { |
| 209 |
$key = "__E2PDF_NT_{$index}__"; |
| 210 |
$placeholders[$key] = $token; |
| 211 |
$index++; |
| 212 |
$string = str_replace($token, $key, $string); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return $string; |
| 217 |
} |
| 218 |
|
| 219 |
private function decode($string, $placeholders) { |
| 220 |
if ($placeholders) { |
| 221 |
$string = str_replace( |
| 222 |
array_keys($placeholders), |
| 223 |
array_values($placeholders), |
| 224 |
$string |
| 225 |
); |
| 226 |
} |
| 227 |
return $string; |
| 228 |
} |
| 229 |
|
| 230 |
public function translate_url($url = '') { |
| 231 |
if (weglot_get_current_language() != weglot_get_original_language()) { |
| 232 |
$request_url_service = weglot_get_request_url_service(); |
| 233 |
$replaced_url = $request_url_service->create_url_object($url)->getForLanguage($request_url_service->get_current_language()); |
| 234 |
if ($replaced_url) { |
| 235 |
$url = $replaced_url; |
| 236 |
} |
| 237 |
} |
| 238 |
return $url; |
| 239 |
} |
| 240 |
|
| 241 |
public function lang($post_id = 0) { |
| 242 |
return weglot_get_current_language(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
if (class_exists('TRP_Translate_Press')) { |
| 250 |
|
| 251 |
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 252 |
class E2pdf_TRP_Translator { |
| 253 |
|
| 254 |
private $helper; |
| 255 |
private $translation; |
| 256 |
private $keys = []; |
| 257 |
private $lang; |
| 258 |
private $default_lang; |
| 259 |
private $translatepress; |
| 260 |
|
| 261 |
public function __construct($translation) { |
| 262 |
global $TRP_LANGUAGE; |
| 263 |
|
| 264 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 265 |
$this->translation = $translation; |
| 266 |
$this->lang = $TRP_LANGUAGE; |
| 267 |
$this->translatepress = TRP_Translate_Press::get_trp_instance(); |
| 268 |
|
| 269 |
$settings = get_option('trp_settings', false); |
| 270 |
$this->default_lang = isset($settings['default-language']) ? $settings['default-language'] : null; |
| 271 |
} |
| 272 |
|
| 273 |
public function update_translation($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 274 |
|
| 275 |
if (!$string) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
global $wpdb; |
| 280 |
$domain = 'E2Pdf Template ' . $template_id; |
| 281 |
|
| 282 |
switch ($this->translation) { |
| 283 |
case '1': |
| 284 |
if ($type === 'template') { |
| 285 |
$exists = $wpdb->get_row( |
| 286 |
$wpdb->prepare( |
| 287 |
'SELECT id, original FROM `' . $wpdb->prefix . 'trp_gettext_original_strings` WHERE domain = %s AND original = %s', |
| 288 |
$domain, |
| 289 |
$string |
| 290 |
) |
| 291 |
); |
| 292 |
if (!$exists) { |
| 293 |
$wpdb->insert( |
| 294 |
$wpdb->prefix . 'trp_gettext_original_strings', |
| 295 |
array( |
| 296 |
'original' => $string, |
| 297 |
'domain' => $domain, |
| 298 |
'context' => '', |
| 299 |
'original_plural' => '', |
| 300 |
) |
| 301 |
); |
| 302 |
} |
| 303 |
$this->keys[] = $string; |
| 304 |
} elseif (false !== strpos($string, '[')) { |
| 305 |
$shortcode_tags = array( |
| 306 |
'e2pdf-translate', |
| 307 |
); |
| 308 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $string, $matches); |
| 309 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 310 |
if (!empty($tagnames)) { |
| 311 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $string, $shortcodes); |
| 312 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 313 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 314 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 315 |
if (!isset($atts['context']) && $shortcode[5]) { |
| 316 |
$substring = $shortcode[5]; |
| 317 |
$exists = $wpdb->get_row( |
| 318 |
$wpdb->prepare( |
| 319 |
'SELECT id, original FROM `' . $wpdb->prefix . 'trp_gettext_original_strings` WHERE domain = %s AND original = %s', |
| 320 |
$domain, |
| 321 |
$substring |
| 322 |
) |
| 323 |
); |
| 324 |
if (!$exists) { |
| 325 |
$wpdb->insert( |
| 326 |
$wpdb->prefix . 'trp_gettext_original_strings', |
| 327 |
array( |
| 328 |
'original' => $substring, |
| 329 |
'domain' => $domain, |
| 330 |
'context' => '', |
| 331 |
'original_plural' => '', |
| 332 |
) |
| 333 |
); |
| 334 |
} |
| 335 |
$this->keys[] = $substring; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
break; |
| 341 |
case '2': |
| 342 |
$exists = $wpdb->get_row( |
| 343 |
$wpdb->prepare( |
| 344 |
'SELECT id, original FROM `' . $wpdb->prefix . 'trp_gettext_original_strings` WHERE domain = %s AND original = %s', |
| 345 |
$domain, |
| 346 |
$string |
| 347 |
) |
| 348 |
); |
| 349 |
if (!$exists) { |
| 350 |
$wpdb->insert( |
| 351 |
$wpdb->prefix . 'trp_gettext_original_strings', |
| 352 |
array( |
| 353 |
'original' => $string, |
| 354 |
'domain' => $domain, |
| 355 |
'context' => '', |
| 356 |
'original_plural' => '', |
| 357 |
) |
| 358 |
); |
| 359 |
} |
| 360 |
$this->keys[] = $string; |
| 361 |
break; |
| 362 |
default: |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
public function get_strings($domain = '') { |
| 368 |
global $wpdb; |
| 369 |
if (!$domain) { |
| 370 |
return []; |
| 371 |
} |
| 372 |
return $wpdb->get_results( |
| 373 |
$wpdb->prepare( |
| 374 |
'SELECT id, original FROM `' . $wpdb->prefix . 'trp_gettext_original_strings` WHERE domain = %s', |
| 375 |
$domain |
| 376 |
) |
| 377 |
); |
| 378 |
} |
| 379 |
|
| 380 |
public function pre_translate($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 381 |
global $wpdb; |
| 382 |
$domain = 'E2Pdf Template ' . $template_id; |
| 383 |
if (!$this->lang || $this->lang == $this->default_lang) { |
| 384 |
return $string; |
| 385 |
} |
| 386 |
|
| 387 |
$translated = $wpdb->get_var( |
| 388 |
$wpdb->prepare( |
| 389 |
'SELECT translated FROM `' . $wpdb->prefix . 'trp_gettext_' . strtolower($this->lang) . '` WHERE original = %s AND domain = %s AND (status = 2 OR status = 1) LIMIT 1', |
| 390 |
$string, |
| 391 |
$domain |
| 392 |
) |
| 393 |
); |
| 394 |
return $translated ? $translated : $string; |
| 395 |
} |
| 396 |
|
| 397 |
public function flush($template_id) { |
| 398 |
global $wpdb; |
| 399 |
$domain = 'E2Pdf Template ' . $template_id; |
| 400 |
|
| 401 |
$strings = $this->get_strings($domain); |
| 402 |
|
| 403 |
if (!empty($strings) && is_array($strings)) { |
| 404 |
foreach ($strings as $name) { |
| 405 |
if (!in_array($name->original, $this->keys, false)) { |
| 406 |
foreach ($this->languages() as $lang) { |
| 407 |
$wpdb->delete( |
| 408 |
$wpdb->prefix . 'trp_gettext_' . strtolower($lang), |
| 409 |
array('original_id' => $name->id) |
| 410 |
); |
| 411 |
} |
| 412 |
$wpdb->delete( |
| 413 |
$wpdb->prefix . 'trp_gettext_original_strings', |
| 414 |
array('id' => $name->id) |
| 415 |
); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
$this->keys = []; |
| 420 |
} |
| 421 |
|
| 422 |
public function translate($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 423 |
add_filter('trp_get_existing_translations', array($this, 'filter_trp_get_existing_translations'), 99, 5); |
| 424 |
if (method_exists($this->translatepress, 'get_component')) { |
| 425 |
$translation_render = $this->translatepress->get_component('translation_render'); |
| 426 |
if (is_object($translation_render) && method_exists($translation_render, 'translate_page')) { |
| 427 |
$string = $translation_render->translate_page($string); |
| 428 |
} |
| 429 |
} |
| 430 |
remove_filter('trp_get_existing_translations', array($this, 'filter_trp_get_existing_translations'), 99); |
| 431 |
return $string; |
| 432 |
} |
| 433 |
|
| 434 |
public function translate_url($url = '') { |
| 435 |
if (method_exists($this->translatepress, 'get_component')) { |
| 436 |
$url_converter = $this->translatepress->get_component('url_converter'); |
| 437 |
if (is_object($url_converter) && method_exists($url_converter, 'get_url_for_language')) { |
| 438 |
$url = $url_converter->get_url_for_language($this->lang(), $url, ''); |
| 439 |
} |
| 440 |
} |
| 441 |
return $url; |
| 442 |
} |
| 443 |
|
| 444 |
public function lang($post_id = 0) { |
| 445 |
return $this->lang; |
| 446 |
} |
| 447 |
|
| 448 |
public function languages() { |
| 449 |
$languages = []; |
| 450 |
if (method_exists($this->translatepress, 'get_component')) { |
| 451 |
$settings_component = $this->translatepress->get_component('settings'); |
| 452 |
if (is_object($settings_component) && method_exists($settings_component, 'get_settings')) { |
| 453 |
$settings = $settings_component->get_settings(); |
| 454 |
if (isset($settings['publish-languages']) && is_array($settings['publish-languages'])) { |
| 455 |
$languages = $settings['publish-languages']; |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
return $languages; |
| 460 |
} |
| 461 |
|
| 462 |
public function filter_trp_get_existing_translations($dictionary, $prepared_query, $strings_array, $language_code, $block_type) { |
| 463 |
global $wpdb; |
| 464 |
if (!is_array($strings_array) || count($strings_array) == 0) { |
| 465 |
return $dictionary; |
| 466 |
} |
| 467 |
if ($block_type == null) { |
| 468 |
$and_block_type = ''; |
| 469 |
} else { |
| 470 |
$and_block_type = ' AND block_type = ' . $block_type; |
| 471 |
} |
| 472 |
$placeholders = array(); |
| 473 |
$values = array(); |
| 474 |
foreach ($strings_array as $string) { |
| 475 |
$wptexturized_string = wptexturize($string); |
| 476 |
if ($string !== $wptexturized_string) { |
| 477 |
$placeholders[] = '%s'; |
| 478 |
$values[] = wptexturize($string); |
| 479 |
} |
| 480 |
} |
| 481 |
if (!empty($values)) { |
| 482 |
if (method_exists($this->translatepress, 'get_component')) { |
| 483 |
$translation_query = $this->translatepress->get_component('query'); |
| 484 |
if (is_object($translation_query) && method_exists($translation_query, 'get_table_name')) { |
| 485 |
$query = 'SELECT original,translated, status FROM `' . sanitize_text_field($this->translatepress->get_component('query')->get_table_name($language_code)) . '` WHERE status != ' . $this->translatepress->get_component('query')->get_constant_not_translated() . $and_block_type . " AND translated <>'' AND original IN "; |
| 486 |
$query .= '( ' . implode(', ', $placeholders) . ' )'; |
| 487 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 488 |
$additional_dictionary = $wpdb->get_results($wpdb->prepare($query, $values), OBJECT_K); |
| 489 |
if (!empty($additional_dictionary) && is_array($additional_dictionary)) { |
| 490 |
foreach ($additional_dictionary as $dictionary_key => $dictionary_object) { |
| 491 |
if (false != strpos($dictionary_key, 'R')) { |
| 492 |
$replace = array( |
| 493 |
'“' => '"', |
| 494 |
'”' => '"', |
| 495 |
'’' => "'", |
| 496 |
'′' => "'", |
| 497 |
'″' => '"', |
| 498 |
'‘' => "'", |
| 499 |
'–' => '-', |
| 500 |
'—' => '-', |
| 501 |
); |
| 502 |
$untexturized_dictionary_key = str_replace(array_keys($replace), $replace, $dictionary_key); |
| 503 |
if ($untexturized_dictionary_key !== $dictionary_key && !isset($dictionary[$untexturized_dictionary_key]) && isset($dictionary_object->original)) { |
| 504 |
$new_dicitionary_object = $dictionary_object; |
| 505 |
$new_dicitionary_object->original = $untexturized_dictionary_key; |
| 506 |
$dictionary[$untexturized_dictionary_key] = $new_dicitionary_object; |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
return $dictionary; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
} |
| 519 |
|
| 520 |
if (function_exists('icl_register_string') && function_exists('icl_t') && !defined('POLYLANG_VERSION') && !class_exists('E2pdf_WPML_Translator')) { |
| 521 |
|
| 522 |
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 523 |
class E2pdf_WPML_Translator { |
| 524 |
|
| 525 |
private $helper; |
| 526 |
private $translation; |
| 527 |
private $keys = []; |
| 528 |
|
| 529 |
public function __construct($translation) { |
| 530 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 531 |
$this->translation = $translation; |
| 532 |
} |
| 533 |
|
| 534 |
public function update_translation($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 535 |
if ($string) { |
| 536 |
switch ($this->translation) { |
| 537 |
case '1': |
| 538 |
if ($type === 'template') { |
| 539 |
$context = 'E2Pdf Template ' . $template_id; |
| 540 |
do_action('wpml_register_single_string', $context, $string, $string, false, apply_filters('wpml_default_language', null)); |
| 541 |
$this->keys[] = $string; |
| 542 |
} elseif (false !== strpos($string, '[')) { |
| 543 |
$shortcode_tags = array( |
| 544 |
'e2pdf-translate', |
| 545 |
); |
| 546 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $string, $matches); |
| 547 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 548 |
if (!empty($tagnames)) { |
| 549 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $string, $shortcodes); |
| 550 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 551 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 552 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 553 |
if (!isset($atts['context']) && $shortcode[5]) { |
| 554 |
$context = 'E2Pdf Template ' . $template_id; |
| 555 |
$substring = $shortcode[5]; |
| 556 |
do_action('wpml_register_single_string', $context, $substring, $substring, false, apply_filters('wpml_default_language', null)); |
| 557 |
$this->keys[] = $substring; |
| 558 |
} |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
break; |
| 563 |
case '2': |
| 564 |
$context = 'E2Pdf Template ' . $template_id; |
| 565 |
$name = 'e2pdf_template_' . $template_id . '_' . $element_id . '_' . $type; |
| 566 |
do_action('wpml_register_single_string', $context, $name, $string, false, apply_filters('wpml_default_language', null)); |
| 567 |
$this->keys[] = $name; |
| 568 |
break; |
| 569 |
default: |
| 570 |
break; |
| 571 |
} |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
public function flush($template_id = 0) { |
| 576 |
$context = 'E2Pdf Template ' . $template_id; |
| 577 |
$strings = $this->get_strings($context); |
| 578 |
if (!empty($strings) && is_array($strings)) { |
| 579 |
foreach ($strings as $name) { |
| 580 |
if (!in_array($name, $this->keys, false) && function_exists('icl_unregister_string')) { |
| 581 |
icl_unregister_string($context, $name); |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
$this->keys = []; |
| 586 |
} |
| 587 |
|
| 588 |
public function get_strings($context = '') { |
| 589 |
global $wpdb; |
| 590 |
return $wpdb->get_col( |
| 591 |
$wpdb->prepare( |
| 592 |
'SELECT name FROM `' . $wpdb->prefix . 'icl_strings` WHERE context = %s', |
| 593 |
$context |
| 594 |
) |
| 595 |
); |
| 596 |
} |
| 597 |
|
| 598 |
public function pre_translate($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 599 |
return $this->translate($string, $template_id, $element_id, $type); |
| 600 |
} |
| 601 |
|
| 602 |
public function translate($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 603 |
switch ($this->translation) { |
| 604 |
case '1': |
| 605 |
if ($type === 'template') { |
| 606 |
$context = 'E2Pdf Template ' . $template_id; |
| 607 |
$string = icl_t($context, $string, $string); |
| 608 |
} elseif (false !== strpos($string, '[')) { |
| 609 |
$shortcode_tags = array( |
| 610 |
'e2pdf-translate', |
| 611 |
); |
| 612 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $string, $matches); |
| 613 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 614 |
if (!empty($tagnames)) { |
| 615 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $string, $shortcodes); |
| 616 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 617 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 618 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 619 |
if (!isset($atts['context']) && $shortcode[5]) { |
| 620 |
$context = 'E2Pdf Template ' . $template_id; |
| 621 |
$substring = $shortcode[5]; |
| 622 |
$string = str_replace($shortcode_value, icl_t($context, $substring, $substring), $string); |
| 623 |
} |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
break; |
| 628 |
case '2': |
| 629 |
$context = 'E2Pdf Template ' . $template_id; |
| 630 |
$name = 'e2pdf_template_' . $template_id . '_' . $element_id . '_' . $type; |
| 631 |
$string = icl_t($context, $name, $string); |
| 632 |
break; |
| 633 |
default: |
| 634 |
break; |
| 635 |
} |
| 636 |
return $string; |
| 637 |
} |
| 638 |
|
| 639 |
public function translate_url($url = '') { |
| 640 |
return $url; |
| 641 |
} |
| 642 |
|
| 643 |
public function lang($post_id = 0) { |
| 644 |
$lang = apply_filters('wpml_current_language', null); |
| 645 |
return empty($lang) ? '' : $lang; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
} |
| 650 |
|
| 651 |
if (defined('POLYLANG_VERSION') && function_exists('PLL') && version_compare(POLYLANG_VERSION, '3.7.0', '>=') && class_exists('PLL_WPML_Compat') && !class_exists('E2pdf_Polylang_Translator')) { |
| 652 |
|
| 653 |
// phpcs:ignore Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 654 |
class E2pdf_Polylang_Translator { |
| 655 |
|
| 656 |
private $helper; |
| 657 |
private $translation; |
| 658 |
private $wpml; |
| 659 |
private $keys = []; |
| 660 |
|
| 661 |
public function __construct($translation) { |
| 662 |
$this->helper = Helper_E2pdf_Helper::instance(); |
| 663 |
$this->translation = $translation; |
| 664 |
$field_types = ['select', 'checkbox', 'radio', 'button_group']; |
| 665 |
foreach ($field_types as $type) { |
| 666 |
add_filter('acf/format_value/type=' . $type, [$this, 'filter_acf_format_value'], 10, 2); |
| 667 |
} |
| 668 |
$this->wpml = PLL_WPML_Compat::instance(); |
| 669 |
} |
| 670 |
|
| 671 |
public function update_translation($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 672 |
if ($string) { |
| 673 |
switch ($this->translation) { |
| 674 |
case '1': |
| 675 |
if ($type === 'template') { |
| 676 |
$context = 'E2Pdf Template ' . $template_id; |
| 677 |
if (method_exists($this->wpml, 'register_string')) { |
| 678 |
$this->wpml->register_string($context, $string, $string); |
| 679 |
} |
| 680 |
$this->keys[] = $string; |
| 681 |
} elseif (false !== strpos($string, '[')) { |
| 682 |
$shortcode_tags = array( |
| 683 |
'e2pdf-translate', |
| 684 |
); |
| 685 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $string, $matches); |
| 686 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 687 |
if (!empty($tagnames)) { |
| 688 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $string, $shortcodes); |
| 689 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 690 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 691 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 692 |
if (!isset($atts['context']) && $shortcode[5]) { |
| 693 |
$context = 'E2Pdf Template ' . $template_id; |
| 694 |
$substring = $shortcode[5]; |
| 695 |
if (method_exists($this->wpml, 'register_string')) { |
| 696 |
$this->wpml->register_string($context, $substring, $substring); |
| 697 |
} |
| 698 |
$this->keys[] = $substring; |
| 699 |
} |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
break; |
| 704 |
case '2': |
| 705 |
$context = 'E2Pdf Template ' . $template_id; |
| 706 |
$name = 'e2pdf_template_' . $template_id . '_' . $element_id . '_' . $type; |
| 707 |
if (method_exists($this->wpml, 'register_string')) { |
| 708 |
$this->wpml->register_string($context, $name, $string); |
| 709 |
} |
| 710 |
$this->keys[] = $name; |
| 711 |
break; |
| 712 |
default: |
| 713 |
break; |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
public function flush($template_id = 0) { |
| 719 |
$context = 'E2Pdf Template ' . $template_id; |
| 720 |
$strings = $this->get_strings($context); |
| 721 |
if (!empty($strings) && is_array($strings) && method_exists($this->wpml, 'unregister_string')) { |
| 722 |
foreach ($strings as $name) { |
| 723 |
if (!in_array($name, $this->keys, false)) { |
| 724 |
$this->wpml->unregister_string($context, $name); |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
$this->keys = []; |
| 729 |
} |
| 730 |
|
| 731 |
public function get_strings($context = '') { |
| 732 |
$strings = array(); |
| 733 |
if (method_exists($this->wpml, 'get_strings')) { |
| 734 |
$all_strings = $this->wpml->get_strings(array()); |
| 735 |
foreach ($all_strings as $string) { |
| 736 |
if ($string['context'] == $context) { |
| 737 |
$strings[] = $string['name']; |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
return $strings; |
| 742 |
} |
| 743 |
|
| 744 |
public function filter_acf_format_value($string = '', $post_id = 0) { |
| 745 |
if (apply_filters('e2pdf_pdf_render', false)) { |
| 746 |
$string = $this->translate($string, $post_id); |
| 747 |
} |
| 748 |
return $string; |
| 749 |
} |
| 750 |
|
| 751 |
public function pre_translate($string = '', $template_id = 0, $element_id = '', $type = '') { |
| 752 |
switch ($this->translation) { |
| 753 |
case '1': |
| 754 |
if (!empty($string) && is_string($string)) { |
| 755 |
if ($type === 'template') { |
| 756 |
$string = $this->translate($string); |
| 757 |
} elseif (false !== strpos($string, '[')) { |
| 758 |
$shortcode_tags = array( |
| 759 |
'e2pdf-translate', |
| 760 |
); |
| 761 |
preg_match_all('@\[([^<>&/\[\]\x00-\x20=]++)@', $string, $matches); |
| 762 |
$tagnames = array_intersect($shortcode_tags, $matches[1]); |
| 763 |
if (!empty($tagnames)) { |
| 764 |
preg_match_all('/' . $this->helper->load('shortcode')->get_shortcode_regex($tagnames) . '/', $string, $shortcodes); |
| 765 |
foreach ($shortcodes[0] as $key => $shortcode_value) { |
| 766 |
$shortcode = $this->helper->load('shortcode')->get_shortcode($shortcodes, $key); |
| 767 |
$atts = shortcode_parse_atts($shortcode[3]); |
| 768 |
if (!isset($atts['context']) && $shortcode[5]) { |
| 769 |
$context = 'E2Pdf Template ' . $template_id; |
| 770 |
$substring = $shortcode[5]; |
| 771 |
$string = str_replace($shortcode_value, $this->translate($substring), $string); |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
break; |
| 778 |
case '2': |
| 779 |
$context = 'E2Pdf Template ' . $template_id; |
| 780 |
$name = 'e2pdf_template_' . $template_id . '_' . $element_id . '_' . $type; |
| 781 |
if (empty($string) && method_exists($this->wpml, 'get_string_by_context_and_name')) { |
| 782 |
$string = $this->wpml->get_string_by_context_and_name($context, $name); |
| 783 |
} |
| 784 |
$string = $this->translate($string); |
| 785 |
break; |
| 786 |
default: |
| 787 |
break; |
| 788 |
} |
| 789 |
return $string; |
| 790 |
} |
| 791 |
|
| 792 |
public function translate($string = '', $post_id = 0) { |
| 793 |
if ($string && function_exists('pll__') && function_exists('pll_translate_string')) { |
| 794 |
$lang = $this->current_lang($post_id); |
| 795 |
return empty($lang) ? pll__($string) : pll_translate_string($string, $lang); |
| 796 |
} |
| 797 |
return $string; |
| 798 |
} |
| 799 |
|
| 800 |
public function translate_url($url = '') { |
| 801 |
return $url; |
| 802 |
} |
| 803 |
|
| 804 |
public function current_lang($post_id = 0) { |
| 805 |
$post_id = (int) $post_id; |
| 806 |
$lang = $post_id && function_exists('pll_get_post_language') ? pll_get_post_language($post_id) : null; |
| 807 |
if (empty($lang)) { |
| 808 |
$lang = PLL()->curlang; |
| 809 |
} |
| 810 |
return $lang; |
| 811 |
} |
| 812 |
|
| 813 |
public function lang($post_id = 0) { |
| 814 |
$lang = $this->current_lang($post_id); |
| 815 |
return isset($lang->locale) ? $lang->locale : ''; |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
} |
| 820 |
|