Skip to content

Commit c75fb5e

Browse files
author
Eric Andrew Lewis
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@38647 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8bc6917 commit c75fb5e

13 files changed

Lines changed: 247 additions & 6 deletions

src/wp-admin/edit-comments.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@
8282
}
8383
}
8484

85+
if ( ! in_array( $doaction, array( 'approve', 'unapprove', 'spam', 'unspam', 'trash', 'delete' ), true ) ) {
86+
/**
87+
* Fires when a custom bulk action should be handled.
88+
*
89+
* The redirect link should be modified with success or failure feedback
90+
* from the action to be used to display feedback to the user.
91+
*
92+
* @since 4.7.0
93+
*
94+
* @param string $redirect_to The redirect URL.
95+
* @param string $doaction The action being taken.
96+
* @param array $comment_ids The comments to take the action on.
97+
*/
98+
$redirect_to = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $redirect_to, $doaction, $comment_ids );
99+
}
100+
85101
wp_defer_comment_counting( false );
86102

87103
if ( $approved )

src/wp-admin/edit-tags.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,26 @@
195195
else
196196
$location = add_query_arg( array( 'error' => true, 'message' => 5 ), $location );
197197
break;
198+
default:
199+
if ( ! $wp_list_table->current_action() || ! isset( $_REQUEST['delete_tags'] ) ) {
200+
break;
201+
}
202+
check_admin_referer( 'bulk-tags' );
203+
$tags = (array) $_REQUEST['delete_tags'];
204+
/**
205+
* Fires when a custom bulk action should be handled.
206+
*
207+
* The sendback link should be modified with success or failure feedback
208+
* from the action to be used to display feedback to the user.
209+
*
210+
* @since 4.7.0
211+
*
212+
* @param string $location The redirect URL.
213+
* @param string $action The action being taken.
214+
* @param array $tags The tag IDs to take the action on.
215+
*/
216+
$location = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $location, $wp_list_table->current_action(), $tags );
217+
break;
198218
}
199219

200220
if ( ! $location && ! empty( $_REQUEST['_wp_http_referer'] ) ) {
@@ -210,7 +230,7 @@
210230
* Filters the taxonomy redirect destination URL.
211231
*
212232
* @since 4.6.0
213-
*
233+
*
214234
* @param string $location The destination URL.
215235
* @param object $tax The taxonomy object.
216236
*/

src/wp-admin/edit.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@
162162
}
163163
}
164164
break;
165+
default:
166+
/**
167+
* Fires when a custom bulk action should be handled.
168+
*
169+
* The sendback link should be modified with success or failure feedback
170+
* from the action to be used to display feedback to the user.
171+
*
172+
* @since 4.7.0
173+
*
174+
* @param string $sendback The redirect URL.
175+
* @param string $doaction The action being taken.
176+
* @param array $post_ids The post IDs to take the action on.
177+
*/
178+
$sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $doaction, $post_ids );
179+
break;
165180
}
166181

167182
$sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback );

src/wp-admin/includes/class-wp-list-table.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ protected function get_bulk_actions() {
436436
*/
437437
protected function bulk_actions( $which = '' ) {
438438
if ( is_null( $this->_actions ) ) {
439-
$no_new_actions = $this->_actions = $this->get_bulk_actions();
439+
$this->_actions = $this->get_bulk_actions();
440440
/**
441441
* Filters the list table Bulk Actions drop-down.
442442
*
@@ -450,7 +450,6 @@ protected function bulk_actions( $which = '' ) {
450450
* @param array $actions An array of the available bulk actions.
451451
*/
452452
$this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions );
453-
$this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions );
454453
$two = '';
455454
} else {
456455
$two = '2';

src/wp-admin/link-manager.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,34 @@
1919
if ( $doaction && isset( $_REQUEST['linkcheck'] ) ) {
2020
check_admin_referer( 'bulk-bookmarks' );
2121

22+
$redirect_to = admin_url( 'link-manager.php' );
23+
$bulklinks = (array) $_REQUEST['linkcheck'];
24+
2225
if ( 'delete' == $doaction ) {
23-
$bulklinks = (array) $_REQUEST['linkcheck'];
2426
foreach ( $bulklinks as $link_id ) {
2527
$link_id = (int) $link_id;
2628

2729
wp_delete_link( $link_id );
2830
}
2931

30-
wp_redirect( add_query_arg('deleted', count( $bulklinks ), admin_url( 'link-manager.php' ) ) );
31-
exit;
32+
$redirect_to = add_query_arg( 'deleted', count( $bulklinks ), $redirect_to );
33+
} else {
34+
/**
35+
* Fires when a custom bulk action should be handled.
36+
*
37+
* The redirect link should be modified with success or failure feedback
38+
* from the action to be used to display feedback to the user.
39+
*
40+
* @since 4.7.0
41+
*
42+
* @param string $redirect_to The redirect URL.
43+
* @param string $doaction The action being taken.
44+
* @param array $bulklinks The links to take the action on.
45+
*/
46+
$redirect_to = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $redirect_to, $doaction, $bulklinks );
3247
}
48+
wp_redirect( $redirect_to );
49+
exit;
3350
} elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
3451
wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
3552
exit;

