Plugin Directory

Changeset 3408169


Ignore:
Timestamp:
12/02/2025 03:06:47 PM (3 months ago)
Author:
uapp
Message:

Tagging version 1.2.1

Location:
auto-translator-polylang
Files:
3 edited
5 copied

Legend:

Unmodified
Added
Removed
  • auto-translator-polylang/tags/1.2.1/auto-translator-for-polylang.php

    r3300741 r3408169  
    55 * Plugin Name: Auto Translator for Polylang
    66 * Description: Plugin to translate your pages and posts working with Polylang
    7  * Version: 1.2.0
     7 * Version: 1.2.1
    88 * Author: UAPP GROUP
    99 * Author URI: https://uapp.group/
     
    2626define('ATFP_PLUGIN_NAME', trim(dirname(ATFP_PLUGIN_BASENAME), '/'));
    2727
    28 const ATFP_VERSION = '1.2.0';
     28const ATFP_VERSION = '1.2.1';
    2929
    3030const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates";
  • auto-translator-polylang/tags/1.2.1/includes/Base/ATFP_Translation.php

    r3300741 r3408169  
    494494
    495495    $translate_shortcodes = get_option('atfp_translate_shortcodes') === 'on';
    496 
    497     $shortcodes = [];
     496    $shortcodes           = [];
    498497
    499498    if (!$translate_shortcodes) {
     
    504503
    505504    $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;
    518529      }
    519530    }
     
    531542
    532543  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);
    538557
    539558    if (is_wp_error($response)) {
     
    545564    }
    546565
    547     $decoded = json_decode($response['body']);
     566    $decoded = json_decode(wp_remote_retrieve_body($response));
    548567
    549568    if (isset($decoded->data->translations) && is_array($decoded->data->translations)) {
    550569      return $decoded->data->translations[0]->translatedText;
    551     } else {
     570    } elseif (isset($decoded->error->message)) {
    552571      add_option('atfp_response_error', sprintf(
    553572        esc_html__('%s', 'auto-translator-polylang'),
     
    624643  private function replace_inline_tags_with_placeholders ($html) {
    625644    $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'];
    630650
    631651    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;
    640659
    641660        $index++;
     
    648667  private function restore_placeholders_with_tags ($text, $tag_map) {
    649668    foreach ($tag_map as $key => $original_html) {
     669
     670      // If original_html looks like a normal opening/closing tag, extract parts
    650671      if (preg_match('/^<([a-z0-9]+)([^>]*)>(.*?)<\/\1>$/is', $original_html, $parts)) {
    651672        $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);
    661703    }
    662704
  • auto-translator-polylang/tags/1.2.1/readme.txt

    r3300741 r3408169  
    44Requires PHP: 7.4
    55Requires at least: 5.8
    6 Tested up to: 6.8.1
    7 Stable tag: 1.2.0
     6Tested up to: 6.8.3
     7Stable tag: 1.2.1
    88License: GPLv3
    99License URI: https://opensource.org/licenses/GPL-3.0
     
    5151== Changelog ==
    5252
     53= 1.2.1 =
     54- Improvement: improved translation logic
     55
    5356= 1.2.0 =
    5457- Feature: Add translation support for All in One SEO Pack plugin
  • auto-translator-polylang/trunk/auto-translator-for-polylang.php

    r3300741 r3408169  
    55 * Plugin Name: Auto Translator for Polylang
    66 * Description: Plugin to translate your pages and posts working with Polylang
    7  * Version: 1.2.0
     7 * Version: 1.2.1
    88 * Author: UAPP GROUP
    99 * Author URI: https://uapp.group/
     
    2626define('ATFP_PLUGIN_NAME', trim(dirname(ATFP_PLUGIN_BASENAME), '/'));
    2727
    28 const ATFP_VERSION = '1.2.0';
     28const ATFP_VERSION = '1.2.1';
    2929
    3030const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates";
  • auto-translator-polylang/trunk/includes/Base/ATFP_Translation.php

    r3300741 r3408169  
    494494
    495495    $translate_shortcodes = get_option('atfp_translate_shortcodes') === 'on';
    496 
    497     $shortcodes = [];
     496    $shortcodes           = [];
    498497
    499498    if (!$translate_shortcodes) {
     
    504503
    505504    $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;
    518529      }
    519530    }
     
    531542
    532543  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);
    538557
    539558    if (is_wp_error($response)) {
     
    545564    }
    546565
    547     $decoded = json_decode($response['body']);
     566    $decoded = json_decode(wp_remote_retrieve_body($response));
    548567
    549568    if (isset($decoded->data->translations) && is_array($decoded->data->translations)) {
    550569      return $decoded->data->translations[0]->translatedText;
    551     } else {
     570    } elseif (isset($decoded->error->message)) {
    552571      add_option('atfp_response_error', sprintf(
    553572        esc_html__('%s', 'auto-translator-polylang'),
     
    624643  private function replace_inline_tags_with_placeholders ($html) {
    625644    $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'];
    630650
    631651    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;
    640659
    641660        $index++;
     
    648667  private function restore_placeholders_with_tags ($text, $tag_map) {
    649668    foreach ($tag_map as $key => $original_html) {
     669
     670      // If original_html looks like a normal opening/closing tag, extract parts
    650671      if (preg_match('/^<([a-z0-9]+)([^>]*)>(.*?)<\/\1>$/is', $original_html, $parts)) {
    651672        $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);
    661703    }
    662704
  • auto-translator-polylang/trunk/readme.txt

    r3300741 r3408169  
    44Requires PHP: 7.4
    55Requires at least: 5.8
    6 Tested up to: 6.8.1
    7 Stable tag: 1.2.0
     6Tested up to: 6.8.3
     7Stable tag: 1.2.1
    88License: GPLv3
    99License URI: https://opensource.org/licenses/GPL-3.0
     
    5151== Changelog ==
    5252
     53= 1.2.1 =
     54- Improvement: improved translation logic
     55
    5356= 1.2.0 =
    5457- Feature: Add translation support for All in One SEO Pack plugin
Note: See TracChangeset for help on using the changeset viewer.