| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- * Export class
- *
- * Handles exporting product categories to CSV
- *
- * @since 1.0.0
- * @package Studiou_WC_Product_Cat_Manage
- * @subpackage Studiou_WC_Product_Cat_Manage/includes
- */
- // If this file is called directly, abort.
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_Product_Cat_Manage_Export {
-
- /**
- * Database handler
- *
- * @var Studiou_WC_Product_Cat_Manage_DB
- */
- private $db;
-
- /**
- * Constructor
- *
- * @param Studiou_WC_Product_Cat_Manage_DB $db Database handler
- */
- public function __construct($db) {
- $this->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', '<?php // Silence is golden');
- }
-
- // Get all categories
- $categories = $this->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;
- }
- }
|