|
@@ -0,0 +1,316 @@
|
|
|
|
|
+<?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 if available
|
|
|
|
|
+ $visibility = 'public';
|
|
|
|
|
+ $protected_passwords = '';
|
|
|
|
|
+
|
|
|
|
|
+ if (class_exists('WC_Protected_Categories')) {
|
|
|
|
|
+ $visibility = get_term_meta($category->term_id, 'visibility', true);
|
|
|
|
|
+ if (empty($visibility)) {
|
|
|
|
|
+ $visibility = 'public';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($visibility === 'protected') {
|
|
|
|
|
+ // Get password protection
|
|
|
|
|
+ $passwords = get_term_meta($category->term_id, 'password', true);
|
|
|
|
|
+ if (!empty($passwords) && is_array($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 if WC Protected Categories is active
|
|
|
|
|
+ if (class_exists('WC_Protected_Categories')) {
|
|
|
|
|
+ update_term_meta($term_id, 'visibility', $category_data['visibility']);
|
|
|
|
|
+
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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 if WC Protected Categories is active
|
|
|
|
|
+ if (class_exists('WC_Protected_Categories')) {
|
|
|
|
|
+ update_term_meta($existing->term_id, 'visibility', $category_data['visibility']);
|
|
|
|
|
+
|
|
|
|
|
+ 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');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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
|
|
|
|
|
+ )
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+}
|