class-studiou-wc-product-cat-manage-export.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Export class
  4. *
  5. * Handles exporting product categories to CSV
  6. *
  7. * @since 1.0.0
  8. * @package Studiou_WC_Product_Cat_Manage
  9. * @subpackage Studiou_WC_Product_Cat_Manage/includes
  10. */
  11. // If this file is called directly, abort.
  12. if (!defined('WPINC')) {
  13. die;
  14. }
  15. class Studiou_WC_Product_Cat_Manage_Export {
  16. /**
  17. * Database handler
  18. *
  19. * @var Studiou_WC_Product_Cat_Manage_DB
  20. */
  21. private $db;
  22. /**
  23. * Constructor
  24. *
  25. * @param Studiou_WC_Product_Cat_Manage_DB $db Database handler
  26. */
  27. public function __construct($db) {
  28. $this->db = $db;
  29. // Register AJAX handlers
  30. add_action('wp_ajax_studiou_wcpcm_export', array($this, 'handle_export_ajax'));
  31. // Add download handler
  32. add_action('admin_init', array($this, 'handle_export_download'));
  33. }
  34. /**
  35. * Handle AJAX export request
  36. */
  37. public function handle_export_ajax() {
  38. // Check nonce
  39. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'studiou-wcpcm-nonce')) {
  40. wp_send_json_error(array(
  41. 'message' => __('Security check failed', 'studiou-wc-product-cat-manage')
  42. ));
  43. }
  44. // Check permissions
  45. if (!current_user_can('manage_woocommerce')) {
  46. wp_send_json_error(array(
  47. 'message' => __('You do not have sufficient permissions', 'studiou-wc-product-cat-manage')
  48. ));
  49. }
  50. try {
  51. // Generate export file
  52. $filename = $this->generate_export_file();
  53. // Return success response
  54. wp_send_json_success(array(
  55. 'message' => __('Product categories exported successfully', 'studiou-wc-product-cat-manage'),
  56. 'download_url' => admin_url('edit.php?post_type=product&page=studiou-export-product-categories&action=download&nonce=' . wp_create_nonce('studiou-wcpcm-export-download'))
  57. ));
  58. } catch (Exception $e) {
  59. wp_send_json_error(array(
  60. 'message' => $e->getMessage()
  61. ));
  62. }
  63. }
  64. /**
  65. * Handle export download
  66. */
  67. public function handle_export_download() {
  68. if (!isset($_GET['page']) || $_GET['page'] !== 'studiou-export-product-categories' ||
  69. !isset($_GET['action']) || $_GET['action'] !== 'download' ||
  70. !isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], 'studiou-wcpcm-export-download')) {
  71. return;
  72. }
  73. // Check permissions
  74. if (!current_user_can('manage_woocommerce')) {
  75. wp_die(__('You do not have sufficient permissions', 'studiou-wc-product-cat-manage'));
  76. }
  77. $uploads_dir = wp_upload_dir();
  78. $export_dir = trailingslashit($uploads_dir['basedir']) . 'studiou-wcpcm-exports/';
  79. $filename = $export_dir . 'product-categories-export.csv';
  80. if (!file_exists($filename)) {
  81. wp_die(__('Export file not found. Please try exporting again.', 'studiou-wc-product-cat-manage'));
  82. }
  83. // Set headers for download
  84. header('Content-Type: text/csv; charset=utf-8');
  85. header('Content-Disposition: attachment; filename=product-categories-export-' . date('Y-m-d') . '.csv');
  86. header('Pragma: no-cache');
  87. header('Expires: 0');
  88. // Output file content
  89. readfile($filename);
  90. exit;
  91. }
  92. /**
  93. * Generate export file
  94. *
  95. * @return string Path to the generated file
  96. * @throws Exception On error
  97. */
  98. public function generate_export_file() {
  99. // Create export directory if it doesn't exist
  100. $uploads_dir = wp_upload_dir();
  101. $export_dir = trailingslashit($uploads_dir['basedir']) . 'studiou-wcpcm-exports/';
  102. if (!file_exists($export_dir)) {
  103. wp_mkdir_p($export_dir);
  104. // Create an index.php file to prevent directory listing
  105. file_put_contents($export_dir . 'index.php', '<?php // Silence is golden');
  106. }
  107. // Get all categories
  108. $categories = $this->db->get_all_product_categories();
  109. // Define CSV headers
  110. $headers = array(
  111. 'name',
  112. 'url-name',
  113. 'parent-name',
  114. 'description',
  115. 'display-type',
  116. 'visibility',
  117. 'protected-passwords',
  118. 'operation'
  119. );
  120. // Generate CSV content
  121. $csv_content = $this->generate_csv($headers, $categories);
  122. // Save to file
  123. $filename = $export_dir . 'product-categories-export.csv';
  124. $result = file_put_contents($filename, $csv_content);
  125. if (false === $result) {
  126. throw new Exception(__('Failed to write export file', 'studiou-wc-product-cat-manage'));
  127. }
  128. return $filename;
  129. }
  130. /**
  131. * Generate CSV content
  132. *
  133. * @param array $headers CSV headers
  134. * @param array $data Data rows
  135. * @return string CSV content
  136. */
  137. private function generate_csv($headers, $data) {
  138. ob_start();
  139. $output = fopen('php://output', 'w');
  140. // Add UTF-8 BOM for Excel compatibility
  141. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  142. // Add headers
  143. fputcsv($output, $headers);
  144. // Add data rows
  145. foreach ($data as $row) {
  146. // Ensure all headers are present in the row
  147. $csv_row = array();
  148. foreach ($headers as $header) {
  149. $csv_row[] = isset($row[$header]) ? $row[$header] : '';
  150. }
  151. fputcsv($output, $csv_row);
  152. }
  153. fclose($output);
  154. $csv_content = ob_get_clean();
  155. return $csv_content;
  156. }
  157. }