diff --git a/includes/class-wp-cli.php b/includes/class-wp-cli.php new file mode 100644 index 0000000..3306859 --- /dev/null +++ b/includes/class-wp-cli.php @@ -0,0 +1,55 @@ + + * : The unique ID of the batch to run. + * + * ## EXAMPLES + * + * wp batch_process email_post_authors + */ + public function __invoke( $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" ); + + $batch_items = $batch->get_all_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." ); + } +} diff --git a/wp-batch-processing.php b/wp-batch-processing.php index d96d188..83e0530 100644 --- a/wp-batch-processing.php +++ b/wp-batch-processing.php @@ -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; } @@ -26,16 +42,8 @@ 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 // require_once 'examples/class-example-batch.php'; +