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
polylang / include / links-subdomain.php

links-subdomain.php in Polylang 1.5, at include/links-subdomain.php

87 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * links model for use when the language code is added in url as a subdomain
5 * for example en.mysite.com/something
6 * implements the "links_model interface"
7 *
8 * @since 1.2
9 */
10 class PLL_Links_Subdomain extends PLL_Links_Model {
11
12 /*
13 * adds the language code in url
14 * links_model interface
15 *
16 * @since 1.2
17 *
18 * @param string $url url to modify
19 * @param object $lang language
20 * @return string modified url
21 */
22 public function add_language_to_link($url, $lang) {
23 if (!empty($lang))
24 $url = $this->options['default_lang'] == $lang->slug && $this->options['hide_default'] ? $url : str_replace('://', '://'.$lang->slug.'.', $url);
25 return $url;
26 }
27
28 /*
29 * returns the url without language code
30 * links_model interface
31 *
32 * @since 1.2
33 *
34 * @param string $url url to modify
35 * @return string modified url
36 */
37 public function remove_language_from_link($url) {
38 foreach ($this->model->get_languages_list() as $language)
39 if (!$this->options['hide_default'] || $this->options['default_lang'] != $language->slug)
40 $languages[] = $language->slug;
41
42 if (!empty($languages))
43 $url = preg_replace('#:\/\/' . '('.implode('|', $languages).')\.#', '://' , $url);
44
45 return $url;
46 }
47
48 /*
49 * returns the language based on language code in url
50 * links_model interface
51 *
52 * @since 1.2
53 *
54 * @return string language slug
55 */
56 public function get_language_from_url() {
57 $pattern = '#('.implode('|', $this->model->get_languages_list(array('fields' => 'slug'))).')\.#';
58 return preg_match($pattern, trailingslashit($_SERVER['HTTP_HOST']), $matches) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
59 }
60
61 /*
62 * returns the home url
63 * links_model interface
64 *
65 * @since 1.3.1
66 *
67 * @param object $lang PLL_Language object
68 * @return string
69 */
70 public function home_url($lang) {
71 return $this->add_language_to_link($this->home, $lang);
72 }
73
74 /*
75 * get hosts managed on the website
76 *
77 * @since 1.5
78 *
79 * @return array list of hosts
80 */
81 public function get_hosts() {
82 foreach ($this->model->get_languages_list() as $lang)
83 $hosts[] = parse_url($this->home_url($lang), PHP_URL_HOST);
84 return $hosts;
85 }
86 }
87