-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsx-under-construction.php
467 lines (380 loc) · 14.8 KB
/
xsx-under-construction.php
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* -----------------------------------------------------------------------------
* Plugin Name: Under Construction
* Description: Redirect not logged in users. Allow testers to see the site sending a magic link.
* Version: 1.3.4
* Requires PHP: 5.6
* Requires CP: 1.4
* Update URI: https://directory.classicpress.net/wp-json/wp/v2/plugins?byslug=xsx-under-construction
* Author: Simone Fioravanti
* Author URI: https://software.gieffeedizioni.it
* Plugin URI: https://software.gieffeedizioni.it
* Text Domain: xsx-under-construction
* Domain Path: /languages
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* -----------------------------------------------------------------------------
* This is free software released under the terms of the General Public License,
* version 2, or later. It is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Full
* text of the license is available at https://www.gnu.org/licenses/gpl-2.0.txt.
* -----------------------------------------------------------------------------
*/
namespace XXSimoXX\UnderConstruction;
require_once 'classes/UpdateClient.class.php';
class UnderConstruction {
private $options = false;
const SLUG = 'xsx-under-construction';
public function __construct() {
add_action('template_redirect', [$this, 'maybe_redirect']);
add_action('admin_menu', [$this, 'create_preview_menu'], 100);
add_action('wp_before_admin_bar_render', [$this, 'toolbar']);
add_action('admin_enqueue_scripts', [$this, 'styles']);
add_filter('rest_authentication_errors', [$this, 'rest_auth']);
register_uninstall_hook(__FILE__, [__CLASS__, 'uninstall']);
}
private function load_options() {
if ($this->options !== false) {
return;
}
$options = get_option('xsx_under_construction', false);
if ($options !== false) {
$this->options = $options;
return;
}
// Default values
$this->options = [
'ver' => '001',
'redirect_to' => plugin_dir_url(__FILE__).'templates/maintenance-1.html',
'keys' => [
['key' => wp_rand(10000000, 99999999), 'notes' => 'Key given to Joe.',],
['key' => wp_rand(10000000, 99999999), 'notes' => 'Key given to Kate.',],
],
];
$this->save_options();
}
private function save_options() {
update_option('xsx_under_construction', $this->options);
}
public function create_preview_menu() {
$page = add_submenu_page(
'options-general.php',
esc_html__('Under Construction', 'xsx-under-construction'),
esc_html__('Under Construction', 'xsx-under-construction'),
'manage_options',
self::SLUG,
[$this, 'render_menu']
);
add_action('load-'.$page, [$this, 'delete_action']);
add_action('load-'.$page, [$this, 'new_action']);
add_action('load-'.$page, [$this, 'url_action']);
}
private function render_embedded () {
$templates = scandir(dirname(__FILE__).'/templates');
foreach ($templates as $template) {
if (!preg_match('{\.html$}', $template)) {
continue;
}
$name = preg_replace('{\.html$}', '', $template);
$image = plugin_dir_url(__FILE__).'images/'.$name.'.svg';
$url = esc_url_raw(plugin_dir_url(__FILE__).'templates/'.$template);
echo '<a href="#" onclick="document.getElementById(\'url\').value=\''.esc_url_raw($url).'\'"><img src="'.esc_url_raw($image).'" title="'.esc_html($name).'" class="xuc-pw"></a>';
}
}
public function render_menu () {
echo '<div class="wrap">';
$this->display_notices();
$this->load_options();
echo '<div class="xuc xuc-general">';
echo '<h1>'.esc_html__('Under Construction', 'xsx-under-construction').'</h1>';
echo '<p>'.esc_html__('Not logged in users can\'t see your site and are redirected. To enable your site back deactivate "Under Construction" plugin.', 'xsx-under-construction').'</p>';
echo '</div>';
echo '<div class="xuc xuc-url">';
echo '<h2>'.esc_html__('Redirect to', 'xsx-under-construction').'</h2>';
echo '<p>'.esc_html__('Here you can change were your not logged in user are redirected to.', 'xsx-under-construction').'<br>';
echo '<form action="'.esc_url_raw(add_query_arg(['action' => 'url'], admin_url('admin.php?page='.self::SLUG))).'" method="POST">';
wp_nonce_field('url', '_xuc');
echo '<label for="url">'.esc_html__('Url to redirect to: ', 'xsx-under-construction').'</label>';
echo '<input type="text" size="'.(int)(strlen($this->options['redirect_to'])).'" name="url" id="url" value="'.esc_url_raw($this->options['redirect_to']).'"></input>';
echo '<input type="submit" class="button button-primary" value="'.esc_html__('Update', 'xsx-under-construction').'"></input>';
echo '</form>';
echo '<p><i>'.esc_html__('Click on the images below to get a basic page.', 'xsx-under-construction').'</i></p>';
$this->render_embedded();
echo '</div>';
echo '<div class="xuc xuc-keys">';
echo '<h2>'.esc_html__('Magic links', 'xsx-under-construction').'</h2>';
echo '<p>'.esc_html__('If you want someone to give a look to this site send them a magic link.', 'xsx-under-construction').'<br>';
/* translators: %1$s is site URL. */
echo sprintf(esc_html__('The link will set a session cookie and redirect them to the actual site: %1$s.', 'xsx-under-construction'), '<i>'.esc_url_raw(site_url()).'</i>').'<br>';
echo esc_html__('Use the "Copy to clipboard" link under the key to get the url to send.', 'xsx-under-construction').'</p>';
$ListTable = new UnderConstructionListTable();
$ListTable->load_items($this->options['keys']);
$ListTable->prepare_items();
$ListTable->display();
echo '<p>'.esc_html__('You can add new magic links. Use the "Notes" field as a reminder. The key will be autogenerated.', 'xsx-under-construction').'<br>';
echo esc_html__('You can also delete magic links and people with that key will again be redirected to the Under Construction page.', 'xsx-under-construction').'</p>';
echo '<form action="'.esc_url_raw(add_query_arg(['action' => 'new'], admin_url('admin.php?page='.self::SLUG))).'" method="POST">';
wp_nonce_field('new', '_xuc');
echo '<label for="new_note">'.esc_html__('Notes about the key: ', 'xsx-under-construction').'</label>';
echo '<input type="text" size="40" maxlength="40" name="new_note" id="new_note" placeholder="'.esc_html__('A note about this key.', 'xsx-under-construction').'"></input>';
echo '<input type="submit" class="button button-primary" value="'.esc_html__('New key', 'xsx-under-construction').'"></input>';
echo '</form>';
echo '</div>';
echo '</div>';
}
private function add_notice($message, $failure = false) {
$other_notices = get_transient('xsx_under_construction_notices');
$notice = $other_notices === false ? '' : $other_notices;
$failure_style = $failure ? 'notice-error' : 'notice-success';
$notice .= '<div class="notice '.$failure_style.' is-dismissible go-away-soon">';
$notice .= ' <p>'.wp_kses($message, ['br' => [], 'i' => [],]).'</p>';
$notice .= '</div>';
$notice .= '<script>jQuery(".go-away-soon").delay(3000).hide("slow", function() {});</script>';
set_transient('xsx_under_construction_notices', $notice, \HOUR_IN_SECONDS);
}
private function display_notices() {
$notices = get_transient('xsx_under_construction_notices');
if ($notices === false) {
return;
}
// This contains html formatted from 'add_notice' function that uses 'wp_kses'.
echo $notices; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
delete_transient('xsx_under_construction_notices');
}
public function new_action() {
if (!isset($_GET['action'])) {
return;
}
if ($_GET['action'] !== 'new') {
return;
}
if (!check_admin_referer('new', '_xuc')) {
return;
}
if (!current_user_can('manage_options')) {
return;
}
if (!isset($_REQUEST['new_note'])) {
return;
}
$note = substr(sanitize_text_field(wp_unslash($_REQUEST['new_note'])), 0, 40);
$this->load_options();
// Generate a non existing random key.
do {
$random_key = wp_rand(10000000, 99999999);
} while (array_search($random_key, array_column($this->options['keys'], 'key')) !== false);
array_push(
$this->options['keys'],
[
'key' => $random_key,
'notes' => $note,
]
);
$this->save_options();
$this->add_notice(esc_html__('New key generated.', 'xsx-under-construction').'<br><i>'.$note.'</i>', false);
$sendback = remove_query_arg(['action', 'new_note', '_xuc'], wp_get_referer());
wp_safe_redirect($sendback);
exit;
}
public function url_action() {
if (!isset($_GET['action'])) {
return;
}
if ($_GET['action'] !== 'url') {
return;
}
if (!check_admin_referer('url', '_xuc')) {
return;
}
if (!current_user_can('manage_options')) {
return;
}
if (!isset($_REQUEST['url'])) {
return;
}
$url = esc_url_raw(wp_unslash($_REQUEST['url']));
$this->load_options();
$this->options['redirect_to'] = $url;
$this->save_options();
$this->add_notice(esc_html__('Url updated.', 'xsx-under-construction').'<br><i>'.$url.'</i>', false);
$sendback = remove_query_arg(['action', 'url', '_xuc'], wp_get_referer());
wp_safe_redirect($sendback);
exit;
}
public function delete_action() {
if (!isset($_GET['action'])) {
return;
}
if ($_GET['action'] !== 'delete') {
return;
}
if (!check_admin_referer('delete', '_xuc')) {
return;
}
if (!current_user_can('manage_options')) {
return;
}
if (!isset($_REQUEST['key'])) {
return;
}
$this->load_options();
$notes = '';
foreach ($this->options['keys'] as $key => $value) {
if ($value['key'] === intval($_REQUEST['key'])) {
$notes = $value['notes'];
unset($this->options['keys'][$key]);
break;
}
}
$this->save_options();
$this->add_notice(esc_html__('Key deleted.', 'xsx-under-construction').'<br><i>'.$notes.'</i>', false);
$sendback = remove_query_arg(['action', 'key', '_xuc'], wp_get_referer());
wp_safe_redirect($sendback);
exit;
}
private function is_valid_key($key) {
$this->load_options();
foreach ($this->options['keys'] as $v) {
if ($v['key'] === intval($key)) {
return true;
}
}
return false;
}
public function maybe_redirect() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
// User logged in can see the site.
if (is_user_logged_in()) {
return;
}
// Don't break WP CLI.
if (defined('WP_CLI') && WP_CLI) {
return;
}
// No needed information? Define the field.
if (!isset($_SERVER['SCRIPT_NAME'])) {
$_SERVER['SCRIPT_NAME'] = '';
}
if (!isset($_SERVER['REQUEST_SCHEME'])) {
$_SERVER['REQUEST_SCHEME'] = '';
}
if (!isset($_SERVER['HTTP_HOST'])) {
$_SERVER['HTTP_HOST'] = '';
}
if (!isset($_SERVER['REQUEST_URI'])) {
$_SERVER['REQUEST_URI'] = '';
}
// Let people login.
if (stripos(sanitize_text_field(wp_unslash($_SERVER['SCRIPT_NAME'])), strrchr(wp_login_url(), '/')) !== false) {
return;
}
// Let people view our maintenance page.
$this->load_options();
if (esc_url_raw(wp_unslash($_SERVER['REQUEST_SCHEME']).'://'.wp_unslash($_SERVER['HTTP_HOST']).wp_unslash($_SERVER['REQUEST_URI'])) === $this->options['redirect_to']) {
return;
}
// Check for the "magic key" cookie.
if (isset($_COOKIE['selective_preview']) && $this->is_valid_key(intval($_COOKIE['selective_preview']))) {
return;
}
// Check if the query is for an Update Manager's endpoint.
if (defined('\XSX_UC_SAFE_UM') && \XSX_UC_SAFE_UM && defined('CodePotent\UpdateManager\PLUGIN_SHORT_SLUG') && preg_match('/^\/\?update=/', esc_url_raw(wp_unslash($_SERVER['REQUEST_URI']))) === 1) {
return;
}
// Redirect to selected destination. Not using wp_safe_redirect for flexibility.
wp_redirect($this->options['redirect_to'], 302); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
exit();
}
public function rest_auth($result) {
if ($result === true || is_wp_error($result) || is_user_logged_in()) {
return $result;
}
if (defined('\XSX_UC_SAFE_REST') && \XSX_UC_SAFE_REST) {
return $result;
}
return new \WP_Error(
'rest_not_logged_in',
esc_html__('You are not currently logged in.', 'xsx-under-construction'),
['status' => 401]
);
}
public function toolbar() {
global $wp_admin_bar;
$wp_admin_bar->add_node([
'id' => self::SLUG,
'title' => esc_html__('Under Construction', 'xsx-under-construction'),
'meta' => ['class' => 'xuc-admin-bar'],
'href' => admin_url('options-general.php?page='.self::SLUG),
]);
}
public function styles() {
wp_enqueue_style('xsx-under-construction-css', plugins_url('css/style.css', __FILE__), [], '0.0.2');
}
public static function uninstall() {
delete_option('xsx_under_construction');
}
}
new UnderConstruction;
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH.'wp-admin/includes/class-wp-list-table.php');
}
class UnderConstructionListTable extends \WP_List_Table {
// Contains the requested filter type (all, plugins or themes).
private $filtertype = 'all';
// Contains the data to be rendered, as we want this to be passed from another class.
private $keys = [];
// Load list items, as we want this to be passed from another class.
public function load_items($keys) {
$this->keys = $keys;
}
// Output columns definition.
public function get_columns() {
return [
'key' => esc_html__('Key', 'xsx-under-construction'),
'link' => esc_html__('Link', 'xsx-under-construction'),
'notes' => esc_html__('Notes', 'xsx-under-construction'),
];
}
// Output hidden columns.
public function get_hidden_columns() {
return [
'link',
];
}
// Just output the column.
public function column_default($item, $column_name) {
return $item[$column_name];
}
// For "Key" column add row actions.
public function column_key($item) {
$actions = [
'delete' => '<a href="'.wp_nonce_url(add_query_arg(['action' => 'delete', 'key' => $item['key']]), 'delete', '_xuc').'">'.esc_html__('Delete', 'xsx-under-construction').'</a>',
'copy' => '<a href="#" onclick="navigator.clipboard.writeText(\''.$item['link'].'\')">'.esc_html__('Copy to clipboard', 'xsx-under-construction').'</a>',
];
$key = '<span class="row-title">'.$item['key'].'</span>';
return sprintf('%1$s %2$s', $key, $this->row_actions($actions));
}
// For "Notes" column if empty add a message.
public function column_notes($item) {
if ($item['notes'] !== '') {
return $item['notes'];
}
return '<i>'.esc_html__('No note for this key.', 'xsx-under-construction').'</i>';
}
// Prepare our columns and insert data.
public function prepare_items() {
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = [];
$this->_column_headers = [$columns, $hidden, $sortable];
$data = [];
foreach ($this->keys as $key) {
$data[] = [
'key' => $key['key'],
'link' => plugin_dir_url(__FILE__).'preview.php?preview='.$key['key'],
'notes' => $key['notes'],
];
}
$this->items = $data;
}
}