Skip to content

Commit 6633639

Browse files
committed
fix merge conflicts
2 parents 695ab98 + e278568 commit 6633639

File tree

6 files changed

+113
-66
lines changed

6 files changed

+113
-66
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ Generate orders with random dates between `--date-start` and the current date.
3030
Generate orders with random dates between `--date-start` and `--date-end`.
3131
- `wp wc generate orders <nr of orders> --date-start=2018-04-01 --date-end=2018-04-24`
3232

33+
Generate orders with a specific status.
34+
- `wp wc generate orders <nr of orders> --status=completed`
35+
36+
You may wish to disable emails if creating a large number of orders as this will trigger emails. To block all emails from your site, install a plugin like [Disable Emails](https://wordpress.org/plugins/disable-emails/).
37+
3338
### Customers
3439

3540
Generate customers based on the number of customers paramater.

includes/Admin/Settings.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ public static function render_admin_page() {
4949
<h2>Generate Orders</h2>
5050
<p>
5151
<input type="number" name="num_orders_to_generate" value="<?php echo esc_attr( self::DEFAULT_NUM_ORDERS ); ?>" min="1" />
52-
Number of orders to generate over
53-
<input type="number" name="order_generation_interval" value="<?php echo esc_attr( self::DEFAULT_ORDER_INTERVAL_MINUTES ); ?>" min="0" />
54-
minutes.
52+
Number of orders to generate.
5553
</p>
5654
<?php submit_button( 'Generate', 'primary', 'generate_orders' ); ?>
55+
56+
<h2>Cancel all scheduled generations</h2>
57+
<p>
58+
</p>
59+
<?php submit_button( 'Cancel all', 'primary', 'cancel_all_generations' ); ?>
5760
</form>
5861
<?php
5962
}
@@ -65,14 +68,16 @@ public static function process_page_submit() {
6568
if ( ! empty( $_POST['generate_products'] ) && ! empty( $_POST['num_products_to_generate'] ) ) {
6669
check_admin_referer( 'generate', 'smoothgenerator_nonce' );
6770
$num_to_generate = absint( $_POST['num_products_to_generate'] );
68-
// @todo kick off generation here
71+
wc_smooth_generate_schedule( 'product', $num_to_generate );
6972
add_action( 'admin_notices', array( __CLASS__, 'product_generating_notice' ) );
70-
} else if ( ! empty( $_POST['generate_orders'] ) && ! empty( $_POST['num_orders_to_generate'] ) && ! empty( $_POST['order_generation_interval'] ) ) {
73+
} else if ( ! empty( $_POST['generate_orders'] ) && ! empty( $_POST['num_orders_to_generate'] ) ) {
7174
check_admin_referer( 'generate', 'smoothgenerator_nonce' );
7275
$num_to_generate = absint( $_POST['num_orders_to_generate'] );
73-
$order_generation_interval = absint( $_POST['order_generation_interval'] );
74-
// @todo kick off generation here
76+
wc_smooth_generate_schedule( 'order', $num_to_generate );
7577
add_action( 'admin_notices', array( __CLASS__, 'order_generating_notice' ) );
78+
} else if ( ! empty( $_POST['cancel_all_generations'] ) ) {
79+
check_admin_referer( 'generate', 'smoothgenerator_nonce' );
80+
wc_smooth_generate_cancel_all();
7681
}
7782
}
7883

includes/CLI.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class CLI extends WP_CLI_Command {
2727
* ## EXAMPLES
2828
* wc generate products 100
2929
*
30-
* @param array $args Argumens specified.
31-
* @param arrat $assoc_args Associative arguments specified.
30+
* @param array $args Arguments specified.
31+
* @param array $assoc_args Associative arguments specified.
3232
*/
3333
public static function products( $args, $assoc_args ) {
3434
list( $amount ) = $args;
@@ -70,18 +70,33 @@ public static function products( $args, $assoc_args ) {
7070
* ## EXAMPLES
7171
* wc generate orders 100
7272
*
73-
* @param array $args Argumens specified.
73+
* @param array $args Arguments specified.
7474
* @param array $assoc_args Associative arguments specified.
7575
*/
7676
public static function orders( $args, $assoc_args ) {
7777
list( $amount ) = $args;
7878

79-
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating orders', $amount );
80-
for ( $i = 1; $i <= $amount; $i++ ) {
81-
Generator\Order::generate( true, $assoc_args );
82-
$progress->tick();
79+
$amount = (int) $amount;
80+
if ( empty( $amount ) ) {
81+
$amount = 100;
82+
}
83+
84+
if ( ! empty( $assoc_args['status'] ) ) {
85+
$status = $assoc_args['status'];
86+
if ( ! wc_is_order_status( 'wc-' . $status ) ) {
87+
WP_CLI::log( "The argument \"$status\" is not a valid order status." );
88+
return;
89+
}
90+
}
91+
92+
if ( $amount > 0 ) {
93+
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating orders', $amount );
94+
for ( $i = 1; $i <= $amount; $i++ ) {
95+
Generator\Order::generate( true, $assoc_args );
96+
$progress->tick();
97+
}
98+
$progress->finish();
8399
}
84-
$progress->finish();
85100
WP_CLI::success( $amount . ' orders generated.' );
86101
}
87102

@@ -99,8 +114,8 @@ public static function orders( $args, $assoc_args ) {
99114
* ## EXAMPLES
100115
* wc generate customers 100
101116
*
102-
* @param array $args Argumens specified.
103-
* @param arrat $assoc_args Associative arguments specified.
117+
* @param array $args Arguments specified.
118+
* @param array $assoc_args Associative arguments specified.
104119
*/
105120
public static function customers( $args, $assoc_args ) {
106121
list( $amount ) = $args;
@@ -118,9 +133,10 @@ public static function customers( $args, $assoc_args ) {
118133
WP_CLI::add_command( 'wc generate orders', array( 'WC\SmoothGenerator\CLI', 'orders' ), array(
119134
'synopsis' => array(
120135
array(
121-
'name' => 'id',
136+
'name' => 'amount',
122137
'type' => 'positional',
123-
'optional' => false,
138+
'optional' => true,
139+
'default' => 100,
124140
),
125141
array(
126142
'name' => 'date-start',
@@ -132,6 +148,11 @@ public static function customers( $args, $assoc_args ) {
132148
'type' => 'assoc',
133149
'optional' => true,
134150
),
151+
array(
152+
'name' => 'status',
153+
'type' => 'assoc',
154+
'optional' => true,
155+
),
135156
),
136157
) );
137158
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) );

includes/GenerateBackgroundProcess.php

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,51 @@
55
* @package SmoothGenerator\Classes
66
*/
77

8-
namespace WC\SmoothGenerator;
9-
10-
defined( 'ABSPATH' ) || exit;
11-
12-
use WP_Background_Process;
8+
use WC\SmoothGenerator\Generator;
139

1410
/**
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.
1616
*/
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;
2631
}
2732

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+
}
3835

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' );
5337

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' );
5547
}
5648
}
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+
}

includes/Generator/Order.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
5858
$order->set_shipping_postcode( $customer->get_shipping_postcode() );
5959
$order->set_shipping_state( $customer->get_shipping_state() );
6060
$order->set_shipping_country( $customer->get_shipping_country() );
61-
$order->set_status( self::random_weighted_element( array(
62-
'completed' => 70,
63-
'processing' => 15,
64-
'on-hold' => 5,
65-
'failed' => 10,
66-
) ) );
61+
$order->set_status( self::get_status( $assoc_args ) );
6762
$order->calculate_totals( true );
6863

6964
$date = self::get_date_created( $assoc_args );
@@ -129,6 +124,26 @@ protected static function get_date_created( $assoc_args ) {
129124
return $dates[ array_rand( $dates ) ];
130125
}
131126

127+
/**
128+
* Returns a status to use as the order's status. If no status argument has been passed, this will
129+
* return a random status.
130+
*
131+
* @param array $assoc_args CLI arguments.
132+
* @return string An order status.
133+
*/
134+
private static function get_status( $assoc_args ) {
135+
if ( ! empty( $assoc_args['status'] ) ) {
136+
return $assoc_args['status'];
137+
} else {
138+
return self::random_weighted_element( array(
139+
'completed' => 70,
140+
'processing' => 15,
141+
'on-hold' => 5,
142+
'failed' => 10,
143+
) );
144+
}
145+
}
146+
132147
/**
133148
* Get random products selected from existing products.
134149
*

wc-smooth-generator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: WooCommerce Smooth Generator
44
* Plugin URI: https://woocommerce.com/
55
* Description: A smooth customer, order and product generator for WooCommerce.
6-
* Version: 1.0.2
6+
* Version: 1.0.1
77
* Author: Automattic
88
* Author URI: https://woocommerce.com
99
*
@@ -17,6 +17,8 @@
1717

1818
defined( 'ABSPATH' ) || exit;
1919

20+
require_once __DIR__ . '/includes/GenerateBackgroundProcess.php';
21+
2022
// autoloader.
2123
if ( ! class_exists( \WC\SmoothGenerator\Plugin::class ) ) {
2224
require __DIR__ . '/vendor/autoload.php';

0 commit comments

Comments
 (0)