class-studiou-wc-fpp-db.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. class Studiou_WC_FPP_DB {
  6. const TABLE_NAME = 'studiou_fpp_files';
  7. public function __construct() {
  8. $this->register_media_taxonomy();
  9. }
  10. private function register_media_taxonomy() {
  11. add_action('init', array($this, 'register_media_category_taxonomy'));
  12. }
  13. public function register_media_category_taxonomy() {
  14. $labels = array(
  15. 'name' => __('Media Categories', 'studiou-wc-free-photo-product'),
  16. 'singular_name' => __('Media Category', 'studiou-wc-free-photo-product'),
  17. 'search_items' => __('Search Media Categories', 'studiou-wc-free-photo-product'),
  18. 'all_items' => __('All Media Categories', 'studiou-wc-free-photo-product'),
  19. 'parent_item' => __('Parent Media Category', 'studiou-wc-free-photo-product'),
  20. 'parent_item_colon' => __('Parent Media Category:', 'studiou-wc-free-photo-product'),
  21. 'edit_item' => __('Edit Media Category', 'studiou-wc-free-photo-product'),
  22. 'update_item' => __('Update Media Category', 'studiou-wc-free-photo-product'),
  23. 'add_new_item' => __('Add New Media Category', 'studiou-wc-free-photo-product'),
  24. 'new_item_name' => __('New Media Category Name', 'studiou-wc-free-photo-product'),
  25. 'menu_name' => __('Media Categories', 'studiou-wc-free-photo-product'),
  26. );
  27. register_taxonomy('studiou_media_category', 'attachment', array(
  28. 'hierarchical' => true,
  29. 'labels' => $labels,
  30. 'show_ui' => true,
  31. 'show_admin_column' => true,
  32. 'query_var' => true,
  33. 'rewrite' => array('slug' => 'media-category'),
  34. 'show_in_rest' => true,
  35. ));
  36. }
  37. public function get_table_name() {
  38. global $wpdb;
  39. return $wpdb->prefix . self::TABLE_NAME;
  40. }
  41. public function create_tables() {
  42. global $wpdb;
  43. $table_name = $this->get_table_name();
  44. $charset_collate = $wpdb->get_charset_collate();
  45. $sql = "CREATE TABLE {$table_name} (
  46. id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  47. product_id bigint(20) unsigned NOT NULL,
  48. variation_id bigint(20) unsigned DEFAULT 0,
  49. order_id bigint(20) unsigned DEFAULT 0,
  50. order_item_id bigint(20) unsigned DEFAULT 0,
  51. attachment_id bigint(20) unsigned NOT NULL,
  52. customer_id bigint(20) unsigned DEFAULT 0,
  53. session_key varchar(64) DEFAULT '',
  54. file_name varchar(255) NOT NULL,
  55. status varchar(20) NOT NULL DEFAULT 'ready',
  56. created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  57. updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  58. PRIMARY KEY (id),
  59. KEY product_id (product_id),
  60. KEY order_id (order_id),
  61. KEY attachment_id (attachment_id),
  62. KEY status (status),
  63. KEY customer_id (customer_id)
  64. ) {$charset_collate};";
  65. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  66. dbDelta($sql);
  67. update_option('studiou_wcfpp_db_version', STUDIOU_WCFPP_VERSION);
  68. }
  69. public function insert_file_record($data) {
  70. global $wpdb;
  71. $defaults = array(
  72. 'product_id' => 0,
  73. 'variation_id' => 0,
  74. 'order_id' => 0,
  75. 'order_item_id' => 0,
  76. 'attachment_id' => 0,
  77. 'customer_id' => get_current_user_id(),
  78. 'session_key' => '',
  79. 'file_name' => '',
  80. 'status' => 'ready',
  81. 'created_at' => current_time('mysql'),
  82. 'updated_at' => current_time('mysql'),
  83. );
  84. $data = wp_parse_args($data, $defaults);
  85. $result = $wpdb->insert(
  86. $this->get_table_name(),
  87. $data,
  88. array('%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s')
  89. );
  90. if ($result === false) {
  91. return false;
  92. }
  93. return $wpdb->insert_id;
  94. }
  95. public function get_file_record($id) {
  96. global $wpdb;
  97. $table = $this->get_table_name();
  98. return $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", $id));
  99. }
  100. public function get_file_records($args = array()) {
  101. global $wpdb;
  102. $table = $this->get_table_name();
  103. $defaults = array(
  104. 'product_id' => 0,
  105. 'order_id' => 0,
  106. 'status' => '',
  107. 'orderby' => 'created_at',
  108. 'order' => 'DESC',
  109. 'limit' => 50,
  110. 'offset' => 0,
  111. 'search' => '',
  112. );
  113. $args = wp_parse_args($args, $defaults);
  114. $where = array('1=1');
  115. $values = array();
  116. if (!empty($args['product_id'])) {
  117. $where[] = 'product_id = %d';
  118. $values[] = $args['product_id'];
  119. }
  120. if (!empty($args['order_id'])) {
  121. $where[] = 'order_id = %d';
  122. $values[] = $args['order_id'];
  123. }
  124. if (!empty($args['status'])) {
  125. $where[] = 'status = %s';
  126. $values[] = $args['status'];
  127. }
  128. if (!empty($args['search'])) {
  129. $where[] = 'file_name LIKE %s';
  130. $values[] = '%' . $wpdb->esc_like($args['search']) . '%';
  131. }
  132. $where_clause = implode(' AND ', $where);
  133. $allowed_orderby = array('id', 'product_id', 'order_id', 'file_name', 'status', 'created_at', 'updated_at');
  134. $orderby = in_array($args['orderby'], $allowed_orderby) ? $args['orderby'] : 'created_at';
  135. $order = strtoupper($args['order']) === 'ASC' ? 'ASC' : 'DESC';
  136. $sql = "SELECT * FROM {$table} WHERE {$where_clause} ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d";
  137. $values[] = (int) $args['limit'];
  138. $values[] = (int) $args['offset'];
  139. if (!empty($values)) {
  140. $sql = $wpdb->prepare($sql, $values);
  141. }
  142. return $wpdb->get_results($sql);
  143. }
  144. public function count_file_records($args = array()) {
  145. global $wpdb;
  146. $table = $this->get_table_name();
  147. $where = array('1=1');
  148. $values = array();
  149. if (!empty($args['product_id'])) {
  150. $where[] = 'product_id = %d';
  151. $values[] = $args['product_id'];
  152. }
  153. if (!empty($args['order_id'])) {
  154. $where[] = 'order_id = %d';
  155. $values[] = $args['order_id'];
  156. }
  157. if (!empty($args['status'])) {
  158. $where[] = 'status = %s';
  159. $values[] = $args['status'];
  160. }
  161. if (!empty($args['search'])) {
  162. $where[] = 'file_name LIKE %s';
  163. $values[] = '%' . $wpdb->esc_like($args['search']) . '%';
  164. }
  165. $where_clause = implode(' AND ', $where);
  166. $sql = "SELECT COUNT(*) FROM {$table} WHERE {$where_clause}";
  167. if (!empty($values)) {
  168. $sql = $wpdb->prepare($sql, $values);
  169. }
  170. return (int) $wpdb->get_var($sql);
  171. }
  172. public function update_file_record($id, $data) {
  173. global $wpdb;
  174. $data['updated_at'] = current_time('mysql');
  175. return $wpdb->update(
  176. $this->get_table_name(),
  177. $data,
  178. array('id' => $id),
  179. null,
  180. array('%d')
  181. );
  182. }
  183. public function update_file_status($id, $status) {
  184. $allowed = array('ready', 'downloaded', 'solved');
  185. if (!in_array($status, $allowed)) {
  186. return false;
  187. }
  188. return $this->update_file_record($id, array('status' => $status));
  189. }
  190. public function delete_file_record($id) {
  191. global $wpdb;
  192. $record = $this->get_file_record($id);
  193. if (!$record) {
  194. return false;
  195. }
  196. // Delete the attachment from media library
  197. if ($record->attachment_id) {
  198. wp_delete_attachment($record->attachment_id, true);
  199. }
  200. return $wpdb->delete(
  201. $this->get_table_name(),
  202. array('id' => $id),
  203. array('%d')
  204. );
  205. }
  206. public function get_file_records_by_ids($ids) {
  207. global $wpdb;
  208. $table = $this->get_table_name();
  209. if (empty($ids)) {
  210. return array();
  211. }
  212. $ids = array_map('intval', $ids);
  213. $placeholders = implode(',', array_fill(0, count($ids), '%d'));
  214. $sql = $wpdb->prepare("SELECT * FROM {$table} WHERE id IN ({$placeholders})", $ids);
  215. return $wpdb->get_results($sql);
  216. }
  217. public function link_file_to_order($file_record_id, $order_id, $order_item_id) {
  218. return $this->update_file_record($file_record_id, array(
  219. 'order_id' => $order_id,
  220. 'order_item_id' => $order_item_id,
  221. ));
  222. }
  223. public function get_media_categories() {
  224. $terms = get_terms(array(
  225. 'taxonomy' => 'studiou_media_category',
  226. 'hide_empty' => false,
  227. ));
  228. if (is_wp_error($terms)) {
  229. return array();
  230. }
  231. return $terms;
  232. }
  233. }