wpml-request.class.php
3.52 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Class WPML_Request
*
* @package wpml-core
* @subpackage wpml-requests
*
* @abstract
*/
use WPML\Language\Detection\CookieLanguage;
use WPML\UrlHandling\WPLoginUrlConverterRules;
abstract class WPML_Request {
/** @var WPML_URL_Converter */
protected $url_converter;
protected $active_languages;
protected $default_language;
/** @var CookieLanguage */
protected $cookieLanguage;
/**
* @param WPML_URL_Converter $url_converter
* @param array $active_languages
* @param string $default_language
* @param CookieLanguage $cookieLanguage
*/
public function __construct(
WPML_URL_Converter $url_converter,
$active_languages,
$default_language,
CookieLanguage $cookieLanguage
) {
$this->url_converter = $url_converter;
$this->active_languages = $active_languages;
$this->default_language = $default_language;
$this->cookieLanguage = $cookieLanguage;
}
abstract protected function get_cookie_name();
/**
* Determines the language of the current request.
*
* @return string|false language code of the current request, determined from the requested url and the user's
* cookie.
*/
abstract public function get_requested_lang();
/**
* Returns the current REQUEST_URI optionally filtered
*
* @param null|int $filter filter to apply to the REQUEST_URI, takes the same arguments
* as filter_var for the filter type.
*
* @return string
*/
public function get_request_uri( $filter = null ) {
$request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/';
if ( $filter !== null ) {
$request_uri = filter_var( $request_uri, $filter );
}
return $request_uri;
}
/**
* @global $wpml_url_converter
*
* @return string|false language code that can be determined from the currently requested URI.
*/
public function get_request_uri_lang() {
/**
* Avoid returning language from URL when wpml_should_skip_saving_language_in_cookies filter hook returns TRUE
* @see https://onthegosystems.myjetbrains.com/youtrack/issue/wpmldev-1544
*/
if ( apply_filters( 'wpml_should_skip_saving_language_in_cookies', false ) ) {
return false;
}
$req_url = isset( $_SERVER['HTTP_HOST'] )
? untrailingslashit( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) : '';
return $this->url_converter->get_language_from_url( $req_url );
}
/**
* @return string language code stored in the user's wp-wpml_current_language cookie
*/
public function get_cookie_lang() {
return $this->cookieLanguage->get( $this->get_cookie_name() );
}
/**
* Checks whether hidden languages are to be displayed at the moment.
* They are displayed in the frontend if the users has the respective option icl_show_hidden_languages set in his
* user_meta. The are displayed in the backend for all admins with manage_option capabilities.
*
* @return bool true if hidden languages are to be shown
*/
public function show_hidden() {
return ! did_action( 'init' )
|| ( get_user_meta( get_current_user_id(), 'icl_show_hidden_languages', true )
|| ( ( is_admin() || wpml_is_rest_request() ) && current_user_can( 'manage_options' ) ) );
}
/**
* Sets the language code of the current screen in the User's wp-wpml_current_language cookie
*
* When user is not logged we must set cookie with JS to avoid issues with cached pages
*
* @param string $lang_code
*/
public function set_language_cookie( $lang_code ) {
$this->cookieLanguage->set( $this->get_cookie_name(), $lang_code );
}
}