Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/class-batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class WP_Batch {
* Data store of batch items
* @var WP_Batch_Item[]
*/
protected $items = array();
public $items = array();

/**
* Initialize
Expand Down
118 changes: 118 additions & 0 deletions includes/class-wp-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
die( 'Direct access is not allowed.' );
}

/**
* Class Batch_Processor_CLI_Command().
*/
class WP_Batch_Processor_CLI_Command extends WP_CLI_Command {

/**
* List all registered batches.
*
* ## EXAMPLES
*
* wp batch-process list
*
* @subcommand list
*/
public function list_batches( $args, $assoc_args ) {
do_action( 'wp_batch_processing_init' );

$batches = WP_Batch_Processor::get_instance()->get_batches();

if ( empty( $batches ) ) {
WP_CLI::error( 'No batches registered.' );
return;
}

$items = array();
foreach ( $batches as $batch ) {
$items[] = array(
'ID' => $batch->id,
'Total' => $batch->get_items_count(),
'Processed' => $batch->get_processed_count(),
'Percentage' => $batch->get_percentage() . '%',
'Finished' => $batch->is_finished() ? 'Yes' : 'No',
);
}

WP_CLI\Utils\format_items( 'table', $items, array( 'ID', 'Total', 'Processed', 'Percentage', 'Finished' ) );
}

/**
* Process a registered batch by ID.
*
* ## OPTIONS
*
* <batch-id>
* : The unique ID of the batch to run.
*
* ## EXAMPLES
*
* wp batch-process process email_post_authors
*
* @subcommand process
*/
public function process( $args, $assoc_args ) {
list( $batch_id ) = $args;
do_action( 'wp_batch_processing_init' );

$batch = WP_Batch_Processor::get_instance()->get_batch( $batch_id );
if ( ! $batch ) {
WP_CLI::error( "Batch with ID '{$batch_id}' not found." );
return;
}

WP_CLI::log( "Starting processing batch: {$batch_id} with {$batch->get_items_count()} items" );

$progress = WP_CLI\Utils\make_progress_bar( 'Processing batch items', $batch->get_items_count() );

/** @var WP_Batch_Item $item */
foreach ( $batch->items as $item ) {
// Process each item in the batch.
try {
if ( ! $batch->is_processed( $item ) ) {
$batch->process( $item );
$batch->mark_as_processed( $item->id );
}
} catch ( Exception $e ) {
WP_CLI::warning( "Error processing item ID {$item->id}: " . $e->getMessage() );
}
$progress->tick();
}

WP_CLI::success( "Batch '{$batch_id}' processing complete." );
}

/**
* Restart a registered batch by ID.
*
* ## OPTIONS
*
* <batch-id>
* : The unique ID of the batch to restart.
*
* ## EXAMPLES
*
* wp batch-process restart email_post_authors
*
* @subcommand restart
*/
public function restart( $args, $assoc_args ) {
list( $batch_id ) = $args;
do_action( 'wp_batch_processing_init' );

$batch = WP_Batch_Processor::get_instance()->get_batch( $batch_id );
if ( ! $batch ) {
WP_CLI::error( "Batch with ID '{$batch_id}' not found." );
return;
}

$batch->restart();

WP_CLI::success( "Batch '{$batch_id}' restarted." );
}
}
25 changes: 16 additions & 9 deletions wp-batch-processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
die;
}

require_once 'includes/class-bp-helper.php';
require_once 'includes/class-bp-singleton.php';
require_once 'includes/class-batch-item.php';
require_once 'includes/class-batch.php';
require_once 'includes/class-batch-processor.php';
require_once 'includes/class-batch-ajax-handler.php';
require_once 'includes/class-batch-list-table.php';
require_once 'includes/class-batch-processor-admin.php';

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once 'includes/class-wp-cli.php';

$wp_cli_command = new WP_Batch_Processor_CLI_Command();
WP_CLI::add_command( 'batch-process', $wp_cli_command );
}

if ( ! is_admin() ) {
return;
}
Expand All @@ -26,15 +42,6 @@
define( 'WP_BP_URL', plugin_dir_url( __FILE__ ) );
}

require_once 'includes/class-bp-helper.php';
require_once 'includes/class-bp-singleton.php';
require_once 'includes/class-batch-item.php';
require_once 'includes/class-batch.php';
require_once 'includes/class-batch-processor.php';
require_once 'includes/class-batch-ajax-handler.php';
require_once 'includes/class-batch-list-table.php';
require_once 'includes/class-batch-processor-admin.php';

WP_Batch_Processor::boot();

// Examples
Expand Down