| 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 { |
| 11 |
public $model, $options; |
| 12 |
protected $home; |
| 13 |
|
| 14 |
/* |
| 15 |
* constructor |
| 16 |
* |
| 17 |
* @since 1.2 |
| 18 |
* |
| 19 |
* @param object $model PLL_Model instance |
| 20 |
*/ |
| 21 |
public function __construct($model) { |
| 22 |
$this->model = &$model; |
| 23 |
$this->options = &$model->options; |
| 24 |
|
| 25 |
$this->home = get_option('home'); |
| 26 |
} |
| 27 |
|
| 28 |
/* |
| 29 |
* adds the language code in url |
| 30 |
* links_model interface |
| 31 |
* |
| 32 |
* @since 1.2 |
| 33 |
* |
| 34 |
* @param string $url url to modify |
| 35 |
* @param object $lang language |
| 36 |
* @return string modified url |
| 37 |
*/ |
| 38 |
public function add_language_to_link($url, $lang) { |
| 39 |
if (!empty($lang)) |
| 40 |
$url = $this->options['default_lang'] == $lang->slug ? $url : str_replace($this->home, $this->options['domains'][$lang->slug], $url); |
| 41 |
return $url; |
| 42 |
} |
| 43 |
|
| 44 |
/* |
| 45 |
* returns the url without language code |
| 46 |
* links_model interface |
| 47 |
* |
| 48 |
* @since 1.2 |
| 49 |
* |
| 50 |
* @param string $url url to modify |
| 51 |
* @return string modified url |
| 52 |
*/ |
| 53 |
public function remove_language_from_link($url) { |
| 54 |
if (!empty($this->options['domains'])) |
| 55 |
$url = preg_replace('#^('.implode('|', $this->options['domains']).')#', $this->home , $url); |
| 56 |
return $url; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* returns the link to the first page |
| 61 |
* links_model interface |
| 62 |
* |
| 63 |
* @since 1.2 |
| 64 |
* |
| 65 |
* @param string $url url to modify |
| 66 |
* @return string modified url |
| 67 |
*/ |
| 68 |
public function remove_paged_from_link($url) { |
| 69 |
return preg_replace('#\/page\/[0-9]+\/#', '/', $url); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
* returns the language based on language code in url |
| 74 |
* links_model interface |
| 75 |
* |
| 76 |
* @since 1.2 |
| 77 |
* |
| 78 |
* @return string language slug |
| 79 |
*/ |
| 80 |
public function get_language_from_url() { |
| 81 |
foreach ($this->options['domains'] as $key => $domain) |
| 82 |
if ($domain == (is_ssl() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']) |
| 83 |
return $key; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
* returns the home url |
| 88 |
* links_model interface |
| 89 |
* |
| 90 |
* @since 1.3.1 |
| 91 |
* |
| 92 |
* @param object $lang PLL_Language object |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
function home_url($lang) { |
| 96 |
if ($lang->slug == $this->options['default_lang']) |
| 97 |
return $this->home; |
| 98 |
|
| 99 |
if (!empty($this->options['domains'][$lang->slug])) |
| 100 |
return $this->options['domains'][$lang->slug]; |
| 101 |
|
| 102 |
} |
| 103 |
} |
| 104 |
|