PHP code example of duckdev / wp-queue-process

1. Go to this page and download the library: Download duckdev/wp-queue-process library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

duckdev / wp-queue-process example snippets


class WP_Example_Request extends \DuckDev\Queue\Async {

	/**
	 * @var string Unique action name.
	 */
	protected $action = 'example_request';

	/**
	 * Perform the work. The dispatched data is available in $_POST.
	 */
	protected function handle() {
		// Actions to perform.
	}
}

$this->example_request = new WP_Example_Request();
$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) )->dispatch();

class WP_Example_Process extends \DuckDev\Queue\Task {

	/**
	 * @var string Unique action name.
	 */
	protected $action = 'example_process';

	/**
	 * Process a single queue item.
	 *
	 * Return the (optionally modified) item to push it back for another
	 * pass, or false to remove it from the queue.
	 *
	 * @param mixed  $item  Queue item to process.
	 * @param string $group Group name the item was saved under.
	 *
	 * @return mixed
	 */
	protected function task( $item, $group ) {
		// Actions to perform.

		return false;
	}

	/**
	 * Optional. Runs once the queue is fully drained.
	 */
	protected function complete() {
		parent::complete();

		// Show a notice, log, etc.
	}
}

$this->example_process = new WP_Example_Process();

foreach ( $items as $item ) {
	$this->example_process->push_to_queue( $item );
}

// Or set the whole queue at once when the data is already shaped correctly:
// $this->example_process->set_queue( $items );

$this->example_process->save( 'my-group' )->dispatch();

$process = new WP_Example_Process( new MyCustomStore( 'my_plugin_example_process' ) );

add_filter( 'http_request_args', function ( $r ) {
	$r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );

	return $r;
} );

src/
├── Async.php                     # Abstract non-blocking request
├── Task.php                      # Abstract background queue (extends Async)
├── Contracts/
│   └── StoreInterface.php        # Where batches are persisted
├── Storage/
│   └── OptionStore.php           # Default driver: (network) options table
├── Support/
│   ├── Batch.php                 # Value object: one stored batch
│   ├── ServerLimits.php          # Time/memory budget guard
│   └── ProcessLock.php           # Single-worker lock (site transient)
└── Exceptions/
    └── QueueException.php