diff --git a/batch.php b/batch.php index a99f412..ea3eedf 100644 --- a/batch.php +++ b/batch.php @@ -102,8 +102,25 @@ function batch_operations_process () { $parameters_array = $current_array['operations'][0][1]; } $parameters_array[] = &$current_array['context']; + + $current_array['message'] = ''; + + global $batch_operations_error_array; + set_error_handler( 'batch_operations_error_handler' , E_NOTICE | E_WARNING ); //run function call_user_func_array( $current_array['operations'][0][0], $parameters_array ); + //check error + if ( ! empty($batch_operations_error_array) ) { + $current_array['success'] = false; + $current_array['error_operations'][] = array( + 'operation' => $current_array['operations'][0][0], + 'parameters' => $parameters_array, + 'error_array' => $batch_operations_error_array + ); + $batch_operations_error_array = NULL; + } + + restore_error_handler(); if ( true == $current_array['context']['finished'] ) { $current_array['context']['sandbox'] = array(); @@ -138,11 +155,21 @@ function batch_operations_process () { set_transient( 'batch_' . $id, $current_array , WEEK_IN_SECONDS ); } else { delete_transient( 'batch_' . $id ); + if ( ! empty( $current_array['finished'] ) ) { + call_user_func_array( $current_array['finished'], array( $current_array['success'], $current_array['context']['results'], $current_array['error_operations'] ) ); + } } wp_send_json( $result ); } +global $batch_operations_error_array; + +function batch_operations_error_handler ($errno, $errmsg, $filename, $linenum, $vars) { + global $batch_operations_error_array; + $batch_operations_error_array=array($errno, $errmsg, $filename, $linenum, $vars); + +} /** * Start batch operations @@ -221,6 +248,9 @@ function batch_operations_start( $batch_arr, $redirect = NULL ) $batch_arr['successful_page'] = $redirect; } + $batch_arr['success'] = true; + $batch_arr['error_operations'] = array(); + 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 ccd2454..aa0cde7 100644 --- a/batch_test.php +++ b/batch_test.php @@ -79,6 +79,39 @@ function batch_operations_test_page_view() { batch_operations_start($batch,get_admin_url( null, 'tools.php' ) . "?page=batch-operations-test"); break; + case 11: + $batch = array( + 'title' => "Test finished_callback without errors", + 'init_message' => 'Custom Init Message', + 'progress_message' => 'Step %current% of %total%.', + 'operations' => array( + array('test_batch_operation_params',array('a',1)), + array(array('TestBatch','Operations'),array()), + array('test_batch_operation_context_finished',array()), + array('test_batch_operation',array()), + ), + 'finished' => 'test_batch_operations_callback' + ); + + batch_operations_start($batch,get_admin_url( null, 'tools.php' ) . "?page=batch-operations-test"); + break; + + case 12: + $batch = array( + 'title' => "Test finished_callback with errors", + 'init_message' => 'Custom Init Message', + 'progress_message' => 'Step %current% of %total%.', + 'operations' => array( + array('test_batch_operation_params',array('a')), + array('test_batch_operation_params',array('Hello','World')), + array(array('TestBatch','Operations'),array()), + array('test_batch_operationh',array()), + ), + 'finished' => 'test_batch_operations_callback' + ); + batch_operations_start($batch,get_admin_url( null, 'tools.php' ) . "?page=batch-operations-test"); + break; + default: break; } @@ -97,7 +130,8 @@ function batch_operations_test_page_view() {
  • Test operations in class
  • Test redirect
  • Test messages -
  • Test finished_callback +
  • Test finished_callback without errors +
  • Test finished_callback with errors results
    ' . print_r( $results, true ) . '

    errors

    ' .
    +      print_r( $errors, true ) . '
    ', 'error'); + } } class TestBatch {