gdarko / wp-batch-processing
在WordPress中轻松处理大量数据。提供数据,设置处理流程,从管理员仪表板运行批量处理器。盈利。
1.2.0
2023-09-19 16:02 UTC
Requires
- php: >=5.3
README
WP Batch Processing 是WordPress插件,用于创建数据批次并逐个处理数据项。它允许您定义一个批次,并逐个处理队列中的批次项。如果您的网络连接断开,还有选项在以后继续。
安装
安装此库有两种方式
-
将此库作为插件安装。它不需要以下任何内容,或者
-
将其作为composer包安装(阅读下面的注意)
composer require gdarko/wp-batch-processing
WP_Batch_Processor::boot();
注意:仅在您通过composer在您的插件或主题中安装插件时调用boot()
方法。
注意:如果使用composer,库将尝试找到其路径,但如果您看到混乱的屏幕,则表示它无法找到样式表/JS文件,您需要在boot()
方法之前手动定义它们。
// Manually define the path constants to eliminate // possible errors when resolving the paths and also // include trailing slash at the end. if ( ! defined('WP_BP_PATH')) { define('WP_BP_PATH', '/path/to/wp-content/plugins/your-plugin/libraries/wp-batch-processing/'); } if ( ! defined('WP_BP_URL')) { define('WP_BP_URL', 'https://site.com/wp-content/plugins/your-plugin/libraries/wp-batch-processing/'); } WP_Batch_Processor::boot();
工作原理
要定义一个批次,您只需要扩展WP_Batch
类,然后稍后注册它。请参考下面的示例来了解如何操作。
该类提供以下属性和方法
$id
- 标识批次(必须是唯一的),$title
- 在管理员区域显示,setup()
- 使用它用WP_Batch_Item
实例填充您的数据的方法,process(WP_Batch_Item $item)
- 用于处理批次中下一个项目的的方法
if ( class_exists( 'WP_Batch' ) ) { /** * Class MY_Example_Batch */ class MY_Example_Batch extends WP_Batch { /** * Unique identifier of each batch * @var string */ public $id = 'email_post_authors'; /** * Describe the batch * @var string */ public $title = 'Email Post Authors'; /** * To setup the batch data use the push() method to add WP_Batch_Item instances to the queue. * * Note: If the operation of obtaining data is expensive, cache it to avoid slowdowns. * * @return void */ public function setup() { $users = get_users( array( 'number' => '40', 'role' => 'author', ) ); foreach ( $users as $user ) { $this->push( new WP_Batch_Item( $user->ID, array( 'author_id' => $user->ID ) ) ); } } /** * Handles processing of batch item. One at a time. * * In order to work it correctly you must return values as follows: * * - TRUE - If the item was processed successfully. * - WP_Error instance - If there was an error. Add message to display it in the admin area. * * @param WP_Batch_Item $item * * @return bool|\WP_Error */ public function process( $item ) { // Retrieve the custom data $author_id = $item->get_value( 'author_id' ); // Return WP_Error if the item processing failed (In our case we simply skip author with user id 5) if ( $author_id == 5 ) { return new WP_Error( 302, "Author skipped" ); } // Do the expensive processing here. // ... // Return true if the item processing is successful. return true; } /** * Called when specific process is finished (all items were processed). * This method can be overriden in the process class. * @return void */ public function finish() { // Do something after process is finished. // You have $this->items, etc. } } }
创建类后,类实例需要注册,以便在管理员区域中的批次列表中可用。
/** * Initialize the batches. */ function wp_batch_processing_init() { $batch = new MY_Example_Batch(); WP_Batch_Processor::get_instance()->register( $batch ); } add_action( 'wp_batch_processing_init', 'wp_batch_processing_init', 15, 1 );
就是这样。
过滤器和动作
设置处理项目之间的延迟。默认为0(无延迟)
function wp_bp_my_custom_delay($delay) { return 2; // in seconds } add_filter('wp_batch_processing_delay', 'wp_bp_my_custom_delay', 10, 1);
示例用例
此工具可以用多种不同的方式使用。例如
- 导入数据
- 下载数据
- 发邮件
- 数据库修改
贡献
如果您发现错误或想提出改进建议,请随意创建一个pull request!
许可证
此插件受GPL v2许可
Copyright (C) 2021 Darko Gjorgjijoski (https://darkog.com)
This file is part of WP Batch Processing
WP Batch Processing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
WP Batch Processing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WP Batch Processing. If not, see <https://gnu.ac.cn/licenses/>.