소스 검색

studiou-wc-product-cat-manage: new approach with direct access to DB

Dalibor Votruba 1 년 전
부모
커밋
0da64e7a9d

+ 1 - 5
studiou-wc-product-cat-manage/assets/css/admin.css

@@ -2,7 +2,7 @@
  * Admin styles for Studiou WC Product Category Manager
  */
 
-.studiou-wcpcm-wrap {
+ .studiou-wcpcm-wrap {
     max-width: 1200px;
     margin: 20px 0;
 }
@@ -81,8 +81,4 @@
     font-family: monospace;
     white-space: pre;
     max-height: 300px;
-}
-
-.studiou-wcpcm-sample-csv pre {
-    max-height: 150px;
 }

+ 48 - 65
studiou-wc-product-cat-manage/includes/class-studiou-wc-product-cat-manage-db.php

@@ -68,28 +68,26 @@ class Studiou_WC_Product_Cat_Manage_DB {
             $display_type = 'default';
         }
         
-        // Get protected category data if available
+        // Get protected category data using direct DB access
         $visibility = 'public';
         $protected_passwords = '';
         
-        if (class_exists('WC_Protected_Categories')) {
-            // Get visibility
-            $visibility = get_term_meta($category->term_id, 'visibility', true);
-            if (empty($visibility)) {
-                $visibility = 'public';
-            }
-            
-            // Get password protection
-            if ($visibility === 'protected') {
-                // Use the Util::get_term_passwords method pattern which the plugin uses
-                $passwords = get_term_meta($category->term_id, 'password', false);
-                
-                // Back-compat - for passwords stored as separate meta items
-                if ($passwords && !empty($passwords[0]) && is_array($passwords[0])) {
+        // Get visibility directly from term meta
+        $visibility_meta = get_term_meta($category->term_id, 'visibility', true);
+        if (!empty($visibility_meta)) {
+            $visibility = $visibility_meta;
+        }
+        
+        // If protected, get passwords directly from term meta
+        if ($visibility === 'protected') {
+            $passwords = get_term_meta($category->term_id, 'password', false);
+            if (!empty($passwords)) {
+                // Handle different password storage formats
+                if (is_array($passwords[0])) {
                     $passwords = $passwords[0];
                 }
                 
-                if (!empty($passwords) && is_array($passwords)) {
+                if (!empty($passwords)) {
                     $protected_passwords = implode('|', $passwords);
                 }
             }
@@ -186,28 +184,8 @@ class Studiou_WC_Product_Cat_Manage_DB {
         // Set display type
         update_term_meta($term_id, 'display_type', $category_data['display-type']);
         
-        // Set visibility and password protection if WC Protected Categories is active
-        if (class_exists('WC_Protected_Categories')) {
-            // Set visibility
-            update_term_meta($term_id, 'visibility', $category_data['visibility']);
-            
-            // Set password protection if visibility is protected and passwords are provided
-            if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
-                $passwords = explode('|', $category_data['protected-passwords']);
-                // Filter out empty passwords
-                $passwords = array_filter($passwords);
-                
-                if (empty($passwords)) {
-                    // If no valid passwords after filtering, set a default one as the plugin does
-                    $passwords = ['password123'];
-                }
-                
-                // Store each password as a separate meta entry
-                foreach ($passwords as $password) {
-                    add_term_meta($term_id, 'password', $password);
-                }
-            }
-        }
+        // Set visibility and password protection using direct DB access
+        $this->set_category_protection($term_id, $category_data);
         
         return array(
             'status' => 'created',
@@ -273,33 +251,8 @@ class Studiou_WC_Product_Cat_Manage_DB {
         // Set display type
         update_term_meta($existing->term_id, 'display_type', $category_data['display-type']);
         
-        // Set visibility and password protection if WC Protected Categories is active
-        if (class_exists('WC_Protected_Categories')) {
-            // First remove existing protection meta
-            delete_term_meta($existing->term_id, 'password');
-            delete_term_meta($existing->term_id, 'user_roles');
-            delete_term_meta($existing->term_id, 'users');
-            
-            // Set visibility
-            update_term_meta($existing->term_id, 'visibility', $category_data['visibility']);
-            
-            // Set password protection if visibility is protected and passwords are provided
-            if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
-                $passwords = explode('|', $category_data['protected-passwords']);
-                // Filter out empty passwords
-                $passwords = array_filter($passwords);
-                
-                if (empty($passwords)) {
-                    // If no valid passwords after filtering, set a default one as the plugin does
-                    $passwords = ['password123'];
-                }
-                
-                // Note: The plugin stores each password as a separate meta entry with the same key
-                foreach ($passwords as $password) {
-                    add_term_meta($existing->term_id, 'password', $password);
-                }
-            }
-        }
+        // Set visibility and password protection using direct DB access
+        $this->set_category_protection($existing->term_id, $category_data);
         
         return array(
             'status' => 'updated',
@@ -347,4 +300,34 @@ class Studiou_WC_Product_Cat_Manage_DB {
             )
         );
     }
+    
+    /**
+     * Set category protection settings
+     * 
+     * @param int $term_id Term ID
+     * @param array $category_data Category data
+     */
+    private function set_category_protection($term_id, $category_data) {
+        // First remove existing protection meta to avoid duplicates
+        delete_term_meta($term_id, 'password');
+        delete_term_meta($term_id, 'visibility');
+        
+        // Set visibility
+        update_term_meta($term_id, 'visibility', $category_data['visibility']);
+        
+        // Set password protection if visibility is protected and passwords are provided
+        if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
+            $passwords = explode('|', $category_data['protected-passwords']);
+            // Filter out empty passwords
+            $passwords = array_filter($passwords);
+            
+            if (empty($passwords)) {
+                // If no valid passwords after filtering, set a default one
+                $passwords = ['password123'];
+            }
+            
+            // Store the passwords array as a single meta entry
+            add_term_meta($term_id, 'password', $passwords);
+        }
+    }
 }

+ 7 - 4
studiou-wc-product-cat-manage/includes/class-studiou-wc-product-cat-manage-export.php

@@ -161,7 +161,11 @@ class Studiou_WC_Product_Cat_Manage_Export {
      * @return string CSV content
      */
     private function generate_csv($headers, $data) {
-        $output = fopen('php://temp', 'r+');
+        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);
@@ -177,10 +181,9 @@ class Studiou_WC_Product_Cat_Manage_Export {
             fputcsv($output, $csv_row);
         }
         
-        rewind($output);
-        $csv_content = stream_get_contents($output);
         fclose($output);
+        $csv_content = ob_get_clean();
         
         return $csv_content;
     }
-}
+}

+ 9 - 2
studiou-wc-product-cat-manage/includes/class-studiou-wc-product-cat-manage-import.php

@@ -144,6 +144,13 @@ class Studiou_WC_Product_Cat_Manage_Import {
             throw new Exception(__('Failed to open the uploaded file', 'studiou-wc-product-cat-manage'));
         }
         
+        // Read and remove UTF-8 BOM if present
+        $bom = fread($file, 3);
+        if ($bom !== "\xEF\xBB\xBF") {
+            // Not a BOM, rewind the file pointer
+            rewind($file);
+        }
+        
         // Read headers
         $headers = fgetcsv($file);
         if (!$headers) {
@@ -283,7 +290,7 @@ class Studiou_WC_Product_Cat_Manage_Import {
             }
             
             // If visibility is protected, passwords are required
-            if ($data['visibility'] === 'protected' && empty($data['protected-passwords'])) {
+            if (isset($data['visibility']) && $data['visibility'] === 'protected' && empty($data['protected-passwords'])) {
                 throw new Exception(sprintf(
                     __('Protected visibility requires at least one password at row %d', 'studiou-wc-product-cat-manage'),
                     $row_number
@@ -307,7 +314,7 @@ class Studiou_WC_Product_Cat_Manage_Import {
         $message = sprintf(__('Imported %d items:', 'studiou-wc-product-cat-manage'), $count) . "\n";
         
         foreach ($results as $result) {
-            $message .= '    ' . $result['message'] . "\n";
+            $message .= "\t" . $result['message'] . "\n";
         }
         
         return $message;

+ 1 - 1
studiou-wc-product-cat-manage/readme.md

@@ -92,4 +92,4 @@ Website: [Quadarax](https://www.quadarax.com)
 
 ## Support
 
-For support, please visit [https://www.quadarax.com/plugins/studiou-wc-product-cat-manage](https://www.quadarax.com/plugins/studiou-wc-product-cat-manage)
+For support, please visit [https://www.quadarax.com/plugins/studiou-wc-product-cat-manage](https://www.quadarax.com/plugins/studiou-wc-product-cat-manage)

+ 18 - 3
studiou-wc-product-cat-manage/studiou-wc-product-cat-manage.php

@@ -10,6 +10,8 @@
  * 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-product-cat-manage
+ * Domain Path: /languages
  */
 
 // If this file is called directly, abort.
@@ -96,6 +98,17 @@ class Studiou_WC_Product_Cat_Manage {
             if (isset($_GET['activate'])) {
                 unset($_GET['activate']);
             }
+            return;
+        }
+        
+        // Check for WooCommerce Protected Categories
+        if (!class_exists('WC_Protected_Categories') || !defined('WC_PROTECTED_CATEGORIES_VERSION') || 
+            version_compare(WC_PROTECTED_CATEGORIES_VERSION, '2.7', '<')) {
+            add_action('admin_notices', array($this, 'protected_categories_missing_notice'));
+            deactivate_plugins(STUDIOU_WCPCM_PLUGIN_BASENAME);
+            if (isset($_GET['activate'])) {
+                unset($_GET['activate']);
+            }
         }
     }
     
@@ -117,11 +130,13 @@ class Studiou_WC_Product_Cat_Manage {
     /**
      * WooCommerce Protected Categories missing notice
      */
-    public function wc_protected_categories_missing_notice() {
+    public function protected_categories_missing_notice() {
         ?>
         <div class="error">
             <p><?php echo sprintf(
-                __('QDR - Studiou WC Export/Import Product Categories requires WooCommerce Protected Categories version 2.7 or higher. Please install or update WooCommerce Protected Categories.', 'studiou-wc-product-cat-manage')
+                __('QDR - Studiou WC Export/Import Product Categories requires WooCommerce Protected Categories version 2.7 or higher. Please %sinstall or update WooCommerce Protected Categories%s.', 'studiou-wc-product-cat-manage'),
+                '<a href="https://barn2.com/wordpress-plugins/woocommerce-protected-categories/" target="_blank">',
+                '</a>'
             ); ?></p>
         </div>
         <?php
@@ -212,4 +227,4 @@ function run_studiou_wc_product_cat_manage() {
 }
 
 // Run the plugin
-run_studiou_wc_product_cat_manage();
+run_studiou_wc_product_cat_manage();

+ 1 - 1
studiou-wc-product-cat-manage/views/import.php

@@ -73,4 +73,4 @@ if (!defined('WPINC')) {
             </a>
         </p>
     </div>
-</div>
+</div>