db = $db; // Register AJAX handlers add_action('wp_ajax_studiou_wcpcm_export', array($this, 'handle_export_ajax')); // Add download handler add_action('admin_init', array($this, 'handle_export_download')); } /** * Handle AJAX export request */ public function handle_export_ajax() { // Check nonce if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'studiou-wcpcm-nonce')) { wp_send_json_error(array( 'message' => __('Security check failed', 'studiou-wc-product-cat-manage') )); } // Check permissions if (!current_user_can('manage_woocommerce')) { wp_send_json_error(array( 'message' => __('You do not have sufficient permissions', 'studiou-wc-product-cat-manage') )); } try { // Generate export file $filename = $this->generate_export_file(); // Return success response wp_send_json_success(array( 'message' => __('Product categories exported successfully', 'studiou-wc-product-cat-manage'), 'download_url' => admin_url('edit.php?post_type=product&page=studiou-export-product-categories&action=download&nonce=' . wp_create_nonce('studiou-wcpcm-export-download')) )); } catch (Exception $e) { wp_send_json_error(array( 'message' => $e->getMessage() )); } } /** * Handle export download */ public function handle_export_download() { if (!isset($_GET['page']) || $_GET['page'] !== 'studiou-export-product-categories' || !isset($_GET['action']) || $_GET['action'] !== 'download' || !isset($_GET['nonce']) || !wp_verify_nonce($_GET['nonce'], 'studiou-wcpcm-export-download')) { return; } // Check permissions if (!current_user_can('manage_woocommerce')) { wp_die(__('You do not have sufficient permissions', 'studiou-wc-product-cat-manage')); } $uploads_dir = wp_upload_dir(); $export_dir = trailingslashit($uploads_dir['basedir']) . 'studiou-wcpcm-exports/'; $filename = $export_dir . 'product-categories-export.csv'; if (!file_exists($filename)) { wp_die(__('Export file not found. Please try exporting again.', 'studiou-wc-product-cat-manage')); } // Set headers for download header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=product-categories-export-' . date('Y-m-d') . '.csv'); header('Pragma: no-cache'); header('Expires: 0'); // Output file content readfile($filename); exit; } /** * Generate export file * * @return string Path to the generated file * @throws Exception On error */ public function generate_export_file() { // Create export directory if it doesn't exist $uploads_dir = wp_upload_dir(); $export_dir = trailingslashit($uploads_dir['basedir']) . 'studiou-wcpcm-exports/'; if (!file_exists($export_dir)) { wp_mkdir_p($export_dir); // Create an index.php file to prevent directory listing file_put_contents($export_dir . 'index.php', 'db->get_all_product_categories(); // Define CSV headers $headers = array( 'name', 'url-name', 'parent-name', 'description', 'display-type', 'visibility', 'protected-passwords', 'operation' ); // Generate CSV content $csv_content = $this->generate_csv($headers, $categories); // Save to file $filename = $export_dir . 'product-categories-export.csv'; $result = file_put_contents($filename, $csv_content); if (false === $result) { throw new Exception(__('Failed to write export file', 'studiou-wc-product-cat-manage')); } return $filename; } /** * Generate CSV content * * @param array $headers CSV headers * @param array $data Data rows * @return string CSV content */ private function generate_csv($headers, $data) { ob_start(); $output = fopen('php://output', 'w'); // Add UTF-8 BOM for Excel compatibility fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF)); // Add headers fputcsv($output, $headers); // Add data rows foreach ($data as $row) { // Ensure all headers are present in the row $csv_row = array(); foreach ($headers as $header) { $csv_row[] = isset($row[$header]) ? $row[$header] : ''; } fputcsv($output, $csv_row); } fclose($output); $csv_content = ob_get_clean(); return $csv_content; } }