Plugin Directory

Changeset 3288431


Ignore:
Timestamp:
05/06/2025 12:12:37 PM (10 months ago)
Author:
uapp
Message:

Tagging version 1.1.2

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

Legend:

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

    r3285501 r3288431  
    55 * Plugin Name: Auto Translator for Polylang
    66 * Description: Plugin to translate your pages and posts working with Polylang
    7  * Version: 1.1.1
     7 * Version: 1.1.2
    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.1.1';
     28const ATFP_VERSION = '1.1.2';
    2929
    3030const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates";
  • auto-translator-polylang/tags/1.1.2/includes/Base/ATFP_Translation.php

    r3285501 r3288431  
    431431    }
    432432
     433    $html = $this->replace_br_tags_with_placeholders($html);
     434
    433435    $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;
    445447        }
    446448      }
     
    448450
    449451    $translated_html = $dom->save();
     452
     453    $translated_html = $this->restore_br_tags_from_placeholders($translated_html);
    450454
    451455    if (!$translate_shortcodes) {
     
    456460  }
    457461
    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);
    462466
    463467    $response = wp_remote_post($url . $query_params);
     
    519523  }
    520524
    521   private function replace_shortcodes_with_placeholders ($content, &$shortcodes) {
    522     $index      = 0;
     525  private function replace_shortcodes_with_placeholders($content, &$shortcodes) {
     526    $index = 0;
    523527    $shortcodes = [];
    524528
    525     return preg_replace_callback('/\[(\[?)([^\[\]]+)(\]?)\]/', function ($matches) use (&$shortcodes, &$index) {
    526       $placeholder              = '<!--SHORTCODE_' . $index . '-->';
    527       $shortcodes[$placeholder] = $matches[0]; // Store original shortcode
     529    $content = preg_replace_callback('/\[(html_block|raw)](.*?)\[\/\1]/is', function ($matches) use (&$shortcodes, &$index) {
     530      $placeholder = "<!--RAWHTML_{$index}-->";
     531      $shortcodes[$placeholder] = $matches[0];
    528532      $index++;
    529533      return $placeholder;
    530534    }, $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);
    531548  }
    532549
    533550  private function restore_shortcodes_from_placeholders($content, $shortcodes) {
    534551    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);
    535609  }
    536610
  • auto-translator-polylang/tags/1.1.2/readme.txt

    r3285501 r3288431  
    44Requires PHP: 7.4
    55Requires at least: 5.8
    6 Tested up to: 6.7.2
    7 Stable tag: 1.1.1
     6Tested up to: 6.8.1
     7Stable tag: 1.1.2
    88License: GPLv3
    99License URI: https://opensource.org/licenses/GPL-3.0
     
    5151== Changelog ==
    5252
     53= 1.1.2 =
     54- Fix: Fixed inline tags handling
     55- Fix: Fixed line breaks handling
     56- Fix: Fixed shortcodes translation
     57
    5358= 1.1.1 =
    5459- Fix: add check on array type for count function
  • auto-translator-polylang/trunk/auto-translator-for-polylang.php

    r3285501 r3288431  
    55 * Plugin Name: Auto Translator for Polylang
    66 * Description: Plugin to translate your pages and posts working with Polylang
    7  * Version: 1.1.1
     7 * Version: 1.1.2
    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.1.1';
     28const ATFP_VERSION = '1.1.2';
    2929
    3030const ATFP_TEMPLATES = ATFP_PLUGIN_DIR . "/templates";
  • auto-translator-polylang/trunk/includes/Base/ATFP_Translation.php

    r3285501 r3288431  
    431431    }
    432432
     433    $html = $this->replace_br_tags_with_placeholders($html);
     434
    433435    $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;
    445447        }
    446448      }
     
    448450
    449451    $translated_html = $dom->save();
     452
     453    $translated_html = $this->restore_br_tags_from_placeholders($translated_html);
    450454
    451455    if (!$translate_shortcodes) {
     
    456460  }
    457461
    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);
    462466
    463467    $response = wp_remote_post($url . $query_params);
     
    519523  }
    520524
    521   private function replace_shortcodes_with_placeholders ($content, &$shortcodes) {
    522     $index      = 0;
     525  private function replace_shortcodes_with_placeholders($content, &$shortcodes) {
     526    $index = 0;
    523527    $shortcodes = [];
    524528
    525     return preg_replace_callback('/\[(\[?)([^\[\]]+)(\]?)\]/', function ($matches) use (&$shortcodes, &$index) {
    526       $placeholder              = '<!--SHORTCODE_' . $index . '-->';
    527       $shortcodes[$placeholder] = $matches[0]; // Store original shortcode
     529    $content = preg_replace_callback('/\[(html_block|raw)](.*?)\[\/\1]/is', function ($matches) use (&$shortcodes, &$index) {
     530      $placeholder = "<!--RAWHTML_{$index}-->";
     531      $shortcodes[$placeholder] = $matches[0];
    528532      $index++;
    529533      return $placeholder;
    530534    }, $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);
    531548  }
    532549
    533550  private function restore_shortcodes_from_placeholders($content, $shortcodes) {
    534551    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);
    535609  }
    536610
  • auto-translator-polylang/trunk/readme.txt

    r3285501 r3288431  
    44Requires PHP: 7.4
    55Requires at least: 5.8
    6 Tested up to: 6.7.2
    7 Stable tag: 1.1.1
     6Tested up to: 6.8.1
     7Stable tag: 1.1.2
    88License: GPLv3
    99License URI: https://opensource.org/licenses/GPL-3.0
     
    5151== Changelog ==
    5252
     53= 1.1.2 =
     54- Fix: Fixed inline tags handling
     55- Fix: Fixed line breaks handling
     56- Fix: Fixed shortcodes translation
     57
    5358= 1.1.1 =
    5459- Fix: add check on array type for count function
Note: See TracChangeset for help on using the changeset viewer.