| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Links model for use when using one domain per language |
| 5 |
* for example mysite.com/sth and mysite.fr/qqch |
| 6 |
* implements the "links_model interface" |
| 7 |
* |
| 8 |
* @since 1.2 |
| 9 |
*/ |
| 10 |
class PLL_Links_Domain extends PLL_Links_Abstract_Domain { |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor |
| 14 |
* |
| 15 |
* @since 1.8 |
| 16 |
* |
| 17 |
* @param object $model PLL_Model instance |
| 18 |
*/ |
| 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' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Adds the language code in url |
| 31 |
* links_model interface |
| 32 |
* |
| 33 |
* @since 1.2 |
| 34 |
* |
| 35 |
* @param string $url url to modify |
| 36 |
* @param object $lang language |
| 37 |
* @return string modified url |
| 38 |
*/ |
| 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 |
} |
| 43 |
return $url; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns the url without language code |
| 48 |
* links_model interface |
| 49 |
* |
| 50 |
* @since 1.2 |
| 51 |
* |
| 52 |
* @param string $url url to modify |
| 53 |
* @return string modified url |
| 54 |
*/ |
| 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; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the home url |
| 64 |
* links_model interface |
| 65 |
* |
| 66 |
* @since 1.3.1 |
| 67 |
* |
| 68 |
* @param object $lang PLL_Language object |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function home_url( $lang ) { |
| 72 |
return trailingslashit( empty( $this->options['domains'][ $lang->slug ] ) ? $this->home : $this->options['domains'][ $lang->slug ] ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get hosts managed on the website |
| 77 |
* |
| 78 |
* @since 1.5 |
| 79 |
* |
| 80 |
* @return array list of hosts |
| 81 |
*/ |
| 82 |
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 |
// The use of the constant is mandatory in PHP 7.2 and PHP 7.3 to avoid a deprecated notice. |
| 87 |
$hosts[ $lang ] = defined( 'INTL_IDNA_VARIANT_UTS46' ) ? idn_to_ascii( $host, 0, INTL_IDNA_VARIANT_UTS46 ) : idn_to_ascii( $host ); |
| 88 |
} |
| 89 |
|
| 90 |
return $hosts; |
| 91 |
} |
| 92 |
} |
| 93 |
|