Request.php
2.09 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
<?php
namespace AIOSEO\Plugin\Common\Traits\Helpers;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Parse the current request.
*
* @since 4.2.1
*/
trait Request {
/**
* Get the current URL.
*
* @since 4.2.1
*
* @return string The current URL.
*/
public function getCurrentUrl() {
return untrailingslashit( $this->getServer() ) . $this->getRequestUrl();
}
/**
* Get the server.
*
* @since 4.2.1
*
* @return string The server.
*/
private function getServer() {
return $this->getProtocol() . '://' . $this->getServerName();
}
/**
* Get the server port.
*
* @since 4.2.1
*
* @return string The server port.
*/
private function getServerPort() {
if (
empty( $_SERVER['SERVER_PORT'] ) ||
80 === (int) $_SERVER['SERVER_PORT'] ||
443 === (int) $_SERVER['SERVER_PORT']
) {
return '';
}
return ':' . (int) $_SERVER['SERVER_PORT'];
}
/**
* Get the protocol.
*
* @since 4.2.1
*
* @return string The protocol.
*/
private function getProtocol() {
return is_ssl() ? 'https' : 'http';
}
/**
* Get the server name (from $_SERVER['SERVER_NAME]), or use the request name ($_SERVER['HTTP_HOST']) if not present.
*
* @since 4.2.1
*
* @return string The server name.
*/
private function getServerName() {
$host = $this->getRequestServerName();
if ( isset( $_SERVER['SERVER_NAME'] ) ) {
$host = wp_unslash( $_SERVER['SERVER_NAME'] ); // phpcs:ignore HM.Security.ValidatedSanitizedInput.InputNotSanitized
}
return $host;
}
/**
* Get the request server name (from $_SERVER['HTTP_HOST]).
*
* @since 4.2.1
*
* @return string The request server name.
*/
private function getRequestServerName() {
$host = '';
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
$host = $_SERVER['HTTP_HOST'];
}
return $host;
}
/**
* Retrieve the request URL.
*
* @since 4.2.1
*
* @return string The request URL.
*/
private function getRequestUrl() {
$url = '';
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$url = $_SERVER['REQUEST_URI'];
}
return rawurldecode( stripslashes( $url ) );
}
}