1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RedirectionIO\Client\Wordpress; |
4
|
|
|
|
5
|
|
|
use RedirectionIO\Client\Sdk\Client; |
6
|
|
|
use RedirectionIO\Client\Sdk\HttpMessage\Request; |
7
|
|
|
use RedirectionIO\Client\Sdk\HttpMessage\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Main plugin file. |
11
|
|
|
* |
12
|
|
|
* This class is the core logic of the plugin. |
13
|
|
|
*/ |
14
|
|
|
class RedirectionIO |
15
|
|
|
{ |
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
add_action('plugins_loaded', [$this, 'findRedirect']); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
add_option('redirectionio', [ |
24
|
|
|
'connections' => [ |
25
|
|
|
[ |
26
|
|
|
'name' => '', |
27
|
|
|
'remote_socket' => '', |
28
|
|
|
], |
29
|
|
|
], |
30
|
|
|
'doNotRedirectAdmin' => true, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function findRedirect() |
35
|
|
|
{ |
36
|
|
|
$options = get_option('redirectionio'); |
37
|
|
|
$connections = []; |
38
|
|
|
|
39
|
|
|
if (false === $options || !isset($options['connections'])) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
foreach ($options['connections'] as $option) { |
44
|
|
|
foreach ($option as $key => $val) { |
45
|
|
|
if ($key === 'name') { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$connections[$option['name']][$key] = $val; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$client = new Client($connections); |
54
|
|
|
$request = new Request( |
55
|
|
|
$_SERVER['HTTP_HOST'], |
56
|
|
|
$_SERVER['REQUEST_URI'], |
57
|
|
|
$_SERVER['HTTP_USER_AGENT'], |
58
|
|
|
$_SERVER['HTTP_REFERER'] |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
if ($this->isAdminPage($request) && $options['doNotRedirectAdmin']) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$response = $client->findRedirect($request); |
66
|
|
|
|
67
|
|
|
if (null === $response) { |
68
|
|
|
$response = new Response(http_response_code()); |
69
|
|
|
$client->log($request, $response); |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$client->log($request, $response); |
75
|
|
|
wp_redirect($response->getLocation(), $response->getStatusCode()); |
76
|
|
|
$this->exitCode(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function exitCode() |
80
|
|
|
{ |
81
|
|
|
exit; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if the requested page belongs to admin area. |
86
|
|
|
* |
87
|
|
|
* @param Request $request |
88
|
|
|
*/ |
89
|
|
|
private function isAdminPage(Request $request) |
90
|
|
|
{ |
91
|
|
|
$adminRoot = str_replace(get_site_url(), '', get_admin_url()); |
92
|
|
|
$requestPath = substr($request->getPath(), 0, strlen($adminRoot)); |
93
|
|
|
|
94
|
|
|
if ($adminRoot === $requestPath) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.