class-custom-columns-manager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. // Manages custom columns in the WooCommerce orders list (HPOS and legacy).
  3. defined('ABSPATH') || exit;
  4. class Custom_Columns_Manager {
  5. public function __construct() {
  6. add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'add_custom_shop_order_column'));
  7. add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'add_custom_shop_order_column_content'), 10, 2);
  8. }
  9. public function add_custom_shop_order_column($columns) {
  10. $new_columns = array();
  11. foreach ($columns as $column_name => $column_info) {
  12. $new_columns[$column_name] = $column_info;
  13. if ($column_name === 'order_total') {
  14. $new_columns['to_print_date'] = __('To Print Date', 'studiou-wc-ord-print-statuses');
  15. $new_columns['in_print_date'] = __('In Print Date', 'studiou-wc-ord-print-statuses');
  16. $new_columns['external_order_number'] = __('External Order Number', 'studiou-wc-ord-print-statuses');
  17. }
  18. }
  19. return $new_columns;
  20. }
  21. /**
  22. * Render cell content. Under HPOS WC passes the WC_Order object as the
  23. * second argument; under legacy CPT it passes the post ID. wc_get_order()
  24. * normalises both forms, so we accept either.
  25. */
  26. public function add_custom_shop_order_column_content($column, $order_or_id) {
  27. $order = wc_get_order($order_or_id);
  28. if (!$order) {
  29. echo '—';
  30. return;
  31. }
  32. switch ($column) {
  33. case 'to_print_date':
  34. echo $this->format_meta_date($order->get_meta('to_print_date'));
  35. break;
  36. case 'in_print_date':
  37. echo $this->format_meta_date($order->get_meta('in_print_date'));
  38. break;
  39. case 'external_order_number':
  40. $value = $order->get_meta('external_ref_ord_no');
  41. echo $value !== '' ? esc_html($value) : '—';
  42. break;
  43. }
  44. }
  45. /**
  46. * Format a MySQL datetime meta value for display.
  47. *
  48. * The stored value (written by current_time('mysql')) is already in WP
  49. * local time. Previously this method used strtotime()/date_i18n() which
  50. * applied the timezone offset twice on hosts where the PHP server
  51. * timezone differs from the WP site timezone (e.g. UTC server +
  52. * Europe/Prague site → display drifted by 1–2 hours).
  53. *
  54. * We now parse the string as WP-local time explicitly, then format via
  55. * wp_date() which respects WP timezone. This keeps locale-aware
  56. * formatting while eliminating the offset drift.
  57. */
  58. private function format_meta_date($value) {
  59. if (empty($value)) {
  60. return '—';
  61. }
  62. $value = trim((string) $value);
  63. // Accept both 'YYYY-MM-DD HH:MM:SS' and 'YYYY-MM-DDTHH:MM[:SS]'.
  64. $normalised = str_replace('T', ' ', $value);
  65. if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?$/', $normalised)) {
  66. return '—';
  67. }
  68. if (strlen($normalised) === 16) { // missing seconds
  69. $normalised .= ':00';
  70. }
  71. try {
  72. $dt = new DateTimeImmutable($normalised, wp_timezone());
  73. } catch (Exception $e) {
  74. return '—';
  75. }
  76. $fmt = get_option('date_format') . ' ' . get_option('time_format');
  77. return esc_html(wp_date($fmt, $dt->getTimestamp()));
  78. }
  79. }