From d27606936bb60191eb5902c6d1226ecc9bca2059 Mon Sep 17 00:00:00 2001 From: Eric Lewis Date: Fri, 23 Sep 2016 20:33:30 +0000 Subject: [PATCH] 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 --- wp-admin/edit-comments.php | 16 ++++++++++++++ wp-admin/edit-tags.php | 22 ++++++++++++++++++- wp-admin/edit.php | 15 +++++++++++++ wp-admin/includes/class-wp-list-table.php | 3 +-- wp-admin/link-manager.php | 23 +++++++++++++++++--- wp-admin/network/site-themes.php | 23 ++++++++++++++++++++ wp-admin/network/site-users.php | 22 +++++++++++++++++++ wp-admin/network/sites.php | 20 +++++++++++++++++ wp-admin/network/themes.php | 25 ++++++++++++++++++++++ wp-admin/network/users.php | 22 +++++++++++++++++++ wp-admin/plugins.php | 26 +++++++++++++++++++++++ wp-admin/upload.php | 14 ++++++++++++ wp-admin/users.php | 22 +++++++++++++++++++ wp-includes/version.php | 2 +- 14 files changed, 248 insertions(+), 7 deletions(-) diff --git a/wp-admin/edit-comments.php b/wp-admin/edit-comments.php index f0ecec92e8..d1f62b49ba 100644 --- a/wp-admin/edit-comments.php +++ b/wp-admin/edit-comments.php @@ -82,6 +82,22 @@ if ( $doaction ) { } } + if ( ! in_array( $doaction, array( 'approve', 'unapprove', 'spam', 'unspam', 'trash', 'delete' ), true ) ) { + /** + * 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 $comment_ids The comments to take the action on. + */ + $redirect_to = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $redirect_to, $doaction, $comment_ids ); + } + wp_defer_comment_counting( false ); if ( $approved ) diff --git a/wp-admin/edit-tags.php b/wp-admin/edit-tags.php index 37991e0d8f..76e7204bd9 100644 --- a/wp-admin/edit-tags.php +++ b/wp-admin/edit-tags.php @@ -195,6 +195,26 @@ case 'editedtag': else $location = add_query_arg( array( 'error' => true, 'message' => 5 ), $location ); break; +default: + if ( ! $wp_list_table->current_action() || ! isset( $_REQUEST['delete_tags'] ) ) { + break; + } + check_admin_referer( 'bulk-tags' ); + $tags = (array) $_REQUEST['delete_tags']; + /** + * 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 $location The redirect URL. + * @param string $action The action being taken. + * @param array $tags The tag IDs to take the action on. + */ + $location = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $location, $wp_list_table->current_action(), $tags ); + break; } if ( ! $location && ! empty( $_REQUEST['_wp_http_referer'] ) ) { @@ -210,7 +230,7 @@ if ( $location ) { * Filters the taxonomy redirect destination URL. * * @since 4.6.0 - * + * * @param string $location The destination URL. * @param object $tax The taxonomy object. */ diff --git a/wp-admin/edit.php b/wp-admin/edit.php index 59cb4f1954..120140a144 100644 --- a/wp-admin/edit.php +++ b/wp-admin/edit.php @@ -162,6 +162,21 @@ if ( $doaction ) { } } break; + default: + /** + * 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 $post_ids The post IDs to take the action on. + */ + $sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $doaction, $post_ids ); + break; } $sendback = remove_query_arg( array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback ); diff --git a/wp-admin/includes/class-wp-list-table.php b/wp-admin/includes/class-wp-list-table.php index 68053517db..0ef4de35aa 100644 --- a/wp-admin/includes/class-wp-list-table.php +++ b/wp-admin/includes/class-wp-list-table.php @@ -436,7 +436,7 @@ class WP_List_Table { */ protected function bulk_actions( $which = '' ) { if ( is_null( $this->_actions ) ) { - $no_new_actions = $this->_actions = $this->get_bulk_actions(); + $this->_actions = $this->get_bulk_actions(); /** * Filters the list table Bulk Actions drop-down. * @@ -450,7 +450,6 @@ class WP_List_Table { * @param array $actions An array of the available bulk actions. */ $this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); - $this->_actions = array_intersect_assoc( $this->_actions, $no_new_actions ); $two = ''; } else { $two = '2'; diff --git a/wp-admin/link-manager.php b/wp-admin/link-manager.php index 79abe65b77..3937fd7f3d 100644 --- a/wp-admin/link-manager.php +++ b/wp-admin/link-manager.php @@ -19,17 +19,34 @@ $doaction = $wp_list_table->current_action(); if ( $doaction && isset( $_REQUEST['linkcheck'] ) ) { check_admin_referer( 'bulk-bookmarks' ); + $redirect_to = admin_url( 'link-manager.php' ); + $bulklinks = (array) $_REQUEST['linkcheck']; + if ( 'delete' == $doaction ) { - $bulklinks = (array) $_REQUEST['linkcheck']; foreach ( $bulklinks as $link_id ) { $link_id = (int) $link_id; wp_delete_link( $link_id ); } - wp_redirect( add_query_arg('deleted', count( $bulklinks ), admin_url( 'link-manager.php' ) ) ); - exit; + $redirect_to = add_query_arg( 'deleted', count( $bulklinks ), $redirect_to ); + } else { + /** + * 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 $bulklinks The links to take the action on. + */ + $redirect_to = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $redirect_to, $doaction, $bulklinks ); } + wp_redirect( $redirect_to ); + exit; } elseif ( ! empty( $_GET['_wp_http_referer'] ) ) { wp_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); exit; diff --git a/wp-admin/network/site-themes.php b/wp-admin/network/site-themes.php index 1e9b92a6e3..91355c394d 100644 --- a/wp-admin/network/site-themes.php +++ b/wp-admin/network/site-themes.php @@ -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 ); diff --git a/wp-admin/network/site-users.php b/wp-admin/network/site-users.php index 59d30c2386..36ce31ae17 100644 --- a/wp-admin/network/site-users.php +++ b/wp-admin/network/site-users.php @@ -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 ) ); diff --git a/wp-admin/network/sites.php b/wp-admin/network/sites.php index 38aa2ec49d..9b3343c89b 100644 --- a/wp-admin/network/sites.php +++ b/wp-admin/network/sites.php @@ -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'] ) ) { diff --git a/wp-admin/network/themes.php b/wp-admin/network/themes.php index 171afc359d..40530ba2bd 100644 --- a/wp-admin/network/themes.php +++ b/wp-admin/network/themes.php @@ -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(); diff --git a/wp-admin/network/users.php b/wp-admin/network/users.php index cc3edc2ca3..437f76538c 100644 --- a/wp-admin/network/users.php +++ b/wp-admin/network/users.php @@ -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' ); diff --git a/wp-admin/plugins.php b/wp-admin/plugins.php index 0e59ed93a1..6474e4691f 100644 --- a/wp-admin/plugins.php +++ b/wp-admin/plugins.php @@ -356,7 +356,33 @@ if ( $action ) { update_site_option( 'recently_activated', array() ); } break; + + default: + if ( isset( $_POST['checked'] ) ) { + check_admin_referer('bulk-plugins'); + $plugins = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array(); + $sendback = wp_get_referer(); + + /** + * 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 $action The action being taken. + * @param array $plugins The plugins to take the action on. + */ + $sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $action, $plugins ); + + wp_safe_redirect( $sendback ); + exit; + } + break; } + } $wp_list_table->prepare_items(); diff --git a/wp-admin/upload.php b/wp-admin/upload.php index b9495ac059..654902353e 100644 --- a/wp-admin/upload.php +++ b/wp-admin/upload.php @@ -163,6 +163,20 @@ if ( $doaction ) { } $location = add_query_arg( 'deleted', count( $post_ids ), $location ); break; + default: + /** + * 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 $location The redirect URL. + * @param string $doaction The action being taken. + * @param array $post_ids The posts to take the action on. + */ + $location = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $location, $doaction, $post_ids ); } wp_redirect( $location ); diff --git a/wp-admin/users.php b/wp-admin/users.php index 194ced94d0..42f34f7fe7 100644 --- a/wp-admin/users.php +++ b/wp-admin/users.php @@ -410,6 +410,28 @@ default: exit; } + if ( $wp_list_table->current_action() && ! empty( $_REQUEST['users'] ) ) { + $userids = $_REQUEST['users']; + $sendback = wp_get_referer(); + + /** + * 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 $action The action being taken. + * @param array $userids The users to take the action on. + */ + $sendback = apply_filters( 'handle_bulk_actions-' . get_current_screen()->id, $sendback, $wp_list_table->current_action(), $userids ); + + wp_safe_redirect( $sendback ); + exit; + } + $wp_list_table->prepare_items(); $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' ); if ( $pagenum > $total_pages && $total_pages > 0 ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 03698f1f9d..c3232dbfb2 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38646'; +$wp_version = '4.7-alpha-38647'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.