|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Sets up all logic related to the Checkout Draft Orders service |
| 4 | + * |
| 5 | + * @package WooCommerce/Blocks |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Automattic\WooCommerce\Blocks\Domain\Services; |
| 9 | + |
| 10 | +use Automattic\WooCommerce\Blocks\Domain\Package; |
| 11 | +use Exception; |
| 12 | +use WC_Order; |
| 13 | + |
| 14 | +/** |
| 15 | + * Service class for adding DraftOrder functionality to WooCommerce core. |
| 16 | + */ |
| 17 | +class DraftOrders { |
| 18 | + |
| 19 | + const DB_STATUS = 'wc-checkout-draft'; |
| 20 | + const STATUS = 'checkout-draft'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Holds the Package instance |
| 24 | + * |
| 25 | + * @var Package |
| 26 | + */ |
| 27 | + private $package; |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructor |
| 31 | + * |
| 32 | + * @param Package $package An instance of the package class. |
| 33 | + */ |
| 34 | + public function __construct( Package $package ) { |
| 35 | + $this->package = $package; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Set all hooks related to adding Checkout Draft order functionality to Woo Core. |
| 40 | + */ |
| 41 | + public function init() { |
| 42 | + if ( $this->package->is_feature_plugin_build() ) { |
| 43 | + add_filter( 'wc_order_statuses', [ $this, 'register_draft_order_status' ] ); |
| 44 | + add_filter( 'woocommerce_register_shop_order_post_statuses', [ $this, 'register_draft_order_post_status' ] ); |
| 45 | + add_filter( 'woocommerce_valid_order_statuses_for_payment', [ $this, 'append_draft_order_post_status' ] ); |
| 46 | + add_action( 'woocommerce_cleanup_draft_orders', [ $this, 'delete_expired_draft_orders' ] ); |
| 47 | + add_action( 'admin_init', [ $this, 'install' ] ); |
| 48 | + } else { |
| 49 | + // Maybe remove existing cronjob if present because it shouldn't be needed in the environment. |
| 50 | + add_action( 'admin_init', [ $this, 'uninstall' ] ); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Installation related logic for Draft order functionality. |
| 56 | + * |
| 57 | + * @internal |
| 58 | + */ |
| 59 | + public function install() { |
| 60 | + $this->maybe_create_cronjobs(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Remove cronjobs if they exist (but only from admin). |
| 65 | + * |
| 66 | + * @internal |
| 67 | + */ |
| 68 | + public function uninstall() { |
| 69 | + $this->maybe_remove_cronjobs(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Maybe create cron events. |
| 74 | + */ |
| 75 | + protected function maybe_create_cronjobs() { |
| 76 | + if ( function_exists( 'as_next_scheduled_action' ) && false === as_next_scheduled_action( 'woocommerce_cleanup_draft_orders' ) ) { |
| 77 | + as_schedule_recurring_action( strtotime( 'midnight tonight' ), DAY_IN_SECONDS, 'woocommerce_cleanup_draft_orders' ); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Unschedule cron jobs that are present. |
| 83 | + */ |
| 84 | + protected function maybe_remove_cronjobs() { |
| 85 | + if ( function_exists( 'as_next_scheduled_action' ) && as_next_scheduled_action( 'woocommerce_cleanup_draft_orders' ) ) { |
| 86 | + as_unschedule_all_actions( 'woocommerce_cleanup_draft_orders' ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Register custom order status for orders created via the API during checkout. |
| 92 | + * |
| 93 | + * Draft order status is used before payment is attempted, during checkout, when a cart is converted to an order. |
| 94 | + * |
| 95 | + * @param array $statuses Array of statuses. |
| 96 | + * @internal |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + public function register_draft_order_status( array $statuses ) { |
| 100 | + $statuses[ self::DB_STATUS ] = _x( 'Draft', 'Order status', 'woo-gutenberg-products-block' ); |
| 101 | + return $statuses; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Register custom order post status for orders created via the API during checkout. |
| 106 | + * |
| 107 | + * @param array $statuses Array of statuses. |
| 108 | + * @internal |
| 109 | +
|
| 110 | + * @return array |
| 111 | + */ |
| 112 | + public function register_draft_order_post_status( array $statuses ) { |
| 113 | + $statuses[ self::DB_STATUS ] = [ |
| 114 | + 'label' => _x( 'Draft', 'Order status', 'woo-gutenberg-products-block' ), |
| 115 | + 'public' => false, |
| 116 | + 'exclude_from_search' => false, |
| 117 | + 'show_in_admin_all_list' => false, |
| 118 | + 'show_in_admin_status_list' => true, |
| 119 | + /* translators: %s: number of orders */ |
| 120 | + 'label_count' => _n_noop( 'Drafts <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>', 'woo-gutenberg-products-block' ), |
| 121 | + ]; |
| 122 | + return $statuses; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Append draft status to a list of statuses. |
| 127 | + * |
| 128 | + * @param array $statuses Array of statuses. |
| 129 | + * @internal |
| 130 | +
|
| 131 | + * @return array |
| 132 | + */ |
| 133 | + public function append_draft_order_post_status( $statuses ) { |
| 134 | + $statuses[] = self::STATUS; |
| 135 | + return $statuses; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Delete draft orders older than a day in batches of 20. |
| 140 | + * |
| 141 | + * Ran on a daily cron schedule. |
| 142 | + * |
| 143 | + * @internal |
| 144 | + */ |
| 145 | + public function delete_expired_draft_orders() { |
| 146 | + $count = 0; |
| 147 | + $batch_size = 20; |
| 148 | + $orders = wc_get_orders( |
| 149 | + [ |
| 150 | + 'date_modified' => '<=' . strtotime( '-1 DAY' ), |
| 151 | + 'limit' => $batch_size, |
| 152 | + 'status' => self::DB_STATUS, |
| 153 | + 'type' => 'shop_order', |
| 154 | + ] |
| 155 | + ); |
| 156 | + |
| 157 | + // do we bail because the query results are unexpected? |
| 158 | + try { |
| 159 | + $this->assert_order_results( $orders, $batch_size ); |
| 160 | + if ( $orders ) { |
| 161 | + foreach ( $orders as $order ) { |
| 162 | + $order->delete( true ); |
| 163 | + $count ++; |
| 164 | + } |
| 165 | + } |
| 166 | + if ( $batch_size === $count && function_exists( 'as_enqueue_async_action' ) ) { |
| 167 | + as_enqueue_async_action( 'woocommerce_cleanup_draft_orders' ); |
| 168 | + } |
| 169 | + } catch ( Exception $error ) { |
| 170 | + wc_caught_exception( $error, __METHOD__ ); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Asserts whether incoming order results are expected given the query |
| 176 | + * this service class executes. |
| 177 | + * |
| 178 | + * @param WC_Order[] $order_results The order results being asserted. |
| 179 | + * @param int $expected_batch_size The expected batch size for the results. |
| 180 | + * @throws Exception If any assertions fail, an exception is thrown. |
| 181 | + */ |
| 182 | + private function assert_order_results( $order_results, $expected_batch_size ) { |
| 183 | + // if not an array, then just return because it won't get handled |
| 184 | + // anyways. |
| 185 | + if ( ! is_array( $order_results ) ) { |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + $suffix = ' This is an indicator that something is filtering WooCommerce or WordPress queries and modifying the query parameters.'; |
| 190 | + |
| 191 | + // if count is greater than our expected batch size, then that's a problem. |
| 192 | + if ( count( $order_results ) > 20 ) { |
| 193 | + throw new Exception( 'There are an unexpected number of results returned from the query.' . $suffix ); |
| 194 | + } |
| 195 | + |
| 196 | + // if any of the returned orders are not draft (or not a WC_Order), then that's a problem. |
| 197 | + foreach ( $order_results as $order ) { |
| 198 | + if ( ! ( $order instanceof WC_Order ) ) { |
| 199 | + throw new Exception( 'The returned results contain a value that is not a WC_Order.' . $suffix ); |
| 200 | + } |
| 201 | + if ( ! $order->has_status( self::STATUS ) ) { |
| 202 | + throw new Exception( 'The results contain an order that is not a `wc-checkout-draft` status in the results.' . $suffix ); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments