Changeset 3288431
- Timestamp:
- 05/06/2025 12:12:37 PM (10 months ago)
- Location:
- auto-translator-polylang
- Files:
-
- 3 edited
- 4 copied
-
tags/1.1.2 (copied) (copied from auto-translator-polylang/trunk)
-
tags/1.1.2/auto-translator-for-polylang.php (copied) (copied from auto-translator-polylang/trunk/auto-translator-for-polylang.php) (2 diffs)
-
tags/1.1.2/includes/Base/ATFP_Translation.php (copied) (copied from auto-translator-polylang/trunk/includes/Base/ATFP_Translation.php) (4 diffs)
-
tags/1.1.2/readme.txt (copied) (copied from auto-translator-polylang/trunk/readme.txt) (2 diffs)
-
trunk/auto-translator-for-polylang.php (modified) (2 diffs)
-
trunk/includes/Base/ATFP_Translation.php (modified) (4 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
auto-translator-polylang/tags/1.1.2/auto-translator-for-polylang.php
r3285501 r3288431 5 5 * Plugin Name: Auto Translator for Polylang 6 6 * Description: Plugin to translate your pages and posts working with Polylang 7 * Version: 1.1. 17 * Version: 1.1.2 8 8 * Author: UAPP GROUP 9 9 * Author URI: https://uapp.group/ … … 26 26 define('ATFP_PLUGIN_NAME', trim(dirname(ATFP_PLUGIN_BASENAME), '/')); 27 27 28 const ATFP_VERSION = '1.1. 1';28 const ATFP_VERSION = '1.1.2'; 29 29 30 30 const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates"; -
auto-translator-polylang/tags/1.1.2/includes/Base/ATFP_Translation.php
r3285501 r3288431 431 431 } 432 432 433 $html = $this->replace_br_tags_with_placeholders($html); 434 433 435 $dom = new ATFP_simple_html_dom(); 434 $dom->load( wp_kses_post($html));435 436 foreach ($dom->find(' text') as $textNode) {437 $trimmedInnerText = trim($textNode->innertext);438 439 if (!empty($trimmedInnerText) && $trimmedInnerText !== '</p>') { 440 $translated _text = $this->translate_text($trimmedInnerText, $target_language);441 442 if ($translated _text) {443 $ newNode = $dom->createTextNode($translated_text);444 $ textNode->innertext = $newNode->innertext;436 $dom->load($html); 437 438 foreach ($dom->find('p, div, span, li') as $block) { 439 if (!empty(trim($block->innertext))) { 440 list($text_with_placeholders, $tag_map) = $this->replace_inline_tags_with_placeholders($block->innertext); 441 442 $translated = $this->translate_text($text_with_placeholders, $target_language); 443 444 if ($translated) { 445 $restored = $this->restore_placeholders_with_tags($translated, $tag_map); 446 $block->innertext = $restored; 445 447 } 446 448 } … … 448 450 449 451 $translated_html = $dom->save(); 452 453 $translated_html = $this->restore_br_tags_from_placeholders($translated_html); 450 454 451 455 if (!$translate_shortcodes) { … … 456 460 } 457 461 458 private function translate_text ($text, $targetLanguage) {459 $api_key = get_option('atfp_api_key');460 $url = 'https://translation.googleapis.com/language/translate/v2';461 $query_params = "?q=" . esc_html(urlencode($text)) . "&target=" . esc_html($targetLanguage) . "&format=html&key=" . esc_html($api_key) . " ";462 private function translate_text($text, $targetLanguage) { 463 $api_key = get_option('atfp_api_key'); 464 $url = 'https://translation.googleapis.com/language/translate/v2'; 465 $query_params = "?q=" . esc_html(urlencode($text)) . "&target=" . esc_html($targetLanguage) . "&format=html&key=" . esc_html($api_key); 462 466 463 467 $response = wp_remote_post($url . $query_params); … … 519 523 } 520 524 521 private function replace_shortcodes_with_placeholders ($content, &$shortcodes) {522 $index = 0;525 private function replace_shortcodes_with_placeholders($content, &$shortcodes) { 526 $index = 0; 523 527 $shortcodes = []; 524 528 525 return preg_replace_callback('/\[(\[?)([^\[\]]+)(\]?)\]/', function ($matches) use (&$shortcodes, &$index) {526 $placeholder = '<!--SHORTCODE_' . $index . '-->';527 $shortcodes[$placeholder] = $matches[0]; // Store original shortcode529 $content = preg_replace_callback('/\[(html_block|raw)](.*?)\[\/\1]/is', function ($matches) use (&$shortcodes, &$index) { 530 $placeholder = "<!--RAWHTML_{$index}-->"; 531 $shortcodes[$placeholder] = $matches[0]; 528 532 $index++; 529 533 return $placeholder; 530 534 }, $content); 535 536 return preg_replace_callback('/\[(\[?)([^\[\]]+)(\]?)]/', function ($matches) use (&$shortcodes, &$index) { 537 $full_shortcode = $matches[0]; 538 539 if (preg_match('/^\[\/?(raw|html_block)/i', $full_shortcode)) { 540 return $full_shortcode; 541 } 542 543 $placeholder = "<!--SHORTCODE_{$index}-->"; 544 $shortcodes[$placeholder] = $full_shortcode; 545 $index++; 546 return $placeholder; 547 }, $content); 531 548 } 532 549 533 550 private function restore_shortcodes_from_placeholders($content, $shortcodes) { 534 551 return str_replace(array_keys($shortcodes), array_values($shortcodes), $content); 552 } 553 554 private function replace_inline_tags_with_placeholders($html) { 555 $dom = new ATFP_simple_html_dom(); 556 $dom->load($html); 557 558 $index = 0; 559 $tag_map = []; 560 561 foreach ($dom->nodes as $node) { 562 if (in_array($node->tag, ['a', 'strong', 'em', 'b', 'i', 'u', 'span'])) { 563 $placeholder_start = "[tag{$index}]"; 564 $placeholder_end = "[/tag{$index}]"; 565 566 $tag_map["tag{$index}"] = $node->outertext; 567 $inner = $node->innertext; 568 569 $node->outertext = $placeholder_start . $inner . $placeholder_end; 570 571 $index++; 572 } 573 } 574 575 return [$dom->save(), $tag_map]; 576 } 577 578 private function restore_placeholders_with_tags($text, $tag_map) { 579 foreach ($tag_map as $key => $original_html) { 580 if (preg_match('/^<([a-z0-9]+)([^>]*)>(.*?)<\/\1>$/is', $original_html, $parts)) { 581 $tag_name = $parts[1]; 582 $attrs = $parts[2]; 583 584 $pattern = "/\\[{$key}\\](.*?)\\[\\/{$key}\\]/s"; 585 586 $text = preg_replace_callback($pattern, function($match) use ($tag_name, $attrs) { 587 $translated_inner = $match[1]; 588 return "<{$tag_name}{$attrs}>{$translated_inner}</{$tag_name}>"; 589 }, $text); 590 } 591 } 592 593 return $text; 594 } 595 596 private function replace_br_tags_with_placeholders($html) { 597 $index = 0; 598 return preg_replace_callback('/<br\s*\/?>/i', function() use (&$index) { 599 $placeholder = "<!--BR_{$index}-->"; 600 $index++; 601 return $placeholder; 602 }, $html); 603 } 604 605 private function restore_br_tags_from_placeholders($text) { 606 return preg_replace_callback('/<!--BR_(\d+)-->/', function($matches) { 607 return '<br />'; 608 }, $text); 535 609 } 536 610 -
auto-translator-polylang/tags/1.1.2/readme.txt
r3285501 r3288431 4 4 Requires PHP: 7.4 5 5 Requires at least: 5.8 6 Tested up to: 6. 7.27 Stable tag: 1.1. 16 Tested up to: 6.8.1 7 Stable tag: 1.1.2 8 8 License: GPLv3 9 9 License URI: https://opensource.org/licenses/GPL-3.0 … … 51 51 == Changelog == 52 52 53 = 1.1.2 = 54 - Fix: Fixed inline tags handling 55 - Fix: Fixed line breaks handling 56 - Fix: Fixed shortcodes translation 57 53 58 = 1.1.1 = 54 59 - Fix: add check on array type for count function -
auto-translator-polylang/trunk/auto-translator-for-polylang.php
r3285501 r3288431 5 5 * Plugin Name: Auto Translator for Polylang 6 6 * Description: Plugin to translate your pages and posts working with Polylang 7 * Version: 1.1. 17 * Version: 1.1.2 8 8 * Author: UAPP GROUP 9 9 * Author URI: https://uapp.group/ … … 26 26 define('ATFP_PLUGIN_NAME', trim(dirname(ATFP_PLUGIN_BASENAME), '/')); 27 27 28 const ATFP_VERSION = '1.1. 1';28 const ATFP_VERSION = '1.1.2'; 29 29 30 30 const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates"; -
auto-translator-polylang/trunk/includes/Base/ATFP_Translation.php
r3285501 r3288431 431 431 } 432 432 433 $html = $this->replace_br_tags_with_placeholders($html); 434 433 435 $dom = new ATFP_simple_html_dom(); 434 $dom->load( wp_kses_post($html));435 436 foreach ($dom->find(' text') as $textNode) {437 $trimmedInnerText = trim($textNode->innertext);438 439 if (!empty($trimmedInnerText) && $trimmedInnerText !== '</p>') { 440 $translated _text = $this->translate_text($trimmedInnerText, $target_language);441 442 if ($translated _text) {443 $ newNode = $dom->createTextNode($translated_text);444 $ textNode->innertext = $newNode->innertext;436 $dom->load($html); 437 438 foreach ($dom->find('p, div, span, li') as $block) { 439 if (!empty(trim($block->innertext))) { 440 list($text_with_placeholders, $tag_map) = $this->replace_inline_tags_with_placeholders($block->innertext); 441 442 $translated = $this->translate_text($text_with_placeholders, $target_language); 443 444 if ($translated) { 445 $restored = $this->restore_placeholders_with_tags($translated, $tag_map); 446 $block->innertext = $restored; 445 447 } 446 448 } … … 448 450 449 451 $translated_html = $dom->save(); 452 453 $translated_html = $this->restore_br_tags_from_placeholders($translated_html); 450 454 451 455 if (!$translate_shortcodes) { … … 456 460 } 457 461 458 private function translate_text ($text, $targetLanguage) {459 $api_key = get_option('atfp_api_key');460 $url = 'https://translation.googleapis.com/language/translate/v2';461 $query_params = "?q=" . esc_html(urlencode($text)) . "&target=" . esc_html($targetLanguage) . "&format=html&key=" . esc_html($api_key) . " ";462 private function translate_text($text, $targetLanguage) { 463 $api_key = get_option('atfp_api_key'); 464 $url = 'https://translation.googleapis.com/language/translate/v2'; 465 $query_params = "?q=" . esc_html(urlencode($text)) . "&target=" . esc_html($targetLanguage) . "&format=html&key=" . esc_html($api_key); 462 466 463 467 $response = wp_remote_post($url . $query_params); … … 519 523 } 520 524 521 private function replace_shortcodes_with_placeholders ($content, &$shortcodes) {522 $index = 0;525 private function replace_shortcodes_with_placeholders($content, &$shortcodes) { 526 $index = 0; 523 527 $shortcodes = []; 524 528 525 return preg_replace_callback('/\[(\[?)([^\[\]]+)(\]?)\]/', function ($matches) use (&$shortcodes, &$index) {526 $placeholder = '<!--SHORTCODE_' . $index . '-->';527 $shortcodes[$placeholder] = $matches[0]; // Store original shortcode529 $content = preg_replace_callback('/\[(html_block|raw)](.*?)\[\/\1]/is', function ($matches) use (&$shortcodes, &$index) { 530 $placeholder = "<!--RAWHTML_{$index}-->"; 531 $shortcodes[$placeholder] = $matches[0]; 528 532 $index++; 529 533 return $placeholder; 530 534 }, $content); 535 536 return preg_replace_callback('/\[(\[?)([^\[\]]+)(\]?)]/', function ($matches) use (&$shortcodes, &$index) { 537 $full_shortcode = $matches[0]; 538 539 if (preg_match('/^\[\/?(raw|html_block)/i', $full_shortcode)) { 540 return $full_shortcode; 541 } 542 543 $placeholder = "<!--SHORTCODE_{$index}-->"; 544 $shortcodes[$placeholder] = $full_shortcode; 545 $index++; 546 return $placeholder; 547 }, $content); 531 548 } 532 549 533 550 private function restore_shortcodes_from_placeholders($content, $shortcodes) { 534 551 return str_replace(array_keys($shortcodes), array_values($shortcodes), $content); 552 } 553 554 private function replace_inline_tags_with_placeholders($html) { 555 $dom = new ATFP_simple_html_dom(); 556 $dom->load($html); 557 558 $index = 0; 559 $tag_map = []; 560 561 foreach ($dom->nodes as $node) { 562 if (in_array($node->tag, ['a', 'strong', 'em', 'b', 'i', 'u', 'span'])) { 563 $placeholder_start = "[tag{$index}]"; 564 $placeholder_end = "[/tag{$index}]"; 565 566 $tag_map["tag{$index}"] = $node->outertext; 567 $inner = $node->innertext; 568 569 $node->outertext = $placeholder_start . $inner . $placeholder_end; 570 571 $index++; 572 } 573 } 574 575 return [$dom->save(), $tag_map]; 576 } 577 578 private function restore_placeholders_with_tags($text, $tag_map) { 579 foreach ($tag_map as $key => $original_html) { 580 if (preg_match('/^<([a-z0-9]+)([^>]*)>(.*?)<\/\1>$/is', $original_html, $parts)) { 581 $tag_name = $parts[1]; 582 $attrs = $parts[2]; 583 584 $pattern = "/\\[{$key}\\](.*?)\\[\\/{$key}\\]/s"; 585 586 $text = preg_replace_callback($pattern, function($match) use ($tag_name, $attrs) { 587 $translated_inner = $match[1]; 588 return "<{$tag_name}{$attrs}>{$translated_inner}</{$tag_name}>"; 589 }, $text); 590 } 591 } 592 593 return $text; 594 } 595 596 private function replace_br_tags_with_placeholders($html) { 597 $index = 0; 598 return preg_replace_callback('/<br\s*\/?>/i', function() use (&$index) { 599 $placeholder = "<!--BR_{$index}-->"; 600 $index++; 601 return $placeholder; 602 }, $html); 603 } 604 605 private function restore_br_tags_from_placeholders($text) { 606 return preg_replace_callback('/<!--BR_(\d+)-->/', function($matches) { 607 return '<br />'; 608 }, $text); 535 609 } 536 610 -
auto-translator-polylang/trunk/readme.txt
r3285501 r3288431 4 4 Requires PHP: 7.4 5 5 Requires at least: 5.8 6 Tested up to: 6. 7.27 Stable tag: 1.1. 16 Tested up to: 6.8.1 7 Stable tag: 1.1.2 8 8 License: GPLv3 9 9 License URI: https://opensource.org/licenses/GPL-3.0 … … 51 51 == Changelog == 52 52 53 = 1.1.2 = 54 - Fix: Fixed inline tags handling 55 - Fix: Fixed line breaks handling 56 - Fix: Fixed shortcodes translation 57 53 58 = 1.1.1 = 54 59 - Fix: add check on array type for count function
Note: See TracChangeset
for help on using the changeset viewer.