'product_cat', 'orderby' => 'name', 'hide_empty' => false, ); $categories = get_terms($args); $formatted_categories = array(); if (!is_wp_error($categories)) { foreach ($categories as $category) { $formatted_categories[] = $this->format_category($category); } } return $formatted_categories; } /** * Format category data * * @param object $category WP_Term object * @return array Formatted category data */ private function format_category($category) { // Get parent category name $parent_name = ''; if ($category->parent > 0) { $parent = get_term($category->parent, 'product_cat'); if (!is_wp_error($parent)) { $parent_name = $parent->name; } } // Get display type $display_type = get_term_meta($category->term_id, 'display_type', true); if (empty($display_type)) { $display_type = 'default'; } // Get protected category data using direct DB access $visibility = 'public'; $protected_passwords = ''; // 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)) { $protected_passwords = implode('|', $passwords); } } } return array( 'name' => $category->name, 'url-name' => $category->slug, 'parent-name' => $parent_name, 'description' => $category->description, 'display-type' => $display_type, 'visibility' => $visibility, 'protected-passwords' => $protected_passwords, 'operation' => 'U' // Default operation for existing categories ); } /** * Find category by name * * @param string $name Category name * @return WP_Term|null Category term object or null if not found */ public function find_category_by_name($name) { $args = array( 'taxonomy' => 'product_cat', 'name' => $name, 'hide_empty' => false, ); $categories = get_terms($args); if (!is_wp_error($categories) && !empty($categories)) { return $categories[0]; } return null; } /** * Create product category * * @param array $category_data Category data * @return array Result with status and message */ public function create_category($category_data) { // Check if category already exists $existing = $this->find_category_by_name($category_data['name']); if ($existing) { return array( 'status' => 'skipped', 'message' => sprintf( __("Product category '%s' already exists. Skipped.", 'studiou-wc-product-cat-manage'), $category_data['name'] ) ); } // Find parent category ID if parent name is provided $parent_id = 0; if (!empty($category_data['parent-name'])) { $parent = $this->find_category_by_name($category_data['parent-name']); if ($parent) { $parent_id = $parent->term_id; } else { return array( 'status' => 'error', 'message' => sprintf( __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'), $category_data['parent-name'] ) ); } } // Create category $args = array( 'description' => $category_data['description'], 'parent' => $parent_id, 'slug' => $category_data['url-name'], ); $result = wp_insert_term($category_data['name'], 'product_cat', $args); if (is_wp_error($result)) { return array( 'status' => 'error', 'message' => $result->get_error_message() ); } $term_id = $result['term_id']; // Set display type update_term_meta($term_id, 'display_type', $category_data['display-type']); // Set visibility and password protection using direct DB access $this->set_category_protection($term_id, $category_data); return array( 'status' => 'created', 'message' => sprintf( __("Product category '%s' created.", 'studiou-wc-product-cat-manage'), $category_data['name'] ) ); } /** * Update product category * * @param array $category_data Category data * @return array Result with status and message */ public function update_category($category_data) { // Check if category exists $existing = $this->find_category_by_name($category_data['name']); if (!$existing) { return array( 'status' => 'error', 'message' => sprintf( __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'), $category_data['name'] ) ); } // Find parent category ID if parent name is provided $parent_id = 0; if (!empty($category_data['parent-name'])) { $parent = $this->find_category_by_name($category_data['parent-name']); if ($parent) { $parent_id = $parent->term_id; } else { return array( 'status' => 'error', 'message' => sprintf( __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'), $category_data['parent-name'] ) ); } } // Update category $args = array( 'description' => $category_data['description'], 'parent' => $parent_id, 'slug' => $category_data['url-name'], ); $result = wp_update_term($existing->term_id, 'product_cat', $args); if (is_wp_error($result)) { return array( 'status' => 'error', 'message' => $result->get_error_message() ); } // Set display type update_term_meta($existing->term_id, 'display_type', $category_data['display-type']); // Set visibility and password protection using direct DB access $this->set_category_protection($existing->term_id, $category_data); return array( 'status' => 'updated', 'message' => sprintf( __("Product category '%s' updated.", 'studiou-wc-product-cat-manage'), $category_data['name'] ) ); } /** * Delete product category * * @param string $category_name Category name * @return array Result with status and message */ public function delete_category($category_name) { // Check if category exists $existing = $this->find_category_by_name($category_name); if (!$existing) { return array( 'status' => 'error', 'message' => sprintf( __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'), $category_name ) ); } // Delete category $result = wp_delete_term($existing->term_id, 'product_cat'); if (is_wp_error($result)) { return array( 'status' => 'error', 'message' => $result->get_error_message() ); } return array( 'status' => 'deleted', 'message' => sprintf( __("Product category '%s' deleted.", 'studiou-wc-product-cat-manage'), $category_name ) ); } /** * 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); } } }