Bläddra i källkod

studiou-wc-product-cat-manage: fix export visibility and password

Dalibor Votruba 1 år sedan
förälder
incheckning
1f09238671

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

@@ -141,7 +141,7 @@ class Studiou_WC_Product_Cat_Manage_DB {
         return false;
     }
     
-    /**
+   /**
      * Format category data
      * 
      * @param object $category WP_Term object
@@ -163,43 +163,73 @@ class Studiou_WC_Product_Cat_Manage_DB {
             $display_type = 'default';
         }
         
-        // Get protected category data if available
+        // Get protected category data
         $visibility = 'public';
         $protected_passwords = '';
         
-        if (class_exists('WC_Protected_Categories')) {
-            // Use Util class to get category visibility info
-            $term_id = $category->term_id;
-            
-            // Use the plugin's own method to get visibility data if available
-            if (class_exists('\\Barn2\\Plugin\\WC_Protected_Categories\\Util')) {
-                $visibility_obj = \Barn2\Plugin\WC_Protected_Categories\Util::get_category_visibility($term_id);
-                if ($visibility_obj) {
+        // Check if WooCommerce Protected Categories plugin is active
+        // The debug shows the Barn2 Util class exists, so let's use it first
+        if (class_exists('\\Barn2\\Plugin\\WC_Protected_Categories\\Util')) {
+            try {
+                $visibility_obj = \Barn2\Plugin\WC_Protected_Categories\Util::get_category_visibility($category->term_id);
+                if ($visibility_obj && method_exists($visibility_obj, 'get_visibility')) {
                     $visibility = $visibility_obj->get_visibility();
                     
-                    if ($visibility === 'protected' && $visibility_obj->has_password_protection()) {
-                        // Get passwords using the plugin's own method
-                        $passwords = \Barn2\Plugin\WC_Protected_Categories\Util::get_term_passwords($term_id);
-                        if (!empty($passwords)) {
+                    // Get passwords using the utility method
+                    if ($visibility === 'protected') {
+                        $passwords = \Barn2\Plugin\WC_Protected_Categories\Util::get_term_passwords($category->term_id);
+                        if (!empty($passwords) && is_array($passwords)) {
                             $protected_passwords = implode('|', $passwords);
                         }
                     }
                 }
-            } else {
-                // Fallback to direct meta access if plugin class not available
-                $visibility = get_term_meta($term_id, 'visibility', true);
-                if (empty($visibility)) {
-                    $visibility = 'public';
-                }
+            } catch (Exception $e) {
+                // Fallback to direct meta access if Util methods fail
+                $visibility = 'public';
+                $protected_passwords = '';
+            }
+        }
+        
+        // Fallback to direct meta access if the Util class didn't work
+        if ($visibility === 'public') {
+            // Get visibility from meta
+            $visibility = get_term_meta($category->term_id, 'visibility', true);
+            if (empty($visibility)) {
+                $visibility = 'public';
+            }
+            
+            // Get passwords if protected
+            if ($visibility === 'protected') {
+                $password_meta = get_term_meta($category->term_id, 'password', true);
                 
-                if ($visibility === 'protected') {
-                    $passwords = get_term_meta($term_id, 'password', false);
-                    if ($passwords && !empty($passwords[0]) && is_array($passwords[0])) {
-                        $passwords = $passwords[0];
+                if (!empty($password_meta)) {
+                    // The debug shows passwords are stored as serialized arrays
+                    // like: a:1:{i:0;s:8:"jH7xK9pF";}
+                    
+                    // First, check if it's already an array (unserialized)
+                    if (is_array($password_meta)) {
+                        $passwords = $password_meta;
+                    } else {
+                        // If it's a string, try to unserialize it
+                        $passwords = maybe_unserialize($password_meta);
                     }
                     
-                    if (!empty($passwords)) {
-                        $protected_passwords = implode('|', $passwords);
+                    // Now extract the actual passwords
+                    if (is_array($passwords)) {
+                        // Filter out empty values and get the actual password strings
+                        $password_array = array();
+                        foreach ($passwords as $password) {
+                            if (!empty($password)) {
+                                $password_array[] = $password;
+                            }
+                        }
+                        
+                        if (!empty($password_array)) {
+                            $protected_passwords = implode('|', $password_array);
+                        }
+                    } elseif (is_string($passwords) && !empty($passwords)) {
+                        // If it's a simple string, use it directly
+                        $protected_passwords = $passwords;
                     }
                 }
             }
@@ -297,8 +327,8 @@ class Studiou_WC_Product_Cat_Manage_DB {
         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 - ensure the "visibility" meta key is used (not wc_protected_categories_visibility)
+        if (class_exists('WC_Protected_Categories') || function_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
@@ -308,12 +338,12 @@ class Studiou_WC_Product_Cat_Manage_DB {
                 $passwords = array_filter($passwords);
                 
                 if (empty($passwords)) {
-                    // If no valid passwords after filtering, set a default one as the plugin does
+                    // 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);
+                update_term_meta($term_id, 'password', $passwords);
             }
         }
         
@@ -382,13 +412,13 @@ class Studiou_WC_Product_Cat_Manage_DB {
         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')) {
+        if (class_exists('WC_Protected_Categories') || function_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 - ensure the "visibility" meta key is used
+            // Set visibility
             update_term_meta($existing->term_id, 'visibility', $category_data['visibility']);
             
             // Set password protection if visibility is protected and passwords are provided
@@ -398,12 +428,12 @@ class Studiou_WC_Product_Cat_Manage_DB {
                 $passwords = array_filter($passwords);
                 
                 if (empty($passwords)) {
-                    // If no valid passwords after filtering, set a default one as the plugin does
+                    // If no valid passwords after filtering, set a default one
                     $passwords = ['password123'];
                 }
                 
                 // Store the passwords array as a single meta entry
-                add_term_meta($existing->term_id, 'password', $passwords);
+                update_term_meta($existing->term_id, 'password', $passwords);
             }
         }
         
@@ -453,4 +483,4 @@ class Studiou_WC_Product_Cat_Manage_DB {
             )
         );
     }
-}
+}