PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.0.3
Translate and Go multilingual – Automatic AI translation – wpLingua v1.0.3
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 / url.php

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

267 lines 5.6 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 * Get the translated URL
11 *
12 * @param string $url
13 * @param string $language_target_id
14 * @return string
15 */
16 function wplng_url_translate( $url, $language_target_id = '' ) {
17
18 // Check if URL is an empty string
19 if ( '' === $url ) {
20 return '';
21 }
22
23 // Check if URL is translatable (exclude /admin/, ...)
24 if ( ! wplng_url_is_translatable( $url ) ) {
25 return $url;
26 }
27
28 // Check if it's an WooCommece AJAX URL
29 if ( str_contains( $url, '?wc-ajax=' ) ) {
30 return $url;
31 }
32
33 // Check if URL is an anchor link for the current page
34 if ( '#' === substr( $url, 0, 1 ) ) {
35 return $url;
36 }
37
38 $languages_target = wplng_get_languages_target();
39
40 if ( '' === $language_target_id ) {
41 $language_target_id = wplng_get_language_current_id();
42 }
43
44 $preg_domain = '';
45 $parsed_url = wp_parse_url( home_url() );
46
47 if ( isset( $parsed_url['host'] )
48 && is_string( $parsed_url['host'] )
49 ) {
50 $preg_domain = preg_quote( $parsed_url['host'] );
51 }
52
53 if ( ! empty( $preg_domain )
54 && preg_match( '#^(http:\/\/|https:\/\/)?' . $preg_domain . '(.*)$#', $url )
55 ) {
56
57 // Check if URL is already translated
58 foreach ( $languages_target as $language_target ) {
59 if ( str_contains( $url, '/' . $language_target['id'] . '/' ) ) {
60 return $url;
61 }
62 }
63
64 $url = preg_replace(
65 '#^(http:\/\/|https:\/\/)?' . $preg_domain . '(.*)$#',
66 '$1' . $preg_domain . '/' . $language_target_id . '$2',
67 $url
68 );
69
70 $url = trailingslashit( $url );
71
72 } elseif ( preg_match( '#^[^\/]+\/[^\/].*$|^\/[^\/].*$#', $url ) ) {
73
74 // Check if URL is already translated
75 foreach ( $languages_target as $language_target ) {
76 if ( substr( $url, 0, 4 ) == '/' . $language_target['id'] . '/'
77 || substr( $url, 0, 3 ) == $language_target['id'] . '/'
78 ) {
79 return $url;
80 }
81 }
82
83 if ( '/' === substr( $url, 0, 1 ) ) {
84 $url = '/' . $language_target_id . $url;
85 } else {
86 $url = $language_target_id . '/' . $url;
87 }
88 }
89
90 $url = apply_filters(
91 'wplng_url_translate',
92 $url
93 );
94
95 return $url;
96 }
97
98
99 /**
100 * Return true is $url is translatable
101 *
102 * @param string $url
103 * @return bool
104 */
105 function wplng_url_is_translatable( $url = '' ) {
106
107 global $wplng_request_uri;
108 $is_translatable = true;
109
110 // Get current URL if $url is empty
111 if ( empty( $url ) ) {
112 $url = sanitize_url( $wplng_request_uri );
113 }
114
115 $url = wp_make_link_relative( $url );
116
117 // Check if is an admin page
118 if ( str_contains( $url, wp_make_link_relative( get_admin_url() ) ) ) {
119 $is_translatable = false;
120 }
121
122 // Check if is wp-comments-post.php
123 if ( $is_translatable
124 && str_contains( $url, 'wp-comments-post.php' )
125 ) {
126 $is_translatable = false;
127 }
128
129 // Check if is in wp-uploads
130 if (
131 $is_translatable
132 && str_contains( $url, wp_make_link_relative( content_url() ) )
133 ) {
134 $is_translatable = false;
135 }
136
137 // Exclude files URL
138 $regex_is_file = '#\.(avi|css|doc|exe|gif|html|jpg|jpeg|mid|midi|mp3|mpg|mpeg|mov|qt|pdf|png|ram|rar|tiff|txt|wav|zip|ico)$#Uis';
139 if ( $is_translatable && preg_match( $regex_is_file, $url ) ) {
140 $is_translatable = false;
141 }
142
143 // Check if URL is excluded in option page
144 if ( $is_translatable ) {
145
146 $url_original = wplng_get_url_original( $url );
147 $url_exclude_regex = wplng_get_url_exclude_regex();
148
149 foreach ( $url_exclude_regex as $regex ) {
150 if ( preg_match( $regex, $url_original ) ) {
151 $is_translatable = false;
152 break;
153 }
154 }
155 }
156
157 $is_translatable = apply_filters(
158 'wplng_url_is_translatable',
159 $is_translatable,
160 $url
161 );
162
163 return $is_translatable;
164 }
165
166
167 /**
168 * Get the REGEX list of excluded URLs
169 *
170 * @return array
171 */
172 function wplng_get_url_exclude_regex() {
173
174 $url_exclude = array();
175
176 // Get, check and sanitize excluded URL REGEX as string
177 $option = get_option( 'wplng_excluded_url' );
178 if ( empty( $option ) || ! is_string( $option ) ) {
179 $option = '';
180 } else {
181 $option = sanitize_textarea_field( $option );
182 }
183
184 // Get exclude URL REGEX as array
185 $option = explode( PHP_EOL, $option );
186
187 // Check each URL REGEX
188 foreach ( $option as $regex ) {
189 $regex = trim( $regex );
190 if ( '' !== $regex ) {
191 $url_exclude[] = '#' . $regex . '#';
192 }
193 }
194
195 // Remove duplicate
196 $url_exclude = array_unique( $url_exclude );
197
198 // Apply wplng_url_exclude_regex filter
199 $url_exclude = apply_filters(
200 'wplng_url_exclude_regex',
201 $url_exclude
202 );
203
204 return $url_exclude;
205 }
206
207
208 /**
209 * Get the untranslated / original URL
210 *
211 * @param string $url
212 * @return string
213 */
214 function wplng_get_url_original( $url = '' ) {
215
216 if ( '' === $url ) {
217 $url = wplng_get_url_current();
218 }
219
220 $target = wplng_get_languages_target_ids();
221
222 foreach ( $target as $target_id ) {
223 $url = str_replace( '/' . $target_id . '/', '/', $url );
224 }
225
226 $url = esc_url( $url );
227
228 $url = apply_filters(
229 'wplng_url_original',
230 $url
231 );
232
233 return $url;
234 }
235
236
237 /**
238 * Get current URL
239 *
240 * @return string
241 */
242 function wplng_get_url_current() {
243 global $wplng_request_uri;
244 return home_url( $wplng_request_uri );
245 }
246
247
248 /**
249 * Get the URL translated in specified language
250 *
251 * @param string $language_id
252 * @return string
253 */
254 function wplng_get_url_current_for_language( $language_id ) {
255
256 global $wplng_request_uri;
257 $path = wplng_get_url_original( $wplng_request_uri );
258
259 if ( wplng_url_is_translatable( $path )
260 && ( wplng_get_language_website_id() !== $language_id )
261 ) {
262 $path = '/' . $language_id . $path;
263 }
264
265 return home_url( $path );
266 }
267