|
11 | 11 | * Data generator base class. |
12 | 12 | */ |
13 | 13 | abstract class Generator { |
14 | | - |
| 14 | + /** |
| 15 | + * Maximum number of objects that can be generated in one batch. |
| 16 | + */ |
15 | 17 | const MAX_BATCH_SIZE = 100; |
16 | 18 |
|
| 19 | + /** |
| 20 | + * Dimension, in pixels, of generated images. |
| 21 | + */ |
17 | 22 | const IMAGE_SIZE = 700; |
18 | 23 |
|
19 | 24 | /** |
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. |
21 | 33 | * |
22 | 34 | * @var \Faker\Generator Factory object. |
23 | 35 | */ |
@@ -59,11 +71,80 @@ abstract public static function generate( $save = true ); |
59 | 71 | //abstract public static function batch( $amount, array $args = array() ); |
60 | 72 |
|
61 | 73 | /** |
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. |
63 | 96 | */ |
64 | 97 | protected static function init_faker() { |
65 | 98 | if ( ! self::$faker ) { |
66 | 99 | 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' ); |
67 | 148 | } |
68 | 149 | } |
69 | 150 |
|
|
0 commit comments