Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/functions/i18n.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + defined( 'ABSPATH' ) || exit;
4 +
5 + /**
6 + * Get all langs to display in admin bar for WPML
7 + *
8 + * @since 1.3.0
9 + *
10 + * @return array $langlinks List of active languages
11 + */
12 + function get_rocket_wpml_langs_for_admin_bar() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
13 + global $sitepress;
14 + $langlinks = [];
15 +
16 + // Making all languages the first option either when it's set or not.
17 + $langlinks[] = [
18 + 'code' => 'all',
19 + 'current' => 'all' === $sitepress->get_current_language(),
20 + 'anchor' => __( 'All languages', 'rocket' ),
21 + 'flag' => '<img class="icl_als_iclflag" src="' . ICL_PLUGIN_URL . '/res/img/icon16.png" alt="all" width="16" height="16" />',
22 + ];
23 +
24 + foreach ( $sitepress->get_active_languages() as $lang ) {
25 + // Get flag.
26 + $flag = $sitepress->get_flag( $lang['code'] );
27 + $flag_url = ICL_PLUGIN_URL . '/res/flags/' . $flag->flag;
28 +
29 + if ( $flag->from_template ) {
30 + $wp_upload_dir = wp_upload_dir();
31 + $flag_url = $wp_upload_dir['baseurl'] . '/flags/' . $flag->flag;
32 + }
33 +
34 + $langlinks[] = [
35 + 'code' => $lang['code'],
36 + 'current' => $lang['code'] === $sitepress->get_current_language(),
37 + 'anchor' => $lang['display_name'],
38 + 'flag' => '<img class="icl_als_iclflag" src="' . esc_url( $flag_url ) . '" alt="' . esc_attr( $lang['code'] ) . '" width="18" height="12" />',
39 + ];
40 + }
41 +
42 + return $langlinks;
43 + }
44 +
45 + /**
46 + * Get all langs to display in admin bar for qTranslate
47 + *
48 + * @since 2.7 add fork param
49 + * @since 1.3.5
50 + *
51 + * @param string $fork qTranslate fork name.
52 + * @return array $langlinks List of active languages
53 + */
54 + function get_rocket_qtranslate_langs_for_admin_bar( $fork = '' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
55 + global $q_config;
56 +
57 + $langlinks = [];
58 + $currentlang = [];
59 +
60 + foreach ( $q_config['enabled_languages'] as $lang ) {
61 +
62 + $langlinks[ $lang ] = [
63 + 'code' => $lang,
64 + 'anchor' => $q_config['language_name'][ $lang ],
65 + 'flag' => '<img src="' . esc_url( trailingslashit( WP_CONTENT_URL ) . $q_config['flag_location'] . $q_config['flag'][ $lang ] ) . '" alt="' . esc_attr( $q_config['language_name'][ $lang ] ) . '" width="18" height="12" />',
66 + ];
67 +
68 + }
69 +
70 + if ( isset( $_GET['lang'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
71 + $lang = sanitize_key( $_GET['lang'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
72 +
73 + if ( 'x' === $fork ) {
74 + if ( qtranxf_isEnabled( $lang ) ) {
75 + $currentlang[ $lang ] = $langlinks[ $lang ];
76 + unset( $langlinks[ $lang ] );
77 + $langlinks = $currentlang + $langlinks;
78 + }
79 + } elseif ( qtrans_isEnabled( $lang ) ) {
80 + $currentlang[ $lang ] = $langlinks[ $lang ];
81 + unset( $langlinks[ $lang ] );
82 + $langlinks = $currentlang + $langlinks;
83 + }
84 + }
85 +
86 + return $langlinks;
87 + }
88 +
89 + /**
90 + * Get all langs to display in admin bar for Polylang
91 + *
92 + * @since 2.2
93 + *
94 + * @return array $langlinks List of active languages
95 + */
96 + function get_rocket_polylang_langs_for_admin_bar() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
97 + global $polylang;
98 +
99 + $langlinks = [];
100 + $currentlang = [];
101 + $langs = [];
102 + $img = '';
103 +
104 + $pll = function_exists( 'PLL' ) ? PLL() : $polylang;
105 +
106 + if ( isset( $pll ) ) {
107 + $langs = $pll->model->get_languages_list();
108 +
109 + if ( ! empty( $langs ) ) {
110 + foreach ( $langs as $lang ) {
111 + if ( ! empty( $lang->flag ) ) {
112 + $img = strpos( $lang->flag, 'img' ) !== false ? $lang->flag . '&nbsp;' : $lang->flag;
113 + }
114 +
115 + if ( isset( $pll->curlang->slug ) && $lang->slug === $pll->curlang->slug ) {
116 + $currentlang[ $lang->slug ] = [
117 + 'code' => $lang->slug,
118 + 'anchor' => $lang->name,
119 + 'flag' => $img,
120 + ];
121 + } else {
122 + $langlinks[ $lang->slug ] = [
123 + 'code' => $lang->slug,
124 + 'anchor' => $lang->name,
125 + 'flag' => $img,
126 + ];
127 + }
128 + }
129 + }
130 + }
131 +
132 + return $currentlang + $langlinks;
133 + }
134 +
135 + /**
136 + * Tell if a translation plugin is activated.
137 + *
138 + * @since 2.0
139 + * @since 3.2.1 Return an identifier on success instead of true.
140 + *
141 + * @return string An identifier corresponding to the active plugin.
142 + */
143 + function rocket_has_i18n() {
144 + global $sitepress, $q_config, $polylang;
145 +
146 + if ( ! empty( $sitepress ) && is_object( $sitepress ) && method_exists( $sitepress, 'get_active_languages' ) ) {
147 + // WPML.
148 + return 'wpml';
149 + }
150 +
151 + if ( ! empty( $polylang ) && function_exists( 'pll_languages_list' ) ) {
152 + $languages = pll_languages_list();
153 +
154 + if ( empty( $languages ) ) {
155 + return '';
156 + }
157 +
158 + // Polylang, Polylang Pro.
159 + return 'polylang';
160 + }
161 +
162 + if ( ! empty( $q_config ) && is_array( $q_config ) ) {
163 + if ( function_exists( 'qtranxf_convertURL' ) ) {
164 + // qTranslate-x.
165 + return 'qtranslate-x';
166 + }
167 +
168 + if ( function_exists( 'qtrans_convertURL' ) ) {
169 + // qTranslate.
170 + return 'qtranslate';
171 + }
172 + }
173 +
174 + $identifier = '';
175 + $default = $identifier;
176 +
177 + /**
178 + * Filters the value of i18n plugin detection
179 + *
180 + * @param string $identifier An identifier value.
181 + */
182 + $identifier = apply_filters( 'rocket_has_i18n', $identifier );
183 +
184 + if ( ! is_string( $identifier ) ) {
185 + $identifier = $default;
186 + }
187 +
188 + return $identifier;
189 + }
190 +
191 + /**
192 + * Get infos of all active languages.
193 + *
194 + * @since 2.0
195 + *
196 + * @return array A list of language codes.
197 + */
198 + function get_rocket_i18n_code() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
199 + $i18n_plugin = rocket_has_i18n();
200 +
201 + if ( ! $i18n_plugin ) {
202 + return [];
203 + }
204 +
205 + if ( 'wpml' === $i18n_plugin ) {
206 + // WPML.
207 + return array_keys( $GLOBALS['sitepress']->get_active_languages() );
208 + }
209 +
210 + if ( 'qtranslate' === $i18n_plugin || 'qtranslate-x' === $i18n_plugin ) {
211 + // qTranslate, qTranslate-x.
212 + return ! empty( $GLOBALS['q_config']['enabled_languages'] ) ? $GLOBALS['q_config']['enabled_languages'] : [];
213 + }
214 +
215 + if ( 'polylang' === $i18n_plugin ) {
216 + // Polylang, Polylang Pro.
217 + return pll_languages_list();
218 + }
219 +
220 + $codes = [];
221 + $default = $codes;
222 +
223 + /**
224 + * Filters the active languages codes list
225 + *
226 + * @param array $codes Array of languages codes.
227 + */
228 + $codes = apply_filters( 'rocket_get_i18n_code', $codes );
229 +
230 + if ( ! is_array( $codes ) ) {
231 + $codes = $default;
232 + }
233 +
234 + return $codes;
235 + }
236 +
237 + /**
238 + * Get all active languages host
239 + *
240 + * @since 2.6.8
241 + *
242 + * @return array $urls List of all active languages host
243 + */
244 + function get_rocket_i18n_host() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
245 + $langs_host = [];
246 + $langs = get_rocket_i18n_uri();
247 +
248 + if ( empty( $langs ) ) {
249 + return $langs_host;
250 + }
251 +
252 + foreach ( $langs as $lang ) {
253 + $langs_host[] = wp_parse_url( $lang, PHP_URL_HOST );
254 + }
255 +
256 + return $langs_host;
257 + }
258 +
259 + /**
260 + * Get all active languages URI.
261 + *
262 + * @since 2.0
263 + *
264 + * @return array $urls List of all active languages URI.
265 + */
266 + function get_rocket_i18n_uri() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
267 + $i18n_plugin = rocket_has_i18n();
268 + $urls = [];
269 +
270 + if ( 'wpml' === $i18n_plugin ) {
271 + // WPML.
272 + foreach ( get_rocket_i18n_code() as $lang ) {
273 + $urls[] = $GLOBALS['sitepress']->language_url( $lang );
274 + }
275 + } elseif ( 'qtranslate' === $i18n_plugin || 'qtranslate-x' === $i18n_plugin ) {
276 + // qTranslate, qTranslate-x.
277 + foreach ( get_rocket_i18n_code() as $lang ) {
278 + if ( 'qtranslate' === $i18n_plugin ) {
279 + $urls[] = qtrans_convertURL( home_url(), $lang, true );
280 + } else {
281 + $urls[] = qtranxf_convertURL( home_url(), $lang, true );
282 + }
283 + }
284 + } elseif ( 'polylang' === $i18n_plugin ) {
285 + // Polylang, Polylang Pro.
286 + $pll = function_exists( 'PLL' ) ? PLL() : $GLOBALS['polylang'];
287 +
288 + if ( ! empty( $pll ) && is_object( $pll ) ) {
289 + if ( ! defined( 'POLYLANG_VERSION' ) || version_compare( POLYLANG_VERSION, '3.4', '<' ) ) {
290 + $urls = wp_list_pluck( $pll->model->get_languages_list(), 'search_url' );
291 + }else {
292 + $languages = $pll->model->get_languages_list();
293 + foreach ( $languages as $language ) {
294 + $urls[] = $language->get_home_url();
295 + }
296 + }
297 + }
298 + }
299 +
300 + /**
301 + * Filters the value of all active languages URI
302 + *
303 + * @param array Array of active languages URI.
304 + */
305 + $urls = apply_filters( 'rocket_get_i18n_uri', $urls );
306 +
307 + if (
308 + ! is_array( $urls )
309 + ||
310 + empty( $urls )
311 + ) {
312 + $urls[] = home_url();
313 + }
314 +
315 + return $urls;
316 + }
317 +
318 + /**
319 + * Get directories paths to preserve languages ​​when purging a domain.
320 + * This function is required when the domains of languages (​​other than the default) are managed by subdirectories.
321 + * By default, when you clear the cache of the french website with the domain example.com, all subdirectory like /en/
322 + * and /de/ are deleted. But, if you have a domain for your english and german websites with example.com/en/ and
323 + * example.com/de/, you want to keep the /en/ and /de/ directory when the french domain is cleared.
324 + *
325 + * @since 3.5.5 Normalize paths + micro-optimization by passing in the cache path.
326 + * @since 2.0
327 + *
328 + * @param string $current_lang The current language code.
329 + * @param string $cache_path Optional. WP Rocket's cache path.
330 + *
331 + * @return array A list of directories path to preserve.
332 + */
333 + function get_rocket_i18n_to_preserve( $current_lang, $cache_path = '' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
334 + // Must not be an empty string.
335 + if ( empty( $current_lang ) ) {
336 + return [];
337 + }
338 +
339 + // Must not be anything else but a string.
340 + if ( ! is_string( $current_lang ) ) {
341 + return [];
342 + }
343 +
344 + $i18n_plugin = rocket_has_i18n();
345 + if ( ! $i18n_plugin ) {
346 + return [];
347 + }
348 +
349 + $langs = get_rocket_i18n_code();
350 + if ( empty( $langs ) ) {
351 + return [];
352 + }
353 +
354 + // Remove current lang to the preserve dirs.
355 + $langs = array_diff( $langs, [ $current_lang ] );
356 +
357 + if ( '' === $cache_path ) {
358 + $cache_path = _rocket_get_wp_rocket_cache_path();
359 + }
360 +
361 + // Stock all URLs of langs to preserve.
362 + $langs_to_preserve = [];
363 + foreach ( $langs as $lang ) {
364 + $parse_url = get_rocket_parse_url( get_rocket_i18n_home_url( $lang ) );
365 + $langs_to_preserve[] = _rocket_normalize_path(
366 + "{$cache_path}{$parse_url['host']}(.*)/" . trim( $parse_url['path'], '/' ),
367 + true // escape directory separators for regex.
368 + );
369 + }
370 +
371 + /**
372 + * Filter directories path to preserve of cache purge.
373 + *
374 + * @since 2.1
375 + *
376 + * @param array $langs_to_preserve List of directories path to preserve.
377 + */
378 + return (array) apply_filters( 'rocket_langs_to_preserve', $langs_to_preserve );
379 + }
380 +
381 + /**
382 + * Get all languages subdomains URLs
383 + *
384 + * @since 2.1
385 + *
386 + * @return array $urls List of languages subdomains URLs
387 + */
388 + function get_rocket_i18n_subdomains() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
389 + $i18n_plugin = rocket_has_i18n();
390 +
391 + if ( ! $i18n_plugin ) {
392 + return [];
393 + }
394 +
395 + $urls = [];
396 + $default = $urls;
397 +
398 + switch ( $i18n_plugin ) {
399 + // WPML.
400 + case 'wpml':
401 + $option = get_option( 'icl_sitepress_settings' );
402 +
403 + if ( 2 === (int) $option['language_negotiation_type'] ) {
404 + $urls = get_rocket_i18n_uri();
405 + }
406 + break;
407 + // qTranslate.
408 + case 'qtranslate':
409 + if ( 3 === (int) $GLOBALS['q_config']['url_mode'] ) {
410 + $urls = get_rocket_i18n_uri();
411 + }
412 + break;
413 + // qTranslate-x.
414 + case 'qtranslate-x':
415 + if ( 3 === (int) $GLOBALS['q_config']['url_mode'] || 4 === (int) $GLOBALS['q_config']['url_mode'] ) {
416 + $urls = get_rocket_i18n_uri();
417 + }
418 + break;
419 + // Polylang, Polylang Pro.
420 + case 'polylang':
421 + $pll = function_exists( 'PLL' ) ? PLL() : $GLOBALS['polylang'];
422 +
423 + if ( ! empty( $pll ) && is_object( $pll ) && ( 2 === (int) $pll->options['force_lang'] || 3 === (int) $pll->options['force_lang'] ) ) {
424 + $urls = get_rocket_i18n_uri();
425 + }
426 + break;
427 + default:
428 + /**
429 + * Filters the list of languages subdomains URLs
430 + *
431 + * @param array $urls Array of languages subdomains URLs.
432 + */
433 + $urls = apply_filters( 'rocket_i18n_subdomains', $urls );
434 +
435 + if ( ! is_array( $urls ) ) {
436 + $urls = $default;
437 + }
438 + }
439 +
440 + return $urls;
441 + }
442 +
443 + /**
444 + * Get home URL of a specific lang.
445 + *
446 + * @since 2.2
447 + *
448 + * @param string $lang The language code. Default is an empty string.
449 + * @return string $url
450 + */
451 + function get_rocket_i18n_home_url( $lang = '' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
452 + $i18n_plugin = rocket_has_i18n();
453 +
454 + $home_url = home_url();
455 + $default = $home_url;
456 +
457 + if ( ! $i18n_plugin ) {
458 + return $home_url;
459 + }
460 +
461 + switch ( $i18n_plugin ) {
462 + // WPML.
463 + case 'wpml':
464 + $home_url = $GLOBALS['sitepress']->language_url( $lang );
465 + break;
466 + // qTranslate.
467 + case 'qtranslate':
468 + $home_url = qtrans_convertURL( home_url(), $lang, true );
469 + break;
470 + // qTranslate-x.
471 + case 'qtranslate-x':
472 + $home_url = qtranxf_convertURL( home_url(), $lang, true );
473 + break;
474 + // Polylang, Polylang Pro.
475 + case 'polylang':
476 + $pll = function_exists( 'PLL' ) ? PLL() : $GLOBALS['polylang'];
477 +
478 + if ( ! empty( $pll->options['force_lang'] ) && isset( $pll->links ) ) {
479 + $home_url = pll_home_url( $lang );
480 + }
481 + break;
482 + default:
483 + /**
484 + * Filters the home URL value for a specific language
485 + *
486 + * @param string $home_url Home URL.
487 + * @param string $lang The language code.
488 + */
489 + $home_url = apply_filters( 'rocket_i18n_home_url', $home_url, $lang );
490 +
491 + if ( ! is_string( $home_url ) ) {
492 + $home_url = $default;
493 + }
494 + }
495 +
496 + return $home_url;
497 + }
498 +
499 + /**
500 + * Get all translated path of a specific post with ID.
501 + *
502 + * @since 2.4
503 + *
504 + * @param int $post_id Post ID.
505 + * @param string $post_type Post Type.
506 + * @param string $regex Regex to include at the end.
507 + * @return array
508 + */
509 + function get_rocket_i18n_translated_post_urls( $post_id, $post_type = 'page', $regex = '' ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
510 + $url = get_permalink( $post_id );
511 + $path = wp_parse_url( $url, PHP_URL_PATH );
512 +
513 + if ( empty( $path ) ) {
514 + return [];
515 + }
516 +
517 + $i18n_plugin = rocket_has_i18n();
518 +
519 + $urls = [];
520 + $default = $urls;
521 +
522 + switch ( $i18n_plugin ) {
523 + // WPML.
524 + case 'wpml':
525 + $langs = get_rocket_i18n_code();
526 +
527 + if ( $langs ) {
528 + foreach ( $langs as $lang ) {
529 + $urls[] = wp_parse_url( get_permalink( icl_object_id( $post_id, $post_type, true, $lang ) ), PHP_URL_PATH ) . $regex;
530 + }
531 + }
532 + break;
533 + // qTranslate & qTranslate-x.
534 + case 'qtranslate':
535 + case 'qtranslate-x':
536 + $langs = $GLOBALS['q_config']['enabled_languages'];
537 + $langs = array_diff( $langs, [ $GLOBALS['q_config']['default_language'] ] );
538 + $urls[] = wp_parse_url( $url, PHP_URL_PATH ) . $regex;
539 +
540 + if ( $langs ) {
541 + foreach ( $langs as $lang ) {
542 + if ( 'qtranslate' === $i18n_plugin ) {
543 + $urls[] = wp_parse_url( qtrans_convertURL( $url, $lang, true ), PHP_URL_PATH ) . $regex;
544 + } elseif ( 'qtranslate-x' === $i18n_plugin ) {
545 + $urls[] = wp_parse_url( qtranxf_convertURL( $url, $lang, true ), PHP_URL_PATH ) . $regex;
546 + }
547 + }
548 + }
549 + break;
550 + // Polylang.
551 + case 'polylang':
552 + if ( function_exists( 'PLL' ) && is_object( PLL()->model ) ) {
553 + $translations = pll_get_post_translations( $post_id );
554 + } elseif ( ! empty( $GLOBALS['polylang']->model ) && is_object( $GLOBALS['polylang']->model ) ) {
555 + $translations = $GLOBALS['polylang']->model->get_translations( 'page', $post_id );
556 + }
557 +
558 + if ( ! empty( $translations ) ) {
559 + foreach ( $translations as $translation_post_id ) {
560 + $urls[] = wp_parse_url( get_permalink( $translation_post_id ), PHP_URL_PATH ) . $regex;
561 + }
562 + }
563 + break;
564 + default:
565 + /**
566 + * Filters the list of translated URLs for a post ID
567 + *
568 + * @param array $urls Array of translated URLs.
569 + * @param string $url URL to use.
570 + * @param string $post_type Post type.
571 + * @param string $regex Pattern to include at the end.
572 + */
573 + $urls = apply_filters( 'rocket_i18n_translated_post_urls', $urls, $url, $post_type, $regex );
574 +
575 + if ( ! is_array( $urls ) ) {
576 + $urls = $default;
577 + }
578 + }
579 +
580 + if ( trim( $path, '/' ) !== '' ) {
581 + $urls[] = $path . $regex;
582 + }
583 +
584 + return array_unique( $urls );
585 + }
586 +
587 + /**
588 + * Returns the home URL, without WPML filters if the plugin is active
589 + *
590 + * @since 3.2.4
591 + *
592 + * @param string $path Path to add to the home URL.
593 + * @return string
594 + */
595 + function rocket_get_home_url( $path = '' ) {
596 + global $wpml_url_filters;
597 + static $home_url = [];
598 + static $has_wpml;
599 +
600 + if ( isset( $home_url[ $path ] ) ) {
601 + return $home_url[ $path ];
602 + }
603 +
604 + if ( ! isset( $has_wpml ) ) {
605 + $has_wpml = $wpml_url_filters && is_object( $wpml_url_filters ) && method_exists( $wpml_url_filters, 'home_url_filter' );
606 + }
607 +
608 + if ( $has_wpml ) {
609 + remove_filter( 'home_url', [ $wpml_url_filters, 'home_url_filter' ], -10 );
610 + }
611 +
612 + $home_url[ $path ] = home_url( $path );
613 +
614 + if ( $has_wpml ) {
615 + add_filter( 'home_url', [ $wpml_url_filters, 'home_url_filter' ], -10, 4 );
616 + }
617 +
618 + return $home_url[ $path ];
619 + }
620 +
621 + /**
622 + * Gets the current language
623 + *
624 + * @since 3.3.3
625 + *
626 + * @return string
627 + */
628 + function rocket_get_current_language() {
629 + $i18n_plugin = rocket_has_i18n();
630 +
631 + if ( ! $i18n_plugin ) {
632 + return '';
633 + }
634 +
635 + $current_language = '';
636 + $default = $current_language;
637 +
638 + if ( 'polylang' === $i18n_plugin && function_exists( 'pll_current_language' ) ) {
639 + return pll_current_language();
640 + } elseif ( 'wpml' === $i18n_plugin ) {
641 + return apply_filters( 'wpml_current_language', null ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
642 + }
643 +
644 + /**
645 + * Filters the current language value
646 + *
647 + * @param string $current_language Current language.
648 + */
649 + $current_language = apply_filters( 'rocket_i18n_current_language', $current_language );
650 +
651 + if ( ! is_string( $current_language ) ) {
652 + $current_language = $default;
653 + }
654 +
655 + return $current_language;
656 + }
657 +