Skip to content

Commit e2ae6f4

Browse files
authored
Ensure WC emails are disabled before generating (#157)
* Ensure WC emails are disabled before generating * Add missing email trigger for failed orders * Clarify generator initialization
1 parent 0c1f3fb commit e2ae6f4

File tree

7 files changed

+89
-89
lines changed

7 files changed

+89
-89
lines changed

includes/CLI.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ public static function orders( $args, $assoc_args ) {
8484
}
8585
}
8686

87-
Generator\Order::disable_emails();
88-
8987
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating orders', $amount );
9088

9189
add_action(
@@ -134,8 +132,6 @@ public static function customers( $args, $assoc_args ) {
134132

135133
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating customers', $amount );
136134

137-
Generator\Customer::disable_emails();
138-
139135
add_action(
140136
'smoothgenerator_customer_generated',
141137
function () use ( $progress ) {

includes/Generator/Coupon.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@
1313
* Customer data generator.
1414
*/
1515
class Coupon extends Generator {
16-
17-
/**
18-
* Init faker library.
19-
*/
20-
protected static function init_faker() {
21-
parent::init_faker();
22-
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
23-
}
24-
2516
/**
2617
* Create a new coupon.
2718
*
@@ -31,7 +22,7 @@ protected static function init_faker() {
3122
* @return \WC_Coupon|\WP_Error Coupon object with data populated.
3223
*/
3324
public static function generate( $save = true, $assoc_args = array() ) {
34-
self::init_faker();
25+
parent::maybe_initialize_generators();
3526

3627
$defaults = array(
3728
'min' => 5,

includes/Generator/Customer.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Customer extends Generator {
2020
* @return \WC_Customer|\WP_Error Customer object with data populated.
2121
*/
2222
public static function generate( $save = true, array $assoc_args = array() ) {
23-
self::init_faker();
23+
parent::maybe_initialize_generators();
2424

2525
$args = filter_var_array(
2626
$assoc_args,
@@ -153,18 +153,4 @@ public static function batch( $amount, array $args = array() ) {
153153

154154
return $customer_ids;
155155
}
156-
157-
/**
158-
* Disable sending WooCommerce emails when generating objects.
159-
*/
160-
public static function disable_emails() {
161-
$email_actions = array(
162-
'woocommerce_new_customer_note',
163-
'woocommerce_created_customer',
164-
);
165-
166-
foreach ( $email_actions as $action ) {
167-
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
168-
}
169-
}
170156
}

includes/Generator/Generator.php

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,25 @@
1111
* Data generator base class.
1212
*/
1313
abstract class Generator {
14-
14+
/**
15+
* Maximum number of objects that can be generated in one batch.
16+
*/
1517
const MAX_BATCH_SIZE = 100;
1618

19+
/**
20+
* Dimension, in pixels, of generated images.
21+
*/
1722
const IMAGE_SIZE = 700;
1823

1924
/**
20-
* Holds the faker factory object.
25+
* Are we ready to generate objects?
26+
*
27+
* @var bool
28+
*/
29+
protected static $ready = false;
30+
31+
/**
32+
* Holds the Faker factory object.
2133
*
2234
* @var \Faker\Generator Factory object.
2335
*/
@@ -59,11 +71,80 @@ abstract public static function generate( $save = true );
5971
//abstract public static function batch( $amount, array $args = array() );
6072

6173
/**
62-
* Init faker library.
74+
* Get ready to generate objects.
75+
*
76+
* This can be run from any generator, but it applies to all generators.
77+
*
78+
* @return void
79+
*/
80+
protected static function maybe_initialize_generators() {
81+
if ( true !== self::$ready ) {
82+
self::init_faker();
83+
self::disable_emails();
84+
85+
// Set this to avoid notices as when you run via WP-CLI SERVER vars are not set, order emails uses this variable.
86+
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
87+
$_SERVER['SERVER_NAME'] = 'localhost';
88+
}
89+
}
90+
91+
self::$ready = true;
92+
}
93+
94+
/**
95+
* Create and store an instance of the Faker library.
6396
*/
6497
protected static function init_faker() {
6598
if ( ! self::$faker ) {
6699
self::$faker = \Faker\Factory::create( 'en_US' );
100+
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
101+
}
102+
}
103+
104+
/**
105+
* Disable sending WooCommerce emails when generating objects.
106+
*
107+
* This needs to run as late in the request as possible so that the callbacks we want to remove
108+
* have actually been added.
109+
*
110+
* @return void
111+
*/
112+
public static function disable_emails() {
113+
$email_actions = array(
114+
// Customer emails.
115+
'woocommerce_new_customer_note',
116+
'woocommerce_created_customer',
117+
// Order emails.
118+
'woocommerce_order_status_pending_to_processing',
119+
'woocommerce_order_status_pending_to_completed',
120+
'woocommerce_order_status_processing_to_cancelled',
121+
'woocommerce_order_status_pending_to_failed',
122+
'woocommerce_order_status_pending_to_on-hold',
123+
'woocommerce_order_status_failed_to_processing',
124+
'woocommerce_order_status_failed_to_completed',
125+
'woocommerce_order_status_failed_to_on-hold',
126+
'woocommerce_order_status_cancelled_to_processing',
127+
'woocommerce_order_status_cancelled_to_completed',
128+
'woocommerce_order_status_cancelled_to_on-hold',
129+
'woocommerce_order_status_on-hold_to_processing',
130+
'woocommerce_order_status_on-hold_to_cancelled',
131+
'woocommerce_order_status_on-hold_to_failed',
132+
'woocommerce_order_status_completed',
133+
'woocommerce_order_status_failed',
134+
'woocommerce_order_fully_refunded',
135+
'woocommerce_order_partially_refunded',
136+
// Product emails.
137+
'woocommerce_low_stock',
138+
'woocommerce_no_stock',
139+
'woocommerce_product_on_backorder',
140+
);
141+
142+
foreach ( $email_actions as $action ) {
143+
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ) );
144+
}
145+
146+
if ( ! has_action( 'woocommerce_allow_send_queued_transactional_email', '__return_false' ) ) {
147+
add_action( 'woocommerce_allow_send_queued_transactional_email', '__return_false' );
67148
}
68149
}
69150

includes/Generator/Order.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ class Order extends Generator {
2020
* @return \WC_Order|false Order object with data populated or false when failed.
2121
*/
2222
public static function generate( $save = true, $assoc_args = array() ) {
23-
// Set this to avoid notices as when you run via WP-CLI SERVER vars are not set, order emails uses this variable.
24-
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
25-
$_SERVER['SERVER_NAME'] = 'localhost';
26-
}
27-
28-
self::init_faker();
23+
parent::maybe_initialize_generators();
2924

3025
$order = new \WC_Order();
3126
$customer = self::get_customer();
@@ -175,44 +170,11 @@ public static function get_customer() {
175170
return new \WC_Customer( $user_id );
176171
}
177172

178-
Customer::disable_emails();
179173
$customer = Customer::generate( ! $guest );
180174

181175
return $customer;
182176
}
183177

184-
/**
185-
* Disable sending WooCommerce emails when generating objects.
186-
*/
187-
public static function disable_emails() {
188-
$email_actions = array(
189-
'woocommerce_low_stock',
190-
'woocommerce_no_stock',
191-
'woocommerce_product_on_backorder',
192-
'woocommerce_order_status_pending_to_processing',
193-
'woocommerce_order_status_pending_to_completed',
194-
'woocommerce_order_status_processing_to_cancelled',
195-
'woocommerce_order_status_pending_to_failed',
196-
'woocommerce_order_status_pending_to_on-hold',
197-
'woocommerce_order_status_failed_to_processing',
198-
'woocommerce_order_status_failed_to_completed',
199-
'woocommerce_order_status_failed_to_on-hold',
200-
'woocommerce_order_status_cancelled_to_processing',
201-
'woocommerce_order_status_cancelled_to_completed',
202-
'woocommerce_order_status_cancelled_to_on-hold',
203-
'woocommerce_order_status_on-hold_to_processing',
204-
'woocommerce_order_status_on-hold_to_cancelled',
205-
'woocommerce_order_status_on-hold_to_failed',
206-
'woocommerce_order_status_completed',
207-
'woocommerce_order_fully_refunded',
208-
'woocommerce_order_partially_refunded',
209-
);
210-
211-
foreach ( $email_actions as $action ) {
212-
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
213-
}
214-
}
215-
216178
/**
217179
* Returns a date to use as the order date. If no date arguments have been passed, this will
218180
* return the current date. If a `date-start` argument is provided, a random date will be chosen

includes/Generator/Product.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,6 @@ class Product extends Generator {
6666
),
6767
);
6868

69-
/**
70-
* Init faker library.
71-
*/
72-
protected static function init_faker() {
73-
parent::init_faker();
74-
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
75-
}
76-
7769
/**
7870
* Return a new product.
7971
*
@@ -82,7 +74,7 @@ protected static function init_faker() {
8274
* @return \WC_Product The product object consisting of random data.
8375
*/
8476
public static function generate( $save = true, $assoc_args = array() ) {
85-
self::init_faker();
77+
parent::maybe_initialize_generators();
8678

8779
$type = self::get_product_type( $assoc_args );
8880
switch ( $type ) {

includes/Generator/Term.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
* Customer data generator.
1212
*/
1313
class Term extends Generator {
14-
/**
15-
* Init faker library.
16-
*/
17-
protected static function init_faker() {
18-
parent::init_faker();
19-
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
20-
}
21-
2214
/**
2315
* Create a new taxonomy term.
2416
*
@@ -44,7 +36,7 @@ public static function generate( $save = true, string $taxonomy = 'product_cat',
4436
);
4537
}
4638

47-
self::init_faker();
39+
parent::maybe_initialize_generators();
4840

4941
if ( $taxonomy_obj->hierarchical ) {
5042
$term_name = ucwords( self::$faker->department( 3 ) );

0 commit comments

Comments
 (0)