class-bulk-actions-manager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. // Handles custom bulk actions for orders.
  3. // visualized
  4. class Bulk_Actions_Manager {
  5. public function __construct() {
  6. add_filter('bulk_actions-woocommerce_page_wc-orders', array($this, 'register_bulk_actions'));
  7. add_filter('handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_actions'), 10, 3);
  8. }
  9. public function register_bulk_actions($bulk_actions) {
  10. $bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
  11. $bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
  12. $bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
  13. UtilsLog::log('Bulk actions registered');
  14. return $bulk_actions;
  15. }
  16. public function handle_bulk_actions($redirect_to, $action, $post_ids) {
  17. switch ($action) {
  18. case 'prepare_to_printing_export':
  19. $redirect_to = $this->prepare_to_printing_export($post_ids, $redirect_to);
  20. break;
  21. case 'set_status_to_prepare_to_printing':
  22. $redirect_to = $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  23. break;
  24. case 'set_status_to_in_printing':
  25. $redirect_to = $this->set_status_to_in_printing($post_ids, $redirect_to);
  26. break;
  27. }
  28. return $redirect_to;
  29. }
  30. private function prepare_to_printing_export($post_ids, $redirect_to) {
  31. // Get orders data using the DB Manager
  32. $orders_data = Studiou_DB_Manager::get_orders_for_printing($post_ids);
  33. // Update order statuses
  34. $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  35. UtilsLog::log('Order statuses set to "to-print" after export.');
  36. UtilsLog::message('Order statuses (' . count($orders_data) . ') set to "to-print" after export.', 'info' );
  37. if (!empty($orders_data)) {
  38. $csv_data = $this->generate_csv($orders_data);
  39. UtilsLog::log('file prepare_to_printing_export.csv exported');
  40. $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
  41. }
  42. return $redirect_to;
  43. }
  44. private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
  45. // Use the DB Manager to update order statuses
  46. Studiou_DB_Manager::set_orders_to_prepare_printing($post_ids);
  47. $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
  48. return $redirect_to;
  49. }
  50. private function set_status_to_in_printing($post_ids, $redirect_to) {
  51. // Use the DB Manager to update order statuses
  52. Studiou_DB_Manager::set_orders_to_in_printing($post_ids);
  53. $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
  54. return $redirect_to;
  55. }
  56. private function generate_csv($data) {
  57. $output = fopen('php://temp', 'w');
  58. fputcsv($output, array_keys((array)$data[0]),','); // Headers
  59. foreach ($data as $row) {
  60. fputcsv($output, (array)$row, ',');
  61. }
  62. rewind($output);
  63. $csv = stream_get_contents($output);
  64. fclose($output);
  65. return $csv;
  66. }
  67. private function output_csv($csv_data, $filename) {
  68. header('Content-Type: text/csv');
  69. header('Content-Disposition: attachment; filename="' . $filename . '"');
  70. echo $csv_data;
  71. exit;
  72. }
  73. }