+$wp_list_table = _get_list_table('WP_Media_List_Table');
+$pagenum = $wp_list_table->get_pagenum();
+
+// Handle bulk actions
+$doaction = $wp_list_table->current_action();
+
+if ( $doaction ) {
+ check_admin_referer('bulk-media');
+
+ if ( 'delete_all' == $doaction ) {
+ $post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type='attachment' AND post_status = 'trash'" );
+ $doaction = 'delete';
+ } elseif ( isset( $_REQUEST['media'] ) ) {
+ $post_ids = $_REQUEST['media'];
+ } elseif ( isset( $_REQUEST['ids'] ) ) {
+ $post_ids = explode( ',', $_REQUEST['ids'] );
+ }
+
+ $location = 'upload.php';
+ if ( $referer = wp_get_referer() ) {
+ if ( false !== strpos( $referer, 'upload.php' ) )
+ $location = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'message', 'ids', 'posted' ), $referer );
+ }
+
+ switch ( $doaction ) {
+ case 'detach':
+ wp_media_attach_action( $_REQUEST['parent_post_id'], 'detach' );
+ break;
+
+ case 'attach':
+ wp_media_attach_action( $_REQUEST['found_post_id'] );
+ break;
+
+ case 'trash':
+ if ( !isset( $post_ids ) )
+ break;
+ foreach ( (array) $post_ids as $post_id ) {
+ if ( !current_user_can( 'delete_post', $post_id ) )
+ wp_die( __( 'You are not allowed to move this post to the trash.' ) );
+
+ if ( !wp_trash_post( $post_id ) )
+ wp_die( __( 'Error in moving to trash.' ) );
+ }
+ $location = add_query_arg( array( 'trashed' => count( $post_ids ), 'ids' => join( ',', $post_ids ) ), $location );
+ break;
+ case 'untrash':
+ if ( !isset( $post_ids ) )
+ break;
+ foreach ( (array) $post_ids as $post_id ) {
+ if ( !current_user_can( 'delete_post', $post_id ) )
+ wp_die( __( 'You are not allowed to move this post out of the trash.' ) );
+
+ if ( !wp_untrash_post( $post_id ) )
+ wp_die( __( 'Error in restoring from trash.' ) );
+ }
+ $location = add_query_arg( 'untrashed', count( $post_ids ), $location );
+ break;
+ case 'delete':
+ if ( !isset( $post_ids ) )
+ break;
+ foreach ( (array) $post_ids as $post_id_del ) {
+ if ( !current_user_can( 'delete_post', $post_id_del ) )
+ wp_die( __( 'You are not allowed to delete this post.' ) );
+
+ if ( !wp_delete_attachment( $post_id_del ) )
+ wp_die( __( 'Error in deleting.' ) );
+ }
+ $location = add_query_arg( 'deleted', count( $post_ids ), $location );
+ break;