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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. // Get all categories regardless of hierarchy, including empty ones
  28. $args = array(
  29. 'taxonomy' => 'product_cat',
  30. 'orderby' => 'term_id', // Order by ID to maintain creation order
  31. 'hide_empty' => false,
  32. 'hierarchical' => false, // Don't nest them, get a flat list
  33. 'get' => 'all', // Get all fields
  34. );
  35. $categories = get_terms($args);
  36. $formatted_categories = array();
  37. if (!is_wp_error($categories) && !empty($categories)) {
  38. // Sort by hierarchy: parent categories first, then children
  39. $sorted_categories = $this->sort_categories_by_hierarchy($categories);
  40. foreach ($sorted_categories as $category) {
  41. $formatted_categories[] = $this->format_category($category);
  42. }
  43. }
  44. return $formatted_categories;
  45. }
  46. /**
  47. * Sort categories by hierarchy - parents first, then children
  48. *
  49. * @param array $categories Array of WP_Term objects
  50. * @return array Sorted array of categories
  51. */
  52. private function sort_categories_by_hierarchy($categories) {
  53. $parents = array();
  54. $children = array();
  55. // Separate parents and children
  56. foreach ($categories as $category) {
  57. if ($category->parent == 0) {
  58. $parents[] = $category;
  59. } else {
  60. $children[] = $category;
  61. }
  62. }
  63. // Sort parents alphabetically
  64. usort($parents, function($a, $b) {
  65. return strcmp($a->name, $b->name);
  66. });
  67. // Recursively add children after their parents
  68. $sorted = array();
  69. foreach ($parents as $parent) {
  70. $sorted[] = $parent;
  71. $sorted = array_merge($sorted, $this->get_children_recursive($parent->term_id, $children));
  72. }
  73. // Add any orphaned children (categories with non-existent parents)
  74. foreach ($children as $child) {
  75. if (!$this->is_child_added($child, $sorted)) {
  76. $sorted[] = $child;
  77. }
  78. }
  79. return $sorted;
  80. }
  81. /**
  82. * Recursively get children of a category
  83. *
  84. * @param int $parent_id Parent category ID
  85. * @param array $all_children Array of all child categories
  86. * @return array Array of child categories
  87. */
  88. private function get_children_recursive($parent_id, $all_children) {
  89. $result = array();
  90. $direct_children = array();
  91. // Find direct children of this parent
  92. foreach ($all_children as $child) {
  93. if ($child->parent == $parent_id) {
  94. $direct_children[] = $child;
  95. }
  96. }
  97. // Sort direct children alphabetically
  98. usort($direct_children, function($a, $b) {
  99. return strcmp($a->name, $b->name);
  100. });
  101. // Add each child and its descendants
  102. foreach ($direct_children as $child) {
  103. $result[] = $child;
  104. $result = array_merge($result, $this->get_children_recursive($child->term_id, $all_children));
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Check if a child category has already been added to the sorted array
  110. *
  111. * @param WP_Term $child Child category to check
  112. * @param array $sorted Array of already sorted categories
  113. * @return bool True if child is already in sorted array
  114. */
  115. private function is_child_added($child, $sorted) {
  116. foreach ($sorted as $category) {
  117. if ($category->term_id == $child->term_id) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. /**
  124. * Format category data
  125. *
  126. * @param object $category WP_Term object
  127. * @return array Formatted category data
  128. */
  129. private function format_category($category) {
  130. // Get parent category name
  131. $parent_name = '';
  132. if ($category->parent > 0) {
  133. $parent = get_term($category->parent, 'product_cat');
  134. if (!is_wp_error($parent)) {
  135. $parent_name = $parent->name;
  136. }
  137. }
  138. // Get display type
  139. $display_type = get_term_meta($category->term_id, 'display_type', true);
  140. if (empty($display_type)) {
  141. $display_type = 'default';
  142. }
  143. // Get protected category data if available
  144. $visibility = 'public';
  145. $protected_passwords = '';
  146. if (class_exists('WC_Protected_Categories')) {
  147. // Use Util class to get category visibility info
  148. $term_id = $category->term_id;
  149. // Use the plugin's own method to get visibility data if available
  150. if (class_exists('\\Barn2\\Plugin\\WC_Protected_Categories\\Util')) {
  151. $visibility_obj = \Barn2\Plugin\WC_Protected_Categories\Util::get_category_visibility($term_id);
  152. if ($visibility_obj) {
  153. $visibility = $visibility_obj->get_visibility();
  154. if ($visibility === 'protected' && $visibility_obj->has_password_protection()) {
  155. // Get passwords using the plugin's own method
  156. $passwords = \Barn2\Plugin\WC_Protected_Categories\Util::get_term_passwords($term_id);
  157. if (!empty($passwords)) {
  158. $protected_passwords = implode('|', $passwords);
  159. }
  160. }
  161. }
  162. } else {
  163. // Fallback to direct meta access if plugin class not available
  164. $visibility = get_term_meta($term_id, 'visibility', true);
  165. if (empty($visibility)) {
  166. $visibility = 'public';
  167. }
  168. if ($visibility === 'protected') {
  169. $passwords = get_term_meta($term_id, 'password', false);
  170. if ($passwords && !empty($passwords[0]) && is_array($passwords[0])) {
  171. $passwords = $passwords[0];
  172. }
  173. if (!empty($passwords)) {
  174. $protected_passwords = implode('|', $passwords);
  175. }
  176. }
  177. }
  178. }
  179. return array(
  180. 'name' => $category->name,
  181. 'url-name' => $category->slug,
  182. 'parent-name' => $parent_name,
  183. 'description' => $category->description,
  184. 'display-type' => $display_type,
  185. 'visibility' => $visibility,
  186. 'protected-passwords' => $protected_passwords,
  187. 'operation' => 'U' // Default operation for existing categories
  188. );
  189. }
  190. /**
  191. * Find category by name
  192. *
  193. * @param string $name Category name
  194. * @return WP_Term|null Category term object or null if not found
  195. */
  196. public function find_category_by_name($name) {
  197. $args = array(
  198. 'taxonomy' => 'product_cat',
  199. 'name' => $name,
  200. 'hide_empty' => false,
  201. );
  202. $categories = get_terms($args);
  203. if (!is_wp_error($categories) && !empty($categories)) {
  204. return $categories[0];
  205. }
  206. return null;
  207. }
  208. /**
  209. * Create product category
  210. *
  211. * @param array $category_data Category data
  212. * @return array Result with status and message
  213. */
  214. public function create_category($category_data) {
  215. // Check if category already exists
  216. $existing = $this->find_category_by_name($category_data['name']);
  217. if ($existing) {
  218. return array(
  219. 'status' => 'skipped',
  220. 'message' => sprintf(
  221. __("Product category '%s' already exists. Skipped.", 'studiou-wc-product-cat-manage'),
  222. $category_data['name']
  223. )
  224. );
  225. }
  226. // Find parent category ID if parent name is provided
  227. $parent_id = 0;
  228. if (!empty($category_data['parent-name'])) {
  229. $parent = $this->find_category_by_name($category_data['parent-name']);
  230. if ($parent) {
  231. $parent_id = $parent->term_id;
  232. } else {
  233. return array(
  234. 'status' => 'error',
  235. 'message' => sprintf(
  236. __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'),
  237. $category_data['parent-name']
  238. )
  239. );
  240. }
  241. }
  242. // Create category
  243. $args = array(
  244. 'description' => $category_data['description'],
  245. 'parent' => $parent_id,
  246. 'slug' => $category_data['url-name'],
  247. );
  248. $result = wp_insert_term($category_data['name'], 'product_cat', $args);
  249. if (is_wp_error($result)) {
  250. return array(
  251. 'status' => 'error',
  252. 'message' => $result->get_error_message()
  253. );
  254. }
  255. $term_id = $result['term_id'];
  256. // Set display type
  257. update_term_meta($term_id, 'display_type', $category_data['display-type']);
  258. // Set visibility and password protection if WC Protected Categories is active
  259. if (class_exists('WC_Protected_Categories')) {
  260. // Set visibility - ensure the "visibility" meta key is used (not wc_protected_categories_visibility)
  261. update_term_meta($term_id, 'visibility', $category_data['visibility']);
  262. // Set password protection if visibility is protected and passwords are provided
  263. if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
  264. $passwords = explode('|', $category_data['protected-passwords']);
  265. // Filter out empty passwords
  266. $passwords = array_filter($passwords);
  267. if (empty($passwords)) {
  268. // If no valid passwords after filtering, set a default one as the plugin does
  269. $passwords = ['password123'];
  270. }
  271. // Store the passwords array as a single meta entry
  272. add_term_meta($term_id, 'password', $passwords);
  273. }
  274. }
  275. return array(
  276. 'status' => 'created',
  277. 'message' => sprintf(
  278. __("Product category '%s' created.", 'studiou-wc-product-cat-manage'),
  279. $category_data['name']
  280. )
  281. );
  282. }
  283. /**
  284. * Update product category
  285. *
  286. * @param array $category_data Category data
  287. * @return array Result with status and message
  288. */
  289. public function update_category($category_data) {
  290. // Check if category exists
  291. $existing = $this->find_category_by_name($category_data['name']);
  292. if (!$existing) {
  293. return array(
  294. 'status' => 'error',
  295. 'message' => sprintf(
  296. __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'),
  297. $category_data['name']
  298. )
  299. );
  300. }
  301. // Find parent category ID if parent name is provided
  302. $parent_id = 0;
  303. if (!empty($category_data['parent-name'])) {
  304. $parent = $this->find_category_by_name($category_data['parent-name']);
  305. if ($parent) {
  306. $parent_id = $parent->term_id;
  307. } else {
  308. return array(
  309. 'status' => 'error',
  310. 'message' => sprintf(
  311. __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'),
  312. $category_data['parent-name']
  313. )
  314. );
  315. }
  316. }
  317. // Update category
  318. $args = array(
  319. 'description' => $category_data['description'],
  320. 'parent' => $parent_id,
  321. 'slug' => $category_data['url-name'],
  322. );
  323. $result = wp_update_term($existing->term_id, 'product_cat', $args);
  324. if (is_wp_error($result)) {
  325. return array(
  326. 'status' => 'error',
  327. 'message' => $result->get_error_message()
  328. );
  329. }
  330. // Set display type
  331. update_term_meta($existing->term_id, 'display_type', $category_data['display-type']);
  332. // Set visibility and password protection if WC Protected Categories is active
  333. if (class_exists('WC_Protected_Categories')) {
  334. // First remove existing protection meta
  335. delete_term_meta($existing->term_id, 'password');
  336. delete_term_meta($existing->term_id, 'user_roles');
  337. delete_term_meta($existing->term_id, 'users');
  338. // Set visibility - ensure the "visibility" meta key is used
  339. update_term_meta($existing->term_id, 'visibility', $category_data['visibility']);
  340. // Set password protection if visibility is protected and passwords are provided
  341. if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
  342. $passwords = explode('|', $category_data['protected-passwords']);
  343. // Filter out empty passwords
  344. $passwords = array_filter($passwords);
  345. if (empty($passwords)) {
  346. // If no valid passwords after filtering, set a default one as the plugin does
  347. $passwords = ['password123'];
  348. }
  349. // Store the passwords array as a single meta entry
  350. add_term_meta($existing->term_id, 'password', $passwords);
  351. }
  352. }
  353. return array(
  354. 'status' => 'updated',
  355. 'message' => sprintf(
  356. __("Product category '%s' updated.", 'studiou-wc-product-cat-manage'),
  357. $category_data['name']
  358. )
  359. );
  360. }
  361. /**
  362. * Delete product category
  363. *
  364. * @param string $category_name Category name
  365. * @return array Result with status and message
  366. */
  367. public function delete_category($category_name) {
  368. // Check if category exists
  369. $existing = $this->find_category_by_name($category_name);
  370. if (!$existing) {
  371. return array(
  372. 'status' => 'error',
  373. 'message' => sprintf(
  374. __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'),
  375. $category_name
  376. )
  377. );
  378. }
  379. // Delete category
  380. $result = wp_delete_term($existing->term_id, 'product_cat');
  381. if (is_wp_error($result)) {
  382. return array(
  383. 'status' => 'error',
  384. 'message' => $result->get_error_message()
  385. );
  386. }
  387. return array(
  388. 'status' => 'deleted',
  389. 'message' => sprintf(
  390. __("Product category '%s' deleted.", 'studiou-wc-product-cat-manage'),
  391. $category_name
  392. )
  393. );
  394. }
  395. }