| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- // Handles custom order status registration and integration.
- defined('ABSPATH') || exit;
- class Order_Status_Manager {
- private $custom_statuses;
- private $print_status_slugs = array('to-print', 'in-print');
- public function __construct() {
- add_action('init', array($this, 'register_custom_order_statuses'));
- add_filter('wc_order_statuses', array($this, 'add_custom_order_statuses_to_list'));
- add_action('woocommerce_order_status_changed', array($this, 'handle_status_transitions'), 10, 3);
- add_action('woocommerce_payment_complete', array($this, 'set_order_processing_status'));
- }
- private function get_custom_status_definitions() {
- if ($this->custom_statuses === null) {
- $this->custom_statuses = array(
- 'wc-to-print' => array(
- 'label' => _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
- 'label_count' => _n_noop(
- 'Prepare to Printing <span class="count">(%s)</span>',
- 'Prepare to Printing <span class="count">(%s)</span>',
- 'studiou-wc-ord-print-statuses'
- ),
- ),
- 'wc-in-print' => array(
- 'label' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
- 'label_count' => _n_noop(
- 'In Printing <span class="count">(%s)</span>',
- 'In Printing <span class="count">(%s)</span>',
- 'studiou-wc-ord-print-statuses'
- ),
- ),
- );
- }
- return $this->custom_statuses;
- }
- public function register_custom_order_statuses() {
- foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
- register_post_status($status_slug, array(
- 'label' => $status_args['label'],
- 'public' => true,
- 'exclude_from_search' => false,
- 'show_in_admin_all_list' => true,
- 'show_in_admin_status_list' => true,
- 'label_count' => $status_args['label_count'],
- ));
- }
- }
- public function add_custom_order_statuses_to_list($order_statuses) {
- $new_order_statuses = array();
- foreach ($order_statuses as $key => $status) {
- $new_order_statuses[$key] = $status;
- if ($key === 'wc-processing') {
- foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
- $new_order_statuses[$status_slug] = $status_args['label'];
- }
- }
- }
- return $new_order_statuses;
- }
- /**
- * Stamp the relevant meta key when an order transitions into a print status.
- * Uses the order object (HPOS-safe) rather than update_post_meta.
- *
- * Note: bulk and import paths in Studiou_DB_Manager also stamp the same
- * meta key explicitly. That redundancy is intentional — the bulk path
- * always re-stamps (a re-run should refresh the timestamp), while this
- * listener handles manual single-order transitions originating from the
- * order edit screen. The "only if empty" guard below preserves the
- * original timestamp for manual flows; the bulk path overwrites it via
- * its own explicit call.
- */
- public function handle_status_transitions($order_id, $old_status, $new_status) {
- UtilsLog::log('Order #' . $order_id . ' status change ' . $old_status . ' -> ' . $new_status);
- if (!in_array($new_status, $this->print_status_slugs, true)) {
- return;
- }
- $order = wc_get_order($order_id);
- if (!$order) {
- return;
- }
- $meta_key = ($new_status === 'to-print') ? 'to_print_date' : 'in_print_date';
- if (!$order->get_meta($meta_key)) {
- $order->update_meta_data($meta_key, current_time('mysql'));
- $order->save();
- }
- }
- /**
- * Move paid orders to "processing" on payment completion — except when the
- * order is already past that point (completed) or has been advanced into the
- * print pipeline (to-print, in-print). Otherwise late payments would silently
- * roll a print-state order back to processing.
- */
- public function set_order_processing_status($order_id) {
- $order = wc_get_order($order_id);
- if (!$order) {
- return;
- }
- $protected = array_merge(array('completed'), $this->print_status_slugs);
- if ($order->has_status($protected)) {
- return;
- }
- // Pass the explanatory note to update_status — that's the canonical
- // place for a status-change note. Adding a second note via
- // add_order_note() would double-log the same event in the order
- // activity feed.
- $order->update_status(
- 'processing',
- __('Payment received — order automatically set to processing by Studiou WC Order Print Statuses plugin.', 'studiou-wc-ord-print-statuses')
- );
- }
- public function get_custom_statuses() {
- return $this->get_custom_status_definitions();
- }
- }
|