add finished_callback
Этот коммит содержится в:
родитель
aa5677fcf6
Коммит
e556b079b3
30
batch.php
30
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%.' );
|
||||
}
|
||||
|
@ -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() {
|
||||
<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>
|
||||
<li><a href="tools.php?page=batch-operations-test&test=10">Test messages</a>
|
||||
<li><a href="tools.php?page=batch-operations-test&test=11">Test finished_callback</a>
|
||||
<li><a href="tools.php?page=batch-operations-test&test=11">Test finished_callback without errors</a>
|
||||
<li><a href="tools.php?page=batch-operations-test&test=12">Test finished_callback with errors</a>
|
||||
</ol>
|
||||
</div>
|
||||
<?php
|
||||
@ -136,6 +170,17 @@ function test_batch_operation_context_finished( &$context ) {
|
||||
function test_batch_operation_params( $a, $b, &$context ) {
|
||||
sleep(2);
|
||||
$context['message'] = '$a=' . $a . ' $b=' . $b ;
|
||||
$context['results'][] = $a . ', ' . $b . '!';return true;
|
||||
}
|
||||
|
||||
function test_batch_operations_callback( $success, $results, $errors ){
|
||||
if ( $success ) {
|
||||
batch_operations_notice('Test finished_callback without errors success.');
|
||||
} else {
|
||||
batch_operations_notice('Test finished_callback with errors success.');
|
||||
batch_operations_notice('<h3>results</h3><pre>' . print_r( $results, true ) . '</pre><h3>errors</h3><pre>' .
|
||||
print_r( $errors, true ) . '</pre>', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
class TestBatch {
|
||||
|
Загрузка…
Ссылка в новой задаче
Block a user