| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- /**
- * Database operations class
- *
- * Handles database operations related to product categories
- *
- * @since 1.0.0
- * @package Studiou_WC_Product_Cat_Manage
- * @subpackage Studiou_WC_Product_Cat_Manage/includes
- */
- // If this file is called directly, abort.
- if (!defined('WPINC')) {
- die;
- }
- class Studiou_WC_Product_Cat_Manage_DB {
-
- /**
- * Constructor
- */
- public function __construct() {
- }
-
- /**
- * Get all product categories
- *
- * @return array Array of product categories with all required properties
- */
- public function get_all_product_categories() {
- $args = array(
- 'taxonomy' => '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);
- }
- }
- }
|