forked from humanmade/Mercator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
575 lines (488 loc) · 15.7 KB
/
admin.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
/**
* Administrative UI and helpers for Mercator
*
* @package Mercator
*/
namespace Mercator\Admin;
use Mercator\Mapping;
use WP_Error;
add_filter( 'wpmu_blogs_columns', __NAMESPACE__ . '\\add_site_list_column' );
add_action( 'manage_sites_custom_column', __NAMESPACE__ . '\\output_site_list_column', 10, 2 );
add_action( 'admin_footer', __NAMESPACE__ . '\\maybe_output_site_tab' );
add_action( 'admin_action_mercator-aliases', __NAMESPACE__ . '\\output_list_page' );
add_action( 'admin_action_mercator-add', __NAMESPACE__ . '\\output_edit_page' );
add_action( 'admin_action_mercator-edit', __NAMESPACE__ . '\\output_edit_page' );
add_filter( 'plugin_row_meta', __NAMESPACE__ . '\\output_sunrise_dropin_note', -10, 4 );
/**
* Add site list column to list
*
* @param array $columns Column map of ID => title
* @return array
*/
function add_site_list_column( $columns ) {
$columns['mercator_aliases'] = __( 'Aliases', 'mercator' );
return $columns;
}
/**
* Output the site list column
*
* @param string $column Column ID
* @param int $site_id Site ID
*/
function output_site_list_column( $column, $site_id ) {
switch ( $column ) {
case 'mercator_aliases':
$mappings = Mapping::get_by_site( $site_id );
if ( ! empty( $mappings ) ) {
foreach ( $mappings as $mapping ) {
// Kinda horrible formatting, but matches the existing
echo esc_html( $mapping->get_domain() ) . '<br />';
}
}
break;
}
}
/**
* Output the site tab if we're on the right page
*
* Outputs the link, then moves it into place using JS, as there are no hooks to
* speak of.
*/
function maybe_output_site_tab() {
if ( ! is_network_admin() ) {
return;
}
if ( $GLOBALS['parent_file'] !== 'sites.php' || $GLOBALS['submenu_file'] !== 'sites.php' ) {
return;
}
$id = isset( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
if ( empty( $id ) ) {
return;
}
$class = ( ! empty( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'mercator-aliases', 'mercator-edit' ), true ) ) ? ' nav-tab-active' : '';
?>
<span id="mercator-aliases-nav-link" class="hide-if-no-js"><a href="<?php echo esc_url( network_admin_url( 'admin.php?action=mercator-aliases' ) . '&id=' . $id ); ?>" class="nav-tab<?php echo $class; ?>"><?php esc_html_e( 'Aliases', 'mercator' ); ?></a></span>
<script>jQuery(function ($) {
$( '#mercator-aliases-nav-link' ).appendTo( $( '.nav-tab-wrapper' ) );
});</script>
<?php
}
/**
* Output the admin page header
*
* @param int $id Site ID
* @param array $messages Messages to display
*/
function output_page_header( $id, $messages = array() ) {
global $pagenow;
$site_url_no_http = preg_replace( '#^http(s)?://#', '', get_blogaddress_by_id( $id ) );
$title_site_link = sprintf( '<a href="%1$s">%2$s</a>', get_blogaddress_by_id( $id ), $site_url_no_http );
$title_site_url_linked = sprintf( __( 'Aliases: %s', 'mercator' ), $title_site_link );
// Load the page header
global $title, $parent_file, $submenu_file;
$title = sprintf( esc_html__( 'Aliases: %s', 'mercator' ), $site_url_no_http );
$parent_file = 'sites.php';
$submenu_file = 'sites.php';
require_once( ABSPATH . 'wp-admin/admin-header.php' );
$add_link = add_query_arg(
array(
'action' => 'mercator-add',
'id' => $id,
),
network_admin_url( 'admin.php' )
);
?>
<div class="wrap">
<h1 id="edit-site">
<?php
echo wp_kses( $title_site_url_linked, array(
'a' => array(
'href' => array(),
),
) );
?>
<a href="<?php echo esc_url( $add_link ); ?>" class="add-new-h2"><?php echo esc_html_x( 'Add New', 'alias', 'mercator' ); ?></a>
</h1>
<h2 class="nav-tab-wrapper wp-clearfix">
<?php
$tabs = array(
'site-info' => array(
'label' => __( 'Info' ),
'url' => 'site-info.php',
),
'site-users' => array(
'label' => __( 'Users' ),
'url' => 'site-users.php',
),
'site-themes' => array(
'label' => __( 'Themes' ),
'url' => 'site-themes.php',
),
'site-settings' => array(
'label' => __( 'Settings' ),
'url' => 'site-settings.php',
),
);
foreach ( $tabs as $tab_id => $tab ) {
$class = ( $pagenow === $tab['url'] ) ? ' nav-tab-active' : '';
printf ( '<a href="%1$s" class="nav-tab %2$s">%3$s</a>', esc_url( $tab['url'] . '?id=' . $id ), esc_attr( $class ), esc_html( $tab['label'] ) );
}
?>
</h2>
<?php
$allowed_tags = array(
'a' => array(
'class' => array(),
'href' => array(),
),
'strong' => array(
'class' => array(),
),
'code' => array(),
);
if ( ! empty( $messages ) ) {
foreach ( $messages as $msg ) {
echo '<div id="message" class="updated"><p>' . wp_kses( $msg, $allowed_tags ) . '</p></div>';
}
}
}
/**
* Output the admin page footer
*/
function output_page_footer() {
echo '</div>';
require_once( ABSPATH . 'wp-admin/admin-footer.php' );
}
/**
* Handle submission of the list page
*
* Handles bulk actions for the list page. Redirects back to itself after
* processing, and exits.
*
* @param int $id Site ID
* @param string $action Action to perform
*/
function handle_list_page_submit( $id, $action ) {
check_admin_referer( 'mercator-aliases-bulk-' . $id );
$sendback = remove_query_arg( array( 'did_action', 'mappings', '_wpnonce' ), wp_get_referer() );
if ( ! $sendback ) {
$sendback = admin_url( $parent_file );
}
$mappings = empty( $_REQUEST['mappings'] ) ? array() : (array) $_REQUEST['mappings'];
$mappings = array_map( 'absint', $mappings );
if ( ! isset( $mappings ) ) {
wp_redirect( $sendback );
exit;
}
$processed = 0;
$args = array(
'did_action' => $action,
'mappings' => join( ',', $mappings ),
);
switch ( $action ) {
case 'activate':
foreach ( $mappings as $id ) {
$mapping = Mapping::get( $id );
if ( is_wp_error( $mapping ) ) {
continue;
}
if ( $mapping->set_active( true ) ) {
$processed++;
}
}
break;
case 'deactivate':
foreach ( $mappings as $id ) {
$mapping = Mapping::get( $id );
if ( is_wp_error( $mapping ) ) {
continue;
}
if ( $mapping->set_active( false ) ) {
$processed++;
}
}
break;
case 'delete':
$args['domains'] = array();
foreach ( $mappings as $id ) {
$mapping = Mapping::get( $id );
if ( is_wp_error( $mapping ) ) {
continue;
}
if ( $mapping->delete() ) {
// Mappings don't exist after we delete them, so pass the
// domain for messages and such
$args['domains'][] = $mapping->get_domain();
$processed++;
}
}
break;
default:
do_action_ref_array( "mercator_aliases_bulk_action-{$action}", array( $mappings, &$processed, $action ) );
break;
}
$args['processed'] = $processed;
$sendback = add_query_arg( $args, $sendback );
wp_safe_redirect( $sendback );
exit();
}
/**
* Output alias editing page
*/
function output_list_page() {
$id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
if ( ! $id ) {
wp_die( esc_html__( 'Invalid site ID.' ) );
}
$id = absint( $id );
$details = get_blog_details( $id );
if ( ! can_edit_network( $details->site_id ) || (int) $details->blog_id !== $id ) {
wp_die( esc_html__( 'You do not have permission to access this page.' ) );
}
$wp_list_table = new Alias_List_Table( array(
'site_id' => $id,
) );
$messages = array();
$bulk_action = $wp_list_table->current_action();
if ( $bulk_action ) {
$messages = handle_list_page_submit( $id, $bulk_action );
}
$pagenum = $wp_list_table->get_pagenum();
$wp_list_table->prepare_items( $id );
// Add messages for bulk actions
if ( ! empty( $_REQUEST['did_action'] ) ) {
$processed = empty( $_REQUEST['processed'] ) ? 0 : absint( $_REQUEST['processed'] );
$did_action = $_REQUEST['did_action'];
$mappings = empty( $_REQUEST['mappings'] ) ? array() : wp_parse_id_list( $_REQUEST['mappings'] );
$mappings = array_map( 'absint', $mappings );
// Special case for single, as it's not really a "bulk" action
if ( $processed === 1 ) {
$bulk_messages = array(
'activate' => __( 'Activated %s', 'mercator' ),
'deactivate' => __( 'Deactivated %s', 'mercator' ),
'delete' => __( 'Deleted %s', 'mercator' ),
'add' => __( 'Created %s', 'mercator' ),
'edit' => __( 'Updated %s', 'mercator' ),
);
if ( $did_action !== 'delete' ) {
$mapping = Mapping::get( $mappings[0] );
$domain = $mapping->get_domain();
} else {
$domain = empty( $_REQUEST['domains'] ) ? array() : $_REQUEST['domains'][0];
}
$placeholder = '<code>' . $domain . '</code>';
} else {
// Note: we still use _n for languages which have special cases on
// e.g. 3, 5, 10, etc
$bulk_messages = array(
'activate' => _n( '%s alias activated.', '%s aliases activated.', $processed ),
'deactivate' => _n( '%s alias deactivated.', '%s aliases deactiaved.', $processed ),
'delete' => _n( '%s alias deleted.', '%s aliases deleted.', $processed ),
'add' => _n( '%s alias created.', '%s aliases created.', $processed ),
'edit' => _n( '%s alias updated.', '%s aliases updated.', $processed ),
);
$placeholder = number_format_i18n( $processed );
}
$bulk_messages = apply_filters( 'mercator_aliases_bulk_messages', $bulk_messages, $processed );
if ( ! empty( $bulk_messages[ $did_action ] ) ) {
$messages[] = sprintf( $bulk_messages[ $did_action ], $placeholder );
}
}
output_page_header( $id, $messages );
?>
<form method="post" action="admin.php?action=mercator-aliases">
<?php $wp_list_table->display(); ?>
</form>
<?php
output_page_footer();
}
/**
* Validate alias parameters
*
* @param array $params Raw input parameters
* @param boolean $check_permission Should we check that the user can edit the network?
* @return array|WP_Error Validated parameters on success, WP_Error otherwise
*/
function validate_alias_parameters( $params, $check_permission = true ) {
$valid = array();
// Validate domain
if ( empty( $params['domain'] ) ) {
return new WP_Error( 'mercator.params.no_domain', __( 'Aliases require a domain name', 'mercator' ) );
}
if ( ! preg_match( '#^[a-z0-9\-.]+$#i', $params['domain'] ) ) {
return new WP_Error( 'mercator.params.domain_invalid_chars', __( 'Domains can only contain alphanumeric characters, dashes (-) and periods (.)', 'mercator' ) );
}
$valid['domain'] = $params['domain'];
// Validate site ID
$valid['site'] = absint( $params['id'] );
if ( empty( $valid['site'] ) ) {
return new WP_Error( 'mercator.params.invalid_site', __( 'Invalid site ID', 'mercator' ) );
}
if ( $check_permission ) {
$details = get_blog_details( $valid['site'] );
// Note: site_id is old terminology, referring to the network ID
if ( ! can_edit_network( $details->site_id ) ) {
return new WP_Error( 'mercator.params.cannot_edit', __( 'You do not have permission to edit this site', 'mercator' ) );
}
}
// Validate active flag
$valid['active'] = empty( $params['active'] ) ? false : true;
return $valid;
}
/**
* Handle submission of the add page
*
* @return array|null List of errors. Issues a redirect and exits on success.
*/
function handle_edit_page_submit( $id, $mapping ) {
$messages = array();
if ( empty( $mapping ) ) {
$did_action = 'add';
check_admin_referer( 'mercator-add-' . $id );
} else {
$did_action = 'edit';
check_admin_referer( 'mercator-edit-' . $mapping->get_id() );
}
// Check that the parameters are correct first
$params = validate_alias_parameters( wp_unslash( $_POST ) );
if ( is_wp_error( $params ) ) {
$messages[] = $params->get_error_message();
if ( $params->get_error_code() === 'mercator.params.domain_invalid_chars' ) {
$messages[] = __( '<strong>Note</strong>: for internationalized domain names, use the ASCII form (e.g, <code>xn--bcher-kva.example</code>)', 'mercator' );
}
return $messages;
}
if ( empty( $mapping ) ) {
// Create the actual mapping
$result = $mapping = Mapping::create( $params['site'], $params['domain'], $params['active'] );
} else {
// Update our existing
$result = $mapping->update( $params );
}
if ( is_wp_error( $result ) ) {
$messages[] = $result->get_error_message();
return $messages;
}
// Success, redirect to alias page
$location = add_query_arg(
array(
'action' => 'mercator-aliases',
'id' => $id,
'did_action' => $did_action,
'mappings' => $mapping->get_id(),
'processed' => 1,
'_wpnonce' => wp_create_nonce( 'mercator-alias-added-' . $mapping->get_id() ),
),
network_admin_url( 'admin.php' )
);
wp_safe_redirect( $location );
exit;
}
/**
* Output alias editing page
*/
function output_edit_page() {
$id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
if ( ! $id ) {
wp_die( esc_html__( 'Invalid site ID.' ) );
}
$id = absint( $id );
$details = get_blog_details( $id );
if ( ! can_edit_network( $details->site_id ) || (int) $details->blog_id !== $id ) {
wp_die( esc_html__( 'You do not have permission to access this page.' ) );
}
// Are we editing?
$mapping = null;
$form_action = network_admin_url( 'admin.php?action=mercator-add' );
if ( ! empty( $_REQUEST['mapping'] ) ) {
$mapping_id = absint( $_REQUEST['mapping'] );
$mapping = Mapping::get( $mapping_id );
if ( is_wp_error( $mapping ) || empty( $mapping ) ) {
wp_die( esc_html__( 'Invalid alias ID.', 'mercator' ) );
}
$form_action = network_admin_url( 'admin.php?action=mercator-edit' );
}
// Handle form submission
$messages = array();
if ( ! empty( $_POST['submit'] ) ) {
$messages = handle_edit_page_submit( $id, $mapping );
}
output_page_header( $id, $messages );
if ( empty( $mapping ) || ! empty( $_POST['_wpnonce'] ) ) {
$domain = empty( $_POST['domain'] ) ? '' : wp_unslash( $_POST['domain'] );
$active = ! empty( $_POST['active'] );
} else {
$domain = $mapping->get_domain();
$active = $mapping->is_active();
}
?>
<form method="post" action="<?php echo esc_url( $form_action ); ?>">
<table class="form-table">
<tr>
<th scope="row">
<label for="mercator-domain"><?php echo esc_html_x( 'Domain Name', 'field name', 'mercator' ); ?></label>
</th>
<td>
<input type="text" class="regular-text code"
name="domain" id="mercator-domain"
value="<?php echo esc_attr( $domain ); ?>" />
</td>
</tr>
<tr>
<th scope="row">
<?php echo esc_html_x( 'Active', 'field name', 'mercator' ); ?>
</th>
<td>
<label>
<input type="checkbox"
name="active" <?php checked( $active ); ?> />
<?php esc_html_e( 'Mark alias as active', 'mercator' ); ?>
</label>
</td>
</tr>
</table>
<input type="hidden" name="id" value="<?php echo esc_attr( $id ); ?>" />
<?php
if ( empty( $mapping ) ) {
wp_nonce_field( 'mercator-add-' . $id );
submit_button( esc_html__( 'Add Alias', 'mercator' ) );
} else {
echo '<input type="hidden" name="mapping" value="' . esc_attr( $mapping->get_id() ) . '" />';
wp_nonce_field( 'mercator-edit-' . $mapping->get_id() );
submit_button( esc_html__( 'Save Alias', 'mercator' ) );
}
?>
</form>
<?php
output_page_footer();
}
/**
* Add note to sunrise.php on dropins list about Mercator
*
* @param array $meta Meta links
* @param string $file Plugin filename (sunrise.php for sunrise)
* @param array $data Data from the plugin header
* @param string $status Status of the plugin
* @return array Modified meta links
*/
function output_sunrise_dropin_note( $meta, $file, $data, $status ) {
if ( $file !== 'sunrise.php' || $status !== 'dropins' ) {
return $meta;
}
$note = '<em>' . wp_kses( sprintf(
__( 'Enhanced by <a href="%1$s" title="%2$s">Mercator</a>', 'mercator' ),
'https://github.com/humanmade/Mercator',
sprintf(
esc_html__( 'Version %s', 'mercator' ),
\Mercator\VERSION
)
), array(
'a' => array(
'href' => array(),
'title' => array(),
),
) ) . '</em>';
array_unshift( $meta, $note );
return $meta;
}