PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.0.4
Translate and Go multilingual – Automatic AI translation – wpLingua v1.0.4
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / util.php

util.php in Translate and Go multilingual – Automatic AI translation – wpLingua 1.0.4, at inc/util.php

210 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Return true is $str is an URL
11 *
12 * @param string $str
13 * @return bool
14 */
15 function wplng_str_is_url( $str ) {
16
17 $parsed = wp_parse_url( $str );
18 $is_url = false;
19
20 if ( is_string( $str )
21 && ( '' !== trim( $str ) )
22 && ( false !== strpos( $str, '/' ) )
23 ) {
24 if ( isset( $parsed['scheme'] )
25 && (
26 ( 'https' === $parsed['scheme'] )
27 || ( 'http' === $parsed['scheme'] )
28 )
29 ) {
30 // URL has http/https/...
31 $is_url = ! ( filter_var( $str, FILTER_VALIDATE_URL ) === false );
32 } else {
33 // PHP filter_var does not support relative urls, so we simulate a full URL
34 $is_url = ( filter_var( 'https://website.com/' . ltrim( $str, '/' ), FILTER_VALIDATE_URL ) !== false );
35 }
36 }
37
38 return $is_url;
39 }
40
41
42 /**
43 * Return true is $str is a translatable text
44 * Return false if $str is a number, mail addredd, symbol, ...
45 *
46 * @param string $text
47 * @return bool
48 */
49 function wplng_text_is_translatable( $text ) {
50
51 // Check if it's a mail address
52 if ( filter_var( $text, FILTER_VALIDATE_EMAIL ) ) {
53 return false;
54 }
55
56 // Get letters only
57 $letters = $text;
58 $letters = html_entity_decode( $letters );
59 $letters = preg_replace( '#[^\p{L}\p{N}]#u', '', $letters );
60 $letters = preg_replace( '#[\d\s]#u', '', $letters );
61
62 return ! empty( $letters );
63 }
64
65
66 /**
67 * Escape texte (used for comparison)
68 *
69 * @param string $text
70 * @return string
71 */
72 function wplng_text_esc( $text ) {
73
74 $text = html_entity_decode( $text );
75 $text = esc_html( $text );
76 $text = esc_attr( $text );
77 $text = str_replace( '\\', '', $text );
78 $text = preg_replace( '#\s+#', ' ', $text );
79 $text = trim( $text );
80
81 return $text;
82 }
83
84
85 /**
86 * Return true if $str is HTML
87 *
88 * @param string $str
89 * @return string
90 */
91 function wplng_str_is_html( $str ) {
92 return $str !== wp_strip_all_tags( $str );
93 }
94
95
96 /**
97 * Return true is $str is a JSON
98 *
99 * @param string $str
100 * @return string
101 */
102 function wplng_str_is_json( $str ) {
103 $decoded = json_decode( $str, true );
104 return ( json_last_error() === JSON_ERROR_NONE ) && is_array( $decoded );
105 }
106
107
108 /**
109 * Return true if $str is a local ID
110 * Ex: fr_FR, fr, FR, ...
111 *
112 * @param string $str
113 * @return bool
114 */
115 function wplng_str_is_locale_id( $str ) {
116
117 $locale = get_locale();
118 $locales = array(
119 $locale, // Ex: fr_FR
120 strtolower( $locale ), // Ex: fr_fr
121 str_replace( '_', '-', $locale ), // Ex: fr-FR
122 strtolower( str_replace( '_', '-', $locale ) ), // Ex: fr-fr
123 substr( $locale, 0, 2 ), // Ex: FR
124 strtolower( substr( $locale, 0, 2 ) ), // Ex: fr
125 );
126
127 return in_array( $str, $locales );
128 }
129
130
131 /**
132 * Return true if a JSON string element is translatable
133 *
134 * @param string $element
135 * @param array $parents
136 * @return bool
137 */
138 function wplng_json_element_is_translatable( $element, $parents ) {
139
140 $is_translatable = false;
141 $json_excluded = wplng_data_excluded_json();
142 $json_to_translate = wplng_data_json_to_translate();
143
144 if ( in_array( $parents, $json_excluded ) ) {
145
146 /**
147 * Is an excluded JSON
148 */
149
150 $is_translatable = false;
151
152 } elseif ( in_array( $parents, $json_to_translate ) ) {
153
154 /**
155 * Is an included JSON
156 */
157
158 $is_translatable = true;
159
160 } else {
161
162 if (
163 ! empty( $parents[0] )
164 && ( '@graph' === $parents[0] )
165 && ( count( $parents ) > 2 )
166 && (
167 (
168 ( 'logo' === $parents[ count( $parents ) - 2 ] )
169 && ( 'caption' === $parents[ count( $parents ) - 1 ] )
170 )
171 || ( 'name' === $parents[ count( $parents ) - 1 ] )
172 )
173 ) {
174
175 /**
176 * Is schema-graph
177 */
178
179 $is_translatable = true;
180
181 } elseif (
182 ! empty( $parents[0] )
183 && ( 'wc_address_i18n_params' === $parents[0] )
184 && ( count( $parents ) > 1 )
185 && (
186 ( 'placeholder' === $parents[ count( $parents ) - 1 ] )
187 || ( 'label' === $parents[ count( $parents ) - 1 ] )
188 )
189 ) {
190
191 /**
192 * Is WooCommerce address params
193 */
194
195 $is_translatable = true;
196 }
197
198 if ( ! wplng_text_is_translatable( $element ) ) {
199 $is_translatable = false;
200 }
201 }
202
203 return apply_filters(
204 'wplng_json_element_is_translatable',
205 $is_translatable,
206 $element,
207 $parents
208 );
209 }
210