| @@ -18,12 +18,10 @@ | ||
| 18 | 18 | */ |
| 19 | 19 | public function __construct( &$model ) { |
| 20 | 20 | parent::__construct( $model ); |
| 21 | 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 | + // Filrer the site url ( mainly to get the correct login form ) | |
| 23 | + add_filter( 'site_url', array( $this, 'site_url' ) ); | |
| 26 | 24 | } |
| 27 | 25 | |
| 28 | 26 | |
| 29 | 27 | /** |
| @@ -36,10 +34,10 @@ | ||
| 36 | 34 | * @param object $lang language |
| 37 | 35 | * @return string modified url |
| 38 | 36 | */ |
| 39 | 37 | 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 ); | |
| 38 | + if ( ! empty( $lang ) && ! empty( $this->options['domains'][ $lang->slug ] ) ) { | |
| 39 | + $url = str_replace( $this->home, $this->options['domains'][ $lang->slug ], $url ); | |
| 42 | 40 | } |
| 43 | 41 | return $url; |
| 44 | 42 | } |
| 45 | 43 | |
| @@ -52,15 +50,30 @@ | ||
| 52 | 50 | * @param string $url url to modify |
| 53 | 51 | * @return string modified url |
| 54 | 52 | */ |
| 55 | 53 | 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 ); | |
| 54 | + if ( ! empty( $this->options['domains'] ) ) { | |
| 55 | + $url = str_replace( ( is_ssl() ? 'https://' : 'http://' ) . parse_url( $url, PHP_URL_HOST ) . parse_url( $this->home, PHP_URL_PATH ), $this->home, $url ); | |
| 58 | 56 | } |
| 59 | 57 | return $url; |
| 60 | 58 | } |
| 61 | 59 | |
| 62 | 60 | /** |
| 61 | + * Returns the language based on language code in url | |
| 62 | + * links_model interface | |
| 63 | + * | |
| 64 | + * @since 1.2 | |
| 65 | + * @since 2.0 add $url argument | |
| 66 | + * | |
| 67 | + * @param string $url optional, defaults to current url | |
| 68 | + * @return string language slug | |
| 69 | + */ | |
| 70 | + public function get_language_from_url( $url = '' ) { | |
| 71 | + $host = empty( $url ) ? $_SERVER['HTTP_HOST'] : parse_url( $url, PHP_URL_HOST ); | |
| 72 | + return ( $lang = array_search( ( is_ssl() ? 'https://' : 'http://' ) . $host . parse_url( $this->home, PHP_URL_PATH ), $this->options['domains'] ) ) ? $lang : ''; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 63 | 76 | * Returns the home url |
| 64 | 77 | * links_model interface |
| 65 | 78 | * |
| 66 | 79 | * @since 1.3.1 |
| @@ -67,9 +80,9 @@ | ||
| 67 | 80 | * |
| 68 | 81 | * @param object $lang PLL_Language object |
| 69 | 82 | * @return string |
| 70 | 83 | */ |
| 71 | - public function home_url( $lang ) { | |
| 84 | + function home_url( $lang ) { | |
| 72 | 85 | return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] ); |
| 73 | 86 | } |
| 74 | 87 | |
| 75 | 88 | /** |
| @@ -80,18 +93,10 @@ | ||
| 80 | 93 | * @return array list of hosts |
| 81 | 94 | */ |
| 82 | 95 | public function get_hosts() { |
| 83 | 96 | $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 | - } | |
| 97 | + foreach ( $this->options['domains'] as $domain ) { | |
| 98 | + $hosts[] = parse_url( $domain, PHP_URL_HOST ); | |
| 93 | 99 | } |
| 94 | - | |
| 95 | 100 | return $hosts; |
| 96 | 101 | } |
| 97 | 102 | } |