prefix . 'batch_operations'; //WP>=3.5 $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `operations` longtext NOT NULL, PRIMARY KEY (`id`) ) $charset_collate AUTO_INCREMENT=1;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } /** * Delete table if deactivate */ function batch_operations_deactivate(){ if ( ! current_user_can( 'activate_plugins' ) ) return; global $wpdb; $query = 'DROP TABLE `' . $wpdb->prefix . 'batch_operations' . '`'; $wpdb->query( $query ); } function batch_operations_load_translation_file() { load_plugin_textdomain( 'batch-operations', false, '/batch_operations/languages' ); } /** * Add backend page without menu item */ function batch_operations_add_page() { add_submenu_page( null, 'Batch operations', 'Batch operations', 'edit_posts', 'batch-operations', 'batch_operations_page_view' ); } /** * View batch operations page */ function batch_operations_page_view() { global $batch_operations_version; global $wpdb; //WP>=3.3 wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'batch_operations_script', plugin_dir_url('') . 'batch_operations/js/batch.min.js', array(), $batch_operations_version ); wp_enqueue_style( 'batch_operations_script', plugin_dir_url('') . 'batch_operations/css/batch.css', array(), $batch_operations_version ); $id = ( intval( $_REQUEST["id"] ) < 0 )? 0 : intval( $_REQUEST["id"] ); $current_array = $wpdb->get_var( 'SELECT `operations` FROM `' . $wpdb->prefix . "batch_operations` WHERE `id` = $id;" ); $title = __( 'Processing', 'batch-operations' ); $init_message = ''; if ( ! empty( $current_array ) ) { $current_array = unserialize( $current_array ); $title = ( empty ( $current_array['title'] ) ) ? $title : $current_array['title'] ; $init_message = ( empty ( $current_array['init_message'] ) ) ? __( 'Initializing.', 'batch-operations' ) : $current_array['init_message'] ; } ?>
* $batch = array( * 'title' => t('Exporting'), * 'operations' => array( * array('my_function_1', array(123, 'qwe')), * array('my_function_2', array()), * ), * 'finished' => 'my_finished_callback', * ); * * batch_operations_start($batch); ** *
* function my_function_1($id, $text, &$context) { * $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 */ function batch_operations_start($batch_arr) { global $wpdb; $batch_arr['context'] = array( 'message' => '', 'sandbox' => array(), 'finished' => true, 'results' => array() ); $batch_arr['count'] = count( $batch_arr['operations'] ); $batch_arr['current'] = 0; if ( empty( $batch_arr['progress_message'] ) ) { $batch_arr['progress_message'] = __('Completed %current% of %total%.'); } $wpdb->insert( $wpdb->prefix . 'batch_operations', array( 'operations' => serialize( $batch_arr ) ), array( '%s' ) ); $location = get_admin_url(null, 'tools.php') . "?page=batch-operations&id=" . $wpdb->insert_id; if ( ! headers_sent() ) { wp_redirect( $location ); } else { // if header is set then runs this hack echo ''; echo ''; } exit(0); }