Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter for custom processors #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ function my_callback_function( $result ) {
}
```

#### Custom Processor
Using a custom processor requires using the `loco_register_batch_processor` filter to register the processor type and a processor class that extends the `Rkv\Locomotive\Abstracts\Batch` class.

Register your custom callback to be used when the type matches your process type:
```
add_filter( 'loco_register_batch_processor', function( $return_value, $type, $args){
if( 'hiroy' == $type' ){
// Return name of class, not class instance
return 'HiRoyProcessor';
}elseif( 'hishawn' == $type ){
// Return name of class, not class instance
return 'HiShawnProcessor';
}else{
// No custom type matched
// Return filter default to allow for other custom types or defaults
return $return_value;
}


}, 10, 3 );




#### Start Batch Process
![Locomotive Menu](screenshot.gif?raw=true "Locomotive Menu")

Expand Down
19 changes: 19 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ function register_batch_process( $args ) {
$args['type'] = '';
}

/**
* Filter to register custom batch process types.
*
* Return the name (fully namespaced) of a your batch processor class NOT an object instance
*
* @param string $batch_processor Batch processor class, must extend Rkv\Locomotive\Abstracts\Batch
* @param string $type Type of processor
* @param array $args Args passed to register_batch_process
*/
$batch_processor = apply_filters( 'loco_register_batch_processor', null, $args['type'], $args );

// Check if filter returned a valid processor.
// If so, use that, else continue to defaults.
if ( is_string( $batch_processor ) && is_subclass_of( $batch_processor, 'Rkv\Locomotive\Abstracts\Batch' ) ) {
$batch_process = new $batch_processor();
$batch_process->register( $args );
return;
}

switch ( $args['type'] ) {
case 'post':
$batch_process = new Posts();
Expand Down