underpin/batch-task-loader

Underpin 批量任务加载器

1.1.0 2021-11-23 14:21 UTC

This package is auto-updated.

Last update: 2024-09-23 22:34:12 UTC


README

辅助WordPress网站添加批量任务的加载器。

安装

使用Composer

composer require underpin/batch-task-loader

手动

此插件使用内置的自动加载器,因此只要它在 Underpin 之前被要求,它应该可以按预期工作。

require_once(__DIR__ . '/underpin-batch-tasks/batch-tasks.php');

设置

  1. 安装 Underpin。请参阅Underpin 文档
  2. 根据需要注册新的批量任务。

批量任务

随着插件的成熟,将大任务(如替换数据库中每条记录的值)分解成更小任务(替换20条记录直到全部替换)的需求变得司空见惯。

问题是,WordPress 没有提供一种轻松创建这些任务的方法。此加载器使您能够快速注册和构建自己的批量任务。

示例

一个非常基本的示例可能看起来像这样。

\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
  ] );

或者,您可以通过扩展 批量任务 并直接引用扩展类来实现这一点,如下所示

underpin()->batch_tasks()->add('key','Namespace\To\Class');

这在使用批量任务时特别有用,因为它们往往非常长,并且嵌套很深。

队列批量通知

一旦注册了批量任务,您可以指示 Underpin 以这种方式显示管理员通知来运行此任务

Underpin\underpin()->batch_tasks()->enqueue('example-batch');

这将向管理员区域添加一个批量任务通知。单击后,批量任务将运行,进度条将指示进度。完成时,它会自动关闭。

image