class-export-handler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Export Handler Class - Handles CSV export functionality
  4. *
  5. * @package StudiouWCCustomReports
  6. */
  7. if (!defined('ABSPATH')) {
  8. exit;
  9. }
  10. class StudiouWC_Export_Handler {
  11. private $db_manager;
  12. private $settings_manager;
  13. public function __construct($db_manager, $settings_manager) {
  14. $this->db_manager = $db_manager;
  15. $this->settings_manager = $settings_manager;
  16. }
  17. public function export_csv() {
  18. if (!current_user_can('manage_woocommerce')) {
  19. wp_die(__('You do not have sufficient permissions to access this page.', StudiouWC_Main::get_text_domain()));
  20. }
  21. $date_from = isset($_POST['date_from']) ? sanitize_text_field($_POST['date_from']) : '';
  22. $date_to = isset($_POST['date_to']) ? sanitize_text_field($_POST['date_to']) : '';
  23. $statuses = isset($_POST['statuses']) ? array_map('sanitize_text_field', $_POST['statuses']) : array();
  24. // Get all data without pagination for export
  25. $report_data = $this->db_manager->get_product_categories_summary($date_from, $date_to, $statuses, 999999, 1, 'category', 'ASC');
  26. $settings = $this->settings_manager->get_settings();
  27. $default_property = $settings['default_property'] ?? '';
  28. $property_values = array();
  29. if (!empty($default_property)) {
  30. $property_values = $this->db_manager->get_property_values($default_property);
  31. }
  32. $this->generate_csv_output($report_data, $property_values);
  33. }
  34. private function generate_csv_output($report_data, $property_values) {
  35. // Set headers for CSV download
  36. header('Content-Type: text/csv; charset=utf-8');
  37. $filename = sprintf(
  38. /* translators: %s: current date */
  39. __('custom-reports-%s.csv', StudiouWC_Main::get_text_domain()),
  40. date('Y-m-d')
  41. );
  42. header('Content-Disposition: attachment; filename="' . $filename . '"');
  43. header('Pragma: no-cache');
  44. header('Expires: 0');
  45. $output = fopen('php://output', 'w');
  46. // Add UTF-8 BOM for Excel compatibility
  47. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  48. // CSV headers
  49. $headers = $this->get_csv_headers($property_values);
  50. fputcsv($output, $headers);
  51. // CSV data
  52. foreach ($report_data as $row) {
  53. $csv_row = $this->format_csv_row($row, $property_values);
  54. fputcsv($output, $csv_row);
  55. }
  56. fclose($output);
  57. exit;
  58. }
  59. private function get_csv_headers($property_values) {
  60. $headers = array(
  61. __('Product Category', StudiouWC_Main::get_text_domain()),
  62. __('Orders Count', StudiouWC_Main::get_text_domain())
  63. );
  64. foreach ($property_values as $slug => $name) {
  65. /* translators: %s: property name */
  66. $headers[] = sprintf(__('%s Count', StudiouWC_Main::get_text_domain()), $name);
  67. }
  68. $headers[] = __('Total Price', StudiouWC_Main::get_text_domain());
  69. return $headers;
  70. }
  71. private function format_csv_row($row, $property_values) {
  72. $csv_row = array(
  73. $row['category'],
  74. $row['orders_count']
  75. );
  76. foreach ($property_values as $slug => $name) {
  77. $csv_row[] = $row['property_counts'][$slug] ?? 0;
  78. }
  79. // Format price without HTML for CSV
  80. $csv_row[] = strip_tags(wc_price($row['total_price']));
  81. return $csv_row;
  82. }
  83. }