Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions includes/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public static function orders( $args, $assoc_args ) {
}
}

Generator\Order::disable_emails();

$progress = \WP_CLI\Utils\make_progress_bar( 'Generating orders', $amount );

add_action(
Expand Down Expand Up @@ -134,8 +132,6 @@ public static function customers( $args, $assoc_args ) {

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

Generator\Customer::disable_emails();

add_action(
'smoothgenerator_customer_generated',
function () use ( $progress ) {
Expand Down
11 changes: 1 addition & 10 deletions includes/Generator/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
* Customer data generator.
*/
class Coupon extends Generator {

/**
* Init faker library.
*/
protected static function init_faker() {
parent::init_faker();
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
}

/**
* Create a new coupon.
*
Expand All @@ -31,7 +22,7 @@ protected static function init_faker() {
* @return \WC_Coupon|\WP_Error Coupon object with data populated.
*/
public static function generate( $save = true, $assoc_args = array() ) {
self::init_faker();
self::initialize();

$defaults = array(
'min' => 5,
Expand Down
16 changes: 1 addition & 15 deletions includes/Generator/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Customer extends Generator {
* @return \WC_Customer|\WP_Error Customer object with data populated.
*/
public static function generate( $save = true, array $assoc_args = array() ) {
self::init_faker();
self::initialize();

$args = filter_var_array(
$assoc_args,
Expand Down Expand Up @@ -153,18 +153,4 @@ public static function batch( $amount, array $args = array() ) {

return $customer_ids;
}

/**
* Disable sending WooCommerce emails when generating objects.
*/
public static function disable_emails() {
$email_actions = array(
'woocommerce_new_customer_note',
'woocommerce_created_customer',
);

foreach ( $email_actions as $action ) {
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
}
}
}
82 changes: 79 additions & 3 deletions includes/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@
* Data generator base class.
*/
abstract class Generator {

/**
* Maximum number of objects that can be generated in one batch.
*/
const MAX_BATCH_SIZE = 100;

/**
* Dimension, in pixels, of generated images.
*/
const IMAGE_SIZE = 700;

/**
* Holds the faker factory object.
* Are we ready to generate objects?
*
* @var bool
*/
protected static $ready = false;

/**
* Holds the Faker factory object.
*
* @var \Faker\Generator Factory object.
*/
Expand Down Expand Up @@ -57,11 +69,75 @@ abstract public static function generate( $save = true );
//abstract public static function batch( $amount, array $args = array() );

/**
* Init faker library.
* Get ready to generate objects.
*
* @return void
*/
protected static function initialize() {
if ( true !== self::$ready ) {
self::init_faker();
self::disable_emails();

// Set this to avoid notices as when you run via WP-CLI SERVER vars are not set, order emails uses this variable.
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
$_SERVER['SERVER_NAME'] = 'localhost';
}
}

self::$ready = true;
}

/**
* Create and store an instance of the Faker library.
*/
protected static function init_faker() {
if ( ! self::$faker ) {
self::$faker = \Faker\Factory::create( 'en_US' );
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
}
}

/**
* Disable sending WooCommerce emails when generating objects.
*
* @return void
*/
public static function disable_emails() {
$email_actions = array(
// Customer emails.
'woocommerce_new_customer_note',
'woocommerce_created_customer',
// Order emails.
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_cancelled_to_processing',
'woocommerce_order_status_cancelled_to_completed',
'woocommerce_order_status_cancelled_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_status_failed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
// Product emails.
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
);

foreach ( $email_actions as $action ) {
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ) );
}

if ( ! has_action( 'woocommerce_allow_send_queued_transactional_email', '__return_false' ) ) {
add_action( 'woocommerce_allow_send_queued_transactional_email', '__return_false' );
}
}

Expand Down
40 changes: 1 addition & 39 deletions includes/Generator/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ class Order extends Generator {
* @return \WC_Order|false Order object with data populated or false when failed.
*/
public static function generate( $save = true, $assoc_args = array() ) {
// Set this to avoid notices as when you run via WP-CLI SERVER vars are not set, order emails uses this variable.
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
$_SERVER['SERVER_NAME'] = 'localhost';
}

self::init_faker();
self::initialize();

$order = new \WC_Order();
$customer = self::get_customer();
Expand Down Expand Up @@ -175,44 +170,11 @@ public static function get_customer() {
return new \WC_Customer( $user_id );
}

Customer::disable_emails();
$customer = Customer::generate( ! $guest );

return $customer;
}

/**
* Disable sending WooCommerce emails when generating objects.
*/
public static function disable_emails() {
$email_actions = array(
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_cancelled_to_processing',
'woocommerce_order_status_cancelled_to_completed',
'woocommerce_order_status_cancelled_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
);

foreach ( $email_actions as $action ) {
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
}
}

/**
* Returns a date to use as the order date. If no date arguments have been passed, this will
* return the current date. If a `date-start` argument is provided, a random date will be chosen
Expand Down
10 changes: 1 addition & 9 deletions includes/Generator/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ class Product extends Generator {
),
);

/**
* Init faker library.
*/
protected static function init_faker() {
parent::init_faker();
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
}

/**
* Return a new product.
*
Expand All @@ -81,7 +73,7 @@ protected static function init_faker() {
* @return \WC_Product The product object consisting of random data.
*/
public static function generate( $save = true, $assoc_args = array() ) {
self::init_faker();
self::initialize();

$type = self::get_product_type( $assoc_args );
switch ( $type ) {
Expand Down
10 changes: 1 addition & 9 deletions includes/Generator/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@
* Customer data generator.
*/
class Term extends Generator {
/**
* Init faker library.
*/
protected static function init_faker() {
parent::init_faker();
self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );
}

/**
* Create a new taxonomy term.
*
Expand All @@ -44,7 +36,7 @@ public static function generate( $save = true, string $taxonomy = 'product_cat',
);
}

self::init_faker();
self::initialize();

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