Explorar o código

Merge branch '1.3.0.studiou-wc-ord-print-statuses'
studiou-wc-ord-print-statusesv.1.3.0 tested

Dalibor Votruba hai 1 ano
pai
achega
3a7221f25c

+ 90 - 104
studiou-wc-ord-print-statuses/includes/class-bulk-actions-manager.php

@@ -1,104 +1,90 @@
-<?php
-// Handles custom bulk actions for orders.
-// visualized
-class Bulk_Actions_Manager {
-    public function __construct() {
-        add_filter('bulk_actions-woocommerce_page_wc-orders', array($this, 'register_bulk_actions'));
-        add_filter('handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_actions'), 10, 3);
-    }
-
-    public function register_bulk_actions($bulk_actions) {
-        $bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
-        $bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
-        $bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
-
-        UtilsLog::log('Bulk actions registered');
-        return $bulk_actions;
-    }
-
-    public function handle_bulk_actions($redirect_to, $action, $post_ids) {
-        switch ($action) {
-            case 'prepare_to_printing_export':
-                $redirect_to = $this->prepare_to_printing_export($post_ids, $redirect_to);
-                break;
-            case 'set_status_to_prepare_to_printing':
-                $redirect_to = $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
-                break;
-            case 'set_status_to_in_printing':
-                $redirect_to = $this->set_status_to_in_printing($post_ids, $redirect_to);
-                break;
-        }
-        return $redirect_to;
-    }
-
-    private function prepare_to_printing_export($post_ids, $redirect_to) {
-        global $wpdb;
-        $orders_data = $wpdb->get_results("SELECT * FROM `vsu_orders_car_prod_qty_img` WHERE order_no IN (" . implode(',', array_map('intval', $post_ids)) . ")");
-
-
-        $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
-        UtilsLog::log('Order statuses set to "to-print" after export.');
-        UtilsLog::message('Order statuses (' . count($orders_data) . ') set to "to-print" after export.', 'info' );
-
-        if (!empty($orders_data)) {
-            $csv_data = $this->generate_csv($orders_data);
-            UtilsLog::log('file prepare_to_printing_export.csv exported');
-            $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
-        }
-        return $redirect_to;
-    }
-
-    private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
-        
-        foreach ($post_ids as $post_id) {
-            UtilsLog::log('Setting status Order #' . $post_id . ' to "to-print"');
-            $order = wc_get_order($post_id);
-            if ($order) {
-                $order->update_status('to-print');
-                update_post_meta($post_id, 'to_print_date', current_time('mysql'));
-                UtilsLog::log('Order #' . $post_id . ' marked as "to-print"');
-                UtilsLog::message('Order #' . $post_id . ' marked as "to-print"' );
-            }
-            else{
-                UtilsLog::log('Order #' . $post_id . ' not found');
-            }
-        }
-        $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
-        return $redirect_to;
-    }
-
-    private function set_status_to_in_printing($post_ids, $redirect_to) {
-        foreach ($post_ids as $post_id) {
-            $order = wc_get_order($post_id);
-            if ($order) {
-                $order->update_status('in-print');
-                update_post_meta($post_id, 'in_print_date', current_time('mysql'));
-                UtilsLog::message('Order #' . $post_id . ' marked as "in-print"' );
-            }
-        }
-        $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
-        return $redirect_to;
-    }
-
-    private function generate_csv($data) {
-        $output = fopen('php://temp', 'w');
-        fputcsv($output, array_keys((array)$data[0]),','); // Headers
-
-        foreach ($data as $row) {
-            fputcsv($output, (array)$row, ',');
-        }
-
-        rewind($output);
-        $csv = stream_get_contents($output);
-        fclose($output);
-
-        return $csv;
-    }
-
-    private function output_csv($csv_data, $filename) {
-        header('Content-Type: text/csv');
-        header('Content-Disposition: attachment; filename="' . $filename . '"');
-        echo $csv_data;
-        exit;
-    }
-}
+<?php
+// Handles custom bulk actions for orders.
+// visualized
+class Bulk_Actions_Manager {
+    public function __construct() {
+        add_filter('bulk_actions-woocommerce_page_wc-orders', array($this, 'register_bulk_actions'));
+        add_filter('handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_actions'), 10, 3);
+    }
+
+    public function register_bulk_actions($bulk_actions) {
+        $bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
+        $bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
+        $bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
+
+        UtilsLog::log('Bulk actions registered');
+        return $bulk_actions;
+    }
+
+    public function handle_bulk_actions($redirect_to, $action, $post_ids) {
+        switch ($action) {
+            case 'prepare_to_printing_export':
+                $redirect_to = $this->prepare_to_printing_export($post_ids, $redirect_to);
+                break;
+            case 'set_status_to_prepare_to_printing':
+                $redirect_to = $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
+                break;
+            case 'set_status_to_in_printing':
+                $redirect_to = $this->set_status_to_in_printing($post_ids, $redirect_to);
+                break;
+        }
+        return $redirect_to;
+    }
+
+    private function prepare_to_printing_export($post_ids, $redirect_to) {
+        // Get orders data using the DB Manager
+        $orders_data = Studiou_DB_Manager::get_orders_for_printing($post_ids);
+        
+        // Update order statuses
+        $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
+        
+        UtilsLog::log('Order statuses set to "to-print" after export.');
+        UtilsLog::message('Order statuses (' . count($orders_data) . ') set to "to-print" after export.', 'info' );
+
+        if (!empty($orders_data)) {
+            $csv_data = $this->generate_csv($orders_data);
+            UtilsLog::log('file prepare_to_printing_export.csv exported');
+            $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
+        }
+        
+        return $redirect_to;
+    }
+
+    private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
+        // Use the DB Manager to update order statuses
+        Studiou_DB_Manager::set_orders_to_prepare_printing($post_ids);
+        
+        $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
+        return $redirect_to;
+    }
+
+    private function set_status_to_in_printing($post_ids, $redirect_to) {
+        // Use the DB Manager to update order statuses
+        Studiou_DB_Manager::set_orders_to_in_printing($post_ids);
+        
+        $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
+        return $redirect_to;
+    }
+
+    private function generate_csv($data) {
+        $output = fopen('php://temp', 'w');
+        fputcsv($output, array_keys((array)$data[0]),','); // Headers
+
+        foreach ($data as $row) {
+            fputcsv($output, (array)$row, ',');
+        }
+
+        rewind($output);
+        $csv = stream_get_contents($output);
+        fclose($output);
+
+        return $csv;
+    }
+
+    private function output_csv($csv_data, $filename) {
+        header('Content-Type: text/csv');
+        header('Content-Disposition: attachment; filename="' . $filename . '"');
+        echo $csv_data;
+        exit;
+    }
+}

