PluginProbe
Polylang / 1.4
Polylang v1.4
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.4, at include/links-subdomain.php

103 lines 2.4 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 {
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 && $this->options['hide_default'] ? $url : str_replace('://', '://'.$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 foreach ($this->model->get_languages_list() as $language)
55 if (!$this->options['hide_default'] || $this->options['default_lang'] != $language->slug)
56 $languages[] = $language->slug;
57
58 if (!empty($languages))
59 $url = preg_replace('#:\/\/' . '('.implode('|', $languages).')\.#', '://' , $url);
60
61 return $url;
62 }
63
64 /*
65 * returns the link to the first page
66 * links_model interface
67 *
68 * @since 1.2
69 *
70 * @param string $url url to modify
71 * @return string modified url
72 */
73 function remove_paged_from_link($url) {
74 return preg_replace('#\/page\/[0-9]+\/#', '/', $url);
75 }
76
77 /*
78 * returns the language based on language code in url
79 * links_model interface
80 *
81 * @since 1.2
82 *
83 * @return string language slug
84 */
85 public function get_language_from_url() {
86 $pattern = '#('.implode('|', $this->model->get_languages_list(array('fields' => 'slug'))).')\.#';
87 return preg_match($pattern, trailingslashit($_SERVER['HTTP_HOST']), $matches) ? $matches[1] : ''; // $matches[1] is the slug of the requested language
88 }
89
90 /*
91 * returns the home url
92 * links_model interface
93 *
94 * @since 1.3.1
95 *
96 * @param object $lang PLL_Language object
97 * @return string
98 */
99 public function home_url($lang) {
100 return $this->add_language_to_link($this->home, $lang);
101 }
102 }
103