Make WordPress Core

Ticket #16031: bulk-action-test-plugin.php

File bulk-action-test-plugin.php, 1.8 KB (added by ericlewis, 10 years ago)
Line 
1<?php
2/*
3Plugin Name: Bulk Actions Test
4Plugin URI:
5Description: Tests bulk actions for all screens
6Author: Matt van Andel
7Version: 1.0
8Author URI:
9*/
10
11add_filter( 'pre_option_link_manager_enabled', '__return_true' );
12
13add_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
31function bulkactiontest_add_bulk_action( $actions ) {
32 $actions['tstbkact'] = 'Test Bulk Action';
33
34 return $actions;
35}
36
37function 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
46function 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
54function bulkactiontest_add_removable_query_args($removable_query_args) {
55 $removable_query_args[] = 'tstbkact';
56 return $removable_query_args;
57}
58add_filter( 'removable_query_args', 'bulkactiontest_add_removable_query_args', 10, 1 );