+ 0 - 122
studiou-wc-ord-print-statuses/includes/class-db-assets-manager.php

@@ -1,122 +0,0 @@
-<?php
-/**
- * Database Assets Manager for Studiou WC Order Print Statuses
- *
- * This class handles the creation and dropping of database assets (views)
- * during plugin activation and deactivation.
- */
-
-class DB_Assets_Manager {
-    /**
-     * Create the necessary database views.
-     */
-    public static function create_views() {
-        global $wpdb;
-
-        $sql = "CREATE OR REPLACE VIEW vsu_orders_car_prod_qty_img AS
-                SELECT
-                    `orders`.`id` AS `order_no`,
-                    `t`.`name` AS `prod_cat`,
-                    `products`.`post_title` AS `prod_name`,
-                    `product_variations`.`post_title` AS `prod_var`,
-                    `product_variations_name`.`name` AS `prod_var_type`,
-                    `images`.`guid` AS `prod_img_url`,
-                    `order_products`.`product_qty` AS `qty`
-                FROM (
-                        (
-                            (
-                                (
-                                    (
-                                        (
-                                            (
-                                                `{$wpdb->prefix}wc_orders` `orders`
-                                            JOIN `{$wpdb->prefix}wc_order_product_lookup` `order_products`
-                                            ON
-                                                (
-                                                    `orders`.`id` = `order_products`.`order_id`
-                                                )
-                                            )
-                                        JOIN `{$wpdb->posts}` `products`
-                                        ON
-                                            (
-                                                `products`.`ID` = `order_products`.`product_id`
-                                            )
-                                        )
-                                    JOIN `{$wpdb->posts}` `product_variations`
-                                    ON
-                                        (
-                                            `product_variations`.`ID` = `order_products`.`variation_id`
-                                        )
-                                    LEFT JOIN `{$wpdb->prefix}wc_product_attributes_lookup` `product_variations_attr`
-                                    ON
-                                        (
-                                            `product_variations_attr`.`product_id` = `product_variations`.`ID` AND `product_variations_attr`.`product_or_parent_id` = `products`.`ID` AND `product_variations_attr`.`taxonomy` = 'pa_format'
-                                        )
-                                    LEFT JOIN `{$wpdb->terms}` `product_variations_name`
-                                    ON
-                                        (
-                                            `product_variations_name`.`term_id` = `product_variations_attr`.`term_id`
-                                        )
-                                    )
-                                LEFT JOIN `{$wpdb->postmeta}` `product_meta`
-                                ON
-                                    (
-                                        `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
-                                    )
-                                )
-                            LEFT JOIN `{$wpdb->posts}` `images`
-                            ON
-                                (
-                                    `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
-                                )
-                            JOIN `{$wpdb->term_relationships}` `tr`
-                            ON
-                                (`products`.`ID` = `tr`.`object_id`)
-                            )
-                        JOIN `{$wpdb->term_taxonomy}` `tt`
-                        ON
-                            (
-                                `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
-                            )
-                        )
-                    JOIN `{$wpdb->terms}` `t`
-                    ON
-                        (`tt`.`term_id` = `t`.`term_id`)
-                    )
-                WHERE
-                    `orders`.`status` = 'wc-processing' AND `tt`.`taxonomy` = 'product_cat'
-                ORDER BY
-                    `orders`.`id`;";
-
-
-
-        $wpdb->query($sql);
-        UtilsLog::log('View created');
-    }
-
-    /**
-     * Drop the database views.
-     */
-    public static function drop_views() {
-        global $wpdb;
-
-        $sql = "DROP VIEW IF EXISTS vsu_orders_car_prod_qty_img";
-
-        $wpdb->query($sql);
-        UtilsLog::log('View dropped');
-    }
-
-    /**
-     * Activation hook callback.
-     */
-    public static function on_activate() {
-        self::create_views();
-    }
-    
-    /**
-     * Deactivation hook callback.
-     */
-    public static function on_deactivate() {
-        self::drop_views();
-    }
-}

+ 247 - 0
studiou-wc-ord-print-statuses/includes/class-db-manager.php

@@ -0,0 +1,247 @@
+<?php
+/**
+ * Database operations manager for the Studiou WC Order Print Statuses plugin.
+ * 
+ * This class centralizes database queries for better maintainability.
+ */
+
+defined('ABSPATH') || exit;
+
+class Studiou_DB_Manager {
+    /**
+     * Get orders data for printing export
+     * 
+     * @param array $order_ids Array of order IDs
+     * @return array Array of order data
+     */
+    public static function get_orders_for_printing($order_ids) {
+        global $wpdb;
+        
+        $query = "
+            SELECT
+                `orders`.`id` AS `order_no`,
+                `t`.`name` AS `prod_cat`,
+                `products`.`post_title` AS `prod_name`,
+                `product_variations`.`post_title` AS `prod_var`,
+                `product_variations_name`.`name` AS `prod_var_type`,
+                `images`.`guid` AS `prod_img_url`,
+                `order_products`.`product_qty` AS `qty`
+            FROM (
+                    (
+                        (
+                            (
+                                (
+                                    (
+                                        (
+                                            `{$wpdb->prefix}wc_orders` `orders`
+                                        JOIN `{$wpdb->prefix}wc_order_product_lookup` `order_products`
+                                        ON
+                                            (
+                                                `orders`.`id` = `order_products`.`order_id`
+                                            )
+                                        )
+                                    JOIN `{$wpdb->posts}` `products`
+                                    ON
+                                        (
+                                            `products`.`ID` = `order_products`.`product_id`
+                                        )
+                                    )
+                                JOIN `{$wpdb->posts}` `product_variations`
+                                ON
+                                    (
+                                        `product_variations`.`ID` = `order_products`.`variation_id`
+                                    )
+                                LEFT JOIN `{$wpdb->prefix}wc_product_attributes_lookup` `product_variations_attr`
+                                ON
+                                    (
+                                        `product_variations_attr`.`product_id` = `product_variations`.`ID` AND `product_variations_attr`.`product_or_parent_id` = `products`.`ID` AND `product_variations_attr`.`taxonomy` = 'pa_format'
+                                    )
+                                LEFT JOIN `{$wpdb->terms}` `product_variations_name`
+                                ON
+                                    (
+                                        `product_variations_name`.`term_id` = `product_variations_attr`.`term_id`
+                                    )
+                                )
+                            LEFT JOIN `{$wpdb->postmeta}` `product_meta`
+                            ON
+                                (
+                                    `product_meta`.`post_id` = `products`.`ID` AND `product_meta`.`meta_key` = '_thumbnail_id'
+                                )
+                            )
+                        LEFT JOIN `{$wpdb->posts}` `images`
+                        ON
+                            (
+                                `images`.`ID` = `product_meta`.`meta_value` AND `images`.`post_type` = 'attachment'
+                            )
+                        JOIN `{$wpdb->term_relationships}` `tr`
+                        ON
+                            (`products`.`ID` = `tr`.`object_id`)
+                        )
+                    JOIN `{$wpdb->term_taxonomy}` `tt`
+                    ON
+                        (
+                            `tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`
+                        )
+                    )
+                JOIN `{$wpdb->terms}` `t`
+                ON
+                    (`tt`.`term_id` = `t`.`term_id`)
+                )
+            WHERE
+                `orders`.`id` IN (" . implode(',', array_map('intval', $order_ids)) . ") 
+                AND `tt`.`taxonomy` = 'product_cat'
+            ORDER BY
+                `orders`.`id`
+        ";
+        
+        $results = $wpdb->get_results($query);
+        
+        // Error handling for database query failures
+        if ($results === false) {
+            UtilsLog::log('SQL Error: ' . $wpdb->last_error);
+            return array();
+        }
+        
+        return $results;
+    }
+
+    /**
+     * Set order status to 'to-print'
+     * 
+     * @param array $order_ids Array of order IDs
+     * @return int Number of orders updated
+     */
+    public static function set_orders_to_prepare_printing($order_ids) {
+        $updated_count = 0;
+        
+        foreach ($order_ids as $order_id) {
+            UtilsLog::log('Setting status Order #' . $order_id . ' to "to-print"');
+            $order = wc_get_order($order_id);
+            if ($order) {
+                $order->update_status('to-print');
+                $order->update_meta_data('to_print_date', current_time('mysql'));
+                $order->save();
+                $updated_count++;
+                UtilsLog::log('Order #' . $order_id . ' marked as "to-print"');
+                UtilsLog::message('Order #' . $order_id . ' marked as "to-print"' );
+            } else {
+                UtilsLog::log('Order #' . $order_id . ' not found');
+            }
+        }
+        
+        return $updated_count;
+    }
+
+    /**
+     * Set order status to 'in-print'
+     * 
+     * @param array $order_ids Array of order IDs
+     * @return int Number of orders updated
+     */
+    public static function set_orders_to_in_printing($order_ids) {
+        $updated_count = 0;
+        
+        foreach ($order_ids as $order_id) {
+            $order = wc_get_order($order_id);
+            if ($order) {
+                $order->update_status('in-print');
+                $order->update_meta_data('in_print_date', current_time('mysql'));
+                $order->save();
+                $updated_count++;
+                UtilsLog::message('Order #' . $order_id . ' marked as "in-print"' );
+            }
+        }
+        
+        return $updated_count;
+    }
+
+    /**
+     * Import inprint protocol data
+     * 
+     * @param array $csv_data Parsed CSV data
+     * @return array Array with updated count and errors
+     */
+    public static function import_inprint_protocol($csv_data) {
+        $updated_count = 0;
+        $errors = array();
+
+        foreach ($csv_data as $row) {
+            if (!isset($row['order_no']) || !isset($row['externalorder']) || !isset($row['externalorderdate'])) {
+                $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
+                continue;
+            }
+
+            $order = wc_get_order($row['order_no']);
+            if (!$order) {
+                $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
+                continue;
+            }
+            
+            $order->update_status('in-print');
+            $order->update_meta_data('in_print_date', current_time('mysql'));
+            $order->update_meta_data('external_ref_ord_no', $row['externalorder']);
+            $order->update_meta_data('external_ref_ord_date', $row['externalorderdate']);
+            $order->save();
+
+            $updated_count++;
+        }
+
+        return array(
+            'updated_count' => $updated_count,
+            'errors' => $errors
+        );
+    }
+
+    /**
+     * Import delivered protocol data
+     * 
+     * @param array $csv_data Parsed CSV data
+     * @return array Array with updated count and errors
+     */
+    public static function import_delivered_protocol($csv_data) {
+        $updated_count = 0;
+        $errors = array();
+
+        foreach ($csv_data as $row) {
+            if (!isset($row['order_no'])) {
+                $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
+                continue;
+            }
+           
+            $order = wc_get_order($row['order_no']);
+            if (!$order) {
+                $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
+                continue;
+            }
+
+            $order->update_status('completed');
+            $order->update_meta_data('external_ref_ord_delivered', current_time('mysql'));
+            $order->save();
+
+            $updated_count++;
+        }
+
+        return array(
+            'updated_count' => $updated_count,
+            'errors' => $errors
+        );
+    }
+
+    /**
+     * Parse CSV file
+     * 
+     * @param string $file_path Path to CSV file
+     * @return array|false Parsed CSV data or false on error
+     */
+    public static function parse_csv($file_path) {
+        $csv_data = array();
+        if (($handle = fopen($file_path, "r")) !== FALSE) {
+            $headers = fgetcsv($handle, 1000, ",");
+            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
+                $csv_data[] = array_combine($headers, $data);
+            }
+            fclose($handle);
+        }
+        return $csv_data;
+    }
+}

+ 108 - 156
studiou-wc-ord-print-statuses/includes/class-import-manager.php

@@ -1,157 +1,109 @@
-<?php
-
-//  Handles import functionality for InPrint and Delivered protocols.
-// visualized
-class Import_Manager {
-    public function __construct() {
-        add_action('admin_menu', array($this, 'add_import_submenu'));
-        add_action('admin_post_import_inprint_protocol', array($this, 'handle_import_inprint_protocol'));
-        add_action('admin_post_import_delivered_protocol', array($this, 'handle_import_delivered_protocol'));
-    }
-
-    public function add_import_submenu() {
-        add_submenu_page(
-            'woocommerce',
-            __('Import Protocols', 'studiou-wc-ord-print-statuses'),
-            __('Import Protocols', 'studiou-wc-ord-print-statuses'),
-            'manage_woocommerce',
-            'studiou-import-protocols',
-            array($this, 'render_import_page')
-        );
-    }
-
-    public function render_import_page() {
-        ?>
-        <div class="wrap">
-            <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
-            
-            <h2><?php _e('Import InPrint Protocol', 'studiou-wc-ord-print-statuses'); ?></h2>
-            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" enctype="multipart/form-data">
-                <input type="hidden" name="action" value="import_inprint_protocol">
-                <?php wp_nonce_field('import_inprint_protocol', 'import_inprint_protocol_nonce'); ?>
-                <input type="file" name="inprint_csv" accept=".csv" required>
-                <?php submit_button(__('Import InPrint Protocol', 'studiou-wc-ord-print-statuses')); ?>
-            </form>
-
-            <h2><?php _e('Import Delivered Protocol', 'studiou-wc-ord-print-statuses'); ?></h2>
-            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" enctype="multipart/form-data">
-                <input type="hidden" name="action" value="import_delivered_protocol">
-                <?php wp_nonce_field('import_delivered_protocol', 'import_delivered_protocol_nonce'); ?>
-                <input type="file" name="delivered_csv" accept=".csv" required>
-                <?php submit_button(__('Import Delivered Protocol', 'studiou-wc-ord-print-statuses')); ?>
-            </form>
-        </div>
-        <?php
-    }
-
-    public function handle_import_inprint_protocol() {
-        if (!current_user_can('manage_woocommerce')) {
-            wp_die(__('You do not have sufficient permissions to access this page.', 'studiou-wc-ord-print-statuses'));
-        }
-
-        check_admin_referer('import_inprint_protocol', 'import_inprint_protocol_nonce');
-
-        if (!isset($_FILES['inprint_csv'])) {
-            wp_die(__('No file uploaded.', 'studiou-wc-ord-print-statuses'));
-        }
-
-        $file = $_FILES['inprint_csv'];
-        $csv_data = $this->parse_csv($file['tmp_name']);
-
-        if (!$csv_data) {
-            wp_die(__('Error parsing CSV file.', 'studiou-wc-ord-print-statuses'));
-        }
-
-        $updated_count = 0;
-        $errors = array();
-
-        foreach ($csv_data as $row) {
-            if (!isset($row['order_no']) || !isset($row['externalorder']) || !isset($row['externalorderdate'])) {
-                $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
-                continue;
-            }
-
-            $order = wc_get_order($row['order_no']);
-            if (!$order) {
-                $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
-                continue;
-            }
-            $order->update_status('in-print');
-            $order->update_meta_data('in_print_date', current_time('mysql'));
-            $order->update_meta_data('external_ref_ord_no', $row['externalorder']);
-            $order->update_meta_data('external_ref_ord_date', $row['externalorderdate']);
-            $order->save();
-
-            $updated_count++;
-        }
-
-        $message = sprintf(__('%d orders updated successfully.', 'studiou-wc-ord-print-statuses'), $updated_count);
-        if (!empty($errors)) {
-            $message .= ' ' . sprintf(__('%d errors occurred: %s', 'studiou-wc-ord-print-statuses'), count($errors), implode(', ', $errors));
-        }
-
-        wp_redirect(add_query_arg('message', urlencode($message), admin_url('admin.php?page=studiou-import-protocols')));
-        //exit;
-    }
-
-    public function handle_import_delivered_protocol() {
-        if (!current_user_can('manage_woocommerce')) {
-            wp_die(__('You do not have sufficient permissions to access this page.', 'studiou-wc-ord-print-statuses'));
-        }
-
-        check_admin_referer('import_delivered_protocol', 'import_delivered_protocol_nonce');
-
-        if (!isset($_FILES['delivered_csv'])) {
-            wp_die(__('No file uploaded.', 'studiou-wc-ord-print-statuses'));
-        }
-
-        $file = $_FILES['delivered_csv'];
-        $csv_data = $this->parse_csv($file['tmp_name']);
-
-        if (!$csv_data) {
-            wp_die(__('Error parsing CSV file.', 'studiou-wc-ord-print-statuses'));
-        }
-
-        $updated_count = 0;
-        $errors = array();
-
-        foreach ($csv_data as $row) {
-            if (!isset($row['order_no'])) {
-                $errors[] = __('Invalid row format in CSV.', 'studiou-wc-ord-print-statuses');
-                continue;
-            }
-           
-            $order = wc_get_order($row['order_no']);
-            if (!$order) {
-                $errors[] = sprintf(__('Order %s not found.', 'studiou-wc-ord-print-statuses'), $row['order_no']);
-                continue;
-            }
-
-            $order->update_status('completed');
-            $order->update_meta_data('external_ref_ord_delivered', current_time('mysql'));
-            $order->save();
-
-            $updated_count++;
-        }
-
-        $message = sprintf(__('%d orders updated successfully.', 'studiou-wc-ord-print-statuses'), $updated_count);
-        if (!empty($errors)) {
-            $message .= ' ' . sprintf(__('%d errors occurred: %s', 'studiou-wc-ord-print-statuses'), count($errors), implode(', ', $errors));
-        }
-
-        wp_redirect(add_query_arg('message', urlencode($message), admin_url('admin.php?page=studiou-import-protocols')));
-        //exit;
-    }
-
-    private function parse_csv($file_path) {
-        $csv_data = array();
-        if (($handle = fopen($file_path, "r")) !== FALSE) {
-            $headers = fgetcsv($handle, 1000, ",");
-            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
-                $csv_data[] = array_combine($headers, $data);
-            }
-            fclose($handle);
-        }
-        return $csv_data;
-    }
+<?php
+//  Handles import functionality for InPrint and Delivered protocols.
+// visualized
+class Import_Manager {
+    public function __construct() {
+        add_action('admin_menu', array($this, 'add_import_submenu'));
+        add_action('admin_post_import_inprint_protocol', array($this, 'handle_import_inprint_protocol'));
+        add_action('admin_post_import_delivered_protocol', array($this, 'handle_import_delivered_protocol'));
+    }
+
+    public function add_import_submenu() {
+        add_submenu_page(
+            'woocommerce',
+            __('Import Protocols', 'studiou-wc-ord-print-statuses'),
+            __('Import Protocols', 'studiou-wc-ord-print-statuses'),
+            'manage_woocommerce',
+            'studiou-import-protocols',
+            array($this, 'render_import_page')
+        );
+    }
+
+    public function render_import_page() {
+        ?>
+        <div class="wrap">
+            <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
+            
+            <h2><?php _e('Import InPrint Protocol', 'studiou-wc-ord-print-statuses'); ?></h2>
+            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" enctype="multipart/form-data">
+                <input type="hidden" name="action" value="import_inprint_protocol">
+                <?php wp_nonce_field('import_inprint_protocol', 'import_inprint_protocol_nonce'); ?>
+                <input type="file" name="inprint_csv" accept=".csv" required>
+                <?php submit_button(__('Import InPrint Protocol', 'studiou-wc-ord-print-statuses')); ?>
+            </form>
+
+            <h2><?php _e('Import Delivered Protocol', 'studiou-wc-ord-print-statuses'); ?></h2>
+            <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" enctype="multipart/form-data">
+                <input type="hidden" name="action" value="import_delivered_protocol">
+                <?php wp_nonce_field('import_delivered_protocol', 'import_delivered_protocol_nonce'); ?>
+                <input type="file" name="delivered_csv" accept=".csv" required>
+                <?php submit_button(__('Import Delivered Protocol', 'studiou-wc-ord-print-statuses')); ?>
+            </form>
+        </div>
+        <?php
+    }
+
+    public function handle_import_inprint_protocol() {
+        if (!current_user_can('manage_woocommerce')) {
+            wp_die(__('You do not have sufficient permissions to access this page.', 'studiou-wc-ord-print-statuses'));
+        }
+
+        check_admin_referer('import_inprint_protocol', 'import_inprint_protocol_nonce');
+
+        if (!isset($_FILES['inprint_csv'])) {
+            wp_die(__('No file uploaded.', 'studiou-wc-ord-print-statuses'));
+        }
+
+        $file = $_FILES['inprint_csv'];
+        $csv_data = Studiou_DB_Manager::parse_csv($file['tmp_name']);
+
+        if (!$csv_data) {
+            wp_die(__('Error parsing CSV file.', 'studiou-wc-ord-print-statuses'));
+        }
+
+        // Process the import using DB Manager
+        $result = Studiou_DB_Manager::import_inprint_protocol($csv_data);
+        $updated_count = $result['updated_count'];
+        $errors = $result['errors'];
+
+        $message = sprintf(__('%d orders updated successfully.', 'studiou-wc-ord-print-statuses'), $updated_count);
+        if (!empty($errors)) {
+            $message .= ' ' . sprintf(__('%d errors occurred: %s', 'studiou-wc-ord-print-statuses'), count($errors), implode(', ', $errors));
+        }
+
+        wp_redirect(add_query_arg('message', urlencode($message), admin_url('admin.php?page=studiou-import-protocols')));
+        exit;
+    }
+
+    public function handle_import_delivered_protocol() {
+        if (!current_user_can('manage_woocommerce')) {
+            wp_die(__('You do not have sufficient permissions to access this page.', 'studiou-wc-ord-print-statuses'));
+        }
+
+        check_admin_referer('import_delivered_protocol', 'import_delivered_protocol_nonce');
+
+        if (!isset($_FILES['delivered_csv'])) {
+            wp_die(__('No file uploaded.', 'studiou-wc-ord-print-statuses'));
+        }
+
+        $file = $_FILES['delivered_csv'];
+        $csv_data = Studiou_DB_Manager::parse_csv($file['tmp_name']);
+
+        if (!$csv_data) {
+            wp_die(__('Error parsing CSV file.', 'studiou-wc-ord-print-statuses'));
+        }
+
+        // Process the import using DB Manager
+        $result = Studiou_DB_Manager::import_delivered_protocol($csv_data);
+        $updated_count = $result['updated_count'];
+        $errors = $result['errors'];
+
+        $message = sprintf(__('%d orders updated successfully.', 'studiou-wc-ord-print-statuses'), $updated_count);
+        if (!empty($errors)) {
+            $message .= ' ' . sprintf(__('%d errors occurred: %s', 'studiou-wc-ord-print-statuses'), count($errors), implode(', ', $errors));
+        }
+
+        wp_redirect(add_query_arg('message', urlencode($message), admin_url('admin.php?page=studiou-import-protocols')));
+        exit;
+    }
 }

+ 62 - 62
studiou-wc-ord-print-statuses/includes/class-order-search-manager.php

@@ -1,63 +1,63 @@
-<?php
-// Extends order search functionality.
-
-class Order_Search_Manager {
-    public function __construct() {
-        add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'add_external_ref_ord_no_to_search'));
-        add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'add_external_ref_ord_no_search_field'));
-        //add_filter('parse_query', array($this, 'process_external_ref_ord_no_search'));
-    }
-
-    /**
-     * Adds the external_ref_ord_no to the list of searchable fields for orders.
-     *
-     * @param array $search_fields The current array of searchable fields.
-     * @return array The updated array of searchable fields.
-     */
-    public function add_external_ref_ord_no_to_search($search_fields) {
-        $search_fields[] = '_external_ref_ord_no';
-        return $search_fields;
-    }
-
-    /**
-     * Adds a search field for external_ref_ord_no in the order list page.
-     */
-    public function add_external_ref_ord_no_search_field() {
-        global $typenow;
-        if ('shop_order' === $typenow) {
-            $external_ref_ord_no = isset($_GET['external_ref_ord_no']) ? sanitize_text_field($_GET['external_ref_ord_no']) : '';
-            ?>
-            <input type="text" name="external_ref_ord_no" value="<?php echo esc_attr($external_ref_ord_no); ?>" placeholder="<?php esc_attr_e('Search by External Order Number', 'studiou-wc-ord-print-statuses'); ?>">
-            <?php
-        }
-    }
-
-    /**
-     * Processes the external_ref_ord_no search query.
-     *
-     * @param WP_Query $query The current WordPress query object.
-     * @return WP_Query The modified query object.
-     */
-    public function process_external_ref_ord_no_search($query) {
-        global $pagenow, $typenow;
-
-        if ('edit.php' !== $pagenow || 'shop_order' !== $typenow || !$query->is_main_query()) {
-            return $query;
-        }
-
-        if (!empty($_GET['external_ref_ord_no'])) {
-            $external_ref_ord_no = sanitize_text_field($_GET['external_ref_ord_no']);
-            $meta_query = $query->get('meta_query') ? $query->get('meta_query') : array();
-
-            $meta_query[] = array(
-                'key' => '_external_ref_ord_no',
-                'value' => $external_ref_ord_no,
-                'compare' => 'LIKE',
-            );
-
-            $query->set('meta_query', $meta_query);
-        }
-
-        return $query;
-    }
+<?php
+// Extends order search functionality.
+
+class Order_Search_Manager {
+    public function __construct() {
+        add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'add_external_ref_ord_no_to_search'));
+        add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'add_external_ref_ord_no_search_field'));
+        add_filter('parse_query', array($this, 'process_external_ref_ord_no_search')); // Uncommented this line
+    }
+
+    /**
+     * Adds the external_ref_ord_no to the list of searchable fields for orders.
+     *
+     * @param array $search_fields The current array of searchable fields.
+     * @return array The updated array of searchable fields.
+     */
+    public function add_external_ref_ord_no_to_search($search_fields) {
+        $search_fields[] = '_external_ref_ord_no';
+        return $search_fields;
+    }
+
+    /**
+     * Adds a search field for external_ref_ord_no in the order list page.
+     */
+    public function add_external_ref_ord_no_search_field() {
+        global $typenow;
+        if ('shop_order' === $typenow) {
+            $external_ref_ord_no = isset($_GET['external_ref_ord_no']) ? sanitize_text_field($_GET['external_ref_ord_no']) : '';
+            ?>
+            <input type="text" name="external_ref_ord_no" value="<?php echo esc_attr($external_ref_ord_no); ?>" placeholder="<?php esc_attr_e('Search by External Order Number', 'studiou-wc-ord-print-statuses'); ?>">
+            <?php
+        }
+    }
+
+    /**
+     * Processes the external_ref_ord_no search query.
+     *
+     * @param WP_Query $query The current WordPress query object.
+     * @return WP_Query The modified query object.
+     */
+    public function process_external_ref_ord_no_search($query) {
+        global $pagenow, $typenow;
+
+        if ('edit.php' !== $pagenow || 'shop_order' !== $typenow || !$query->is_main_query()) {
+            return $query;
+        }
+
+        if (!empty($_GET['external_ref_ord_no'])) {
+            $external_ref_ord_no = sanitize_text_field($_GET['external_ref_ord_no']);
+            $meta_query = $query->get('meta_query') ? $query->get('meta_query') : array();
+
+            $meta_query[] = array(
+                'key' => '_external_ref_ord_no',
+                'value' => $external_ref_ord_no,
+                'compare' => 'LIKE',
+            );
+
+            $query->set('meta_query', $meta_query);
+        }
+
+        return $query;
+    }
 }

+ 97 - 104
studiou-wc-ord-print-statuses/studiou-wc-ord-print-statuses.php

@@ -1,104 +1,97 @@
-<?php
-/**
- * Plugin Name: QDR - Studiou WC Order Print Statuses
- * Plugin URI: https://www.quadarax.com/plugins/studiou-wc-ord-print-statuses
- * Description: Adds custom order statuses (wc-to-print, wc-in-print), bulk actions, and status change timestamps to WooCommerce for print orders to third party providers
- * Version: 1.2
- * Requires at least: 5.8
- * Requires PHP: 7.2
- * Author: Dalibor Votruba
- * Author URI: https://www.quadarax.com
- * License: GPL v2 or later
- * License URI: https://www.gnu.org/licenses/gpl-2.0.html
- * Text Domain: studiou-wc-ord-print-statuses
- * Domain Path: /languages
- */
-
-defined('ABSPATH') || exit;
-
-// load logging feature
-require_once plugin_dir_path(__FILE__) . 'includes/utils-log.php';
-// Load the DB_Assets_Manager class
-require_once plugin_dir_path(__FILE__) . 'includes/class-db-assets-manager.php';
-
-// Register activation and deactivation hooks
-register_activation_hook(__FILE__, array('DB_Assets_Manager', 'on_activate'));
-register_deactivation_hook(__FILE__, array('DB_Assets_Manager', 'on_deactivate'));
-
-// Main plugin class
-class Studiou_WC_Ord_Print_Statuses {
-    private $order_status_manager;
-    private $order_fields_manager;
-    private $order_search_manager;
-    private $bulk_actions_manager;
-    private $custom_columns_manager;
-    private $import_manager;
-
-    public static function initPlugin() {
-        $class = __CLASS__;
-        new $class;
-    }
-
-    public function __construct() {
-        // add_action('plugins_loaded', array($this, 'init'));
-        $this->init();
-    }
-
-    public function init() {
-        if (!class_exists('WooCommerce')) {
-            add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
-            return;
-        }
-
-        $this->load_dependencies();
-        $this->initialize_managers();
-        UtilsLog::log("Plugin initialized");
-    }
-
-    private function load_dependencies() {
-        $files = array(
-            'class-order-status-manager.php',
-            'class-order-fields-manager.php',
-            'class-order-search-manager.php',
-            'class-bulk-actions-manager.php',
-            'class-custom-columns-manager.php',
-            'class-import-manager.php'
-        );
-
-        foreach ($files as $file) {
-            require_once plugin_dir_path(__FILE__) . 'includes/' . $file;
-        }
-
-        UtilsLog::log('Dependencies loaded');
-    }
-
-    private function initialize_managers() {
-        $this->order_status_manager = new Order_Status_Manager();
-        $this->order_fields_manager = new Order_Fields_Manager();
-        $this->order_search_manager = new Order_Search_Manager();
-        $this->bulk_actions_manager = new Bulk_Actions_Manager();
-        $this->custom_columns_manager = new Custom_Columns_Manager();
-        $this->import_manager = new Import_Manager();
-
-        UtilsLog::log('Managers initialized');
-    }
-
-    public function woocommerce_missing_notice() {
-        ?>
-        <div class="error">
-            <p><?php esc_html_e('Studiou WC Order Print Statuses requires WooCommerce to be installed and active.', 'studiou-wc-ord-print-statuses'); ?></p>
-        </div>
-        <?php
-    }
-}
-
-// Load the plugin text domain - translations
-function studiou_wc_ord_print_statuses_load_textdomain() {
-    load_plugin_textdomain('studiou-wc-ord-print-statuses', false, dirname(plugin_basename(__FILE__)) . '/languages');
-    UtilsLog::log("Text domain (translations) loaded");
-}
-add_action('plugins_loaded', 'studiou_wc_ord_print_statuses_load_textdomain');
-// Initialize the plugin
-add_action( 'plugins_loaded', array( 'Studiou_WC_Ord_Print_Statuses', 'initPlugin' ));
-
-
+<?php
+/**
+ * Plugin Name: QDR - Studiou WC Order Print Statuses
+ * Plugin URI: https://www.quadarax.com/plugins/studiou-wc-ord-print-statuses
+ * Description: Adds custom order statuses (wc-to-print, wc-in-print), bulk actions, and status change timestamps to WooCommerce for print orders to third party providers
+ * Version: 1.3.0
+ * Requires at least: 5.8
+ * Requires PHP: 7.2
+ * Author: Dalibor Votruba
+ * Author URI: https://www.quadarax.com
+ * License: GPL v2 or later
+ * License URI: https://www.gnu.org/licenses/gpl-2.0.html
+ * Text Domain: studiou-wc-ord-print-statuses
+ * Domain Path: /languages
+ */
+
+defined('ABSPATH') || exit;
+
+// load logging feature
+require_once plugin_dir_path(__FILE__) . 'includes/utils-log.php';
+
+// Main plugin class
+class Studiou_WC_Ord_Print_Statuses {
+    private $order_status_manager;
+    private $order_fields_manager;
+    private $order_search_manager;
+    private $bulk_actions_manager;
+    private $custom_columns_manager;
+    private $import_manager;
+
+    public static function initPlugin() {
+        $class = __CLASS__;
+        new $class;
+    }
+
+    public function __construct() {
+        // add_action('plugins_loaded', array($this, 'init'));
+        $this->init();
+    }
+
+    public function init() {
+        if (!class_exists('WooCommerce')) {
+            add_action('admin_notices', array($this, 'woocommerce_missing_notice'));
+            return;
+        }
+
+        $this->load_dependencies();
+        $this->initialize_managers();
+        UtilsLog::log("Plugin initialized");
+    }
+
+    private function load_dependencies() {
+        $files = array(
+            'class-db-manager.php',
+            'class-order-status-manager.php',
+            'class-order-fields-manager.php',
+            'class-order-search-manager.php',
+            'class-bulk-actions-manager.php',
+            'class-custom-columns-manager.php',
+            'class-import-manager.php'
+        );
+
+        foreach ($files as $file) {
+            require_once plugin_dir_path(__FILE__) . 'includes/' . $file;
+        }
+
+        UtilsLog::log('Dependencies loaded');
+    }
+
+    private function initialize_managers() {
+        $this->order_status_manager = new Order_Status_Manager();
+        $this->order_fields_manager = new Order_Fields_Manager();
+        $this->order_search_manager = new Order_Search_Manager();
+        $this->bulk_actions_manager = new Bulk_Actions_Manager();
+        $this->custom_columns_manager = new Custom_Columns_Manager();
+        $this->import_manager = new Import_Manager();
+
+        UtilsLog::log('Managers initialized');
+    }
+
+    public function woocommerce_missing_notice() {
+        ?>
+        <div class="error">
+            <p><?php esc_html_e('Studiou WC Order Print Statuses requires WooCommerce to be installed and active.', 'studiou-wc-ord-print-statuses'); ?></p>
+        </div>
+        <?php
+    }
+}
+
+// Load the plugin text domain - translations
+function studiou_wc_ord_print_statuses_load_textdomain() {
+    load_plugin_textdomain('studiou-wc-ord-print-statuses', false, dirname(plugin_basename(__FILE__)) . '/languages');
+    UtilsLog::log("Text domain (translations) loaded");
+}
+add_action('plugins_loaded', 'studiou_wc_ord_print_statuses_load_textdomain');
+// Initialize the plugin
+add_action( 'plugins_loaded', array( 'Studiou_WC_Ord_Print_Statuses', 'initPlugin' ));