1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RedirectionIO\Client\Wordpress; |
4
|
|
|
|
5
|
|
|
use RedirectionIO\Client\Sdk\Client; |
6
|
|
|
use RedirectionIO\Client\Sdk\Exception\AgentNotFoundException; |
7
|
|
|
use RedirectionIO\Client\Sdk\HttpMessage\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* RedirectionIOSettingsPage class. |
11
|
|
|
* |
12
|
|
|
* This class is used to create a new page in the admin area |
13
|
|
|
* dedicated to redirection.io. It lets you configuring the plugin as you like. |
14
|
|
|
*/ |
15
|
|
|
class RedirectionIOSettingsPage |
16
|
|
|
{ |
17
|
|
|
private $overrider; |
18
|
|
|
|
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->overrider = new WPCoreFunctionsOverrider(); |
22
|
|
|
|
23
|
|
|
add_action('init', [$this, 'setTranslations']); |
24
|
|
|
add_action('admin_menu', [$this, 'setUp']); |
25
|
|
|
add_action('admin_init', [$this, 'registerSettings']); |
26
|
|
|
add_action('admin_enqueue_scripts', [$this, 'registerAssets']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
add_options_page('redirection.io', 'redirection.io', 'manage_options', 'redirectionio', [$this, 'outputContent']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setTranslations() |
35
|
|
|
{ |
36
|
|
|
load_plugin_textdomain('redirectionio', false, dirname(plugin_basename(__FILE__)) . '/../languages'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function outputContent() |
40
|
|
|
{ |
41
|
|
|
if (!current_user_can('manage_options')) { |
42
|
|
|
wp_die(__('You do not have sufficient permissions to access this page.')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$title = 'redirection.io'; |
46
|
|
|
$intro = |
47
|
|
|
'<p>' . |
48
|
|
|
__('redirection.io let you track HTTP errors and setup useful HTTP redirections.', 'redirectionio') . |
49
|
|
|
'</p><p>' . |
50
|
|
|
sprintf(__('This plugin works in combination with <a href="%s">redirection.io</a> |
51
|
|
|
and need an installed and configured agent on your server. |
52
|
|
|
</br> |
53
|
|
|
Before using it, please make sure that you have :', 'redirectionio'), esc_url('//redirection.io')) . |
54
|
|
|
'<ul>' . |
55
|
|
|
'<li>' . sprintf(__('created a redirection.io account <a href="">here</a>', 'redirectionio'), esc_url('//redirection.io')) . '</li>' . |
56
|
|
|
'<li>' . sprintf(__('followed the <a href="">installation guide</a> to setup a redirection.io agent on your server', 'redirectionio'), esc_url('//redirection.io')) . '</li>' . |
57
|
|
|
'</ul>' . |
58
|
|
|
'</p><p>' . |
59
|
|
|
__('Drop us an email to [email protected] if you need help or have any question.', 'redirectionio') . |
60
|
|
|
'</p><p>' . |
61
|
|
|
__('Note: in most cases, you only have one agent, so you only need to configure one connection.', 'redirectionio') . |
62
|
|
|
'</p>' |
63
|
|
|
; |
64
|
|
|
$confirm = __('Are you sure ?', 'redirectionio'); |
65
|
|
|
|
66
|
|
|
echo ' |
67
|
|
|
<div class="wrap" id="redirectionio"> |
68
|
|
|
<h1>' . $title . '</h1> |
69
|
|
|
<div>' . $intro . '</div> |
70
|
|
|
<form method="post" action="options.php"> |
71
|
|
|
'; |
72
|
|
|
|
73
|
|
|
settings_fields('redirectionio-group'); |
74
|
|
|
$this->overrider->doSettingsSections('redirectionio'); |
75
|
|
|
|
76
|
|
|
submit_button(); |
77
|
|
|
|
78
|
|
|
echo ' |
79
|
|
|
</form></div> |
80
|
|
|
<script> |
81
|
|
|
var confirmStr = \'' . $confirm . '\'; |
82
|
|
|
</script> |
83
|
|
|
'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function registerSettings() |
87
|
|
|
{ |
88
|
|
|
register_setting( |
89
|
|
|
'redirectionio-group', |
90
|
|
|
'redirectionio', |
91
|
|
|
[$this, 'sanitizeInput'] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$options = get_option('redirectionio'); |
95
|
|
|
|
96
|
|
|
foreach ($options['connections'] as $i => $option) { |
97
|
|
|
add_settings_section( |
98
|
|
|
'redirectionio-section-' . $i, |
99
|
|
|
sprintf(__('Connection #%s', 'redirectionio'), $i + 1), |
100
|
|
|
[$this, 'printSection'], |
101
|
|
|
'redirectionio' |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
foreach ($option as $key => $value) { |
105
|
|
|
switch ($key) { |
106
|
|
|
case 'name': |
107
|
|
|
$title = __('Name', 'redirectionio'); |
108
|
|
|
$required = false; |
109
|
|
|
$description = __('[Optional] If you have multiple connections, you may find useful to name them for better readibility.', 'redirectionio'); |
110
|
|
|
$placeholder = __('my-connection', 'redirectionio'); |
111
|
|
|
break; |
112
|
|
|
case 'remote_socket': |
113
|
|
|
$title = __('Agent address', 'redirectionio'); |
114
|
|
|
$required = true; |
115
|
|
|
$description = __('[Required] Insert here your agent address. Internet Domain socket (AF_INET) and Unix Domain socket (AF_UNIX) are supported.<br/> Examples: 192.168.1.1:20301, agent.my-website.com:10301, /var/run/my-agent.sock', 'redirectionio'); |
116
|
|
|
$placeholder = '192.168.1.1:20301'; |
117
|
|
|
break; |
118
|
|
|
default: |
119
|
|
|
$title = 'unknown'; |
120
|
|
|
$required = false; |
121
|
|
|
$description = ''; |
122
|
|
|
$placeholder = ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
add_settings_field( |
126
|
|
|
$i . '_' . $key, |
127
|
|
|
$title . ($required ? '*' : ''), |
128
|
|
|
[$this, 'printField'], |
129
|
|
|
'redirectionio', |
130
|
|
|
'redirectionio-section-' . $i, |
131
|
|
|
[ |
132
|
|
|
'id' => $i, |
133
|
|
|
'type' => $key, |
134
|
|
|
'value' => $value, |
135
|
|
|
'placeholder' => $placeholder, |
136
|
|
|
'description' => $description, |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// Add doNotRedirectAdmin option |
143
|
|
|
add_settings_section( |
144
|
|
|
'redirectionio-section-do-not-redirect-admin', |
145
|
|
|
__('Disable redirections for admin area', 'redirectionio'), |
146
|
|
|
[$this, 'printSection'], |
147
|
|
|
'redirectionio' |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
add_settings_field( |
151
|
|
|
'redirectionio-checkbox-do-not-redirect-admin', |
152
|
|
|
__("Yes, I wan't to disable it", 'redirectionio'), |
153
|
|
|
[$this, 'printCheckbox'], |
154
|
|
|
'redirectionio', |
155
|
|
|
'redirectionio-section-do-not-redirect-admin', |
156
|
|
|
$options['doNotRedirectAdmin'] |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $input |
162
|
|
|
*/ |
163
|
|
|
public function sanitizeInput(array $input) |
164
|
|
|
{ |
165
|
|
|
$newInput = []; |
166
|
|
|
|
167
|
|
|
foreach ($input['connections'] as $i => $option) { |
168
|
|
|
foreach ($option as $key => $value) { |
169
|
|
|
$newInput['connections'][$i][$key] = sanitize_text_field($input['connections'][$i][$key]); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$newInput['doNotRedirectAdmin'] = $input['doNotRedirectAdmin']; |
174
|
|
|
|
175
|
|
|
return $newInput; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function printSection() |
179
|
|
|
{ |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param array $args |
184
|
|
|
*/ |
185
|
|
|
public function printField(array $args) |
186
|
|
|
{ |
187
|
|
|
$id = isset($args['id']) ? $args['id'] : ''; |
188
|
|
|
$type = isset($args['type']) ? $args['type'] : ''; |
189
|
|
|
$value = isset($args['value']) ? $args['value'] : ''; |
190
|
|
|
$placeholder = isset($args['placeholder']) ? $args['placeholder'] : ''; |
191
|
|
|
$description = isset($args['description']) ? $args['description'] : ''; |
192
|
|
|
|
193
|
|
|
echo "<input id='redirectionio_{$id}_{$type}' name='redirectionio[connections][$id][$type]' size='40' type='text' value='$value' placeholder='$placeholder' />"; |
194
|
|
|
echo "<p class='description' id='redirectionio_{$id}_{$type}_description'>$description</p>"; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param bool $checked |
199
|
|
|
*/ |
200
|
|
|
public function printCheckbox($checked) |
201
|
|
|
{ |
202
|
|
|
echo '<input id="redirectionio_doNotRedirectAdmin" name="redirectionio[doNotRedirectAdmin]" type="checkbox" ' . ($checked ? 'checked' : '') . ' />'; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function registerAssets() |
206
|
|
|
{ |
207
|
|
|
wp_enqueue_style('redirectionio', plugins_url('../assets/css/redirectionio.css', __FILE__)); |
208
|
|
|
wp_enqueue_script('redirectionio', plugins_url('../assets/js/redirectionio.js', __FILE__), [], false, true); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return a connection (associative array with (`port` && `host`) || `remote_socket` keys). |
213
|
|
|
* |
214
|
|
|
* @param mixed $page |
215
|
|
|
* @param mixed $section |
216
|
|
|
*/ |
217
|
|
|
public static function getConnectionFromSection($page, $section) |
218
|
|
|
{ |
219
|
|
|
global $wp_settings_fields; |
220
|
|
|
|
221
|
|
|
if (!isset($wp_settings_fields[$page][$section])) { |
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
foreach ((array) $wp_settings_fields[$page][$section] as $field) { |
226
|
|
|
if ($field['args']['type'] === 'remote_socket') { |
227
|
|
|
$remoteSocket = $field['args']['value']; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return ['remote_socket' => $remoteSocket]; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Test if a connection is currently working. |
236
|
|
|
* |
237
|
|
|
* $connection param should be an associative array |
238
|
|
|
* with (`port` && `host`) || `remote_socket` keys |
239
|
|
|
* |
240
|
|
|
* @param mixed $connection array|bool(false) |
241
|
|
|
*/ |
242
|
|
|
public static function isWorkingConnection($connection) |
243
|
|
|
{ |
244
|
|
|
if ($connection === false) { |
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$client = new Client( |
249
|
|
|
['checkStatus' => [ |
250
|
|
|
'host' => $connection['host'], |
251
|
|
|
'port' => $connection['port'], |
252
|
|
|
'remote_socket' => $connection['remote_socket'], |
253
|
|
|
]], |
254
|
|
|
10000, |
255
|
|
|
true |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
try { |
259
|
|
|
$request = new Request('', '', ''); |
260
|
|
|
$response = $client->findRedirect($request); |
|
|
|
|
261
|
|
|
} catch (AgentNotFoundException $e) { |
262
|
|
|
return false; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return true; |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|