Changeset 3408169
- Timestamp:
- 12/02/2025 03:06:47 PM (3 months ago)
- Location:
- auto-translator-polylang
- Files:
-
- 3 edited
- 5 copied
-
tags/1.2.1 (copied) (copied from auto-translator-polylang/trunk)
-
tags/1.2.1/assets/js/tableFilters.min.js (copied) (copied from auto-translator-polylang/trunk/assets/js/tableFilters.min.js)
-
tags/1.2.1/auto-translator-for-polylang.php (copied) (copied from auto-translator-polylang/trunk/auto-translator-for-polylang.php) (2 diffs)
-
tags/1.2.1/includes/Base/ATFP_Translation.php (copied) (copied from auto-translator-polylang/trunk/includes/Base/ATFP_Translation.php) (6 diffs)
-
tags/1.2.1/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) (6 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
auto-translator-polylang/tags/1.2.1/auto-translator-for-polylang.php
r3300741 r3408169 5 5 * Plugin Name: Auto Translator for Polylang 6 6 * Description: Plugin to translate your pages and posts working with Polylang 7 * Version: 1.2. 07 * Version: 1.2.1 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.2. 0';28 const ATFP_VERSION = '1.2.1'; 29 29 30 30 const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates"; -
auto-translator-polylang/tags/1.2.1/includes/Base/ATFP_Translation.php
r3300741 r3408169 494 494 495 495 $translate_shortcodes = get_option('atfp_translate_shortcodes') === 'on'; 496 497 $shortcodes = []; 496 $shortcodes = []; 498 497 499 498 if (!$translate_shortcodes) { … … 504 503 505 504 $dom = new ATFP_simple_html_dom(); 506 $dom->load($html); 507 508 foreach ($dom->find('p, div, span, li') as $block) { 509 if (!empty(trim($block->innertext))) { 510 [$text_with_placeholders, $tag_map] = $this->replace_inline_tags_with_placeholders($block->innertext); 511 512 $translated = $this->translate_text($text_with_placeholders, $target_language); 513 514 if ($translated) { 515 $restored = $this->restore_placeholders_with_tags($translated, $tag_map); 516 $block->innertext = $restored; 517 } 505 $dom->load($html, true, false); 506 507 foreach ($dom->find('*') as $node) { 508 509 if (in_array($node->tag, ['script', 'style', 'img'])) { 510 continue; 511 } 512 513 $text = trim($node->innertext); 514 if ($text === '') { 515 continue; 516 } 517 518 if (preg_match('/^<!--.*-->$/', $text)) { 519 continue; 520 } 521 522 [$text_with_placeholders, $tag_map] = $this->replace_inline_tags_with_placeholders($node->innertext); 523 524 $translated_text = $this->translate_text($text_with_placeholders, $target_language); 525 526 if ($translated_text) { 527 $restored = $this->restore_placeholders_with_tags($translated_text, $tag_map); 528 $node->innertext = $restored; 518 529 } 519 530 } … … 531 542 532 543 private function translate_text ($text, $targetLanguage) { 533 $api_key = get_option('atfp_api_key'); 534 $url = 'https://translation.googleapis.com/language/translate/v2'; 535 $query_params = "?q=" . esc_html(urlencode($text)) . "&target=" . esc_html($targetLanguage) . "&format=html&key=" . esc_html($api_key); 536 537 $response = wp_remote_post($url . $query_params); 544 $api_key = get_option('atfp_api_key'); 545 $url = 'https://translation.googleapis.com/language/translate/v2'; 546 547 $args = [ 548 'body' => [ 549 'q' => $text, 550 'target' => $targetLanguage, 551 'format' => 'html', 552 'key' => $api_key, 553 ], 554 ]; 555 556 $response = wp_remote_post($url, $args); 538 557 539 558 if (is_wp_error($response)) { … … 545 564 } 546 565 547 $decoded = json_decode( $response['body']);566 $decoded = json_decode(wp_remote_retrieve_body($response)); 548 567 549 568 if (isset($decoded->data->translations) && is_array($decoded->data->translations)) { 550 569 return $decoded->data->translations[0]->translatedText; 551 } else {570 } elseif (isset($decoded->error->message)) { 552 571 add_option('atfp_response_error', sprintf( 553 572 esc_html__('%s', 'auto-translator-polylang'), … … 624 643 private function replace_inline_tags_with_placeholders ($html) { 625 644 $dom = new ATFP_simple_html_dom(); 626 $dom->load($html); 627 628 $index = 0; 629 $tag_map = []; 645 $dom->load($html, true, false); 646 647 $index = 0; 648 $tag_map = []; 649 $inline_tags = ['a', 'strong', 'em', 'b', 'i', 'u', 'span', 'code']; 630 650 631 651 foreach ($dom->nodes as $node) { 632 if (in_array($node->tag, ['a', 'strong', 'em', 'b', 'i', 'u', 'span'])) { 633 $placeholder_start = "[tag{$index}]"; 634 $placeholder_end = "[/tag{$index}]"; 635 636 $tag_map["tag{$index}"] = $node->outertext; 637 $inner = $node->innertext; 638 639 $node->outertext = $placeholder_start . $inner . $placeholder_end; 652 if (isset($node->tag) && in_array($node->tag, $inline_tags)) { 653 $placeholder_start = "[ATFP_TAG_{$index}]"; 654 $placeholder_end = "[/ATFP_TAG_{$index}]"; 655 656 $tag_map["ATFP_TAG_{$index}"] = $node->outertext; 657 658 $node->outertext = $placeholder_start . $node->innertext . $placeholder_end; 640 659 641 660 $index++; … … 648 667 private function restore_placeholders_with_tags ($text, $tag_map) { 649 668 foreach ($tag_map as $key => $original_html) { 669 670 // If original_html looks like a normal opening/closing tag, extract parts 650 671 if (preg_match('/^<([a-z0-9]+)([^>]*)>(.*?)<\/\1>$/is', $original_html, $parts)) { 651 672 $tag_name = $parts[1]; 652 $attrs = $parts[2]; 653 654 $pattern = "/\\[{$key}\\](.*?)\\[\\/{$key}\\]/s"; 655 656 $text = preg_replace_callback($pattern, function ($match) use ($tag_name, $attrs) { 657 $translated_inner = $match[1]; 658 return "<{$tag_name}{$attrs}>{$translated_inner}</{$tag_name}>"; 659 }, $text); 660 } 673 $attrs = $parts[2]; // includes leading space if present 674 // Build two safe patterns: 675 // 1) wrapper pattern: [<key>]...[/<key>] (used when replace_inline created start/end placeholders) 676 $wrapper_pattern = '/' . preg_quote('[' . $key . ']', '/') . '(.*?)' . preg_quote('[/' . $key . ']', '/') . '/s'; 677 678 // If wrapper placeholders exist in the translated text — replace preserving translated inner content 679 if (preg_match($wrapper_pattern, $text)) { 680 $text = preg_replace_callback($wrapper_pattern, function ($match) use ($tag_name, $attrs) { 681 $translated_inner = $match[1]; 682 return "<{$tag_name}{$attrs}>{$translated_inner}</{$tag_name}>"; 683 }, $text); 684 685 // go to next placeholder 686 continue; 687 } 688 689 // 2) token pattern: plain token (e.g. __ATFP_TAG_0__) 690 $token_pattern = '/' . preg_quote($key, '/') . '/'; 691 692 if (preg_match($token_pattern, $text)) { 693 $text = preg_replace($token_pattern, $original_html, $text); 694 continue; 695 } 696 697 // No placeholder found — nothing to do for this entry 698 continue; 699 } 700 701 // If original_html is not a standard tag (rare), fall back to str_replace 702 $text = str_replace($key, $original_html, $text); 661 703 } 662 704 -
auto-translator-polylang/tags/1.2.1/readme.txt
r3300741 r3408169 4 4 Requires PHP: 7.4 5 5 Requires at least: 5.8 6 Tested up to: 6.8. 17 Stable tag: 1.2. 06 Tested up to: 6.8.3 7 Stable tag: 1.2.1 8 8 License: GPLv3 9 9 License URI: https://opensource.org/licenses/GPL-3.0 … … 51 51 == Changelog == 52 52 53 = 1.2.1 = 54 - Improvement: improved translation logic 55 53 56 = 1.2.0 = 54 57 - Feature: Add translation support for All in One SEO Pack plugin -
auto-translator-polylang/trunk/auto-translator-for-polylang.php
r3300741 r3408169 5 5 * Plugin Name: Auto Translator for Polylang 6 6 * Description: Plugin to translate your pages and posts working with Polylang 7 * Version: 1.2. 07 * Version: 1.2.1 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.2. 0';28 const ATFP_VERSION = '1.2.1'; 29 29 30 30 const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates"; -
auto-translator-polylang/trunk/includes/Base/ATFP_Translation.php
r3300741 r3408169 494 494 495 495 $translate_shortcodes = get_option('atfp_translate_shortcodes') === 'on'; 496 497 $shortcodes = []; 496 $shortcodes = []; 498 497 499 498 if (!$translate_shortcodes) { … … 504 503 505 504 $dom = new ATFP_simple_html_dom(); 506 $dom->load($html); 507 508 foreach ($dom->find('p, div, span, li') as $block) { 509 if (!empty(trim($block->innertext))) { 510 [$text_with_placeholders, $tag_map] = $this->replace_inline_tags_with_placeholders($block->innertext); 511 512 $translated = $this->translate_text($text_with_placeholders, $target_language); 513 514 if ($translated) { 515 $restored = $this->restore_placeholders_with_tags($translated, $tag_map); 516 $block->innertext = $restored; 517 } 505 $dom->load($html, true, false); 506 507 foreach ($dom->find('*') as $node) { 508 509 if (in_array($node->tag, ['script', 'style', 'img'])) { 510 continue; 511 } 512 513 $text = trim($node->innertext); 514 if ($text === '') { 515 continue; 516 } 517 518 if (preg_match('/^<!--.*-->$/', $text)) { 519 continue; 520 } 521 522 [$text_with_placeholders, $tag_map] = $this->replace_inline_tags_with_placeholders($node->innertext); 523 524 $translated_text = $this->translate_text($text_with_placeholders, $target_language); 525 526 if ($translated_text) { 527 $restored = $this->restore_placeholders_with_tags($translated_text, $tag_map); 528 $node->innertext = $restored; 518 529 } 519 530 } … … 531 542 532 543 private function translate_text ($text, $targetLanguage) { 533 $api_key = get_option('atfp_api_key'); 534 $url = 'https://translation.googleapis.com/language/translate/v2'; 535 $query_params = "?q=" . esc_html(urlencode($text)) . "&target=" . esc_html($targetLanguage) . "&format=html&key=" . esc_html($api_key); 536 537 $response = wp_remote_post($url . $query_params); 544 $api_key = get_option('atfp_api_key'); 545 $url = 'https://translation.googleapis.com/language/translate/v2'; 546 547 $args = [ 548 'body' => [ 549 'q' => $text, 550 'target' => $targetLanguage, 551 'format' => 'html', 552 'key' => $api_key, 553 ], 554 ]; 555 556 $response = wp_remote_post($url, $args); 538 557 539 558 if (is_wp_error($response)) { … … 545 564 } 546 565 547 $decoded = json_decode( $response['body']);566 $decoded = json_decode(wp_remote_retrieve_body($response)); 548 567 549 568 if (isset($decoded->data->translations) && is_array($decoded->data->translations)) { 550 569 return $decoded->data->translations[0]->translatedText; 551 } else {570 } elseif (isset($decoded->error->message)) { 552 571 add_option('atfp_response_error', sprintf( 553 572 esc_html__('%s', 'auto-translator-polylang'), … … 624 643 private function replace_inline_tags_with_placeholders ($html) { 625 644 $dom = new ATFP_simple_html_dom(); 626 $dom->load($html); 627 628 $index = 0; 629 $tag_map = []; 645 $dom->load($html, true, false); 646 647 $index = 0; 648 $tag_map = []; 649 $inline_tags = ['a', 'strong', 'em', 'b', 'i', 'u', 'span', 'code']; 630 650 631 651 foreach ($dom->nodes as $node) { 632 if (in_array($node->tag, ['a', 'strong', 'em', 'b', 'i', 'u', 'span'])) { 633 $placeholder_start = "[tag{$index}]"; 634 $placeholder_end = "[/tag{$index}]"; 635 636 $tag_map["tag{$index}"] = $node->outertext; 637 $inner = $node->innertext; 638 639 $node->outertext = $placeholder_start . $inner . $placeholder_end; 652 if (isset($node->tag) && in_array($node->tag, $inline_tags)) { 653 $placeholder_start = "[ATFP_TAG_{$index}]"; 654 $placeholder_end = "[/ATFP_TAG_{$index}]"; 655 656 $tag_map["ATFP_TAG_{$index}"] = $node->outertext; 657 658 $node->outertext = $placeholder_start . $node->innertext . $placeholder_end; 640 659 641 660 $index++; … … 648 667 private function restore_placeholders_with_tags ($text, $tag_map) { 649 668 foreach ($tag_map as $key => $original_html) { 669 670 // If original_html looks like a normal opening/closing tag, extract parts 650 671 if (preg_match('/^<([a-z0-9]+)([^>]*)>(.*?)<\/\1>$/is', $original_html, $parts)) { 651 672 $tag_name = $parts[1]; 652 $attrs = $parts[2]; 653 654 $pattern = "/\\[{$key}\\](.*?)\\[\\/{$key}\\]/s"; 655 656 $text = preg_replace_callback($pattern, function ($match) use ($tag_name, $attrs) { 657 $translated_inner = $match[1]; 658 return "<{$tag_name}{$attrs}>{$translated_inner}</{$tag_name}>"; 659 }, $text); 660 } 673 $attrs = $parts[2]; // includes leading space if present 674 // Build two safe patterns: 675 // 1) wrapper pattern: [<key>]...[/<key>] (used when replace_inline created start/end placeholders) 676 $wrapper_pattern = '/' . preg_quote('[' . $key . ']', '/') . '(.*?)' . preg_quote('[/' . $key . ']', '/') . '/s'; 677 678 // If wrapper placeholders exist in the translated text — replace preserving translated inner content 679 if (preg_match($wrapper_pattern, $text)) { 680 $text = preg_replace_callback($wrapper_pattern, function ($match) use ($tag_name, $attrs) { 681 $translated_inner = $match[1]; 682 return "<{$tag_name}{$attrs}>{$translated_inner}</{$tag_name}>"; 683 }, $text); 684 685 // go to next placeholder 686 continue; 687 } 688 689 // 2) token pattern: plain token (e.g. __ATFP_TAG_0__) 690 $token_pattern = '/' . preg_quote($key, '/') . '/'; 691 692 if (preg_match($token_pattern, $text)) { 693 $text = preg_replace($token_pattern, $original_html, $text); 694 continue; 695 } 696 697 // No placeholder found — nothing to do for this entry 698 continue; 699 } 700 701 // If original_html is not a standard tag (rare), fall back to str_replace 702 $text = str_replace($key, $original_html, $text); 661 703 } 662 704 -
auto-translator-polylang/trunk/readme.txt
r3300741 r3408169 4 4 Requires PHP: 7.4 5 5 Requires at least: 5.8 6 Tested up to: 6.8. 17 Stable tag: 1.2. 06 Tested up to: 6.8.3 7 Stable tag: 1.2.1 8 8 License: GPLv3 9 9 License URI: https://opensource.org/licenses/GPL-3.0 … … 51 51 == Changelog == 52 52 53 = 1.2.1 = 54 - Improvement: improved translation logic 55 53 56 = 1.2.0 = 54 57 - Feature: Add translation support for All in One SEO Pack plugin
Note: See TracChangeset
for help on using the changeset viewer.