Skip to content

Commit b80f6f4

Browse files
committed
Refactor user deletion form, improve UI.
1 parent 41840c4 commit b80f6f4

File tree

4 files changed

+109
-104
lines changed

4 files changed

+109
-104
lines changed

src/wp-admin/includes/deprecated.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,3 +1589,14 @@ function image_attachment_fields_to_save( $post, $attachment ) {
15891589

15901590
return $post;
15911591
}
1592+
1593+
/**
1594+
* Was used to add JavaScript to the delete users form.
1595+
*
1596+
* @since 3.5.0
1597+
* @deprecated 6.9.0
1598+
* @access private
1599+
*/
1600+
function delete_users_add_js() {
1601+
_deprecated_function( __FUNCTION__, '6.9.0' );
1602+
}

src/wp-admin/includes/ms.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ function confirm_delete_users( $users ) {
874874
<p><?php _e( 'You have chosen to delete the following users from all networks and sites.' ); ?></p>
875875
<?php endif; ?>
876876

877-
<form action="users.php?action=dodelete" method="post">
877+
<form action="users.php?action=dodelete" method="post" class="delete-and-reassign-users-form">
878878
<input type="hidden" name="dodelete" />
879879
<?php
880880
wp_nonce_field( 'ms-users-delete' );
@@ -949,22 +949,31 @@ function confirm_delete_users( $users ) {
949949
printf( __( 'Site: %s' ), $user_site );
950950
?>
951951
</li>
952-
<?php // TODO: Fix duplicate IDs ?>
953-
<li><label><input type="radio" id="delete_option0" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="delete" required />
954-
<?php _e( 'Delete all content.' ); ?></label></li>
955-
<li><label><input type="radio" id="delete_option1" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="reassign" required />
956-
<?php _e( 'Attribute all content to:' ); ?></label>
957-
<?php
958-
wp_dropdown_users(
959-
array(
960-
'name' => "blog[$user_id][$key]",
961-
'include' => $blog_users,
962-
'show' => 'display_name_with_login',
963-
)
964-
);
965-
?>
952+
<li>
953+
<label>
954+
<input type="radio" id="delete_option_<?php echo esc_attr( $details->userblog_id . '_' . $delete_user->ID ); ?>" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="delete" required />
955+
<?php _e( 'Delete all content.' ); ?>
956+
</label>
957+
</li>
958+
<li>
959+
<label>
960+
<input type="radio" id="reassign_option_<?php echo esc_attr( $details->userblog_id . '_' . $delete_user->ID ); ?>" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="reassign" required />
961+
<?php _e( 'Attribute all content to:' ); ?>
962+
</label>
966963

964+
<?php
965+
wp_dropdown_users(
966+
array(
967+
'show_option_none' => __( 'Select a user' ),
968+
'name' => "blog[$user_id][$key]",
969+
'include' => $blog_users,
970+
'show' => 'display_name_with_login',
971+
'id' => "reassign_user_{$details->userblog_id}_{$delete_user->ID}",
972+
)
973+
);
974+
?>
967975

976+
</li>
968977
</li>
969978
</ul>
970979
<?php

src/wp-admin/includes/user.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -561,26 +561,6 @@ function default_password_nag() {
561561
);
562562
}
563563

564-
/**
565-
* @since 3.5.0
566-
* @access private
567-
*/
568-
function delete_users_add_js() {
569-
?>
570-
<script>
571-
jQuery( function($) {
572-
var submit = $('#submit').prop('disabled', true);
573-
$('input[name="delete_option"]').one('change', function() {
574-
submit.prop('disabled', false);
575-
});
576-
$('#reassign_user').focus( function() {
577-
$('#delete_option1').prop('checked', true).trigger('change');
578-
});
579-
} );
580-
</script>
581-
<?php
582-
}
583-
584564
/**
585565
* Optional SSL preference that can be turned on by hooking to the 'personal_options' action.
586566
*

src/wp-admin/users.php

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,12 @@
206206
continue;
207207
}
208208

209-
switch ( $_REQUEST['delete_option'] ) {
209+
switch ( $_REQUEST['delete_option'][ $id ] ) {
210210
case 'delete':
211211
wp_delete_user( $id );
212212
break;
213213
case 'reassign':
214-
wp_delete_user( $id, $_REQUEST['reassign_user'] );
214+
wp_delete_user( $id, $_REQUEST['reassign_user'][ $id ] );
215215
break;
216216
}
217217

@@ -299,40 +299,9 @@
299299
$user_ids = array_diff( $user_ids, array( $current_user->ID ) );
300300
}
301301

302-
/**
303-
* Filters whether the users being deleted have additional content
304-
* associated with them outside of the `post_author` and `link_owner` relationships.
305-
*
306-
* @since 5.2.0
307-
*
308-
* @param bool $users_have_additional_content Whether the users have additional content. Default false.
309-
* @param int[] $user_ids Array of IDs for users being deleted.
310-
*/
311-
$users_have_content = (bool) apply_filters( 'users_have_additional_content', false, $user_ids );
312-
313-
if ( $user_ids && ! $users_have_content ) {
314-
if ( $wpdb->get_var(
315-
"SELECT ID FROM {$wpdb->posts}
316-
WHERE post_author IN( " . implode( ',', $user_ids ) . ' )
317-
LIMIT 1'
318-
) ) {
319-
$users_have_content = true;
320-
} elseif ( $wpdb->get_var(
321-
"SELECT link_id FROM {$wpdb->links}
322-
WHERE link_owner IN( " . implode( ',', $user_ids ) . ' )
323-
LIMIT 1'
324-
) ) {
325-
$users_have_content = true;
326-
}
327-
}
328-
329-
if ( $users_have_content ) {
330-
add_action( 'admin_head', 'delete_users_add_js' );
331-
}
332-
333302
require_once ABSPATH . 'wp-admin/admin-header.php';
334303
?>
335-
<form method="post" name="updateusers" id="updateusers">
304+
<form method="post" name="updateusers" id="updateusers" class="delete-and-reassign-users-form">
336305
<?php wp_nonce_field( 'delete-users' ); ?>
337306
<?php echo $referer; ?>
338307

@@ -359,6 +328,7 @@
359328
<ul>
360329
<?php
361330
$go_delete = 0;
331+
$users_have_content = false;
362332

363333
foreach ( $all_user_ids as $id ) {
364334
$user = get_userdata( $id );
@@ -384,6 +354,76 @@
384354
$id,
385355
$user->user_login
386356
);
357+
358+
/**
359+
* Filters whether the users being deleted have additional content
360+
* associated with them outside of the `post_author` and `link_owner` relationships.
361+
*
362+
* @since 5.2.0
363+
*
364+
* @param bool $users_have_additional_content Whether the users have additional content. Default false.
365+
* @param int[] $user_ids Array of IDs for users being deleted.
366+
*/
367+
$user_has_content = (bool) apply_filters( 'users_have_additional_content', false, array( $id ) );
368+
369+
if ( ! $user_has_content ) {
370+
if ( $wpdb->get_var(
371+
$wpdb->prepare(
372+
"SELECT ID FROM {$wpdb->posts}
373+
WHERE post_author = %d
374+
LIMIT 1",
375+
$id
376+
)
377+
) ) {
378+
$user_has_content = true;
379+
} elseif ( $wpdb->get_var(
380+
$wpdb->prepare(
381+
"SELECT link_id FROM {$wpdb->links}
382+
WHERE link_owner = %d
383+
LIMIT 1",
384+
$id
385+
)
386+
) ) {
387+
$user_has_content = true;
388+
}
389+
}
390+
391+
if ( ! $user_has_content ) {
392+
?>
393+
<input type="hidden" name="delete_option[<?php echo esc_attr( $id ); ?>]" value="delete" required />
394+
<p><legend><?php _e( 'This user does not have any content.' ); ?></legend></p>
395+
<?php
396+
} else {
397+
?>
398+
<fieldset>
399+
<p><legend><?php _e( 'What should be done with content owned by this user?' ); ?></legend></p>
400+
401+
<ul style="list-style:none;">
402+
<li>
403+
<input type="radio" id="delete_option_<?php echo esc_attr( $id ); ?>" name="delete_option[<?php echo esc_attr( $id ); ?>]" value="delete" required />
404+
<label for="delete_option_<?php echo esc_attr( $id ); ?>"><?php _e( 'Delete all content.' ); ?></label>
405+
</li>
406+
<li>
407+
<input type="radio" id="reassign_option_<?php echo esc_attr( $id ); ?>" name="delete_option[<?php echo esc_attr( $id ); ?>]" value="reassign" required />
408+
<label for="reassign_option_<?php echo esc_attr( $id ); ?>"><?php _e( 'Attribute all content to:' ); ?></label>
409+
<?php
410+
wp_dropdown_users(
411+
array(
412+
'show_option_none' => __( 'Select a user' ),
413+
'name' => 'reassign_user[' . $id . ']',
414+
'id' => 'reassign_user_' . $id,
415+
'exclude' => $user_ids,
416+
'show' => 'display_name_with_login',
417+
)
418+
);
419+
?>
420+
</li>
421+
</ul>
422+
</fieldset>
423+
<?php
424+
$users_have_content = true;
425+
}
426+
387427
echo "</li>\n";
388428

389429
++$go_delete;
@@ -394,41 +434,6 @@
394434

395435
<?php
396436
if ( $go_delete ) :
397-
398-
if ( ! $users_have_content ) :
399-
?>
400-
<input type="hidden" name="delete_option" value="delete" />
401-
<?php else : ?>
402-
<fieldset>
403-
<?php if ( 1 === $go_delete ) : ?>
404-
<p><legend><?php _e( 'What should be done with content owned by this user?' ); ?></legend></p>
405-
<?php else : ?>
406-
<p><legend><?php _e( 'What should be done with content owned by these users?' ); ?></legend></p>
407-
<?php endif; ?>
408-
409-
<ul style="list-style:none;">
410-
<li>
411-
<input type="radio" id="delete_option0" name="delete_option" value="delete" />
412-
<label for="delete_option0"><?php _e( 'Delete all content.' ); ?></label>
413-
</li>
414-
<li>
415-
<input type="radio" id="delete_option1" name="delete_option" value="reassign" />
416-
<label for="delete_option1"><?php _e( 'Attribute all content to:' ); ?></label>
417-
<?php
418-
wp_dropdown_users(
419-
array(
420-
'name' => 'reassign_user',
421-
'exclude' => $user_ids,
422-
'show' => 'display_name_with_login',
423-
)
424-
);
425-
?>
426-
</li>
427-
</ul>
428-
</fieldset>
429-
<?php
430-
endif;
431-
432437
/**
433438
* Fires at the end of the delete users form prior to the confirm button.
434439
*

0 commit comments

Comments
 (0)