From cdd700af4a2fd6a8d86d2de923c3014e239c652d Mon Sep 17 00:00:00 2001 From: Joel James Date: Mon, 2 Aug 2021 12:35:58 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20=20Releasing=201.0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++++----- src/Task.php | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3b04e01..1294381 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ WP Queue Process can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks. * Inspired by [TechCrunch WP Asynchronous Tasks](https://github.com/techcrunch/wp-async-task). -* Forked from [WP Background Processing](https://github.com/deliciousbrains/wp-background-processing). +* Forked from [WP Background Processing](https://github.com/deliciousbrains/wp-background-processing) with few extra options. __Requires PHP 5.2+__ @@ -101,11 +101,12 @@ class WP_Example_Process extends \DuckDev\Queue\Task { * in the next pass through. Or, return false to remove the * item from the queue. * - * @param mixed $item Queue item to iterate over - * + * @param mixed $item Queue item to iterate over + * @param string $group Group name of the task (Useful when performing multiple tasks). + * * @return mixed */ - protected function task( $item ) { + protected function task( $item, $group ) { // Actions to perform return false; @@ -155,7 +156,7 @@ foreach ( $items as $item ) { Save and dispatch the queue: -`$this->example_process->save()->dispatch();` +`$this->example_process->save( 'my-task' ')->dispatch();` ### BasicAuth diff --git a/src/Task.php b/src/Task.php index 87d4eed..9043a2a 100644 --- a/src/Task.php +++ b/src/Task.php @@ -126,18 +126,24 @@ public function push_to_queue( $data ) { /** * Save the process queue. * + * @param string $group Group name. + * * @since 1.0.0 + * @since 1.0.1 Added group option. * @access public * * @return Task $this */ - public function save() { + public function save( $group = 'default' ) { // Generate key. $key = $this->generate_key(); // Save the data to database. if ( ! empty( $this->data ) ) { + // Save data. update_site_option( $key, $this->data ); + // Save group name too. + update_site_option( $key . '_group', $group ); } return $this; @@ -175,6 +181,7 @@ public function update( $key, $data ) { */ public function delete( $key ) { delete_site_option( $key ); + delete_site_option( $key . '_group' ); return $this; } @@ -375,9 +382,10 @@ protected function get_batch() { ); // Create new object. - $batch = new stdClass(); - $batch->key = $query->$column; - $batch->data = maybe_unserialize( $query->$value_column ); + $batch = new stdClass(); + $batch->key = $query->$column; + $batch->data = maybe_unserialize( $query->$value_column ); + $batch->group = get_site_option( $batch->key . '_group', 'default' ); return $batch; } @@ -406,7 +414,7 @@ protected function handle() { // Process each item in batch. foreach ( $batch->data as $key => $value ) { // Run task. - $task = $this->task( $value ); + $task = $this->task( $value, $batch->group ); // If task failed add to queue again. if ( false !== $task ) { @@ -699,12 +707,14 @@ public function cancel_process() { * in the next pass through. Or, return false to remove the * item from the queue. * - * @param mixed $item Queue item to iterate over. + * @param mixed $item Queue item to iterate over. + * @param string $group Group name of the task (Useful when processing multiple tasks). * * @since 1.0.0 + * @since 1.0.1 Added group option. * @access protected * * @return mixed */ - abstract protected function task( $item ); + abstract protected function task( $item, $group ); }