Allow custom bulk actions in admin list tables.

Bulk action filtering was introduced in 3.1, but only to remove default bulk actions, not add new ones.

Bulk actions can now be registered for all admin list table dropdowns via the `bulk_actions-{get_current_screen()->id}` filter. Handling custom bulk actions can be performed in the corresponding and newly introduced `handle_bulk_actions-${get_current_screen()->id}` filter.

Props scribu, flixos90, Veraxus.
See #16031.


Built from https://develop.svn.wordpress.org/trunk@38647


git-svn-id: http://core.svn.wordpress.org/trunk@38590 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Eric Lewis
2016-09-23 20:33:30 +00:00
parent c3ad13e72e
commit d27606936b
14 changed files with 248 additions and 7 deletions

View File

@@ -122,6 +122,29 @@ if ( $action ) {
$n = 'none';
}
break;
default:
if ( isset( $_POST['checked'] ) ) {
check_admin_referer( 'bulk-themes' );
$themes = (array) $_POST['checked'];
$n = count( $themes );
/**
* Fires when a custom bulk action should be handled.
*
* The redirect link should be modified with success or failure feedback
* from the action to be used to display feedback to the user.
*
* @since 4.7.0
*
* @param string $referer The redirect URL.
* @param string $action The action being taken.
* @param array $themes The themes to take the action on.
* @param int $site_id The current site id
*/
$referer = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $referer, $action, $themes, $id );
} else {
$action = 'error';
$n = 'none';
}
}
update_option( 'allowedthemes', $allowed_themes );

View File

@@ -164,6 +164,28 @@ if ( $action ) {
$update = 'err_promote';
}
break;
default:
if ( ! isset( $_REQUEST['users'] ) ) {
break;
}
check_admin_referer( 'bulk-users' );
$userids = $_REQUEST['users'];
/**
* Fires when a custom bulk action should be handled.
*
* The redirect link should be modified with success or failure feedback
* from the action to be used to display feedback to the user.
*
* @since 4.7.0
*
* @param string $referer The redirect URL.
* @param string $action The action being taken.
* @param array $userids The users to take the action on.
* @param int $id The id of the current site
*/
$referer = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $referer, $action, $userids, $id );
$update = $action;
break;
}
wp_safe_redirect( add_query_arg( 'update', $update, $referer ) );

View File

@@ -160,6 +160,26 @@ if ( isset( $_GET['action'] ) ) {
wp_die( __( 'Sorry, you are not allowed to change the current site.' ) );
}
}
if ( ! in_array( $doaction, array( 'delete', 'spam', 'notspam' ), true ) ) {
$redirect_to = wp_get_referer();
$blogs = (array) $_POST['allblogs'];
/**
* Fires when a custom bulk action should be handled.
*
* The redirect link should be modified with success or failure feedback
* from the action to be used to display feedback to the user.
*
* @since 4.7.0
*
* @param string $redirect_to The redirect URL.
* @param string $doaction The action being taken.
* @param array $blogs The blogs to take the action on.
* @param int $site_id The current site id.
*/
$redirect_to = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $redirect_to, $doaction, $blogs, $id );
wp_safe_redirect( $redirect_to );
exit();
}
} else {
$location = network_admin_url( 'sites.php' );
if ( ! empty( $_REQUEST['paged'] ) ) {

View File

@@ -195,7 +195,32 @@ if ( $action ) {
's' => $s
), network_admin_url( 'themes.php' ) ) );
exit;
default:
$themes = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
if ( empty( $themes ) ) {
wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
exit;
}
check_admin_referer( 'bulk-themes' );
/**
* Fires when a custom bulk action should be handled.
*
* The redirect link should be modified with success or failure feedback
* from the action to be used to display feedback to the user.
*
* @since 4.7.0
*
* @param string $referer The redirect URL.
* @param string $action The action being taken.
* @param array $themes The themes to take the action on.
*/
$referer = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $referer, $action, $themes );
wp_safe_redirect( $referer );
exit;
}
}
$wp_list_table->prepare_items();

View File

@@ -93,6 +93,28 @@ if ( isset( $_GET['action'] ) ) {
}
}
if ( ! in_array( $doaction, array( 'delete', 'spam', 'notspam' ), true ) ) {
$sendback = wp_get_referer();
$user_ids = (array) $_POST['allusers'];
/**
* Fires when a custom bulk action should be handled.
*
* The sendback link should be modified with success or failure feedback
* from the action to be used to display feedback to the user.
*
* @since 4.7.0
*
* @param string $sendback The redirect URL.
* @param string $doaction The action being taken.
* @param array $user_ids The users to take the action on.
*/
$sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $doaction, $user_ids );
wp_safe_redirect( $sendback );
exit();
}
wp_safe_redirect( add_query_arg( array( 'updated' => 'true', 'action' => $userfunction ), wp_get_referer() ) );
} else {
$location = network_admin_url( 'users.php' );