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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 using direct DB access
  62. $visibility = 'public';
  63. $protected_passwords = '';
  64. // Get visibility directly from term meta
  65. $visibility_meta = get_term_meta($category->term_id, 'visibility', true);
  66. if (!empty($visibility_meta)) {
  67. $visibility = $visibility_meta;
  68. }
  69. // If protected, get passwords directly from term meta
  70. if ($visibility === 'protected') {
  71. $passwords = get_term_meta($category->term_id, 'password', false);
  72. if (!empty($passwords)) {
  73. // Handle different password storage formats
  74. if (is_array($passwords[0])) {
  75. $passwords = $passwords[0];
  76. }
  77. if (!empty($passwords)) {
  78. $protected_passwords = implode('|', $passwords);
  79. }
  80. }
  81. }
  82. return array(
  83. 'name' => $category->name,
  84. 'url-name' => $category->slug,
  85. 'parent-name' => $parent_name,
  86. 'description' => $category->description,
  87. 'display-type' => $display_type,
  88. 'visibility' => $visibility,
  89. 'protected-passwords' => $protected_passwords,
  90. 'operation' => 'U' // Default operation for existing categories
  91. );
  92. }
  93. /**
  94. * Find category by name
  95. *
  96. * @param string $name Category name
  97. * @return WP_Term|null Category term object or null if not found
  98. */
  99. public function find_category_by_name($name) {
  100. $args = array(
  101. 'taxonomy' => 'product_cat',
  102. 'name' => $name,
  103. 'hide_empty' => false,
  104. );
  105. $categories = get_terms($args);
  106. if (!is_wp_error($categories) && !empty($categories)) {
  107. return $categories[0];
  108. }
  109. return null;
  110. }
  111. /**
  112. * Create product category
  113. *
  114. * @param array $category_data Category data
  115. * @return array Result with status and message
  116. */
  117. public function create_category($category_data) {
  118. // Check if category already exists
  119. $existing = $this->find_category_by_name($category_data['name']);
  120. if ($existing) {
  121. return array(
  122. 'status' => 'skipped',
  123. 'message' => sprintf(
  124. __("Product category '%s' already exists. Skipped.", 'studiou-wc-product-cat-manage'),
  125. $category_data['name']
  126. )
  127. );
  128. }
  129. // Find parent category ID if parent name is provided
  130. $parent_id = 0;
  131. if (!empty($category_data['parent-name'])) {
  132. $parent = $this->find_category_by_name($category_data['parent-name']);
  133. if ($parent) {
  134. $parent_id = $parent->term_id;
  135. } else {
  136. return array(
  137. 'status' => 'error',
  138. 'message' => sprintf(
  139. __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'),
  140. $category_data['parent-name']
  141. )
  142. );
  143. }
  144. }
  145. // Create category
  146. $args = array(
  147. 'description' => $category_data['description'],
  148. 'parent' => $parent_id,
  149. 'slug' => $category_data['url-name'],
  150. );
  151. $result = wp_insert_term($category_data['name'], 'product_cat', $args);
  152. if (is_wp_error($result)) {
  153. return array(
  154. 'status' => 'error',
  155. 'message' => $result->get_error_message()
  156. );
  157. }
  158. $term_id = $result['term_id'];
  159. // Set display type
  160. update_term_meta($term_id, 'display_type', $category_data['display-type']);
  161. // Set visibility and password protection using direct DB access
  162. $this->set_category_protection($term_id, $category_data);
  163. return array(
  164. 'status' => 'created',
  165. 'message' => sprintf(
  166. __("Product category '%s' created.", 'studiou-wc-product-cat-manage'),
  167. $category_data['name']
  168. )
  169. );
  170. }
  171. /**
  172. * Update product category
  173. *
  174. * @param array $category_data Category data
  175. * @return array Result with status and message
  176. */
  177. public function update_category($category_data) {
  178. // Check if category exists
  179. $existing = $this->find_category_by_name($category_data['name']);
  180. if (!$existing) {
  181. return array(
  182. 'status' => 'error',
  183. 'message' => sprintf(
  184. __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'),
  185. $category_data['name']
  186. )
  187. );
  188. }
  189. // Find parent category ID if parent name is provided
  190. $parent_id = 0;
  191. if (!empty($category_data['parent-name'])) {
  192. $parent = $this->find_category_by_name($category_data['parent-name']);
  193. if ($parent) {
  194. $parent_id = $parent->term_id;
  195. } else {
  196. return array(
  197. 'status' => 'error',
  198. 'message' => sprintf(
  199. __("Parent category '%s' not found.", 'studiou-wc-product-cat-manage'),
  200. $category_data['parent-name']
  201. )
  202. );
  203. }
  204. }
  205. // Update category
  206. $args = array(
  207. 'description' => $category_data['description'],
  208. 'parent' => $parent_id,
  209. 'slug' => $category_data['url-name'],
  210. );
  211. $result = wp_update_term($existing->term_id, 'product_cat', $args);
  212. if (is_wp_error($result)) {
  213. return array(
  214. 'status' => 'error',
  215. 'message' => $result->get_error_message()
  216. );
  217. }
  218. // Set display type
  219. update_term_meta($existing->term_id, 'display_type', $category_data['display-type']);
  220. // Set visibility and password protection using direct DB access
  221. $this->set_category_protection($existing->term_id, $category_data);
  222. return array(
  223. 'status' => 'updated',
  224. 'message' => sprintf(
  225. __("Product category '%s' updated.", 'studiou-wc-product-cat-manage'),
  226. $category_data['name']
  227. )
  228. );
  229. }
  230. /**
  231. * Delete product category
  232. *
  233. * @param string $category_name Category name
  234. * @return array Result with status and message
  235. */
  236. public function delete_category($category_name) {
  237. // Check if category exists
  238. $existing = $this->find_category_by_name($category_name);
  239. if (!$existing) {
  240. return array(
  241. 'status' => 'error',
  242. 'message' => sprintf(
  243. __("Product category '%s' not found.", 'studiou-wc-product-cat-manage'),
  244. $category_name
  245. )
  246. );
  247. }
  248. // Delete category
  249. $result = wp_delete_term($existing->term_id, 'product_cat');
  250. if (is_wp_error($result)) {
  251. return array(
  252. 'status' => 'error',
  253. 'message' => $result->get_error_message()
  254. );
  255. }
  256. return array(
  257. 'status' => 'deleted',
  258. 'message' => sprintf(
  259. __("Product category '%s' deleted.", 'studiou-wc-product-cat-manage'),
  260. $category_name
  261. )
  262. );
  263. }
  264. /**
  265. * Set category protection settings
  266. *
  267. * @param int $term_id Term ID
  268. * @param array $category_data Category data
  269. */
  270. private function set_category_protection($term_id, $category_data) {
  271. // First remove existing protection meta to avoid duplicates
  272. delete_term_meta($term_id, 'password');
  273. delete_term_meta($term_id, 'visibility');
  274. // Set visibility
  275. update_term_meta($term_id, 'visibility', $category_data['visibility']);
  276. // Set password protection if visibility is protected and passwords are provided
  277. if ($category_data['visibility'] === 'protected' && !empty($category_data['protected-passwords'])) {
  278. $passwords = explode('|', $category_data['protected-passwords']);
  279. // Filter out empty passwords
  280. $passwords = array_filter($passwords);
  281. if (empty($passwords)) {
  282. // If no valid passwords after filtering, set a default one
  283. $passwords = ['password123'];
  284. }
  285. // Store the passwords array as a single meta entry
  286. add_term_meta($term_id, 'password', $passwords);
  287. }
  288. }
  289. }