class-studiou-wc-product-cat-manage-db.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /**
  3. * Database operations class
  4. *
  5. * Handles database operations related to product categories
  6. *
  7. * @since 1.0.0
  8. * @package Studiou_WC_Product_Cat_Manage
  9. * @subpackage Studiou_WC_Product_Cat_Manage/includes
  10. */
  11. // If this file is called directly, abort.
  12. if (!defined('WPINC')) {
  13. die;
  14. }
  15. class Studiou_WC_Product_Cat_Manage_DB {
  16. /**
  17. * Constructor
  18. */
  19. public function __construct() {
  20. }
  21. /**
  22. * Get all product categories
  23. *
  24. * @return array Array of product categories with all required properties
  25. */
  26. public function get_all_product_categories() {
  27. $args = array(
  28. 'taxonomy' => 'product_cat',
  29. 'orderby' => 'name',
  30. 'hide_empty' => false,
  31. );
  32. $categories = get_terms($args);
  33. $formatted_categories = array();
  34. if (!is_wp_error($categories)) {
  35. foreach ($categories as $category) {
  36. $formatted_categories[] = $this->format_category($category);
  37. }
  38. }
  39. return $formatted_categories;
  40. }
  41. /**
  42. * Format category data
  43. *
  44. * @param object $category WP_Term object
  45. * @return array Formatted category data
  46. */
  47. private function format_category($category) {
  48. // Get parent category name
  49. $parent_name = '';
  50. if ($category->parent > 0) {
  51. $parent = get_term($category->parent, 'product_cat');
  52. if (!is_wp_error($parent)) {
  53. $parent_name = $parent->name;
  54. }
  55. }
  56. // Get display type
  57. $display_type = get_term_meta($category->term_id, 'display_type', true);
  58. if (empty($display_type)) {
  59. $display_type = 'default';
  60. }
  61. // Get protected category data if available
  62. $visibility = 'public';
  63. $protected_passwords = '';
  64. if (class_exists('WC_Protected_Categories')) {
  65. $visibility = get_term_meta($category->term_id, 'visibility', true);
  66. if (empty($visibility)) {
  67. $visibility = 'public';
  68. }
  69. if ($visibility === 'protected') {
  70. // Get password protection
  71. $passwords = get_term_meta($category->term_id, 'password', true);
  72. if (!empty($passwords) && is_array($passwords)) {
  73. $protected_passwords = implode('|', $passwords);
  74. }
  75. }
  76. }
  77. return array(
  78. 'name' => $category->name,
  79. 'url-name' => $category->slug,
  80. 'parent-name' => $parent_name,
  81. 'description' => $category->description,
  82. 'display-type' => $display_type,
  83. 'visibility' => $visibility,
  84. 'protected-passwords' => $protected_passwords,
  85. 'operation' => 'U' // Default operation for existing categories
  86. );
  87. }
  88. /**
  89. * Find category by name
  90. *
  91. * @param string $name Category name
  92. * @return WP_Term|null Category term object or null if not found
  93. */
  94. public function find_category_by_name($name) {
  95. $args = array(
  96. 'taxonomy' => 'product_cat',
  97. 'name' => $name,
  98. 'hide_empty' => false,
  99. );
  100. $categories = get_terms($args);
  101. if (!is_wp_error($categories) && !empty($categories)) {
  102. return $categories[0];
  103. }
  104. return null;
  105. }
  106. /**
  107. * Create product category
  108. *
  109. * @param array $category_data Category data
  110. * @return array Result with status and message
  111. */
  112. public function create_category($category_data) {
  113. // Check if category already exists
  114. $existing = $this->find_category_by_name($category_data['name']);
  115. if ($existing) {
  116. return array(
  117. 'status' => 'skipped',
  118. 'message' => sprintf(
  119. __("Product category '%s' already exists. Skipped.", 'studiou-wc-product-cat-manage'),
  120. $category_data['name']
  121. )
  122. );
  123. }
  124. // Find parent category ID if parent name is provided
  125. $parent_id = 0;
  126. if (!empty($category_data['parent-name'])) {
  127. $parent = $this->find_category_by_name($category_data['parent-name']);
  128. if ($parent) {
  129. $parent_id = $parent->term_id;
  130. } else {
  131. return array(
  132. 'status' => 'error',
  133. 'message' => sprintf(
  134. __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'),
  135. $category_data['parent-name']
  136. )
  137. );
  138. }
  139. }
  140. // Create category
  141. $args = array(
  142. 'description' => $category_data['description'],
  143. 'parent' => $parent_id,
  144. 'slug' => $category_data['url-name'],
  145. );
  146. $result = wp_insert_term($category_data['name'], 'product_cat', $args);
  147. if (is_wp_error($result)) {
  148. return array(
  149. 'status' => 'error',
  150. 'message' => $result->get_error_message()
  151. );
  152. }
  153. $term_id = $result['term_id'];
  154. // Set display type
  155. update_term_meta($term_id, 'display_type', $category_data['display-type']);
  156. // Set visibility and password protection if WC Protected Categories is active
  157. if (class_exists('WC_Protected_Categories')) {
  158. update_term_meta($term_id, 'visibility', $category_data['visibility']);
  159. if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
  160. $passwords = explode('|', $category_data['protected-passwords']);
  161. // Store passwords as an array
  162. update_term_meta($term_id, 'password', $passwords);
  163. }
  164. }
  165. return array(
  166. 'status' => 'created',
  167. 'message' => sprintf(
  168. __("Product category '%s' created.", 'studiou-wc-product-cat-manage'),
  169. $category_data['name']
  170. )
  171. );
  172. }
  173. /**
  174. * Update product category
  175. *
  176. * @param array $category_data Category data
  177. * @return array Result with status and message
  178. */
  179. public function update_category($category_data) {
  180. // Check if category exists
  181. $existing = $this->find_category_by_name($category_data['name']);
  182. if (!$existing) {
  183. return array(
  184. 'status' => 'error',
  185. 'message' => sprintf(
  186. __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'),
  187. $category_data['name']
  188. )
  189. );
  190. }
  191. // Find parent category ID if parent name is provided
  192. $parent_id = 0;
  193. if (!empty($category_data['parent-name'])) {
  194. $parent = $this->find_category_by_name($category_data['parent-name']);
  195. if ($parent) {
  196. $parent_id = $parent->term_id;
  197. } else {
  198. return array(
  199. 'status' => 'error',
  200. 'message' => sprintf(
  201. __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'),
  202. $category_data['parent-name']
  203. )
  204. );
  205. }
  206. }
  207. // Update category
  208. $args = array(
  209. 'description' => $category_data['description'],
  210. 'parent' => $parent_id,
  211. 'slug' => $category_data['url-name'],
  212. );
  213. $result = wp_update_term($existing->term_id, 'product_cat', $args);
  214. if (is_wp_error($result)) {
  215. return array(
  216. 'status' => 'error',
  217. 'message' => $result->get_error_message()
  218. );
  219. }
  220. // Set display type
  221. update_term_meta($existing->term_id, 'display_type', $category_data['display-type']);
  222. // Set visibility and password protection if WC Protected Categories is active
  223. if (class_exists('WC_Protected_Categories')) {
  224. update_term_meta($existing->term_id, 'visibility', $category_data['visibility']);
  225. if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
  226. $passwords = explode('|', $category_data['protected-passwords']);
  227. // Store passwords as an array
  228. update_term_meta($existing->term_id, 'password', $passwords);
  229. } else {
  230. // Remove passwords if visibility is not protected
  231. delete_term_meta($existing->term_id, 'password');
  232. }
  233. }
  234. return array(
  235. 'status' => 'updated',
  236. 'message' => sprintf(
  237. __("Product category '%s' updated.", 'studiou-wc-product-cat-manage'),
  238. $category_data['name']
  239. )
  240. );
  241. }
  242. /**
  243. * Delete product category
  244. *
  245. * @param string $category_name Category name
  246. * @return array Result with status and message
  247. */
  248. public function delete_category($category_name) {
  249. // Check if category exists
  250. $existing = $this->find_category_by_name($category_name);
  251. if (!$existing) {
  252. return array(
  253. 'status' => 'error',
  254. 'message' => sprintf(
  255. __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'),
  256. $category_name
  257. )
  258. );
  259. }
  260. // Delete category
  261. $result = wp_delete_term($existing->term_id, 'product_cat');
  262. if (is_wp_error($result)) {
  263. return array(
  264. 'status' => 'error',
  265. 'message' => $result->get_error_message()
  266. );
  267. }
  268. return array(
  269. 'status' => 'deleted',
  270. 'message' => sprintf(
  271. __("Product category '%s' deleted.", 'studiou-wc-product-cat-manage'),
  272. $category_name
  273. )
  274. );
  275. }
  276. }