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