Changeset 3381590
- Timestamp:
- 10/21/2025 03:14:19 AM (5 months ago)
- Location:
- linguise
- Files:
-
- 14 edited
- 1 copied
-
tags/2.1.76 (copied) (copied from linguise/trunk)
-
tags/2.1.76/linguise.php (modified) (1 diff)
-
tags/2.1.76/readme.txt (modified) (2 diffs)
-
tags/2.1.76/src/FragmentBase.php (modified) (1 diff)
-
tags/2.1.76/src/FragmentHandler.php (modified) (12 diffs)
-
tags/2.1.76/src/constants.php (modified) (1 diff)
-
tags/2.1.76/src/thirdparty/wc/woocommerce.php (modified) (7 diffs)
-
tags/2.1.76/vendor/composer/installed.php (modified) (2 diffs)
-
trunk/linguise.php (modified) (1 diff)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/src/FragmentBase.php (modified) (1 diff)
-
trunk/src/FragmentHandler.php (modified) (12 diffs)
-
trunk/src/constants.php (modified) (1 diff)
-
trunk/src/thirdparty/wc/woocommerce.php (modified) (7 diffs)
-
trunk/vendor/composer/installed.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
linguise/tags/2.1.76/linguise.php
r3379190 r3381590 5 5 * Plugin URI: https://www.linguise.com/ 6 6 * Description: Linguise translation plugin 7 * Version:2.1.7 57 * Version:2.1.76 8 8 * Text Domain: linguise 9 9 * Domain Path: /languages -
linguise/tags/2.1.76/readme.txt
r3379190 r3381590 4 4 Requires at least: 4.0 5 5 Tested up to: 6.8 6 Stable tag:2.1.7 56 Stable tag:2.1.76 7 7 Requires PHP: 7.0 8 8 License: GPLv2 or later … … 104 104 105 105 == Changelog == 106 = 2.1.76 = 107 - Fix: It does gets translated but then gets back again to default lang 108 106 109 = 2.1.75 = 107 110 - Fix: WooCommerce Issue: checkout process error -
linguise/tags/2.1.76/src/FragmentBase.php
r3366846 r3381590 16 16 */ 17 17 protected static $default_filters = [ 18 [ 19 'key' => '(state|country)\.label$', 20 'mode' => 'regex_full', 21 'kind' => 'allow' 22 ], 18 23 [ 19 24 'key' => 'nonce', -
linguise/tags/2.1.76/src/FragmentHandler.php
r3366846 r3381590 21 21 * @var string 22 22 */ 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'; 24 24 /** 25 25 * Regex/matcher for our custom HTML fragment … … 29 29 * @var string 30 30 */ 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'; 32 32 33 33 /** … … 156 156 $tag = 'img'; 157 157 } 158 $frag_format = $fragment['format']; 159 if (isset($fragment['skip']) && $fragment['skip']) { 160 $frag_format .= '-skip'; 161 } 162 158 163 $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="' . $frag ment['format']. '" data-fragment-mode="' . $json_fragments['mode'] . '"';164 $html .= $fragment['key'] . '" data-fragment-format="' . $frag_format . '" data-fragment-mode="' . $json_fragments['mode'] . '"'; 160 165 if ($json_fragments['mode'] === 'attribute' && isset($json_fragments['attribute'])) { 161 166 $html .= ' data-fragment-extra-id="' . $json_fragments['attribute'] . '"'; … … 198 203 $fragment_value = isset($match[9]) ? $match[9] : ''; 199 204 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 200 211 if ($fragment_format === 'link' || $fragment_format === 'media-img' || $fragment_format === 'media-imgset') { 201 212 $fragment_value = $match[8]; … … 245 256 'format' => $fragment_format, 246 257 'match' => $match[0], 258 'skip' => $is_skipped, // If `skip` is true, then don't replace this fragment (but remove it) 247 259 ]; 248 260 } … … 320 332 $is_strict = true; 321 333 } 334 322 335 $collected_temp = self::collectFragmentFromJson($json_data, $is_strict); 323 336 if (!empty($collected_temp)) { … … 408 421 } 409 422 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 410 430 Debug::log('FragmentHandler -> Collected: ' . json_encode($all_fragments, JSON_PRETTY_PRINT)); 411 431 … … 454 474 $json_data = new JsonObject(json_decode('{' . $match_data . '}', true)); 455 475 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 } 456 480 $decoded_key = self::unwrapKey($fragment['key']); 457 481 try { … … 484 508 $json_data = new JsonObject(json_decode($before_match, true)); 485 509 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 } 486 514 $decoded_key = self::unwrapKey($fragment['key']); 487 515 try { … … 530 558 531 559 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 } 532 564 // remove the html fragment from the translated page 533 565 $decoded_key = self::unwrapKey($fragment['key']); … … 577 609 $mode = $fragment_list['mode']; 578 610 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']); 580 617 continue; 581 618 } … … 618 655 } 619 656 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); 621 658 if (!empty($mod_html_data)) { 622 659 $html_data = $mod_html_data; -
linguise/tags/2.1.76/src/constants.php
r3379190 r3381590 1 1 <?php 2 2 if (!defined('LINGUISE_SCRIPT_TRANSLATION_VERSION')) { 3 define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.7 5');3 define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.76'); 4 4 } 5 5 6 6 if (!defined('LINGUISE_VERSION')) { 7 define('LINGUISE_VERSION', '2.1.7 5');7 define('LINGUISE_VERSION', '2.1.76'); 8 8 } -
linguise/tags/2.1.76/src/thirdparty/wc/woocommerce.php
r3379190 r3381590 4 4 5 5 use Linguise\WordPress\Helper as WPHelper; 6 use Linguise\WordPress\HTMLHelper; 6 7 7 8 defined('ABSPATH') || die(''); … … 66 67 'flatsome_ajax_add_to_cart', 67 68 '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 ] 68 94 ]; 69 95 … … 199 225 remove_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1); 200 226 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 201 231 remove_action('wp_loaded', [$this, 'hookAddCartFragments']); 202 232 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); 203 236 } 204 237 … … 236 269 add_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1); 237 270 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 238 275 add_action('wp_loaded', [$this, 'hookAddCartFragments']); 239 276 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); 240 280 } 241 281 … … 520 560 } 521 561 562 if (is_string($html_content)) { 563 // modify it a bit more 564 $html_content = $this->hookReviewOrderBeforePayment($html_content); 565 } 566 522 567 $html_content .= '</body></html>'; 523 568 … … 585 630 } 586 631 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; 587 680 } 588 681 … … 689 782 return $location; 690 783 } 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 } 691 919 } -
linguise/tags/2.1.76/vendor/composer/installed.php
r3379190 r3381590 4 4 'pretty_version' => 'dev-master', 5 5 'version' => 'dev-master', 6 'reference' => ' b6b8eee0c339ae1716f9660867305e5032da50ac',6 'reference' => '66343e165289beebabeb64e2bba481179fff665d', 7 7 'type' => 'library', 8 8 'install_path' => __DIR__ . '/../../', … … 32 32 'pretty_version' => 'dev-master', 33 33 'version' => 'dev-master', 34 'reference' => ' b6b8eee0c339ae1716f9660867305e5032da50ac',34 'reference' => '66343e165289beebabeb64e2bba481179fff665d', 35 35 'type' => 'library', 36 36 'install_path' => __DIR__ . '/../../', -
linguise/trunk/linguise.php
r3379190 r3381590 5 5 * Plugin URI: https://www.linguise.com/ 6 6 * Description: Linguise translation plugin 7 * Version:2.1.7 57 * Version:2.1.76 8 8 * Text Domain: linguise 9 9 * Domain Path: /languages -
linguise/trunk/readme.txt
r3379190 r3381590 4 4 Requires at least: 4.0 5 5 Tested up to: 6.8 6 Stable tag:2.1.7 56 Stable tag:2.1.76 7 7 Requires PHP: 7.0 8 8 License: GPLv2 or later … … 104 104 105 105 == Changelog == 106 = 2.1.76 = 107 - Fix: It does gets translated but then gets back again to default lang 108 106 109 = 2.1.75 = 107 110 - Fix: WooCommerce Issue: checkout process error -
linguise/trunk/src/FragmentBase.php
r3366846 r3381590 16 16 */ 17 17 protected static $default_filters = [ 18 [ 19 'key' => '(state|country)\.label$', 20 'mode' => 'regex_full', 21 'kind' => 'allow' 22 ], 18 23 [ 19 24 'key' => 'nonce', -
linguise/trunk/src/FragmentHandler.php
r3366846 r3381590 21 21 * @var string 22 22 */ 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'; 24 24 /** 25 25 * Regex/matcher for our custom HTML fragment … … 29 29 * @var string 30 30 */ 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'; 32 32 33 33 /** … … 156 156 $tag = 'img'; 157 157 } 158 $frag_format = $fragment['format']; 159 if (isset($fragment['skip']) && $fragment['skip']) { 160 $frag_format .= '-skip'; 161 } 162 158 163 $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="' . $frag ment['format']. '" data-fragment-mode="' . $json_fragments['mode'] . '"';164 $html .= $fragment['key'] . '" data-fragment-format="' . $frag_format . '" data-fragment-mode="' . $json_fragments['mode'] . '"'; 160 165 if ($json_fragments['mode'] === 'attribute' && isset($json_fragments['attribute'])) { 161 166 $html .= ' data-fragment-extra-id="' . $json_fragments['attribute'] . '"'; … … 198 203 $fragment_value = isset($match[9]) ? $match[9] : ''; 199 204 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 200 211 if ($fragment_format === 'link' || $fragment_format === 'media-img' || $fragment_format === 'media-imgset') { 201 212 $fragment_value = $match[8]; … … 245 256 'format' => $fragment_format, 246 257 'match' => $match[0], 258 'skip' => $is_skipped, // If `skip` is true, then don't replace this fragment (but remove it) 247 259 ]; 248 260 } … … 320 332 $is_strict = true; 321 333 } 334 322 335 $collected_temp = self::collectFragmentFromJson($json_data, $is_strict); 323 336 if (!empty($collected_temp)) { … … 408 421 } 409 422 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 410 430 Debug::log('FragmentHandler -> Collected: ' . json_encode($all_fragments, JSON_PRETTY_PRINT)); 411 431 … … 454 474 $json_data = new JsonObject(json_decode('{' . $match_data . '}', true)); 455 475 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 } 456 480 $decoded_key = self::unwrapKey($fragment['key']); 457 481 try { … … 484 508 $json_data = new JsonObject(json_decode($before_match, true)); 485 509 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 } 486 514 $decoded_key = self::unwrapKey($fragment['key']); 487 515 try { … … 530 558 531 559 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 } 532 564 // remove the html fragment from the translated page 533 565 $decoded_key = self::unwrapKey($fragment['key']); … … 577 609 $mode = $fragment_list['mode']; 578 610 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']); 580 617 continue; 581 618 } … … 618 655 } 619 656 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); 621 658 if (!empty($mod_html_data)) { 622 659 $html_data = $mod_html_data; -
linguise/trunk/src/constants.php
r3379190 r3381590 1 1 <?php 2 2 if (!defined('LINGUISE_SCRIPT_TRANSLATION_VERSION')) { 3 define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.7 5');3 define('LINGUISE_SCRIPT_TRANSLATION_VERSION', 'wordpress_plugin/2.1.76'); 4 4 } 5 5 6 6 if (!defined('LINGUISE_VERSION')) { 7 define('LINGUISE_VERSION', '2.1.7 5');7 define('LINGUISE_VERSION', '2.1.76'); 8 8 } -
linguise/trunk/src/thirdparty/wc/woocommerce.php
r3379190 r3381590 4 4 5 5 use Linguise\WordPress\Helper as WPHelper; 6 use Linguise\WordPress\HTMLHelper; 6 7 7 8 defined('ABSPATH') || die(''); … … 66 67 'flatsome_ajax_add_to_cart', 67 68 '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 ] 68 94 ]; 69 95 … … 199 225 remove_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1); 200 226 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 201 231 remove_action('wp_loaded', [$this, 'hookAddCartFragments']); 202 232 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); 203 236 } 204 237 … … 236 269 add_filter('woocommerce_order_button_html', [$this, 'hookOrderButtonHTML'], 1000, 1); 237 270 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 238 275 add_action('wp_loaded', [$this, 'hookAddCartFragments']); 239 276 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); 240 280 } 241 281 … … 520 560 } 521 561 562 if (is_string($html_content)) { 563 // modify it a bit more 564 $html_content = $this->hookReviewOrderBeforePayment($html_content); 565 } 566 522 567 $html_content .= '</body></html>'; 523 568 … … 585 630 } 586 631 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; 587 680 } 588 681 … … 689 782 return $location; 690 783 } 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 } 691 919 } -
linguise/trunk/vendor/composer/installed.php
r3379190 r3381590 4 4 'pretty_version' => 'dev-master', 5 5 'version' => 'dev-master', 6 'reference' => ' b6b8eee0c339ae1716f9660867305e5032da50ac',6 'reference' => '66343e165289beebabeb64e2bba481179fff665d', 7 7 'type' => 'library', 8 8 'install_path' => __DIR__ . '/../../', … … 32 32 'pretty_version' => 'dev-master', 33 33 'version' => 'dev-master', 34 'reference' => ' b6b8eee0c339ae1716f9660867305e5032da50ac',34 'reference' => '66343e165289beebabeb64e2bba481179fff665d', 35 35 'type' => 'library', 36 36 'install_path' => __DIR__ . '/../../',
Note: See TracChangeset
for help on using the changeset viewer.