underpin / batch-task-loader
Batch task Loader for Underpin
Requires
- underpin/script-loader: ^1.0
- underpin/style-loader: ^1.0
- underpin/underpin: ^2.0
This package is auto-updated.
Last update: 2024-12-23 23:10:52 UTC
README
Loader That assists with adding batch tasks to a WordPress website.
Installation
Using Composer
composer require underpin/batch-task-loader
Manually
This plugin uses a built-in autoloader, so as long as it is required before Underpin, it should work as-expected.
require_once(__DIR__ . '/underpin-batch-tasks/batch-tasks.php');
Setup
- Install Underpin. See Underpin Docs
- Register new batch tasks as-needed.
Batch tasks
As a plugin matures, the need to break a big task (like replacing the value of every record in a database) into smaller tasks (replace 20 records until all are replaced) becomes commonplace.
The problem is, WordPress doesn't provide a way to create these tasks easily. This loader makes it possible to register and build your own batch tasks quickly.
Example
A very basic example could look something like this.
\Underpin\underpin()->batch_tasks()->add( 'example-batch', [ 'description' => 'A batch task that does nothing 20 times', 'name' => 'Batch Task Example', 'tasks_per_request' => 50, 'stop_on_error' => true, 'total_items' => 1000, 'notice_message' => 'Run the most pointless batch task ever made.', 'button_text' => 'LETS GO.', 'capability' => 'administrator', 'batch_id' => 'example-batch', 'task_callback' => '__return_null', // The callback that iterates on every task 'finish_process_callback' => '__return_null', // The callback that runs after everything is finished 'prepare_task_callback' => '__return_null', // The callback that runs before each task 'finish_task_callback' => '__return_null', // The callback that runs after each task ] );
Alternatively, you can extend batch task
and reference the extended class directly, like so:
underpin()->batch_tasks()->add('key','Namespace\To\Class');
This is especially useful when using batch tasks, since they have a tendency to get quite long, and nest deep.
Enqueuing the batch notice
Once a batch task is registered, you can instruct Underpin to display the admin notice to run this task like so:
Underpin\underpin()->batch_tasks()->enqueue('example-batch');
This will add a batch task notice to the admin area. Once clicked, the batch task will run, with a progress bar to indicate progress. When complete, it will self-close.