src/wp-admin/network/site-themes.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,29 @@
122122
$n = 'none';
123123
}
124124
break;
125+
default:
126+
if ( isset( $_POST['checked'] ) ) {
127+
check_admin_referer( 'bulk-themes' );
128+
$themes = (array) $_POST['checked'];
129+
$n = count( $themes );
130+
/**
131+
* Fires when a custom bulk action should be handled.
132+
*
133+
* The redirect link should be modified with success or failure feedback
134+
* from the action to be used to display feedback to the user.
135+
*
136+
* @since 4.7.0
137+
*
138+
* @param string $referer The redirect URL.
139+
* @param string $action The action being taken.
140+
* @param array $themes The themes to take the action on.
141+
* @param int $site_id The current site id
142+
*/
143+
$referer = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $referer, $action, $themes, $id );
144+
} else {
145+
$action = 'error';
146+
$n = 'none';
147+
}
125148
}
126149

127150
update_option( 'allowedthemes', $allowed_themes );

src/wp-admin/network/site-users.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@
164164
$update = 'err_promote';
165165
}
166166
break;
167+
default:
168+
if ( ! isset( $_REQUEST['users'] ) ) {
169+
break;
170+
}
171+
check_admin_referer( 'bulk-users' );
172+
$userids = $_REQUEST['users'];
173+
/**
174+
* Fires when a custom bulk action should be handled.
175+
*
176+
* The redirect link should be modified with success or failure feedback
177+
* from the action to be used to display feedback to the user.
178+
*
179+
* @since 4.7.0
180+
*
181+
* @param string $referer The redirect URL.
182+
* @param string $action The action being taken.
183+
* @param array $userids The users to take the action on.
184+
* @param int $id The id of the current site
185+
*/
186+
$referer = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $referer, $action, $userids, $id );
187+
$update = $action;
188+
break;
167189
}
168190

169191
wp_safe_redirect( add_query_arg( 'update', $update, $referer ) );

src/wp-admin/network/sites.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@
160160
wp_die( __( 'Sorry, you are not allowed to change the current site.' ) );
161161
}
162162
}
163+
if ( ! in_array( $doaction, array( 'delete', 'spam', 'notspam' ), true ) ) {
164+
$redirect_to = wp_get_referer();
165+
$blogs = (array) $_POST['allblogs'];
166+
/**
167+
* Fires when a custom bulk action should be handled.
168+
*
169+
* The redirect link should be modified with success or failure feedback
170+
* from the action to be used to display feedback to the user.
171+
*
172+
* @since 4.7.0
173+
*
174+
* @param string $redirect_to The redirect URL.
175+
* @param string $doaction The action being taken.
176+
* @param array $blogs The blogs to take the action on.
177+
* @param int $site_id The current site id.
178+
*/
179+
$redirect_to = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $redirect_to, $doaction, $blogs, $id );
180+
wp_safe_redirect( $redirect_to );
181+
exit();
182+
}
163183
} else {
164184
$location = network_admin_url( 'sites.php' );
165185
if ( ! empty( $_REQUEST['paged'] ) ) {

src/wp-admin/network/themes.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,32 @@
195195
's' => $s
196196
), network_admin_url( 'themes.php' ) ) );
197197
exit;
198+
default:
199+
$themes = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
200+
if ( empty( $themes ) ) {
201+
wp_safe_redirect( add_query_arg( 'error', 'none', $referer ) );
202+
exit;
203+
}
204+
check_admin_referer( 'bulk-themes' );
205+
206+
/**
207+
* Fires when a custom bulk action should be handled.
208+
*
209+
* The redirect link should be modified with success or failure feedback
210+
* from the action to be used to display feedback to the user.
211+
*
212+
* @since 4.7.0
213+
*
214+
* @param string $referer The redirect URL.
215+
* @param string $action The action being taken.
216+
* @param array $themes The themes to take the action on.
217+
*/
218+
$referer = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $referer, $action, $themes );
219+
220+
wp_safe_redirect( $referer );
221+
exit;
198222
}
223+
199224
}
200225

201226
$wp_list_table->prepare_items();

src/wp-admin/network/users.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@
9393
}
9494
}
9595

96+
if ( ! in_array( $doaction, array( 'delete', 'spam', 'notspam' ), true ) ) {
97+
$sendback = wp_get_referer();
98+
99+
$user_ids = (array) $_POST['allusers'];
100+
/**
101+
* Fires when a custom bulk action should be handled.
102+
*
103+
* The sendback link should be modified with success or failure feedback
104+
* from the action to be used to display feedback to the user.
105+
*
106+
* @since 4.7.0
107+
*
108+
* @param string $sendback The redirect URL.
109+
* @param string $doaction The action being taken.
110+
* @param array $user_ids The users to take the action on.
111+
*/
112+
$sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $doaction, $user_ids );
113+
114+
wp_safe_redirect( $sendback );
115+
exit();
116+
}
117+
96118
wp_safe_redirect( add_query_arg( array( 'updated' => 'true', 'action' => $userfunction ), wp_get_referer() ) );
97119
} else {
98120
$location = network_admin_url( 'users.php' );

0 commit comments

Comments
 (0)