diff --git a/Gruntfile.js b/Gruntfile.js index d38e740..23a956b 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -16,7 +16,8 @@ module.exports = function(grunt) { compile: { files: { - 'css/batch.css':'css/batch.styl' + 'css/batch.css':'css/batch.styl', + 'css/notice.css':'css/notice.styl' } } }, @@ -24,7 +25,8 @@ module.exports = function(grunt) { csso: { dist: { files: { - 'css/batch.css':'css/batch.css' + 'css/batch.css':'css/batch.css', + 'css/notice.css':'css/notice.css' } } } diff --git a/batch.php b/batch.php index 57b29d4..a99f412 100644 --- a/batch.php +++ b/batch.php @@ -14,11 +14,17 @@ add_action( 'admin_menu', 'batch_operations_add_page' ); add_action( 'wp_ajax_batch_operations', 'batch_operations_process' ); // Add translations -add_action( 'init', 'batch_operations_load_translation_file'); +add_action( 'init', 'batch_operations_load_translation_file' ); + +// Add huck for view messages +add_action( 'admin_notices', 'batch_operations_notice_view' ); global $batch_operations_version; $batch_operations_version = '0.1.0'; +/** + * Load translation file + */ function batch_operations_load_translation_file() { load_plugin_textdomain( 'batch-operations', false, '/batch_operations/languages' ); } @@ -155,12 +161,14 @@ function batch_operations_process () { * * * * * Sample callback_batch_operation(): * @@ -169,23 +177,27 @@ function batch_operations_process () { * $context['results'][] = $text . $id; * $context['message'] = 'Text + id ='.$text . $id; * } + * * * The $context array gathers batch context information about the execution (read), * as well as 'return values' for the current operation (write) * The following keys are provided : + * * 'results' (read / write): The array of results gathered so far by * the batch processing, for the current operation to append its own. + * * 'message' (write): A text message displayed in the progress page. * The following keys allow for multi-step operations : + * * 'sandbox' (read / write): An array that can be freely used to * store persistent data between iterations. It is recommended to * use this instead of $_SESSION, which is unsafe if the user * continues browsing in a separate window while the batch is processing. + * * 'finished' (write): A float number between 0 and 1 informing * the processing engine of the completion level for the operation. * 1 (or no value explicitly set) means the operation is finished * and the batch processing can continue to the next operation. - * * * @param array $batch_arr array operations and more * @param string $redirect Url to redirect to when the batch has finished processing @@ -228,4 +240,59 @@ function batch_operations_start( $batch_arr, $redirect = NULL ) echo ''; } exit(0); +} + +/** + * Set message for next view for current user + * + * @param string $message message text + * @param string $type type of message: info, success, warning, error. default - info + */ +function batch_operations_notice( $message, $type = 'info' ) { + + if ( false === ( $messages = get_transient( 'batch_operations_notice' ) ) ) { + $messages = array(); + } + + $messages[ get_current_user_id() ][] = array( + 'message' => $message, + 'type' => $type + ); + + set_transient( 'batch_operations_notice', $messages ); + +} + +/** + * View message + */ +function batch_operations_notice_view() { + if ( $messages = get_transient( 'batch_operations_notice' ) ) { + $current_uid = get_current_user_id(); + if ( ! empty( $messages[ $current_uid ] ) ) { + // if version < 4.1.1 then add css + global $wp_version; + + $version = explode( '.', $wp_version ); + + if ( 4 > $version[0] || ( ( 4 == $version[0] && 1 >= $version[1] ) && 2 == count( $version ) ) ) { + global $batch_operations_version; + wp_enqueue_style( 'batch_operations_script', plugin_dir_url('') . 'batch_operations/css/notice.css', array(), $batch_operations_version ); + } + + // print message + foreach ( $messages[ $current_uid ] as $key => $value ) { + echo '

' . $value['message'] . '

'; + } + + //delete messages + unset( $messages[ $current_uid ] ); + + if ( empty( $messages ) ) { + delete_transient( 'batch_operations_notice' ); + } else { + set_transient( 'batch_operations_notice', $messages ); + } + } + } } \ No newline at end of file diff --git a/batch_test.php b/batch_test.php index 1242158..ccd2454 100644 --- a/batch_test.php +++ b/batch_test.php @@ -74,6 +74,11 @@ function batch_operations_test_page_view() { batch_operations_start($batch,get_admin_url( null, 'tools.php' ) . "?page=batch-operations-test"); break; + case 10: + $batch['operations'][]=array('test_batch_operation_messages',array()); + batch_operations_start($batch,get_admin_url( null, 'tools.php' ) . "?page=batch-operations-test"); + break; + default: break; } @@ -91,11 +96,22 @@ function batch_operations_test_page_view() {
  • Test params
  • Test operations in class
  • Test redirect +
  • Test messages +
  • Test finished_callback