From 41a4b0e0dadb761a01d0f8fbb2e3139ae2622368 Mon Sep 17 00:00:00 2001
From: Igor V Belousov <igor@belousovv.ru>
Date: Fri, 7 Aug 2015 14:07:08 +0300
Subject: [PATCH] add custom redirect

---
 batch.php      | 11 +++++++++--
 batch_test.php |  6 ++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/batch.php b/batch.php
index 5430b03..79eec8a 100644
--- a/batch.php
+++ b/batch.php
@@ -58,7 +58,7 @@ function batch_operations_page_view() {
 
   ?>
   <script type="text/javascript">
-    var batch_id='<?php print $id; ?>',successful_page='<?php print get_admin_url(); ?>';
+    var batch_id='<?php print $id; ?>',successful_page='<?php echo $current_array['successful_page']; ?>';
   </script>
   <div class="wrap">
     <h2><?php echo $title ?></h2>
@@ -185,8 +185,9 @@ function batch_operations_process () {
  * </pre>
  *
  * @param array $batch_arr array operations and more
+ * @param string $redirect Url to redirect to when the batch has finished processing
  */
-function batch_operations_start($batch_arr)
+function batch_operations_start( $batch_arr, $redirect = NULL )
 {
   $id = rand( 100, 999 ) . strtoupper( md5( date( 'YMDBs' ) ) ) . rand( 1000, 9999 );
 
@@ -199,6 +200,12 @@ function batch_operations_start($batch_arr)
   $batch_arr['count']   = count( $batch_arr['operations'] );
   $batch_arr['current'] = 0;
 
+  if ( empty( $redirect ) ) {
+    $batch_arr['successful_page'] =  get_admin_url();
+  } else {
+    $batch_arr['successful_page'] = $redirect;
+  }
+
   if ( empty( $batch_arr['progress_message'] ) ) {
     $batch_arr['progress_message'] = __( 'Completed %current% of %total%.' );
   }
diff --git a/batch_test.php b/batch_test.php
index fafd7b1..1242158 100644
--- a/batch_test.php
+++ b/batch_test.php
@@ -69,6 +69,11 @@ function batch_operations_test_page_view() {
       batch_operations_start($batch);
       break;
 
+    case 9:
+      $batch['operations'][]=array('test_batch_operation_params',array('c',3));
+      batch_operations_start($batch,get_admin_url( null, 'tools.php' ) . "?page=batch-operations-test");
+      break;
+
     default:
       break;
   }
@@ -85,6 +90,7 @@ function batch_operations_test_page_view() {
       <li><a href="tools.php?page=batch-operations-test&test=6">Test <strong>$context['finished']</strong></a>
       <li><a href="tools.php?page=batch-operations-test&test=7">Test params</a>
       <li><a href="tools.php?page=batch-operations-test&test=8">Test operations in class</a>
+      <li><a href="tools.php?page=batch-operations-test&test=9">Test redirect</a>
     </ol>
   </div>
   <?php