| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Bulk Actions Test |
|---|
| 4 | Plugin URI: |
|---|
| 5 | Description: Tests bulk actions for all screens |
|---|
| 6 | Author: Matt van Andel |
|---|
| 7 | Version: 1.0 |
|---|
| 8 | Author URI: |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | add_filter( 'pre_option_link_manager_enabled', '__return_true' ); |
|---|
| 12 | |
|---|
| 13 | add_action( 'current_screen', function () { |
|---|
| 14 | |
|---|
| 15 | // The query arg needs to be removed from the REQUEST_URI so the message doesn't |
|---|
| 16 | // persist, see wp-admin/edit.php |
|---|
| 17 | $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'tstbkact' ), $_SERVER['REQUEST_URI'] ); |
|---|
| 18 | |
|---|
| 19 | // Add new bulk action to all screens |
|---|
| 20 | add_filter( 'bulk_actions-' . get_current_screen()->id, 'bulkactiontest_add_bulk_action' ); |
|---|
| 21 | |
|---|
| 22 | // Add new bulk action handler to all screens |
|---|
| 23 | add_filter( 'handle_bulk_actions-' . get_current_screen()->id, 'bulkactiontest_handle_bulk_action', 10, 3 ); |
|---|
| 24 | |
|---|
| 25 | // Display notice after bulk action is handled |
|---|
| 26 | add_action( 'admin_notices', 'bulkactiontest_admin_notice' ); |
|---|
| 27 | add_action( 'network_admin_notices', 'bulkactiontest_admin_notice' ); |
|---|
| 28 | } ); |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | function bulkactiontest_add_bulk_action( $actions ) { |
|---|
| 32 | $actions['tstbkact'] = 'Test Bulk Action'; |
|---|
| 33 | |
|---|
| 34 | return $actions; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | function bulkactiontest_handle_bulk_action( $sendback, $action = false, $items = false, $site_id = false ) { |
|---|
| 38 | if ( $action === 'tstbkact' ) { |
|---|
| 39 | $sendback = add_query_arg( 'tstbkact', count( $items ), $sendback ); |
|---|
| 40 | return $sendback; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | return $sendback; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | function bulkactiontest_admin_notice() { |
|---|
| 47 | $current_screen = get_current_screen(); |
|---|
| 48 | |
|---|
| 49 | if ( isset( $_REQUEST['tstbkact'] ) ) { |
|---|
| 50 | printf( '<div id="message" class="updated fade"><p>Test bulk action caught for %s items on %s!</p></div>', $_REQUEST['tstbkact'], $current_screen->id ); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | function bulkactiontest_add_removable_query_args($removable_query_args) { |
|---|
| 55 | $removable_query_args[] = 'tstbkact'; |
|---|
| 56 | return $removable_query_args; |
|---|
| 57 | } |
|---|
| 58 | add_filter( 'removable_query_args', 'bulkactiontest_add_removable_query_args', 10, 1 ); |
|---|