|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace NewfoldLabs\WP\Module\Onboarding\TaskManagers; |
| 4 | + |
| 5 | +use NewfoldLabs\WP\Module\Onboarding\Tasks\ImageSideloadTask; |
| 6 | +use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
| 7 | + |
| 8 | +/** |
| 9 | + * Task Manager for Image Sideloading |
| 10 | + * |
| 11 | + * Manages a queue of ImageSideloadTask objects and processes them in FIFO order. |
| 12 | + */ |
| 13 | +class ImageSideloadTaskManager { |
| 14 | + |
| 15 | + /** |
| 16 | + * Option name for storing the queue |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + const QUEUE_OPTION = 'nfd_image_sideload_queue'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Option name for storing the processing status |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + const STATUS_OPTION = 'nfd_image_sideload_status'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Add a task to the queue |
| 31 | + * |
| 32 | + * @param ImageSideloadTask $task The task to add |
| 33 | + * @return bool True on success, false on failure |
| 34 | + */ |
| 35 | + public static function add_to_queue( ImageSideloadTask $task ) { |
| 36 | + $queue = self::get_queue(); |
| 37 | + |
| 38 | + // Check if task already exists |
| 39 | + $task_id = $task->get_id(); |
| 40 | + foreach ( $queue as $existing_task ) { |
| 41 | + if ( $existing_task['id'] === $task_id ) { |
| 42 | + return false; // Task already exists |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Add task to queue (FIFO - add to end) |
| 47 | + $queue[] = array( |
| 48 | + 'id' => $task_id, |
| 49 | + 'post_id' => $task->get_post_id(), |
| 50 | + 'urls' => $task->get_image_urls(), |
| 51 | + 'status' => 'pending', |
| 52 | + 'created' => time(), |
| 53 | + ); |
| 54 | + |
| 55 | + self::set_queue( $queue ); |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Get the current queue |
| 61 | + * |
| 62 | + * @return array |
| 63 | + */ |
| 64 | + public static function get_queue() { |
| 65 | + $queue = get_option( Options::get_option_name( self::QUEUE_OPTION ), array() ); |
| 66 | + if ( ! is_array( $queue ) ) { |
| 67 | + $queue = array(); |
| 68 | + } |
| 69 | + return $queue; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Set the queue |
| 74 | + * |
| 75 | + * @param array $queue The queue to set |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public static function set_queue( $queue ) { |
| 79 | + update_option( Options::get_option_name( self::QUEUE_OPTION ), $queue ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the processing status |
| 84 | + * |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + public static function get_status() { |
| 88 | + return get_option( Options::get_option_name( self::STATUS_OPTION ), 'idle' ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Set the processing status |
| 93 | + * |
| 94 | + * @param string $status The status to set |
| 95 | + * @return void |
| 96 | + */ |
| 97 | + public static function set_status( $status ) { |
| 98 | + update_option( Options::get_option_name( self::STATUS_OPTION ), $status ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Process the next task in the queue |
| 103 | + * |
| 104 | + * @return bool|\WP_Error True if task was processed, false if queue is empty, WP_Error on failure |
| 105 | + */ |
| 106 | + public static function process_next_task() { |
| 107 | + $queue = self::get_queue(); |
| 108 | + |
| 109 | + if ( empty( $queue ) ) { |
| 110 | + self::set_status( 'idle' ); |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + // Get the next task |
| 115 | + $task_data = array_shift( $queue ); |
| 116 | + |
| 117 | + // Mark as processing |
| 118 | + $task_data['status'] = 'processing'; |
| 119 | + $task_data['started'] = time(); |
| 120 | + |
| 121 | + // Create task object and execute |
| 122 | + $task = new ImageSideloadTask( $task_data['post_id'], $task_data['urls'] ); |
| 123 | + $result = $task->execute(); |
| 124 | + |
| 125 | + if ( is_wp_error( $result ) ) { |
| 126 | + // Mark as failed |
| 127 | + $task_data['status'] = 'failed'; |
| 128 | + $task_data['error'] = $result->get_error_message(); |
| 129 | + $task_data['completed'] = time(); |
| 130 | + |
| 131 | + // Add back to queue for retry |
| 132 | + $queue[] = $task_data; |
| 133 | + |
| 134 | + self::set_queue( $queue ); |
| 135 | + self::set_status( 'processing' ); |
| 136 | + |
| 137 | + return $result; |
| 138 | + } |
| 139 | + |
| 140 | + // Mark as completed |
| 141 | + $task_data['status'] = 'completed'; |
| 142 | + $task_data['completed'] = time(); |
| 143 | + |
| 144 | + self::set_queue( $queue ); |
| 145 | + self::set_status( 'processing' ); |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Process the entire queue |
| 152 | + * |
| 153 | + * @param int $max_tasks Maximum number of tasks to process in one run (default: 5) |
| 154 | + * @return array Array with 'processed' and 'remaining' counts |
| 155 | + */ |
| 156 | + public static function process_queue( $max_tasks = 5 ) { |
| 157 | + $processed = 0; |
| 158 | + $queue = self::get_queue(); |
| 159 | + $initial_count = count( $queue ); |
| 160 | + |
| 161 | + self::set_status( 'processing' ); |
| 162 | + |
| 163 | + while ( $processed < $max_tasks && ! empty( $queue ) ) { |
| 164 | + $result = self::process_next_task(); |
| 165 | + |
| 166 | + if ( $result === false ) { |
| 167 | + break; // Queue is empty |
| 168 | + } |
| 169 | + |
| 170 | + if ( is_wp_error( $result ) ) { |
| 171 | + // Continue processing other tasks even if one fails |
| 172 | + $processed++; |
| 173 | + continue; |
| 174 | + } |
| 175 | + |
| 176 | + $processed++; |
| 177 | + } |
| 178 | + |
| 179 | + $remaining = count( self::get_queue() ); |
| 180 | + |
| 181 | + if ( $remaining === 0 ) { |
| 182 | + self::set_status( 'idle' ); |
| 183 | + } |
| 184 | + |
| 185 | + return array( |
| 186 | + 'processed' => $processed, |
| 187 | + 'remaining' => $remaining, |
| 188 | + 'total' => $initial_count, |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Clear the queue |
| 194 | + * |
| 195 | + * @return void |
| 196 | + */ |
| 197 | + public static function clear_queue() { |
| 198 | + self::set_queue( array() ); |
| 199 | + self::set_status( 'idle' ); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Get queue statistics |
| 204 | + * |
| 205 | + * @return array |
| 206 | + */ |
| 207 | + public static function get_stats() { |
| 208 | + $queue = self::get_queue(); |
| 209 | + $stats = array( |
| 210 | + 'total' => count( $queue ), |
| 211 | + 'pending' => 0, |
| 212 | + 'processing' => 0, |
| 213 | + 'completed' => 0, |
| 214 | + 'failed' => 0, |
| 215 | + ); |
| 216 | + |
| 217 | + foreach ( $queue as $task ) { |
| 218 | + $status = $task['status'] ?? 'pending'; |
| 219 | + if ( isset( $stats[ $status ] ) ) { |
| 220 | + $stats[ $status ]++; |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + return $stats; |
| 225 | + } |
| 226 | +} |
0 commit comments