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

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