[wp-trac] [WordPress Trac] #16031: New bulk actions hook missing catcher behavior

WordPress Trac noreply at wordpress.org
Sat Aug 29 23:05:33 UTC 2015


#16031: New bulk actions hook missing catcher behavior
----------------------------+-----------------------------
 Reporter:  Veraxus         |       Owner:
     Type:  enhancement     |      Status:  assigned
 Priority:  normal          |   Milestone:  Future Release
Component:  Administration  |     Version:  3.1
 Severity:  normal          |  Resolution:
 Keywords:  needs-patch     |     Focuses:
----------------------------+-----------------------------

Comment (by Veraxus):

 FYI, I'm giving this another go and should have a patch ready by tonight
 or tomorrow (PST).

 There are several hurdles, which I ''think'' I've solved…

 1. The hook cannot be located inside `WP_List_Table`; This is because…
 1. Redirects are involved on almost every screen when bulk actions are
 handled. Bulk actions are handled, a redirect string is created, and the
 browser is redirected to a "cleaned" URL to prevent duplicated execution.
 1. The various admin screens have dramatically different bulk action &
 redirect handling… ''and'' different conventions.
 1. Admin notices must also be catchable post-bulk-handling (which requires
 that the redirect URL be filterable).

 To get a proper handler hook, I'm going through every single admin screen
 that uses a list table and (where appropriate) adding a new dynamic filter
 following any other hard-coded bulk actions. The filter allows
 modification of the (1) redirect string and passes (2) the selected
 objects and (3) the current action as additional arguments.

 The filter looks like this (although it is, by necessity, tailored to each
 screen's variables)…

 {{{
 /**
  * A filter that allows for handling of custom bulk actions.
  *
  * The dynamic portion of the hook name, `$current_screen->id`, refers
  * to the ID of the current screen, usually a string.
  *
  * @since 4.4.0
  *
  * @param string $redirect_to A string containing the post-action redirect
 URI
  * @param array $object_ids An array of the selected objects to execute
 action on
  * @param string $doaction The current bulk action
  */
 $redirect_to =
 apply_filters("handle_other_bulk_action-{$current_screen->id}",
 $redirect_to, $object_ids, $doaction );
 }}}

 Usage works like so…

 {{{
 $screen = 'edit-post'; // for example

 // Add custom bulk action
 add_filter( 'bulk_actions-'.$screen, function ( $actions ) {
         //unset( $actions['trash'] );
         $actions['custom'] = __( 'Custom' );
         return $actions;
 } );

 // Handle custom bulk action & modify redirect for admin notice
 add_action( 'handle_other_bulk_action-'.$screen, function ( $redirect_to,
 $items, $current_action ) {
         $redirect_to = remove_query_arg( 'custom_completed', $redirect_to
 );
         if ( 'custom' === $current_action ) {
                 $redirect_to = add_query_arg( 'custom_completed', count(
 $items ), $redirect_to );
         }
         return $redirect_to;
 }, 10, 3 );

 // Output admin notice when bulk handling complete
 add_action( 'admin_notices', function () {
         global $current_screen;

         if ( !empty($_REQUEST['custom_completed']) )
                 echo "<div id=\"message\" class=\"updated fade\"><p>Custom
 bulk action caught for {$_REQUEST['custom_completed']} items on
 {$current_screen->id}!</p></div>";
 } );
 }}}

 Stay tuned.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/16031#comment:59>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list