ソースを参照

fix studiou-wc-product-cat-manage: db class

Dalibor Votruba 1 年間 前
コミット
b74e7a4069

+ 43 - 9
studiou-wc-product-cat-manage/includes/class-studiou-wc-product-cat-manage-db.php

@@ -73,14 +73,22 @@ class Studiou_WC_Product_Cat_Manage_DB {
         $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') {
-                // Get password protection
-                $passwords = get_term_meta($category->term_id, 'password', true);
+                // 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])) {
+                    $passwords = $passwords[0];
+                }
+                
                 if (!empty($passwords) && is_array($passwords)) {
                     $protected_passwords = implode('|', $passwords);
                 }
@@ -180,12 +188,24 @@ class Studiou_WC_Product_Cat_Manage_DB {
         
         // 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']);
-                // Store passwords as an array
-                update_term_meta($term_id, 'password', $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);
+                }
             }
         }
         
@@ -255,15 +275,29 @@ class Studiou_WC_Product_Cat_Manage_DB {
         
         // 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']);
-                // Store passwords as an array
-                update_term_meta($existing->term_id, 'password', $passwords);
-            } else {
-                // Remove passwords if visibility is not protected
-                delete_term_meta($existing->term_id, 'password');
+                // 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);
+                }
             }
         }