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