|
5 | 5 | * @package SmoothGenerator\Classes |
6 | 6 | */ |
7 | 7 |
|
8 | | -namespace WC\SmoothGenerator; |
9 | | - |
10 | | -defined( 'ABSPATH' ) || exit; |
11 | | - |
12 | | -use WP_Background_Process; |
| 8 | +use WC\SmoothGenerator\Generator; |
13 | 9 |
|
14 | 10 | /** |
15 | | - * Background data generation and creation class. |
| 11 | + * Calls generator for object type. |
| 12 | + * |
| 13 | + * @param string $type Type of object to generate. |
| 14 | + * |
| 15 | + * @return false If task was successful. |
16 | 16 | */ |
17 | | -class GenerateBackgroundProcess extends WP_Background_Process { |
18 | | - |
19 | | - /** |
20 | | - * Initiate new background process. |
21 | | - */ |
22 | | - public function __construct() { |
23 | | - $this->prefix = 'wp_' . get_current_blog_id(); |
24 | | - $this->action = 'wc_smoothgenerator_generate'; |
25 | | - parent::__construct(); |
| 17 | +function wc_smooth_generate_object( $type ) { |
| 18 | + // Check what generation task to perform. |
| 19 | + switch ( $type ) { |
| 20 | + case 'order': |
| 21 | + Generator\Order::generate(); |
| 22 | + break; |
| 23 | + case 'product': |
| 24 | + Generator\Product::generate(); |
| 25 | + break; |
| 26 | + case 'customer': |
| 27 | + Generator\Customer::generate(); |
| 28 | + break; |
| 29 | + default: |
| 30 | + return false; |
26 | 31 | } |
27 | 32 |
|
28 | | - /** |
29 | | - * Code to execute for each item in the queue |
30 | | - * |
31 | | - * @param array $item Item data. |
32 | | - * @return boolean |
33 | | - */ |
34 | | - protected function task( $item ) { |
35 | | - if ( ! is_array( $item ) && ! isset( $item['task'] ) ) { |
36 | | - return false; |
37 | | - } |
| 33 | + return false; |
| 34 | +} |
38 | 35 |
|
39 | | - // Check what generation task to perform. |
40 | | - switch ( $item['task'] ) { |
41 | | - case 'order': |
42 | | - Generator\Order::generate(); |
43 | | - break; |
44 | | - case 'product': |
45 | | - Generator\Product::generate(); |
46 | | - break; |
47 | | - case 'customer': |
48 | | - Generator\Customer::generate(); |
49 | | - break; |
50 | | - default: |
51 | | - return false; |
52 | | - } |
| 36 | +add_action( 'wc_smooth_generate_object', 'wc_smooth_generate_object' ); |
53 | 37 |
|
54 | | - return false; |
| 38 | +/** |
| 39 | + * Schedule async actions for generation of objects. |
| 40 | + * |
| 41 | + * @param string $type Type of object to generate. |
| 42 | + * @param int $qty Quantity of objects. |
| 43 | + */ |
| 44 | +function wc_smooth_generate_schedule( $type, $qty ) { |
| 45 | + for ( $i = 0; $i < $qty; $i++ ) { |
| 46 | + as_enqueue_async_action( 'wc_smooth_generate_object', array( 'type' => $type ), 'wc_smooth_generate_object_group' ); |
55 | 47 | } |
56 | 48 | } |
| 49 | + |
| 50 | +/** |
| 51 | + * Cancel any scheduled generation actions. |
| 52 | + */ |
| 53 | +function wc_smooth_generate_cancel_all() { |
| 54 | + as_unschedule_all_actions( '', array(), 'wc_smooth_generate_object_group' ); |
| 55 | +} |
0 commit comments