From afa82f30e8cd7efffb13c7a984cc47873ab8aafb Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 3 Jan 2025 12:56:52 -0800 Subject: [PATCH 1/2] Ensure WC emails are disabled before generating This consolidates the steps that each generator class needs to take before generating objects into one `initialize` method. These steps include creating an instance of the Faker library and unhooking all the callbacks that send transactional emails. This ensures that the emails are disabled regardless of which entrypoint to Smooth Generator is used (cli or web UI). Fixes #140 --- includes/CLI.php | 4 -- includes/Generator/Coupon.php | 11 +---- includes/Generator/Customer.php | 16 +------ includes/Generator/Generator.php | 81 ++++++++++++++++++++++++++++++-- includes/Generator/Order.php | 40 +--------------- includes/Generator/Product.php | 10 +--- includes/Generator/Term.php | 10 +--- 7 files changed, 83 insertions(+), 89 deletions(-) diff --git a/includes/CLI.php b/includes/CLI.php index f85f392..766fb4f 100644 --- a/includes/CLI.php +++ b/includes/CLI.php @@ -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( @@ -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 ) { diff --git a/includes/Generator/Coupon.php b/includes/Generator/Coupon.php index f4e7c2c..8f12241 100644 --- a/includes/Generator/Coupon.php +++ b/includes/Generator/Coupon.php @@ -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. * @@ -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, diff --git a/includes/Generator/Customer.php b/includes/Generator/Customer.php index f93b7d4..914f04e 100644 --- a/includes/Generator/Customer.php +++ b/includes/Generator/Customer.php @@ -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, @@ -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 ); - } - } } diff --git a/includes/Generator/Generator.php b/includes/Generator/Generator.php index bb41a9b..8377362 100644 --- a/includes/Generator/Generator.php +++ b/includes/Generator/Generator.php @@ -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. */ @@ -57,11 +69,74 @@ 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_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' ); } } diff --git a/includes/Generator/Order.php b/includes/Generator/Order.php index 11dc38f..af940ed 100644 --- a/includes/Generator/Order.php +++ b/includes/Generator/Order.php @@ -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(); @@ -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 diff --git a/includes/Generator/Product.php b/includes/Generator/Product.php index 8038aa2..1a3ac0f 100644 --- a/includes/Generator/Product.php +++ b/includes/Generator/Product.php @@ -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. * @@ -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 ) { diff --git a/includes/Generator/Term.php b/includes/Generator/Term.php index 969c606..7775fab 100644 --- a/includes/Generator/Term.php +++ b/includes/Generator/Term.php @@ -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. * @@ -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 ) ); From 8c42ed885fd1c63d07eef1d102694f5919198beb Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:25:25 -0800 Subject: [PATCH 2/2] Add missing email trigger for failed orders --- includes/Generator/Generator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/Generator/Generator.php b/includes/Generator/Generator.php index 8377362..39ad5b9 100644 --- a/includes/Generator/Generator.php +++ b/includes/Generator/Generator.php @@ -123,6 +123,7 @@ public static function disable_emails() { '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.