PluginProbe
Polylang / 2.0.12
Polylang v2.0.12
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
← All changes | include/links-domain.php +29 -32 3.02.0.12 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Links model for use when using one domain per language
8 5 * for example mysite.com/sth and mysite.fr/qqch
@@ -12,15 +9,8 @@
12 9 */
13 10 class PLL_Links_Domain extends PLL_Links_Abstract_Domain {
14 11
15 12 /**
16 - * An array with language code as keys and the host as values.
17 - *
18 - * @var string[]
19 - */
20 - protected $hosts;
21 -
22 - /**
23 13 * Constructor
24 14 *
25 15 * @since 1.8
26 16 *
@@ -30,10 +20,10 @@
30 20 parent::__construct( $model );
31 21
32 22 $this->hosts = $this->get_hosts();
33 23
34 - // Filter the site url ( mainly to get the correct login form )
35 - add_filter( 'site_url', array( $this, 'site_url' ) );
24 + // Filrer the site url ( mainly to get the correct login form )
25 + add_filter( 'site_url', array( $this, 'site_url' ) );
36 26 }
37 27
38 28
39 29 /**
@@ -41,15 +31,15 @@
41 31 * links_model interface
42 32 *
43 33 * @since 1.2
44 34 *
45 - * @param string $url The url to modify.
46 - * @param PLL_Language $lang The language object.
47 - * @return string Modified url.
35 + * @param string $url url to modify
36 + * @param object $lang language
37 + * @return string modified url
48 38 */
49 39 public function add_language_to_link( $url, $lang ) {
50 40 if ( ! empty( $lang ) && ! empty( $this->hosts[ $lang->slug ] ) ) {
51 - $url = preg_replace( '#://(' . wp_parse_url( $this->home, PHP_URL_HOST ) . ')($|/.*)#', '://' . $this->hosts[ $lang->slug ] . '$2', $url );
41 + $url = str_replace( '://' . parse_url( $this->home, PHP_URL_HOST ), '://' . $this->hosts[ $lang->slug ], $url );
52 42 }
53 43 return $url;
54 44 }
55 45
@@ -63,45 +53,52 @@
63 53 * @return string modified url
64 54 */
65 55 public function remove_language_from_link( $url ) {
66 56 if ( ! empty( $this->hosts ) ) {
67 - $url = preg_replace( '#://(' . implode( '|', $this->hosts ) . ')($|/.*)#', '://' . wp_parse_url( $this->home, PHP_URL_HOST ) . '$2', $url );
57 + $url = preg_replace( '#:\/\/(' . implode( '|', $this->hosts ) . ')#', '://' . parse_url( $this->home, PHP_URL_HOST ), $url );
68 58 }
69 59 return $url;
70 60 }
71 61
72 62 /**
73 - * Returns the home url in a given language.
74 - * links_model interface.
63 + * Returns the language based on language code in url
64 + * links_model interface
75 65 *
66 + * @since 1.2
67 + * @since 2.0 add $url argument
68 + *
69 + * @param string $url optional, defaults to current url
70 + * @return string language slug
71 + */
72 + public function get_language_from_url( $url = '' ) {
73 + $host = empty( $url ) ? $_SERVER['HTTP_HOST'] : parse_url( $url, PHP_URL_HOST );
74 + return ( $lang = array_search( $host , $this->hosts ) ) ? $lang : '';
75 + }
76 +
77 + /**
78 + * Returns the home url
79 + * links_model interface
80 + *
76 81 * @since 1.3.1
77 82 *
78 - * @param PLL_Language $lang PLL_Language object.
83 + * @param object $lang PLL_Language object
79 84 * @return string
80 85 */
81 - public function home_url( $lang ) {
86 + function home_url( $lang ) {
82 87 return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] );
83 88 }
84 89
85 90 /**
86 - * Get hosts managed on the website.
91 + * Get hosts managed on the website
87 92 *
88 93 * @since 1.5
89 94 *
90 - * @return string[] List of hosts.
95 + * @return array list of hosts
91 96 */
92 97 public function get_hosts() {
93 98 $hosts = array();
94 99 foreach ( $this->options['domains'] as $lang => $domain ) {
95 - $host = wp_parse_url( $domain, PHP_URL_HOST );
96 - // idn_to_ascii is much faster than the WordPress method.
97 - if ( function_exists( 'idn_to_ascii' ) ) {
98 - // The use of the constant is mandatory in PHP 7.2 and PHP 7.3 to avoid a deprecated notice.
99 - $hosts[ $lang ] = defined( 'INTL_IDNA_VARIANT_UTS46' ) ? idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 ) : idn_to_ascii( $host );
100 - } else {
101 - $hosts[ $lang ] = Requests_IDNAEncoder::encode( $host );
102 - }
100 + $hosts[ $lang ] = parse_url( $domain, PHP_URL_HOST );
103 101 }
104 -
105 102 return $hosts;
106 103 }
107 104 }