TranslatePress – Translate Multilingual sites - Version 2.1.4

Version Description

  • Optimized automatic translation usage by improved recognition of cdata, js scripts and encoded html
  • Fixed conflict with Ultimate Dashboard
Download this release

Release Info

Developer razvan.mo
Plugin Icon 128x128 TranslatePress – Translate Multilingual sites
Version 2.1.4
Comparing to
See all releases

Code changes from version 2.1.3 to 2.1.4

class-translate-press.php CHANGED
@@ -58,7 +58,7 @@ class TRP_Translate_Press{
58
  define( 'TRP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
59
  define( 'TRP_PLUGIN_BASE', plugin_basename( __DIR__ . '/index.php' ) );
60
  define( 'TRP_PLUGIN_SLUG', 'translatepress-multilingual' );
61
- define( 'TRP_PLUGIN_VERSION', '2.1.3' );
62
 
63
  wp_cache_add_non_persistent_groups(array('trp'));
64
 
58
  define( 'TRP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
59
  define( 'TRP_PLUGIN_BASE', plugin_basename( __DIR__ . '/index.php' ) );
60
  define( 'TRP_PLUGIN_SLUG', 'translatepress-multilingual' );
61
+ define( 'TRP_PLUGIN_VERSION', '2.1.4' );
62
 
63
  wp_cache_add_non_persistent_groups(array('trp'));
64
 
includes/class-translation-render.php CHANGED
@@ -14,6 +14,7 @@ class TRP_Translation_Render{
14
  protected $url_converter;
15
  /* @var TRP_Translation_Manager */
16
  protected $translation_manager;
 
17
 
18
  /**
19
  * TRP_Translation_Render constructor.
@@ -22,6 +23,8 @@ class TRP_Translation_Render{
22
  */
23
  public function __construct( $settings ){
24
  $this->settings = $settings;
 
 
25
  }
26
 
27
  /**
@@ -646,6 +649,18 @@ class TRP_Translation_Render{
646
  }
647
 
648
  $no_translate_selectors = apply_filters( 'trp_no_translate_selectors', array( '#wpadminbar' ), $TRP_LANGUAGE );
 
 
 
 
 
 
 
 
 
 
 
 
649
 
650
  /*
651
  * process the types of strings we can currently have: no-translate, translation-block, text, input, textarea, etc.
@@ -698,8 +713,37 @@ class TRP_Translation_Render{
698
  && !$this->trp_is_numeric($trimmed_string)
699
  && !preg_match('/^\d+%$/',$trimmed_string)
700
  && !$this->has_ancestor_attribute( $row, $no_translate_attribute )
701
- && !$this->has_ancestor_class( $row, 'translation-block') )
 
 
702
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
703
  // $translateable_strings array needs to be in sync in $nodes array
704
  $string_count = array_push( $translateable_strings, $trimmed_string );
705
  $node_type_to_push = ( in_array( $parent->tag, array( 'button', 'option' ) ) ) ? $parent->tag : 'text';
@@ -737,9 +781,24 @@ class TRP_Translation_Render{
737
  && !$this->has_ancestor_attribute( $row, $no_translate_attribute )
738
  && !$this->has_ancestor_attribute( $row, $no_translate_attribute . '-' . $current_node_accessor_selector )
739
  && !$this->has_ancestor_class( $row, 'translation-block')
740
- && $row->tag != 'link' )
 
 
741
  {
742
- $entity_decoded_trimmed_string = html_entity_decode( $trimmed_string );
 
 
 
 
 
 
 
 
 
 
 
 
 
743
  array_push( $translateable_strings, $entity_decoded_trimmed_string );
744
  array_push( $nodes, array( 'node'=>$row, 'type' => $node_accessor_key ) );
745
  if ( ! apply_filters( 'trp_allow_machine_translation_for_string', true, $entity_decoded_trimmed_string, $current_node_accessor_selector, $node_accessor, $row ) ){
@@ -1807,4 +1866,49 @@ class TRP_Translation_Render{
1807
  }
1808
  }
1809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1810
  }
14
  protected $url_converter;
15
  /* @var TRP_Translation_Manager */
16
  protected $translation_manager;
17
+ protected $common_html_tags;
18
 
19
  /**
20
  * TRP_Translation_Render constructor.
23
  */
24
  public function __construct( $settings ){
25
  $this->settings = $settings;
26
+ // apply_filters only once instead of everytime is_html() is used
27
+ $this->common_html_tags = implode( '|', apply_filters('trp_common_html_tags', array( 'html', 'body', 'table', 'tbody', 'thead', 'th', 'td', 'tr', 'div', 'p', 'span', 'b', 'a', 'strong', 'center', 'br', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'img' ) ) );
28
  }
29
 
30
  /**
649
  }
650
 
651
  $no_translate_selectors = apply_filters( 'trp_no_translate_selectors', array( '#wpadminbar' ), $TRP_LANGUAGE );
652
+ $ignore_cdata = apply_filters('trp_ignore_cdata', true );
653
+ $translate_encoded_html_as_string = apply_filters('trp_translate_encoded_html_as_string', false );
654
+ $translate_encoded_html_as_html = apply_filters('trp_translate_encoded_html_as_html', true );
655
+ // used for skipping minified scripts but can be used for anything
656
+ $skip_strings_containing_key_terms = apply_filters('trp_skip_strings_containing_key_terms',
657
+ array(
658
+ array(
659
+ 'terms'=> array( 'function', 'return', 'if', '==' ),
660
+ 'operator' => 'and'
661
+ )
662
+ )
663
+ );
664
 
665
  /*
666
  * process the types of strings we can currently have: no-translate, translation-block, text, input, textarea, etc.
713
  && !$this->trp_is_numeric($trimmed_string)
714
  && !preg_match('/^\d+%$/',$trimmed_string)
715
  && !$this->has_ancestor_attribute( $row, $no_translate_attribute )
716
+ && !$this->has_ancestor_class( $row, 'translation-block')
717
+ && ( !$ignore_cdata || ( strpos($trimmed_string, '<![CDATA[') !== 0 && strpos($trimmed_string, '&lt;![CDATA[') !== 0 ) )
718
+ && !$this->contains_substrings($trimmed_string, $skip_strings_containing_key_terms ) )
719
  {
720
+ if ( !$translate_encoded_html_as_string ){
721
+ $is_html = false;
722
+ if ( $translate_encoded_html_as_html ){
723
+ if ( $this->is_html($trimmed_string) ){
724
+ // prevent potential infinite loops. Only call translate_page once recursively
725
+ add_filter( 'trp_translate_encoded_html_as_html', '__return_false' );
726
+
727
+ $row->outertext = str_replace( $trimmed_string, $this->translate_page( $trimmed_string ), $row->outertext );
728
+ remove_filter( 'trp_translate_encoded_html_as_html', '__return_false' );
729
+ $is_html = true;
730
+ }else {
731
+ $entity_decoded_trimmed_string = html_entity_decode( $trimmed_string );
732
+ if ( $this->is_html( $entity_decoded_trimmed_string ) ) {
733
+ // prevent potential infinite loops. Only call translate_page once recursively
734
+ add_filter( 'trp_translate_encoded_html_as_html', '__return_false' );
735
+
736
+ $row->outertext = str_replace( $trimmed_string, htmlentities( $this->translate_page( $entity_decoded_trimmed_string ) ), $row->outertext );
737
+ remove_filter( 'trp_translate_encoded_html_as_html', '__return_false' );
738
+ $is_html = true;
739
+ }
740
+ }
741
+ }
742
+ if ( $is_html ) {
743
+ continue;
744
+ }
745
+ }
746
+
747
  // $translateable_strings array needs to be in sync in $nodes array
748
  $string_count = array_push( $translateable_strings, $trimmed_string );
749
  $node_type_to_push = ( in_array( $parent->tag, array( 'button', 'option' ) ) ) ? $parent->tag : 'text';
781
  && !$this->has_ancestor_attribute( $row, $no_translate_attribute )
782
  && !$this->has_ancestor_attribute( $row, $no_translate_attribute . '-' . $current_node_accessor_selector )
783
  && !$this->has_ancestor_class( $row, 'translation-block')
784
+ && $row->tag != 'link'
785
+ && ( !$ignore_cdata || ( strpos($trimmed_string, '<![CDATA[') !== 0 && strpos($trimmed_string, '&lt;![CDATA[') !== 0 ) )
786
+ && !$this->contains_substrings($trimmed_string, $skip_strings_containing_key_terms ) )
787
  {
788
+ $entity_decoded_trimmed_string = html_entity_decode( $trimmed_string );
789
+ if ( !$translate_encoded_html_as_string ){
790
+ if ( $translate_encoded_html_as_html ){
791
+ if ( $this->is_html($entity_decoded_trimmed_string) ){
792
+ // prevent potential infinite loops. Only call translate_page once recursively
793
+ add_filter( 'trp_translate_encoded_html_as_html', '__return_false' );
794
+
795
+ $row->setAttribute( $current_node_accessor_selector, str_replace( $trimmed_string, esc_attr( htmlentities($this->translate_page( $entity_decoded_trimmed_string )) ), $row->$current_node_accessor_selector ) );
796
+ remove_filter( 'trp_translate_encoded_html_as_html', '__return_false' );
797
+ continue;
798
+ }
799
+ }
800
+ }
801
+
802
  array_push( $translateable_strings, $entity_decoded_trimmed_string );
803
  array_push( $nodes, array( 'node'=>$row, 'type' => $node_accessor_key ) );
804
  if ( ! apply_filters( 'trp_allow_machine_translation_for_string', true, $entity_decoded_trimmed_string, $current_node_accessor_selector, $node_accessor, $row ) ){
1866
  }
1867
  }
1868
 
1869
+ /**
1870
+ * Whether a text contains html tags.
1871
+ * Match an opening or closing tag among given html tags.
1872
+ * @param $string
1873
+ * @return bool
1874
+ */
1875
+ public function is_html( $string ){
1876
+ $pattern = '/<\/?(' . $this->common_html_tags . ')(\s[^>]*)?(\s?\/)?\>/';
1877
+ return preg_match($pattern, $string, $matches);
1878
+ }
1879
+
1880
+ /**
1881
+ * Matches the conditions set by $key_term_arrays
1882
+ *
1883
+ * See $skip_strings_containing_key_terms for config
1884
+ *
1885
+ * @param $string
1886
+ * @param $key_terms_arrays
1887
+ * @return bool
1888
+ */
1889
+ public function contains_substrings($string, $key_terms_arrays){
1890
+ foreach( $key_terms_arrays as $key_terms ) {
1891
+ if ( !empty( $key_terms['operator'] ) && !empty( $key_terms['terms'] ) && is_array( $key_terms ) ) {
1892
+ if ( $key_terms['operator'] == 'or' ){
1893
+ foreach ( $key_terms['terms'] as $term ) {
1894
+ if ( stripos( $string, $term ) !== false ) {
1895
+ return true;
1896
+ }
1897
+ }
1898
+ }
1899
+ if ( $key_terms['operator'] == 'and' ){
1900
+ foreach ( $key_terms['terms'] as $array_key => $term ) {
1901
+ if ( stripos( $string, $term ) !== false ) {
1902
+ unset($key_terms['terms'][$array_key]);
1903
+ if ( count ($key_terms['terms'] ) == 0 ){
1904
+ return true;
1905
+ }
1906
+ }
1907
+ }
1908
+ }
1909
+ }
1910
+ }
1911
+ return false;
1912
+ }
1913
+
1914
  }
includes/class-url-converter.php CHANGED
@@ -9,6 +9,7 @@ class TRP_Url_Converter {
9
 
10
  protected $absolute_home;
11
  protected $settings;
 
12
 
13
  /**
14
  * TRP_Url_Converter constructor.
@@ -17,6 +18,8 @@ class TRP_Url_Converter {
17
  */
18
  public function __construct( $settings ){
19
  $this->settings = $settings;
 
 
20
  }
21
 
22
  /**
@@ -74,7 +77,6 @@ class TRP_Url_Converter {
74
  */
75
  public function is_admin_request() {
76
  $current_url = $this->cur_page_url();
77
- $admin_url = strtolower( admin_url() );
78
 
79
  // we can't use wp_get_referer() It looks like it creates an infinite loop because it calls home_url() and we're filtering that
80
  // array('http','https') is added because of a compatibility issue with Scriptless Social Sharing that created an infinite loop
@@ -88,7 +90,7 @@ class TRP_Url_Converter {
88
  }
89
 
90
  //consider an admin request a call to the rest api that came from the admin area
91
- if( false !== strpos( $current_url, '/wp-json/' ) && 0 === strpos( $referrer, $admin_url ) ){
92
  return true;
93
  }
94
 
@@ -96,11 +98,11 @@ class TRP_Url_Converter {
96
  * Check if this is a admin request. If true, it
97
  * could also be a AJAX request from the frontend.
98
  */
99
- if ( 0 === strpos( $current_url, $admin_url ) ) {
100
  /**
101
  * Check if the user comes from a admin page.
102
  */
103
- if ( 0 === strpos( $referrer, $admin_url ) ) {
104
  return true;
105
  } else {
106
  if ( function_exists( 'wp_doing_ajax' ) ) {
9
 
10
  protected $absolute_home;
11
  protected $settings;
12
+ protected $admin_url;
13
 
14
  /**
15
  * TRP_Url_Converter constructor.
18
  */
19
  public function __construct( $settings ){
20
  $this->settings = $settings;
21
+ //$admin_url is declared here because it was causing a conflict with Ultimate Dashboard since there was an action hooked on site_url
22
+ $this->admin_url = strtolower( admin_url() );
23
  }
24
 
25
  /**
77
  */
78
  public function is_admin_request() {
79
  $current_url = $this->cur_page_url();
 
80
 
81
  // we can't use wp_get_referer() It looks like it creates an infinite loop because it calls home_url() and we're filtering that
82
  // array('http','https') is added because of a compatibility issue with Scriptless Social Sharing that created an infinite loop
90
  }
91
 
92
  //consider an admin request a call to the rest api that came from the admin area
93
+ if( false !== strpos( $current_url, '/wp-json/' ) && 0 === strpos( $referrer, $this->admin_url ) ){
94
  return true;
95
  }
96
 
98
  * Check if this is a admin request. If true, it
99
  * could also be a AJAX request from the frontend.
100
  */
101
+ if ( 0 === strpos( $current_url, $this->admin_url ) ) {
102
  /**
103
  * Check if the user comes from a admin page.
104
  */
105
+ if ( 0 === strpos( $referrer, $this->admin_url ) ) {
106
  return true;
107
  } else {
108
  if ( function_exists( 'wp_doing_ajax' ) ) {
index.php CHANGED
@@ -3,14 +3,14 @@
3
  Plugin Name: TranslatePress - Multilingual
4
  Plugin URI: https://translatepress.com/
5
  Description: Experience a better way of translating your WordPress site using a visual front-end translation editor, with full support for WooCommerce and site builders.
6
- Version: 2.1.3
7
  Author: Cozmoslabs, Razvan Mocanu, Madalin Ungureanu, Cristophor Hurduban
8
  Author URI: https://cozmoslabs.com/
9
  Text Domain: translatepress-multilingual
10
  Domain Path: /languages
11
  License: GPL2
12
  WC requires at least: 2.5.0
13
- WC tested up to: 5.7.1
14
 
15
  == Copyright ==
16
  Copyright 2017 Cozmoslabs (www.cozmoslabs.com)
3
  Plugin Name: TranslatePress - Multilingual
4
  Plugin URI: https://translatepress.com/
5
  Description: Experience a better way of translating your WordPress site using a visual front-end translation editor, with full support for WooCommerce and site builders.
6
+ Version: 2.1.4
7
  Author: Cozmoslabs, Razvan Mocanu, Madalin Ungureanu, Cristophor Hurduban
8
  Author URI: https://cozmoslabs.com/
9
  Text Domain: translatepress-multilingual
10
  Domain Path: /languages
11
  License: GPL2
12
  WC requires at least: 2.5.0
13
+ WC tested up to: 5.8
14
 
15
  == Copyright ==
16
  Copyright 2017 Cozmoslabs (www.cozmoslabs.com)
languages/translatepress-multilingual.pot CHANGED
@@ -721,59 +721,59 @@ msgstr ""
721
  msgid "<strong>Warning:</strong> Some strings have possibly incorrectly encoded characters. This may result in breaking the queries, rendering the page untranslated in live mode. Consider revising the following strings or their method of outputting."
722
  msgstr ""
723
 
724
- #: includes/class-translation-render.php:150
725
  msgid "Description"
726
  msgstr ""
727
 
728
- #: includes/class-translation-render.php:156
729
  msgid "OG Title"
730
  msgstr ""
731
 
732
- #: includes/class-translation-render.php:162
733
  msgid "OG Site Name"
734
  msgstr ""
735
 
736
- #: includes/class-translation-render.php:168
737
  msgid "OG Description"
738
  msgstr ""
739
 
740
- #: includes/class-translation-render.php:174
741
  msgid "OG Image Alt"
742
  msgstr ""
743
 
744
- #: includes/class-translation-render.php:180
745
  msgid "Twitter Title"
746
  msgstr ""
747
 
748
- #: includes/class-translation-render.php:186
749
  msgid "Twitter Description"
750
  msgstr ""
751
 
752
- #: includes/class-translation-render.php:192
753
  msgid "Twitter Image Alt"
754
  msgstr ""
755
 
756
- #: includes/class-translation-render.php:196
757
  msgid "Page Title"
758
  msgstr ""
759
 
760
- #: includes/class-translation-render.php:202
761
  msgid "Dublin Core Title"
762
  msgstr ""
763
 
764
- #: includes/class-translation-render.php:208
765
  msgid "Dublin Core Description"
766
  msgstr ""
767
 
768
- #: includes/class-translation-render.php:214
769
  msgid "OG Image"
770
  msgstr ""
771
 
772
- #: includes/class-translation-render.php:220
773
  msgid "OG Image Secure URL"
774
  msgstr ""
775
 
776
- #: includes/class-translation-render.php:226
777
  msgid "Twitter Image"
778
  msgstr ""
779
 
721
  msgid "<strong>Warning:</strong> Some strings have possibly incorrectly encoded characters. This may result in breaking the queries, rendering the page untranslated in live mode. Consider revising the following strings or their method of outputting."
722
  msgstr ""
723
 
724
+ #: includes/class-translation-render.php:153
725
  msgid "Description"
726
  msgstr ""
727
 
728
+ #: includes/class-translation-render.php:159
729
  msgid "OG Title"
730
  msgstr ""
731
 
732
+ #: includes/class-translation-render.php:165
733
  msgid "OG Site Name"
734
  msgstr ""
735
 
736
+ #: includes/class-translation-render.php:171
737
  msgid "OG Description"
738
  msgstr ""
739
 
740
+ #: includes/class-translation-render.php:177
741
  msgid "OG Image Alt"
742
  msgstr ""
743
 
744
+ #: includes/class-translation-render.php:183
745
  msgid "Twitter Title"
746
  msgstr ""
747
 
748
+ #: includes/class-translation-render.php:189
749
  msgid "Twitter Description"
750
  msgstr ""
751
 
752
+ #: includes/class-translation-render.php:195
753
  msgid "Twitter Image Alt"
754
  msgstr ""
755
 
756
+ #: includes/class-translation-render.php:199
757
  msgid "Page Title"
758
  msgstr ""
759
 
760
+ #: includes/class-translation-render.php:205
761
  msgid "Dublin Core Title"
762
  msgstr ""
763
 
764
+ #: includes/class-translation-render.php:211
765
  msgid "Dublin Core Description"
766
  msgstr ""
767
 
768
+ #: includes/class-translation-render.php:217
769
  msgid "OG Image"
770
  msgstr ""
771
 
772
+ #: includes/class-translation-render.php:223
773
  msgid "OG Image Secure URL"
774
  msgstr ""
775
 
776
+ #: includes/class-translation-render.php:229
777
  msgid "Twitter Image"
778
  msgstr ""
779
 
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: translate, translation, multilingual, automatic translation, bilingual, fr
5
  Requires at least: 3.1.0
6
  Tested up to: 5.8.1
7
  Requires PHP: 5.6.20
8
- Stable tag: 2.1.3
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -140,6 +140,10 @@ For more information please check out our [documentation](https://translatepress
140
 
141
 
142
  == Changelog ==
 
 
 
 
143
  = 2.1.3 =
144
  * Added Opposite Language menu item when using 2 languages
145
  * Added support for opposite language in floating language switcher
5
  Requires at least: 3.1.0
6
  Tested up to: 5.8.1
7
  Requires PHP: 5.6.20
8
+ Stable tag: 2.1.4
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
140
 
141
 
142
  == Changelog ==
143
+ = 2.1.4 =
144
+ * Optimized automatic translation usage by improved recognition of cdata, js scripts and encoded html
145
+ * Fixed conflict with Ultimate Dashboard
146
+
147
  = 2.1.3 =
148
  * Added Opposite Language menu item when using 2 languages
149
  * Added support for opposite language in floating language switcher