PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.22
E2Pdf – Export Pdf Tool for WordPress v1.32.22
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / classes / helper / e2pdf-translator.php

e2pdf-translator.php in E2Pdf – Export Pdf Tool for WordPress 1.32.22, at classes/helper/e2pdf-translator.php

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