Plugin Directory

Changeset 3152996


Ignore:
Timestamp:
09/17/2024 03:56:12 AM (19 months ago)
Author:
mvpis
Message:

Updating to better handle shortcodes

Location:
fluentc-translation
Files:
2753 added
3 edited

Legend:

Unmodified
Added
Removed
  • fluentc-translation/trunk/fluentc_wordpress_plugin.php

    r3152297 r3152996  
    77 * Plugin URI: https://github.com/fluentc/wordpress-plugin
    88 * Description: A plugin that enables website owners to easily install the FluentC Translation on their WordPress site.
    9  * Version: 1.8.3
     9 * Version: 1.8.4
    1010 * Author: FluentC
    1111 * Author URI: https://www.fluentc.ai
     
    1717define( 'FLUENTC_DIR', __DIR__ );
    1818define( 'FLUENTC_SLUG', 'fluentc_translation' );
    19 define( 'FLUENTC_TRANSLATION_VERSION', "1.8.3" );
     19define( 'FLUENTC_TRANSLATION_VERSION', "1.8.4" );
    2020define( 'FLUENTC_TRANSLATION_PLUGIN_DIR', plugin_dir_path(__FILE__) );
    2121define( 'FLUENTC_TRANSLATION_PLUGIN_URL', plugin_dir_url(__FILE__) );
  • fluentc-translation/trunk/readme.txt

    r3152297 r3152996  
    55Requires at least: 4.6
    66Tested up to: 6.6.2
    7 Stable tag: 1.8.3
     7Stable tag: 1.8.4
    88Requires PHP: 7.3
    99License: GPLv2 or later
  • fluentc-translation/trunk/src/actions/class-wordpress.php

    r3152297 r3152996  
    523523    }
    524524
    525     /**
    526      * Updates Home page link
    527      *
    528      * @param  mixed $content HTML Content.
    529      */
    530     public function fluentcFilterContent( $content ) {
    531         // if Language is set, translate description if not return description.
    532         $language_code = $this->fluentc_language->get_fluentc_language();
    533         $widgetapikey  = get_option( 'fluentc_api_key' );
    534        
    535         if ( function_exists( 'wc_get_page_id' ) ) {
    536             if ( is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'cart' ) ) ) {
    537                 return $content;
    538             }
    539         }
    540         if ( $language_code ) {
    541             // If language code is set, modify the output accordingly.
    542             $text = $this->fluentc_connenct->get_translation_content( $widgetapikey, $this->site_language, $language_code, $content );
    543             return $this->fluentc_html->find_and_replace( html_entity_decode( $content ), $text, $language_code, $this->fluentc_connenct->get_language_list_string( $widgetapikey ) );
    544         } else {
    545             // If no language code, return the default output.
    546             return $content;
    547         }
    548     }
     525   
    549526
    550527    /**
     
    660637
    661638    /**
    662      * Updates cart link
    663      *
    664      * @param  mixed $content HTML Content.
    665      */
    666     public function fluentc_woocommerce_cart_shortcode_filter( $content ) {
    667         // if Language is set, translate description if not return description.
    668         $language_code = $this->fluentc_language->get_fluentc_language();
    669         $widgetapikey  = get_option( 'fluentc_api_key' );
    670        
    671         if ( $language_code ) {
    672             // If language code is set, modify the output accordingly.
    673             $text = $this->fluentc_connenct->get_translation_content( $widgetapikey, $this->site_language, $language_code, $content );
    674             return $this->fluentc_html->find_and_replace( $content, $text, $language_code, $this->fluentc_connenct->get_language_list_string( $widgetapikey ) );
    675         } else {
    676             // If no language code, return the default output.
    677             return $content;
    678         }
    679     }
    680 
    681     /**
    682639     * Updates checkout link
    683640     *
     
    688645    public function fluentc_woocommerce_checkout_shortcode_filter( $output, $tag, $attr ) {
    689646        // if Language is set, translate description if not return description.
    690         $language_code = $this->fluentc_language->get_fluentc_language();
    691         $widgetapikey  = get_option( 'fluentc_api_key' );
    692        
    693         if ( $language_code ) {
    694             // If language code is set, modify the output accordingly.
    695             $text = $this->fluentc_connenct->get_translation_content( $widgetapikey, $this->site_language, $language_code, $output );
    696             return $this->fluentc_html->find_and_replace( $output, $text, $language_code, $this->fluentc_connenct->get_language_list_string( $widgetapikey ) );
    697         } else {
    698             // If no language code, return the default output.
     647        $language_code = $this->language_code;
     648    if (!$language_code || is_null($output) || $output === '') {
     649        return $output;
     650    }
     651    if ( function_exists( 'wc_get_page_id' ) ) {
     652        if ( is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'cart' ) ) ) {
    699653            return $output;
    700654        }
    701655    }
    702 
    703     /**
    704      * Updates myaccount link
    705      *
    706      * @param  mixed $content HTML Content.
    707      */
    708     public function fluentc_woocommerce_my_account_shortcode_filter( $content ) {
    709         // if Language is set, translate description if not return description.
    710         $language_code = $this->fluentc_language->get_fluentc_language();
    711         $widgetapikey  = get_option( 'fluentc_api_key' );
    712        
    713         if ( $language_code ) {
    714             // If language code is set, modify the output accordingly.
    715             $text = $this->fluentc_connenct->get_translation_content( $widgetapikey, $this->site_language, $language_code, $content );
    716             return $this->fluentc_html->find_and_replace( $content, $text, $language_code, $this->fluentc_connenct->get_language_list_string( $widgetapikey ) );
    717         } else {
    718             // If no language code, return the default output.
    719             return $content;
    720         }
    721     }
    722 
    723     /**
    724      * Updates product
    725      *
    726      * @param  mixed $content HTML Content.
    727      */
    728     public function fluentc_woocommerce_product_shortcode_filter( $content ) {
    729         // if Language is set, translate description if not return description.
    730         $language_code = $this->fluentc_language->get_fluentc_language();
    731         $widgetapikey  = get_option( 'fluentc_api_key' );
    732        
    733         if ( $language_code ) {
    734             // If language code is set, modify the output accordingly.
    735             $text = $this->fluentc_connenct->get_translation_content( $widgetapikey, $this->site_language, $language_code, $content );
    736             return $this->fluentc_html->find_and_replace( $content, $text, $language_code, $this->fluentc_connenct->get_language_list_string( $widgetapikey ) );
    737         } else {
    738             // If no language code, return the default output.
    739             return $content;
    740         }
     656    $html = html_entity_decode($output, ENT_QUOTES | ENT_HTML5, 'UTF-8');
     657
     658    if (empty($html)) {
     659        return $output;
     660    }
     661
     662    $options = new Options();
     663    $options->setCleanupInput(false);
     664    $options->setWhitespaceTextNode(false);
     665
     666    $dom = new Dom;
     667    $dom->loadStr($html, $options);
     668
     669    $texts_to_translate = [];
     670    $entry_map = [];
     671    $entry_skip_map = [];
     672
     673    $elements = $dom->find(selector: '*:not(script):not(style):not(code):not(doctype):not(figure):not(pre):not(noscript):not(iframe):not(object):not(embed):not(svg):not(math):not(canvas)');
     674    foreach ($elements as $element) {
     675        $nodes = ($element instanceof \PHPHtmlParser\Dom\Node\HtmlNode) ? $element->getChildren() : [$element];
     676        foreach ($nodes as $node) {
     677            $this->processNode($node, $texts_to_translate, $entry_map, $entry_skip_map);
     678        }
     679    }
     680
     681    if (!empty($texts_to_translate)) {
     682        $uncached_texts = [];
     683        $cached_translations = [];
     684
     685        foreach ($texts_to_translate as $text) {
     686            $key = hash('md5', $text);
     687            $cache_key = $this->site_language . $language_code . $key;
     688            $cached_translation = $this->fluentc_cache->get($cache_key);
     689
     690            if ($cached_translation) {
     691                $json_cache = json_decode($cached_translation);
     692                if (isset($json_cache->data->translateSite->body)) {
     693                    $cached_translations[] = $json_cache->data->translateSite->body;
     694                }
     695            } else {
     696                $uncached_texts[] = $text;
     697            }
     698        }
     699
     700        if (!empty($uncached_texts)) {
     701            $text_labels = $this->process_text_labels($uncached_texts);
     702
     703            $translated_texts = $this->fluentc_connenct->get_translation_content(
     704                $this->widgetapikey,
     705                $this->site_language,
     706                $language_code,
     707                $text_labels
     708            );
     709
     710            if (isset($translated_texts->data->translateSite->body) && !empty($translated_texts->data->translateSite->body)) {
     711                $cached_translations = array_merge($cached_translations, $translated_texts->data->translateSite->body);
     712            } else {
     713                do_action('qm/debug', 'Translation error: Data not found in response.');
     714            }
     715        }
     716
     717        $this->applyTranslations($entry_map, $cached_translations, $language_code);
     718    } else {
     719       // do_action('qm/info', 'No translation needed.');
     720    }
     721
     722    $this->applySkippedTranslations($entry_skip_map);
     723
     724    $root = $dom->root;
     725    if ($root && method_exists($root, 'innerHtml')) {
     726        try {
     727            $html = $root->innerHtml();
     728            if ($html !== null && $html !== '') {
     729                // Additional check to ensure $html is a valid string
     730                if (is_string($html) && strlen(trim($html)) > 0) {
     731                    $html = $this->fluentc_html->find_and_replace($html, null, $language_code, $this->fluentc_connenct->get_language_list_string($this->widgetapikey));
     732                    return $html;
     733                }
     734            }
     735        } catch (\Exception $e) {
     736            // Log the error for debugging
     737            error_log('Error in filter_block: ' . $e->getMessage());
     738        }
     739    }
     740
     741// If we reach here, it means there was no valid innerHTML
     742return $output;
    741743    }
    742744
     
    777779        }
    778780   
    779         $wp_locale = get_locale();
    780     $wp_language = substr($wp_locale, 0, 2);
     781   
     782    $wp_language = $this->fluentc_language->get_fluentc_language();
    781783
    782784    // Get the user's language
Note: See TracChangeset for help on using the changeset viewer.