| @@ -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 |
| @@ -23,10 +20,10 @@ | ||
| 23 | 20 | parent::__construct( $model ); |
| 24 | 21 | |
| 25 | 22 | $this->hosts = $this->get_hosts(); |
| 26 | 23 | |
| 27 | - // Filter the site url ( mainly to get the correct login form ) | |
| 28 | - 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' ) ); | |
| 29 | 26 | } |
| 30 | 27 | |
| 31 | 28 | |
| 32 | 29 | /** |
| @@ -40,9 +37,9 @@ | ||
| 40 | 37 | * @return string modified url |
| 41 | 38 | */ |
| 42 | 39 | public function add_language_to_link( $url, $lang ) { |
| 43 | 40 | 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 ); | |
| 41 | + $url = str_replace( '://' . parse_url( $this->home, PHP_URL_HOST ), '://' . $this->hosts[ $lang->slug ], $url ); | |
| 45 | 42 | } |
| 46 | 43 | return $url; |
| 47 | 44 | } |
| 48 | 45 | |
| @@ -56,14 +53,29 @@ | ||
| 56 | 53 | * @return string modified url |
| 57 | 54 | */ |
| 58 | 55 | public function remove_language_from_link( $url ) { |
| 59 | 56 | if ( ! empty( $this->hosts ) ) { |
| 60 | - $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 ); | |
| 61 | 58 | } |
| 62 | 59 | return $url; |
| 63 | 60 | } |
| 64 | 61 | |
| 65 | 62 | /** |
| 63 | + * Returns the language based on language code in url | |
| 64 | + * links_model interface | |
| 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 | + /** | |
| 66 | 78 | * Returns the home url |
| 67 | 79 | * links_model interface |
| 68 | 80 | * |
| 69 | 81 | * @since 1.3.1 |
| @@ -70,9 +82,9 @@ | ||
| 70 | 82 | * |
| 71 | 83 | * @param object $lang PLL_Language object |
| 72 | 84 | * @return string |
| 73 | 85 | */ |
| 74 | - public function home_url( $lang ) { | |
| 86 | + function home_url( $lang ) { | |
| 75 | 87 | return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] ); |
| 76 | 88 | } |
| 77 | 89 | |
| 78 | 90 | /** |
| @@ -84,17 +96,9 @@ | ||
| 84 | 96 | */ |
| 85 | 97 | public function get_hosts() { |
| 86 | 98 | $hosts = array(); |
| 87 | 99 | 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 | - } | |
| 100 | + $hosts[ $lang ] = parse_url( $domain, PHP_URL_HOST ); | |
| 96 | 101 | } |
| 97 | - | |
| 98 | 102 | return $hosts; |
| 99 | 103 | } |
| 100 | 104 | } |