PluginProbe
Polylang / 3.7.2
Polylang v3.7.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / links-domain.php

links-domain.php in Polylang 3.7.2, at include/links-domain.php

118 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Links model for use when using one domain per language
8 * for example mysite.com/something and mysite.fr/quelquechose.
9 *
10 * @since 1.2
11 */
12 class PLL_Links_Domain extends PLL_Links_Abstract_Domain {
13
14 /**
15 * An array with language code as keys and the host as values.
16 *
17 * @var string[]
18 */
19 protected $hosts;
20
21 /**
22 * Constructor.
23 *
24 * @since 1.8
25 *
26 * @param object $model PLL_Model instance.
27 */
28 public function __construct( &$model ) {
29 parent::__construct( $model );
30
31 $this->hosts = $this->get_hosts();
32
33 // Filters the site url (mainly to get the correct login form).
34 add_filter( 'site_url', array( $this, 'site_url' ) );
35 }
36
37
38 /**
39 * Switches the primary domain to a secondary domain in the url.
40 *
41 * @since 1.2
42 * @since 3.4 Accepts now a language slug.
43 *
44 * @param string $url The url to modify.
45 * @param PLL_Language|string|false $language Language object or slug.
46 * @return string The modified url.
47 */
48 public function add_language_to_link( $url, $language ) {
49 if ( $language instanceof PLL_Language ) {
50 $language = $language->slug;
51 }
52
53 if ( ! empty( $language ) && ! empty( $this->hosts[ $language ] ) ) {
54 $url = preg_replace( '#://(' . wp_parse_url( $this->home, PHP_URL_HOST ) . ')($|/.*)#', '://' . $this->hosts[ $language ] . '$2', $url );
55 }
56 return $url;
57 }
58
59 /**
60 * Returns the url with the primary domain.
61 *
62 * @since 1.2
63 *
64 * @param string $url The url to modify.
65 * @return string The modified url.
66 */
67 public function remove_language_from_link( $url ) {
68 if ( ! empty( $this->hosts ) ) {
69 $url = preg_replace( '#://(' . implode( '|', $this->hosts ) . ')($|/.*)#', '://' . wp_parse_url( $this->home, PHP_URL_HOST ) . '$2', $url );
70 }
71 return $url;
72 }
73
74 /**
75 * Returns the home url in a given language.
76 *
77 * @since 1.3.1
78 * @since 3.4 Accepts now a language slug.
79 *
80 * @param PLL_Language|string $language Language object or slug.
81 * @return string
82 */
83 public function home_url( $language ) {
84 if ( $language instanceof PLL_Language ) {
85 $language = $language->slug;
86 }
87
88 return trailingslashit( empty( $this->options['domains'][ $language ] ) ? $this->home : $this->options['domains'][ $language ] );
89 }
90
91 /**
92 * Get the hosts managed on the website.
93 *
94 * @since 1.5
95 *
96 * @return string[] List of hosts.
97 */
98 public function get_hosts() {
99 $hosts = array();
100 foreach ( (array) $this->options['domains'] as $lang => $domain ) {
101 $host = wp_parse_url( $domain, PHP_URL_HOST );
102
103 if ( ! is_string( $host ) ) {
104 continue;
105 }
106
107 // The function idn_to_ascii() is much faster than the WordPress method.
108 if ( function_exists( 'idn_to_ascii' ) && defined( 'INTL_IDNA_VARIANT_UTS46' ) ) {
109 $hosts[ $lang ] = (string) idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 );
110 } else {
111 $hosts[ $lang ] = \WpOrg\Requests\IdnaEncoder::encode( $host ); // Since WP 6.2.
112 }
113 }
114
115 return $hosts;
116 }
117 }
118