class-order-status-manager.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // Handles custom order status registration and integration.
  3. defined('ABSPATH') || exit;
  4. class Order_Status_Manager {
  5. private $custom_statuses;
  6. private $print_status_slugs = array('to-print', 'in-print');
  7. public function __construct() {
  8. add_action('init', array($this, 'register_custom_order_statuses'));
  9. add_filter('wc_order_statuses', array($this, 'add_custom_order_statuses_to_list'));
  10. add_action('woocommerce_order_status_changed', array($this, 'handle_status_transitions'), 10, 3);
  11. add_action('woocommerce_payment_complete', array($this, 'set_order_processing_status'));
  12. }
  13. private function get_custom_status_definitions() {
  14. if ($this->custom_statuses === null) {
  15. $this->custom_statuses = array(
  16. 'wc-to-print' => array(
  17. 'label' => _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
  18. 'label_count' => _n_noop(
  19. 'Prepare to Printing <span class="count">(%s)</span>',
  20. 'Prepare to Printing <span class="count">(%s)</span>',
  21. 'studiou-wc-ord-print-statuses'
  22. ),
  23. ),
  24. 'wc-in-print' => array(
  25. 'label' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
  26. 'label_count' => _n_noop(
  27. 'In Printing <span class="count">(%s)</span>',
  28. 'In Printing <span class="count">(%s)</span>',
  29. 'studiou-wc-ord-print-statuses'
  30. ),
  31. ),
  32. );
  33. }
  34. return $this->custom_statuses;
  35. }
  36. public function register_custom_order_statuses() {
  37. foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
  38. register_post_status($status_slug, array(
  39. 'label' => $status_args['label'],
  40. 'public' => true,
  41. 'exclude_from_search' => false,
  42. 'show_in_admin_all_list' => true,
  43. 'show_in_admin_status_list' => true,
  44. 'label_count' => $status_args['label_count'],
  45. ));
  46. }
  47. }
  48. public function add_custom_order_statuses_to_list($order_statuses) {
  49. $new_order_statuses = array();
  50. $inserted = false;
  51. foreach ($order_statuses as $key => $status) {
  52. $new_order_statuses[$key] = $status;
  53. if ($key === 'wc-processing') {
  54. foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
  55. $new_order_statuses[$status_slug] = $status_args['label'];
  56. }
  57. $inserted = true;
  58. }
  59. }
  60. // Fallback: append at the end if `wc-processing` was removed by
  61. // another plugin. Better to surface the custom statuses somewhere
  62. // in the dropdown than to drop them silently.
  63. if (!$inserted) {
  64. foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
  65. $new_order_statuses[$status_slug] = $status_args['label'];
  66. }
  67. }
  68. return $new_order_statuses;
  69. }
  70. /**
  71. * Stamp the relevant meta key when an order transitions into a print status.
  72. * Uses the order object (HPOS-safe) rather than update_post_meta.
  73. *
  74. * Note: bulk and import paths in Studiou_DB_Manager also stamp the same
  75. * meta key explicitly. That redundancy is intentional — the bulk path
  76. * always re-stamps (a re-run should refresh the timestamp), while this
  77. * listener handles manual single-order transitions originating from the
  78. * order edit screen. The "only if empty" guard below preserves the
  79. * original timestamp for manual flows; the bulk path overwrites it via
  80. * its own explicit call.
  81. */
  82. public function handle_status_transitions($order_id, $old_status, $new_status) {
  83. UtilsLog::log('Order #' . $order_id . ' status change ' . $old_status . ' -> ' . $new_status);
  84. if (!in_array($new_status, $this->print_status_slugs, true)) {
  85. return;
  86. }
  87. $order = wc_get_order($order_id);
  88. if (!$order) {
  89. return;
  90. }
  91. $meta_key = ($new_status === 'to-print') ? 'to_print_date' : 'in_print_date';
  92. if (!$order->get_meta($meta_key)) {
  93. $order->update_meta_data($meta_key, current_time('mysql'));
  94. $order->save();
  95. }
  96. }
  97. /**
  98. * Move paid orders to "processing" on payment completion — except when the
  99. * order is already past that point (completed) or has been advanced into the
  100. * print pipeline (to-print, in-print). Otherwise late payments would silently
  101. * roll a print-state order back to processing.
  102. */
  103. public function set_order_processing_status($order_id) {
  104. $order = wc_get_order($order_id);
  105. if (!$order) {
  106. return;
  107. }
  108. $protected = array_merge(array('completed'), $this->print_status_slugs);
  109. if ($order->has_status($protected)) {
  110. return;
  111. }
  112. // Pass the explanatory note to update_status — that's the canonical
  113. // place for a status-change note. Adding a second note via
  114. // add_order_note() would double-log the same event in the order
  115. // activity feed.
  116. $order->update_status(
  117. 'processing',
  118. __('Payment received — order automatically set to processing by Studiou WC Order Print Statuses plugin.', 'studiou-wc-ord-print-statuses')
  119. );
  120. }
  121. public function get_custom_statuses() {
  122. return $this->get_custom_status_definitions();
  123. }
  124. }