-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinksModelQuery.php
More file actions
94 lines (90 loc) · 2.95 KB
/
LinksModelQuery.php
File metadata and controls
94 lines (90 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once 'ILinksModel.php';
/**
* Links model class for default Permalinks
* for example mysite.com/?somevar=something&lang=en
*/
class LinksModelQuery implements ILinksModel
{
const QUERY_LANGUAGE = 'lang';
/**
* Used to store the home url before it is filtered
*/
protected $home;
/**
* @var array List of hosts managed on the website
*/
protected $hosts;
protected $defaultLanguage = WpTranslate::LANGUAGE_DEFAULT_CODE;
protected $acceptedLanguages = WpTranslate::LANGUAGE_DEFAULT_CODE;
private $patternGetLanguageCode = '';
/**
* @inheritdoc
*/
public function __construct(string $defaultLanguage, string $acceptedLanguages)
{
if(empty($defaultLanguage) == false) {
$this->defaultLanguage = $defaultLanguage;
}
if(empty($acceptedLanguages) == false) {
$this->acceptedLanguages = $acceptedLanguages;
}
$this->home = home_url();
if (empty($this->acceptedLanguages) == false) {
$queryVar = self::QUERY_LANGUAGE;
$this->patternGetLanguageCode = "#{$queryVar}=({$this->acceptedLanguages})#";
}
$this->hosts = array_values([parse_url($this->home, PHP_URL_HOST)]);
add_filter('allowed_redirect_hosts', [$this, 'handleAllowedRedirectHosts']);
}
/**
* @inheritdoc
*/
public function setLanguageCode(string $url, string $languageCode)
{
$url = remove_query_arg(self::QUERY_LANGUAGE, $url);
if (empty($languageCode) == false && $languageCode != $this->defaultLanguage) {
$url = add_query_arg(self::QUERY_LANGUAGE, $languageCode, $url);
}
return $url;
}
/**
* @inheritdoc
*/
public function getLanguageCode()
{
$languageCode = '';
$url = $this->getUrlFromRequest();
if (empty($this->patternGetLanguageCode) == false &&
preg_match($this->patternGetLanguageCode, trailingslashit($url), $matches)) {
$languageCode = $matches[1];
}
return $languageCode;
}
/**
* Get Url that has language parameter with value in it
*/
function getUrlFromRequest()
{
if (empty($_SERVER['HTTP_ORIGIN']) == false &&
empty($_SERVER['HTTP_REFERER']) == false &&
$_SERVER['HTTP_ORIGIN'] == $this->home &&
$_SERVER['HTTP_ORIGIN'] != $_SERVER['HTTP_REFERER']) {
return $_SERVER['HTTP_REFERER'];
} else {
return $_SERVER['REQUEST_URI'];
}
}
function getUrlForLanguage(string $languageCode){
return $this->setLanguageCode($this->getUrlFromRequest(), $languageCode);
}
/**
* Adds our Domains or SubDomains to allowed hosts for safe redirection
* @param array $hosts Allowed hosts
* @return array
*/
public function handleAllowedRedirectHosts(array $hosts)
{
return array_unique(array_merge($hosts, $this->hosts));
}
}