Plugin Directory

Changeset 3381590


Ignore:
Timestamp:
10/21/2025 03:14:19 AM (5 months ago)
Author:
linguise
Message:

Updating to version 2.1.76

Location:
linguise
Files:
14 edited
1 copied

Legend:

Unmodified
Added
Removed
  • linguise/tags/2.1.76/linguise.php

    r3379190 r3381590  
    55 * Plugin URI: https://www.linguise.com/
    66 * Description: Linguise translation plugin
    7  * Version:2.1.75
     7 * Version:2.1.76
    88 * Text Domain: linguise
    99 * Domain Path: /languages
  • linguise/tags/2.1.76/readme.txt

    r3379190 r3381590  
    44Requires at least: 4.0
    55Tested up to: 6.8
    6 Stable tag:2.1.75
     6Stable tag:2.1.76
    77Requires PHP: 7.0
    88License: GPLv2 or later
     
    104104
    105105== Changelog ==
     106= 2.1.76 =
     107- Fix: It does gets translated but then gets back again to default lang
     108
    106109= 2.1.75 =
    107110- Fix: WooCommerce Issue: checkout process error
  • linguise/tags/2.1.76/src/FragmentBase.php

    r3366846 r3381590  
    1616     */
    1717    protected static $default_filters = [
     18        [
     19            'key' => '(state|country)\.label$',
     20            'mode' => 'regex_full',
     21            'kind' => 'allow'
     22        ],
    1823        [
    1924            'key' => 'nonce',
  • linguise/tags/2.1.76/src/FragmentHandler.php

    r3366846 r3381590  
    2121     * @var string
    2222     */
    23     protected static $frag_html_match = '/<(div|a|linguise-main|img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="(link|html|html-main|text|media-img|media-imgset)" data-fragment-mode="(auto|override|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?>(.*?)<\/\1>/si';
     23    protected static $frag_html_match = '/<(div|a|linguise-main|img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="((?:link|html|html-main|text|media-img|media-imgset)(?:-skip)?)" data-fragment-mode="(auto|override|skip|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?>(.*?)<\/\1>/si';
    2424    /**
    2525     * Regex/matcher for our custom HTML fragment
     
    2929     * @var string
    3030     */
    31     protected static $frag_html_match_self_close = '/<(img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="(link|html|html-main|text|media-img|media-imgset)" data-fragment-mode="(auto|override|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?\s*\/?>/si';
     31    protected static $frag_html_match_self_close = '/<(img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="((?:link|html|html-main|text|media-img|media-imgset)(?:-skip)?)" data-fragment-mode="(auto|override|skip|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?\s*\/?>/si';
    3232
    3333    /**
     
    156156                $tag = 'img';
    157157            }
     158            $frag_format = $fragment['format'];
     159            if (isset($fragment['skip']) && $fragment['skip']) {
     160                $frag_format .= '-skip';
     161            }
     162
    158163            $html .= '<' . $tag . ' class="linguise-fragment" data-fragment-name="' . $fragment_name . '" data-fragment-param="' . $fragment_param . '" data-fragment-key="';
    159             $html .= $fragment['key'] . '" data-fragment-format="' . $fragment['format'] . '" data-fragment-mode="' . $json_fragments['mode'] . '"';
     164            $html .= $fragment['key'] . '" data-fragment-format="' . $frag_format . '" data-fragment-mode="' . $json_fragments['mode'] . '"';
    160165            if ($json_fragments['mode'] === 'attribute' && isset($json_fragments['attribute'])) {
    161166                $html .= ' data-fragment-extra-id="' . $json_fragments['attribute'] . '"';
     
    198203            $fragment_value = isset($match[9]) ? $match[9] : '';
    199204
     205            $is_skipped = false;
     206            if (substr($fragment_format, -5) === '-skip') {
     207                $is_skipped = true;
     208                $fragment_format = substr($fragment_format, 0, -5);
     209            }
     210
    200211            if ($fragment_format === 'link' || $fragment_format === 'media-img' || $fragment_format === 'media-imgset') {
    201212                $fragment_value = $match[8];
     
    245256                'format' => $fragment_format,
    246257                'match' => $match[0],
     258                'skip' => $is_skipped, // If `skip` is true, then don't replace this fragment (but remove it)
    247259            ];
    248260        }
     
    320332                $is_strict = true;
    321333            }
     334
    322335            $collected_temp = self::collectFragmentFromJson($json_data, $is_strict);
    323336            if (!empty($collected_temp)) {
     
    408421        }
    409422
     423        // Run filters
     424        $filtered_fragments = $all_fragments;
     425        $filtered_fragments = apply_filters('linguise_after_fragment_collection', $filtered_fragments, $html_data, $html_dom);
     426        if (is_array($filtered_fragments)) {
     427            $all_fragments = $filtered_fragments;
     428        }
     429
    410430        Debug::log('FragmentHandler -> Collected: ' . json_encode($all_fragments, JSON_PRETTY_PRINT));
    411431
     
    454474            $json_data = new JsonObject(json_decode('{' . $match_data . '}', true));
    455475            foreach ($fragment_info['fragments'] as $fragment) {
     476                if (isset($fragment['skip']) && $fragment['skip']) {
     477                    // If skip is true, then don't replace this fragment (but remove it)
     478                    continue;
     479                }
    456480                $decoded_key = self::unwrapKey($fragment['key']);
    457481                try {
     
    484508            $json_data = new JsonObject(json_decode($before_match, true));
    485509            foreach ($fragment_info['fragments'] as $fragment) {
     510                if (isset($fragment['skip']) && $fragment['skip']) {
     511                    // If skip is true, then don't replace this fragment (but remove it)
     512                    continue;
     513                }
    486514                $decoded_key = self::unwrapKey($fragment['key']);
    487515                try {
     
    530558
    531559        foreach ($fragments as $fragment) {
     560            if (isset($fragment['skip']) && $fragment['skip']) {
     561                // If skip is true, then don't replace this fragment (but remove it)
     562                continue;
     563            }
    532564            // remove the html fragment from the translated page
    533565            $decoded_key = self::unwrapKey($fragment['key']);
     
    577609                $mode = $fragment_list['mode'];
    578610
    579                 if (!in_array($mode, ['auto', 'override'])) {
     611                if (!in_array($mode, ['auto', 'override', 'skip'])) {
     612                    continue;
     613                }
     614
     615                if ($mode === 'skip') {
     616                    $html_data = self::cleanupFragments($html_data, $fragment_list['fragments']);
    580617                    continue;
    581618                }
     
    618655        }
    619656
    620         $mod_html_data = apply_filters('linguise_after_fragment_translation', $html_data);
     657        $mod_html_data = apply_filters('linguise_after_fragment_translation', $html_data, $fragments);
    621658        if (!empty($mod_html_data)) {
    622659            $html_data = $mod_html_data;
  • linguise/tags/2.1.76/src/constants.php

    r3379190 r3381590  
    11<?php
    22if (!defined('LINGUISE_SCRIPT_TRANSLATION_VERSION')) {
    3     define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.75');
     3    define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.76');
    44}
    55
    66if (!defined('LINGUISE_VERSION')) {
    7     define('LINGUISE_VERSION', '2.1.75');
     7    define('LINGUISE_VERSION', '2.1.76');
    88}
  • linguise/tags/2.1.76/src/thirdparty/wc/woocommerce.php

    r3379190 r3381590  
    44
    55use Linguise\WordPress\Helper as WPHelper;
     6use Linguise\WordPress\HTMLHelper;
    67
    78defined('ABSPATH') || die('');
     
    6667        'flatsome_ajax_add_to_cart',
    6768        'bootscore_ajax_add_to_cart',
     69    ];
     70
     71    /**
     72     * A collection of HTML attributes that you want to translate
     73     *
     74     * @var string[]
     75     */
     76    protected static $fragment_attributes = [
     77        [
     78            'name' => 'wc-order-button-payment',
     79            'key' => 'data-order_button_text',
     80            'mode' => 'string',
     81            'match_mode' => 'all',
     82            'matchers' => [
     83                [
     84                    'type' => 'tag',
     85                    'key' => 'input'
     86                ],
     87                [
     88                    'type' => 'attribute',
     89                    'key' => 'name',
     90                    'value' => 'payment_method'
     91                ]
     92            ]
     93        ]
    6894    ];
    6995
     
    199225        remove_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1);
    200226
     227        remove_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10);
     228        remove_filter('woocommerce_form_field_state', [$this, 'hookFormFieldsSelectTranslations'], 10);
     229        remove_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10);
     230
    201231        remove_action('wp_loaded', [$this, 'hookAddCartFragments']);
    202232        remove_action('wp_redirect', [$this, 'hookWPRedirect']);
     233
     234        remove_filter('linguise_after_fragment_collection', [$this, 'linguiseFragmentCollectionAdjustment'], 20);
     235        remove_filter('linguise_after_fragment_translation', [$this, 'linguiseFragmentTranslationAdjusment'], 20);
    203236    }
    204237
     
    236269        add_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1);
    237270
     271        add_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10, 1);
     272        add_filter('woocommerce_form_field_state', [$this, 'hookFormFieldsSelectTranslations'], 10, 1);
     273        add_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10, 1);
     274
    238275        add_action('wp_loaded', [$this, 'hookAddCartFragments']);
    239276        add_action('wp_redirect', [$this, 'hookWPRedirect']);
     277
     278        add_filter('linguise_after_fragment_collection', [$this, 'linguiseFragmentCollectionAdjustment'], 20, 3);
     279        add_filter('linguise_after_fragment_translation', [$this, 'linguiseFragmentTranslationAdjusment'], 20, 2);
    240280    }
    241281
     
    520560        }
    521561
     562        if (is_string($html_content)) {
     563            // modify it a bit more
     564            $html_content = $this->hookReviewOrderBeforePayment($html_content);
     565        }
     566
    522567        $html_content .= '</body></html>';
    523568
     
    585630        }
    586631        return str_replace('<button ', '<button data-linguise-translate-attributes="value data-value" ', $html);
     632    }
     633
     634    /**
     635     * Translate the payment method selection input in checkout page
     636     *
     637     * @param string $html The HTML of the payment method selection input
     638     *
     639     * @return string
     640     */
     641    public function hookReviewOrderBeforePayment($html)
     642    {
     643        // 1. Translate the payment method name
     644        $replaced = preg_replace('/(<input[^>]+name=["\']payment_method["\'][^>]+)(>)/i', '$1 data-linguise-translate-attributes="data-order_button_text"$2', $html);
     645        if (!empty($replaced)) {
     646            return $replaced;
     647        }
     648        return $html;
     649    }
     650
     651    /**
     652     * Translate select form fields attributes like data-placeholder, data-label
     653     *
     654     * @param string $field The HTML of the select field
     655     *
     656     * @return string
     657     */
     658    public function hookFormFieldsSelectTranslations($field)
     659    {
     660        $matched_fields = [];
     661        // find data-placeholder="..."
     662        // find data-label="..."
     663        preg_match_all('/data-(placeholder|label)=["\']([^"\']+)["\']/', $field, $matched_fields, PREG_SET_ORDER);
     664        if (empty($matched_fields)) {
     665            return $field;
     666        }
     667
     668        $only_attrs = [];
     669        foreach ($matched_fields as $match) {
     670            $attr_name = $match[1];
     671            $only_attrs[] = 'data-' . $attr_name;
     672        }
     673
     674        $merged_attrs = implode(' ', array_unique($only_attrs));
     675        $replaced = preg_replace('/(<(?:select|input)[^>]+)(>)/i', '$1 data-linguise-translate-attributes="' . $merged_attrs . '"$2', $field);
     676        if (!empty($replaced)) {
     677            return $replaced;
     678        }
     679        return $field;
    587680    }
    588681
     
    689782        return $location;
    690783    }
     784
     785    /**
     786     * Add more fragment specially stringified JSON fragments
     787     *
     788     * @param array        $fragments   The fragments to be translated
     789     * @param string       $html_string The HTML string containing the fragments
     790     * @param \DOMDocument $html_dom    The DOMDocument object of the HTML
     791     *
     792     * @return array The modified fragments to be translated
     793     */
     794    public function linguiseFragmentCollectionAdjustment($fragments, $html_string, $html_dom)
     795    {
     796        require_once realpath(__DIR__ . '/../../HTMLHelper.php'); // in case not loaded yet
     797        require_once realpath(__DIR__ . '/../../FragmentBase.php'); // in case not loaded yet
     798        require_once realpath(__DIR__ . '/../../FragmentHandler.php'); // in case not loaded yet
     799
     800        $scripts = $html_dom->getElementsByTagName('script');
     801        foreach ($scripts as $script) {
     802            $script_content = HTMLHelper::unclobberCdataInternal($script->textContent);
     803
     804            // try to find var wc_address_i18n_params = { ... };
     805            $match_res = preg_match('/var\s+wc_address_i18n_params\s*=\s*(\{.*?\});/s', $script_content, $matches);
     806            if ($match_res === false || $match_res === 0) {
     807                continue;
     808            }
     809
     810            $json_data = json_decode($matches[1], true);
     811            if ($json_data === null) {
     812                continue;
     813            }
     814
     815            if (isset($json_data['locale']) && is_string($json_data['locale'])) {
     816                // try parsing as JSON
     817                $inner_locale_json = json_decode($json_data['locale'], true);
     818                // get last error
     819                if (json_last_error() !== JSON_ERROR_NONE) {
     820                    continue;
     821                }
     822
     823                $collected = FragmentHandler::collectFragmentFromJson($inner_locale_json);
     824                // loop through this one and add skip=true
     825                foreach ($collected as &$fragment) {
     826                    $fragment['skip'] = true;
     827                }
     828
     829                if (!isset($fragments['wc_custom_fragment_handling_params'])) {
     830                    $fragments['wc_custom_address_i18n_params'] = [];
     831                }
     832
     833                if (!isset($fragments['wc_custom_address_i18n_params']['wc_address_i18n_params'])) {
     834                    $fragments['wc_custom_address_i18n_params']['wc_address_i18n_params'] = [];
     835                }
     836
     837                $fragments['wc_custom_address_i18n_params']['wc_address_i18n_params'] = [
     838                    'mode' => 'skip',
     839                    'fragments' => $collected,
     840                ];
     841            }
     842        }
     843
     844        return $fragments;
     845    }
     846
     847    /**
     848     * Apply back the manually adjusted HTML after fragment translation
     849     *
     850     * @param string $html_data The HTML data after fragment translation
     851     * @param array  $fragments The fragments that were translated
     852     *
     853     * @return string The modified HTML data after applying back the manually adjusted HTML
     854     */
     855    public function linguiseFragmentTranslationAdjusment($html_data, $fragments)
     856    {
     857        // We need to find the script tag that contains var wc_address_i18n_params = {...};
     858        // Then we need to replace the locale part with the new translated JSON
     859        require_once realpath(__DIR__ . '/../../HTMLHelper.php'); // in case not loaded yet
     860        require_once realpath(__DIR__ . '/../../FragmentBase.php'); // in case not loaded yet
     861        require_once realpath(__DIR__ . '/../../FragmentHandler.php'); // in case not loaded yet
     862
     863        // Nothing to do here for now
     864        foreach ($fragments as $fragment_name => $fragment_jsons) {
     865            if ($fragment_name !== 'wc_custom_address_i18n_params') {
     866                continue;
     867            }
     868
     869            foreach ($fragment_jsons as $fragment_param => $fragment_list) {
     870                $mode = $fragment_list['mode'];
     871                if ($mode !== 'skip') {
     872                    continue;
     873                }
     874
     875                $fragment_data = $fragment_list['fragments'];
     876
     877                if ($fragment_param === 'wc_address_i18n_params') {
     878                    $match_res = preg_match('/var\s+wc_address_i18n_params\s*=\s*(\{.*?\});/s', $html_data, $matches);
     879                    if ($match_res === false || $match_res === 0) {
     880                        continue;
     881                    }
     882
     883                    $json_data = json_decode($matches[1], true);
     884                    if ($json_data === null) {
     885                        continue;
     886                    }
     887
     888                    // Remove the $skip key
     889                    foreach ($fragment_data as &$fragment) {
     890                        unset($fragment['skip']);
     891                    }
     892
     893                    if (isset($json_data['locale']) && is_string($json_data['locale'])) {
     894                        $inner_locale_json = json_decode($json_data['locale'], true);
     895                        if ($inner_locale_json === null) {
     896                            continue;
     897                        }
     898
     899                        $replaced_json = FragmentHandler::applyTranslatedFragmentsForAuto($inner_locale_json, $fragment_data);
     900
     901                        $replaced_data = preg_replace(
     902                            '/var\s+wc_address_i18n_params\s*=\s*\{.*?\};/s',
     903                            'var wc_address_i18n_params = ' . json_encode(array_merge($json_data, ['locale' => json_encode($replaced_json)])) . ';',
     904                            $html_data,
     905                            1,
     906                            $count
     907                        );
     908
     909                        if ($count) {
     910                            $html_data = $replaced_data;
     911                        }
     912                    }
     913                }
     914            }
     915        }
     916
     917        return $html_data;
     918    }
    691919}
  • linguise/tags/2.1.76/vendor/composer/installed.php

    r3379190 r3381590  
    44        'pretty_version' => 'dev-master',
    55        'version' => 'dev-master',
    6         'reference' => 'b6b8eee0c339ae1716f9660867305e5032da50ac',
     6        'reference' => '66343e165289beebabeb64e2bba481179fff665d',
    77        'type' => 'library',
    88        'install_path' => __DIR__ . '/../../',
     
    3232            'pretty_version' => 'dev-master',
    3333            'version' => 'dev-master',
    34             'reference' => 'b6b8eee0c339ae1716f9660867305e5032da50ac',
     34            'reference' => '66343e165289beebabeb64e2bba481179fff665d',
    3535            'type' => 'library',
    3636            'install_path' => __DIR__ . '/../../',
  • linguise/trunk/linguise.php

    r3379190 r3381590  
    55 * Plugin URI: https://www.linguise.com/
    66 * Description: Linguise translation plugin
    7  * Version:2.1.75
     7 * Version:2.1.76
    88 * Text Domain: linguise
    99 * Domain Path: /languages
  • linguise/trunk/readme.txt

    r3379190 r3381590  
    44Requires at least: 4.0
    55Tested up to: 6.8
    6 Stable tag:2.1.75
     6Stable tag:2.1.76
    77Requires PHP: 7.0
    88License: GPLv2 or later
     
    104104
    105105== Changelog ==
     106= 2.1.76 =
     107- Fix: It does gets translated but then gets back again to default lang
     108
    106109= 2.1.75 =
    107110- Fix: WooCommerce Issue: checkout process error
  • linguise/trunk/src/FragmentBase.php

    r3366846 r3381590  
    1616     */
    1717    protected static $default_filters = [
     18        [
     19            'key' => '(state|country)\.label$',
     20            'mode' => 'regex_full',
     21            'kind' => 'allow'
     22        ],
    1823        [
    1924            'key' => 'nonce',
  • linguise/trunk/src/FragmentHandler.php

    r3366846 r3381590  
    2121     * @var string
    2222     */
    23     protected static $frag_html_match = '/<(div|a|linguise-main|img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="(link|html|html-main|text|media-img|media-imgset)" data-fragment-mode="(auto|override|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?>(.*?)<\/\1>/si';
     23    protected static $frag_html_match = '/<(div|a|linguise-main|img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="((?:link|html|html-main|text|media-img|media-imgset)(?:-skip)?)" data-fragment-mode="(auto|override|skip|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?>(.*?)<\/\1>/si';
    2424    /**
    2525     * Regex/matcher for our custom HTML fragment
     
    2929     * @var string
    3030     */
    31     protected static $frag_html_match_self_close = '/<(img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="(link|html|html-main|text|media-img|media-imgset)" data-fragment-mode="(auto|override|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?\s*\/?>/si';
     31    protected static $frag_html_match_self_close = '/<(img) class="linguise-fragment" data-fragment-name="([^"]*)" data-fragment-param="([^"]*)" data-fragment-key="([^"]*)" data-fragment-format="((?:link|html|html-main|text|media-img|media-imgset)(?:-skip)?)" data-fragment-mode="(auto|override|skip|attribute)"(?: data-fragment-extra-id="([^"]*)")?(?: (?:href|src|srcset)="([^"]*)")?\s*\/?>/si';
    3232
    3333    /**
     
    156156                $tag = 'img';
    157157            }
     158            $frag_format = $fragment['format'];
     159            if (isset($fragment['skip']) && $fragment['skip']) {
     160                $frag_format .= '-skip';
     161            }
     162
    158163            $html .= '<' . $tag . ' class="linguise-fragment" data-fragment-name="' . $fragment_name . '" data-fragment-param="' . $fragment_param . '" data-fragment-key="';
    159             $html .= $fragment['key'] . '" data-fragment-format="' . $fragment['format'] . '" data-fragment-mode="' . $json_fragments['mode'] . '"';
     164            $html .= $fragment['key'] . '" data-fragment-format="' . $frag_format . '" data-fragment-mode="' . $json_fragments['mode'] . '"';
    160165            if ($json_fragments['mode'] === 'attribute' && isset($json_fragments['attribute'])) {
    161166                $html .= ' data-fragment-extra-id="' . $json_fragments['attribute'] . '"';
     
    198203            $fragment_value = isset($match[9]) ? $match[9] : '';
    199204
     205            $is_skipped = false;
     206            if (substr($fragment_format, -5) === '-skip') {
     207                $is_skipped = true;
     208                $fragment_format = substr($fragment_format, 0, -5);
     209            }
     210
    200211            if ($fragment_format === 'link' || $fragment_format === 'media-img' || $fragment_format === 'media-imgset') {
    201212                $fragment_value = $match[8];
     
    245256                'format' => $fragment_format,
    246257                'match' => $match[0],
     258                'skip' => $is_skipped, // If `skip` is true, then don't replace this fragment (but remove it)
    247259            ];
    248260        }
     
    320332                $is_strict = true;
    321333            }
     334
    322335            $collected_temp = self::collectFragmentFromJson($json_data, $is_strict);
    323336            if (!empty($collected_temp)) {
     
    408421        }
    409422
     423        // Run filters
     424        $filtered_fragments = $all_fragments;
     425        $filtered_fragments = apply_filters('linguise_after_fragment_collection', $filtered_fragments, $html_data, $html_dom);
     426        if (is_array($filtered_fragments)) {
     427            $all_fragments = $filtered_fragments;
     428        }
     429
    410430        Debug::log('FragmentHandler -> Collected: ' . json_encode($all_fragments, JSON_PRETTY_PRINT));
    411431
     
    454474            $json_data = new JsonObject(json_decode('{' . $match_data . '}', true));
    455475            foreach ($fragment_info['fragments'] as $fragment) {
     476                if (isset($fragment['skip']) && $fragment['skip']) {
     477                    // If skip is true, then don't replace this fragment (but remove it)
     478                    continue;
     479                }
    456480                $decoded_key = self::unwrapKey($fragment['key']);
    457481                try {
     
    484508            $json_data = new JsonObject(json_decode($before_match, true));
    485509            foreach ($fragment_info['fragments'] as $fragment) {
     510                if (isset($fragment['skip']) && $fragment['skip']) {
     511                    // If skip is true, then don't replace this fragment (but remove it)
     512                    continue;
     513                }
    486514                $decoded_key = self::unwrapKey($fragment['key']);
    487515                try {
     
    530558
    531559        foreach ($fragments as $fragment) {
     560            if (isset($fragment['skip']) && $fragment['skip']) {
     561                // If skip is true, then don't replace this fragment (but remove it)
     562                continue;
     563            }
    532564            // remove the html fragment from the translated page
    533565            $decoded_key = self::unwrapKey($fragment['key']);
     
    577609                $mode = $fragment_list['mode'];
    578610
    579                 if (!in_array($mode, ['auto', 'override'])) {
     611                if (!in_array($mode, ['auto', 'override', 'skip'])) {
     612                    continue;
     613                }
     614
     615                if ($mode === 'skip') {
     616                    $html_data = self::cleanupFragments($html_data, $fragment_list['fragments']);
    580617                    continue;
    581618                }
     
    618655        }
    619656
    620         $mod_html_data = apply_filters('linguise_after_fragment_translation', $html_data);
     657        $mod_html_data = apply_filters('linguise_after_fragment_translation', $html_data, $fragments);
    621658        if (!empty($mod_html_data)) {
    622659            $html_data = $mod_html_data;
  • linguise/trunk/src/constants.php

    r3379190 r3381590  
    11<?php
    22if (!defined('LINGUISE_SCRIPT_TRANSLATION_VERSION')) {
    3     define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.75');
     3    define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.76');
    44}
    55
    66if (!defined('LINGUISE_VERSION')) {
    7     define('LINGUISE_VERSION', '2.1.75');
     7    define('LINGUISE_VERSION', '2.1.76');
    88}
  • linguise/trunk/src/thirdparty/wc/woocommerce.php

    r3379190 r3381590  
    44
    55use Linguise\WordPress\Helper as WPHelper;
     6use Linguise\WordPress\HTMLHelper;
    67
    78defined('ABSPATH') || die('');
     
    6667        'flatsome_ajax_add_to_cart',
    6768        'bootscore_ajax_add_to_cart',
     69    ];
     70
     71    /**
     72     * A collection of HTML attributes that you want to translate
     73     *
     74     * @var string[]
     75     */
     76    protected static $fragment_attributes = [
     77        [
     78            'name' => 'wc-order-button-payment',
     79            'key' => 'data-order_button_text',
     80            'mode' => 'string',
     81            'match_mode' => 'all',
     82            'matchers' => [
     83                [
     84                    'type' => 'tag',
     85                    'key' => 'input'
     86                ],
     87                [
     88                    'type' => 'attribute',
     89                    'key' => 'name',
     90                    'value' => 'payment_method'
     91                ]
     92            ]
     93        ]
    6894    ];
    6995
     
    199225        remove_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1);
    200226
     227        remove_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10);
     228        remove_filter('woocommerce_form_field_state', [$this, 'hookFormFieldsSelectTranslations'], 10);
     229        remove_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10);
     230
    201231        remove_action('wp_loaded', [$this, 'hookAddCartFragments']);
    202232        remove_action('wp_redirect', [$this, 'hookWPRedirect']);
     233
     234        remove_filter('linguise_after_fragment_collection', [$this, 'linguiseFragmentCollectionAdjustment'], 20);
     235        remove_filter('linguise_after_fragment_translation', [$this, 'linguiseFragmentTranslationAdjusment'], 20);
    203236    }
    204237
     
    236269        add_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1);
    237270
     271        add_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10, 1);
     272        add_filter('woocommerce_form_field_state', [$this, 'hookFormFieldsSelectTranslations'], 10, 1);
     273        add_filter('woocommerce_form_field_select', [$this, 'hookFormFieldsSelectTranslations'], 10, 1);
     274
    238275        add_action('wp_loaded', [$this, 'hookAddCartFragments']);
    239276        add_action('wp_redirect', [$this, 'hookWPRedirect']);
     277
     278        add_filter('linguise_after_fragment_collection', [$this, 'linguiseFragmentCollectionAdjustment'], 20, 3);
     279        add_filter('linguise_after_fragment_translation', [$this, 'linguiseFragmentTranslationAdjusment'], 20, 2);
    240280    }
    241281
     
    520560        }
    521561
     562        if (is_string($html_content)) {
     563            // modify it a bit more
     564            $html_content = $this->hookReviewOrderBeforePayment($html_content);
     565        }
     566
    522567        $html_content .= '</body></html>';
    523568
     
    585630        }
    586631        return str_replace('<button ', '<button data-linguise-translate-attributes="value data-value" ', $html);
     632    }
     633
     634    /**
     635     * Translate the payment method selection input in checkout page
     636     *
     637     * @param string $html The HTML of the payment method selection input
     638     *
     639     * @return string
     640     */
     641    public function hookReviewOrderBeforePayment($html)
     642    {
     643        // 1. Translate the payment method name
     644        $replaced = preg_replace('/(<input[^>]+name=["\']payment_method["\'][^>]+)(>)/i', '$1 data-linguise-translate-attributes="data-order_button_text"$2', $html);
     645        if (!empty($replaced)) {
     646            return $replaced;
     647        }
     648        return $html;
     649    }
     650
     651    /**
     652     * Translate select form fields attributes like data-placeholder, data-label
     653     *
     654     * @param string $field The HTML of the select field
     655     *
     656     * @return string
     657     */
     658    public function hookFormFieldsSelectTranslations($field)
     659    {
     660        $matched_fields = [];
     661        // find data-placeholder="..."
     662        // find data-label="..."
     663        preg_match_all('/data-(placeholder|label)=["\']([^"\']+)["\']/', $field, $matched_fields, PREG_SET_ORDER);
     664        if (empty($matched_fields)) {
     665            return $field;
     666        }
     667
     668        $only_attrs = [];
     669        foreach ($matched_fields as $match) {
     670            $attr_name = $match[1];
     671            $only_attrs[] = 'data-' . $attr_name;
     672        }
     673
     674        $merged_attrs = implode(' ', array_unique($only_attrs));
     675        $replaced = preg_replace('/(<(?:select|input)[^>]+)(>)/i', '$1 data-linguise-translate-attributes="' . $merged_attrs . '"$2', $field);
     676        if (!empty($replaced)) {
     677            return $replaced;
     678        }
     679        return $field;
    587680    }
    588681
     
    689782        return $location;
    690783    }
     784
     785    /**
     786     * Add more fragment specially stringified JSON fragments
     787     *
     788     * @param array        $fragments   The fragments to be translated
     789     * @param string       $html_string The HTML string containing the fragments
     790     * @param \DOMDocument $html_dom    The DOMDocument object of the HTML
     791     *
     792     * @return array The modified fragments to be translated
     793     */
     794    public function linguiseFragmentCollectionAdjustment($fragments, $html_string, $html_dom)
     795    {
     796        require_once realpath(__DIR__ . '/../../HTMLHelper.php'); // in case not loaded yet
     797        require_once realpath(__DIR__ . '/../../FragmentBase.php'); // in case not loaded yet
     798        require_once realpath(__DIR__ . '/../../FragmentHandler.php'); // in case not loaded yet
     799
     800        $scripts = $html_dom->getElementsByTagName('script');
     801        foreach ($scripts as $script) {
     802            $script_content = HTMLHelper::unclobberCdataInternal($script->textContent);
     803
     804            // try to find var wc_address_i18n_params = { ... };
     805            $match_res = preg_match('/var\s+wc_address_i18n_params\s*=\s*(\{.*?\});/s', $script_content, $matches);
     806            if ($match_res === false || $match_res === 0) {
     807                continue;
     808            }
     809
     810            $json_data = json_decode($matches[1], true);
     811            if ($json_data === null) {
     812                continue;
     813            }
     814
     815            if (isset($json_data['locale']) && is_string($json_data['locale'])) {
     816                // try parsing as JSON
     817                $inner_locale_json = json_decode($json_data['locale'], true);
     818                // get last error
     819                if (json_last_error() !== JSON_ERROR_NONE) {
     820                    continue;
     821                }
     822
     823                $collected = FragmentHandler::collectFragmentFromJson($inner_locale_json);
     824                // loop through this one and add skip=true
     825                foreach ($collected as &$fragment) {
     826                    $fragment['skip'] = true;
     827                }
     828
     829                if (!isset($fragments['wc_custom_fragment_handling_params'])) {
     830                    $fragments['wc_custom_address_i18n_params'] = [];
     831                }
     832
     833                if (!isset($fragments['wc_custom_address_i18n_params']['wc_address_i18n_params'])) {
     834                    $fragments['wc_custom_address_i18n_params']['wc_address_i18n_params'] = [];
     835                }
     836
     837                $fragments['wc_custom_address_i18n_params']['wc_address_i18n_params'] = [
     838                    'mode' => 'skip',
     839                    'fragments' => $collected,
     840                ];
     841            }
     842        }
     843
     844        return $fragments;
     845    }
     846
     847    /**
     848     * Apply back the manually adjusted HTML after fragment translation
     849     *
     850     * @param string $html_data The HTML data after fragment translation
     851     * @param array  $fragments The fragments that were translated
     852     *
     853     * @return string The modified HTML data after applying back the manually adjusted HTML
     854     */
     855    public function linguiseFragmentTranslationAdjusment($html_data, $fragments)
     856    {
     857        // We need to find the script tag that contains var wc_address_i18n_params = {...};
     858        // Then we need to replace the locale part with the new translated JSON
     859        require_once realpath(__DIR__ . '/../../HTMLHelper.php'); // in case not loaded yet
     860        require_once realpath(__DIR__ . '/../../FragmentBase.php'); // in case not loaded yet
     861        require_once realpath(__DIR__ . '/../../FragmentHandler.php'); // in case not loaded yet
     862
     863        // Nothing to do here for now
     864        foreach ($fragments as $fragment_name => $fragment_jsons) {
     865            if ($fragment_name !== 'wc_custom_address_i18n_params') {
     866                continue;
     867            }
     868
     869            foreach ($fragment_jsons as $fragment_param => $fragment_list) {
     870                $mode = $fragment_list['mode'];
     871                if ($mode !== 'skip') {
     872                    continue;
     873                }
     874
     875                $fragment_data = $fragment_list['fragments'];
     876
     877                if ($fragment_param === 'wc_address_i18n_params') {
     878                    $match_res = preg_match('/var\s+wc_address_i18n_params\s*=\s*(\{.*?\});/s', $html_data, $matches);
     879                    if ($match_res === false || $match_res === 0) {
     880                        continue;
     881                    }
     882
     883                    $json_data = json_decode($matches[1], true);
     884                    if ($json_data === null) {
     885                        continue;
     886                    }
     887
     888                    // Remove the $skip key
     889                    foreach ($fragment_data as &$fragment) {
     890                        unset($fragment['skip']);
     891                    }
     892
     893                    if (isset($json_data['locale']) && is_string($json_data['locale'])) {
     894                        $inner_locale_json = json_decode($json_data['locale'], true);
     895                        if ($inner_locale_json === null) {
     896                            continue;
     897                        }
     898
     899                        $replaced_json = FragmentHandler::applyTranslatedFragmentsForAuto($inner_locale_json, $fragment_data);
     900
     901                        $replaced_data = preg_replace(
     902                            '/var\s+wc_address_i18n_params\s*=\s*\{.*?\};/s',
     903                            'var wc_address_i18n_params = ' . json_encode(array_merge($json_data, ['locale' => json_encode($replaced_json)])) . ';',
     904                            $html_data,
     905                            1,
     906                            $count
     907                        );
     908
     909                        if ($count) {
     910                            $html_data = $replaced_data;
     911                        }
     912                    }
     913                }
     914            }
     915        }
     916
     917        return $html_data;
     918    }
    691919}
  • linguise/trunk/vendor/composer/installed.php

    r3379190 r3381590  
    44        'pretty_version' => 'dev-master',
    55        'version' => 'dev-master',
    6         'reference' => 'b6b8eee0c339ae1716f9660867305e5032da50ac',
     6        'reference' => '66343e165289beebabeb64e2bba481179fff665d',
    77        'type' => 'library',
    88        'install_path' => __DIR__ . '/../../',
     
    3232            'pretty_version' => 'dev-master',
    3333            'version' => 'dev-master',
    34             'reference' => 'b6b8eee0c339ae1716f9660867305e5032da50ac',
     34            'reference' => '66343e165289beebabeb64e2bba481179fff665d',
    3535            'type' => 'library',
    3636            'install_path' => __DIR__ . '/../../',
Note: See TracChangeset for help on using the changeset viewer.