class-studiou-wc-fpp-admin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. class Studiou_WC_FPP_Admin {
  6. /** @var Studiou_WC_FPP_DB */
  7. private $db;
  8. public function __construct($db) {
  9. $this->db = $db;
  10. // AJAX handlers
  11. add_action('wp_ajax_studiou_wcfpp_update_status', array($this, 'ajax_update_status'));
  12. add_action('wp_ajax_studiou_wcfpp_delete_files', array($this, 'ajax_delete_files'));
  13. add_action('wp_ajax_studiou_wcfpp_download_zip', array($this, 'ajax_download_zip'));
  14. add_action('wp_ajax_studiou_wcfpp_bulk_status', array($this, 'ajax_bulk_status'));
  15. // Handle direct ZIP download
  16. add_action('admin_init', array($this, 'handle_zip_download'));
  17. }
  18. public function render_summary_page() {
  19. // Process filters
  20. $current_status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : '';
  21. $current_product = isset($_GET['product_id']) ? absint($_GET['product_id']) : 0;
  22. $current_search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
  23. $current_page = isset($_GET['paged']) ? max(1, absint($_GET['paged'])) : 1;
  24. $per_page = 30;
  25. $args = array(
  26. 'status' => $current_status,
  27. 'product_id' => $current_product,
  28. 'search' => $current_search,
  29. 'limit' => $per_page,
  30. 'offset' => ($current_page - 1) * $per_page,
  31. 'orderby' => isset($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : 'created_at',
  32. 'order' => isset($_GET['order']) ? sanitize_text_field($_GET['order']) : 'DESC',
  33. );
  34. $records = $this->db->get_file_records($args);
  35. $total = $this->db->count_file_records($args);
  36. $total_pages = ceil($total / $per_page);
  37. // Get products that have FPP enabled for filter dropdown
  38. $fpp_products = $this->get_fpp_products();
  39. include STUDIOU_WCFPP_PLUGIN_DIR . 'views/admin-summary.php';
  40. }
  41. private function get_fpp_products() {
  42. $args = array(
  43. 'post_type' => 'product',
  44. 'post_status' => 'publish',
  45. 'posts_per_page' => -1,
  46. 'meta_query' => array(
  47. array(
  48. 'key' => '_studiou_fpp_enabled',
  49. 'value' => 'yes',
  50. ),
  51. ),
  52. 'fields' => 'ids',
  53. );
  54. $product_ids = get_posts($args);
  55. $products = array();
  56. foreach ($product_ids as $pid) {
  57. $products[$pid] = get_the_title($pid);
  58. }
  59. return $products;
  60. }
  61. public function ajax_update_status() {
  62. check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
  63. if (!current_user_can('manage_woocommerce')) {
  64. wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
  65. return;
  66. }
  67. $file_id = isset($_POST['file_id']) ? absint($_POST['file_id']) : 0;
  68. $status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : '';
  69. if (!$file_id || !$status) {
  70. wp_send_json_error(array('message' => __('Invalid data.', 'studiou-wc-free-photo-product')));
  71. return;
  72. }
  73. $result = $this->db->update_file_status($file_id, $status);
  74. if ($result === false) {
  75. wp_send_json_error(array('message' => __('Failed to update status.', 'studiou-wc-free-photo-product')));
  76. return;
  77. }
  78. wp_send_json_success(array('message' => __('Status updated.', 'studiou-wc-free-photo-product')));
  79. }
  80. public function ajax_bulk_status() {
  81. check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
  82. if (!current_user_can('manage_woocommerce')) {
  83. wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
  84. return;
  85. }
  86. $file_ids = isset($_POST['file_ids']) ? array_map('absint', (array) $_POST['file_ids']) : array();
  87. $status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : '';
  88. if (empty($file_ids) || !$status) {
  89. wp_send_json_error(array('message' => __('Invalid data.', 'studiou-wc-free-photo-product')));
  90. return;
  91. }
  92. $updated = 0;
  93. foreach ($file_ids as $fid) {
  94. if ($this->db->update_file_status($fid, $status)) {
  95. $updated++;
  96. }
  97. }
  98. wp_send_json_success(array(
  99. 'message' => sprintf(__('%d file(s) updated.', 'studiou-wc-free-photo-product'), $updated),
  100. ));
  101. }
  102. public function ajax_delete_files() {
  103. check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
  104. if (!current_user_can('manage_woocommerce')) {
  105. wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
  106. return;
  107. }
  108. $file_ids = isset($_POST['file_ids']) ? array_map('absint', (array) $_POST['file_ids']) : array();
  109. if (empty($file_ids)) {
  110. wp_send_json_error(array('message' => __('No files selected.', 'studiou-wc-free-photo-product')));
  111. return;
  112. }
  113. $deleted = 0;
  114. foreach ($file_ids as $fid) {
  115. if ($this->db->delete_file_record($fid)) {
  116. $deleted++;
  117. }
  118. }
  119. wp_send_json_success(array(
  120. 'message' => sprintf(__('%d file(s) deleted.', 'studiou-wc-free-photo-product'), $deleted),
  121. ));
  122. }
  123. public function ajax_download_zip() {
  124. check_ajax_referer('studiou-wcfpp-nonce', 'nonce');
  125. if (!current_user_can('manage_woocommerce')) {
  126. wp_send_json_error(array('message' => __('Insufficient permissions.', 'studiou-wc-free-photo-product')));
  127. return;
  128. }
  129. $file_ids = isset($_POST['file_ids']) ? array_map('absint', (array) $_POST['file_ids']) : array();
  130. if (empty($file_ids)) {
  131. wp_send_json_error(array('message' => __('No files selected.', 'studiou-wc-free-photo-product')));
  132. return;
  133. }
  134. $records = $this->db->get_file_records_by_ids($file_ids);
  135. if (empty($records)) {
  136. wp_send_json_error(array('message' => __('No files found.', 'studiou-wc-free-photo-product')));
  137. return;
  138. }
  139. // Generate ZIP
  140. $upload_dir = wp_upload_dir();
  141. $zip_name = 'fpp-files-' . date('Y-m-d-His') . '.zip';
  142. $zip_path = $upload_dir['basedir'] . '/studiou-fpp-chunks/' . $zip_name;
  143. if (!class_exists('ZipArchive')) {
  144. wp_send_json_error(array('message' => __('ZIP extension not available on server.', 'studiou-wc-free-photo-product')));
  145. return;
  146. }
  147. $zip = new ZipArchive();
  148. if ($zip->open($zip_path, ZipArchive::CREATE) !== true) {
  149. wp_send_json_error(array('message' => __('Could not create ZIP file.', 'studiou-wc-free-photo-product')));
  150. return;
  151. }
  152. $added = 0;
  153. foreach ($records as $record) {
  154. $file_path = get_attached_file($record->attachment_id);
  155. if ($file_path && file_exists($file_path)) {
  156. // Use order number prefix if available
  157. $prefix = $record->order_id ? 'order-' . $record->order_id . '_' : '';
  158. $zip->addFile($file_path, $prefix . $record->file_name);
  159. $added++;
  160. }
  161. }
  162. $zip->close();
  163. if ($added === 0) {
  164. if (file_exists($zip_path)) {
  165. unlink($zip_path);
  166. }
  167. wp_send_json_error(array('message' => __('No files could be added to archive.', 'studiou-wc-free-photo-product')));
  168. return;
  169. }
  170. // Generate a nonce-protected download URL
  171. $download_url = add_query_arg(array(
  172. 'studiou_fpp_download' => 1,
  173. 'file' => $zip_name,
  174. '_wpnonce' => wp_create_nonce('studiou-fpp-zip-download'),
  175. ), admin_url('admin.php'));
  176. wp_send_json_success(array(
  177. 'download_url' => $download_url,
  178. 'file_count' => $added,
  179. ));
  180. }
  181. public function handle_zip_download() {
  182. if (!isset($_GET['studiou_fpp_download']) || !isset($_GET['file'])) {
  183. return;
  184. }
  185. if (!wp_verify_nonce($_GET['_wpnonce'], 'studiou-fpp-zip-download')) {
  186. wp_die(__('Security check failed.', 'studiou-wc-free-photo-product'));
  187. }
  188. if (!current_user_can('manage_woocommerce')) {
  189. wp_die(__('Insufficient permissions.', 'studiou-wc-free-photo-product'));
  190. }
  191. $file_name = sanitize_file_name($_GET['file']);
  192. $upload_dir = wp_upload_dir();
  193. $file_path = $upload_dir['basedir'] . '/studiou-fpp-chunks/' . $file_name;
  194. if (!file_exists($file_path) || pathinfo($file_name, PATHINFO_EXTENSION) !== 'zip') {
  195. wp_die(__('File not found.', 'studiou-wc-free-photo-product'));
  196. }
  197. header('Content-Type: application/zip');
  198. header('Content-Disposition: attachment; filename="' . $file_name . '"');
  199. header('Content-Length: ' . filesize($file_path));
  200. header('Pragma: no-cache');
  201. readfile($file_path);
  202. // Clean up the ZIP file after download
  203. unlink($file_path);
  204. exit;
  205. }
  206. public static function get_status_label($status) {
  207. $labels = array(
  208. 'ready' => __('Ready', 'studiou-wc-free-photo-product'),
  209. 'downloaded' => __('Downloaded', 'studiou-wc-free-photo-product'),
  210. 'solved' => __('Solved', 'studiou-wc-free-photo-product'),
  211. );
  212. return isset($labels[$status]) ? $labels[$status] : $status;
  213. }
  214. public static function get_status_class($status) {
  215. $classes = array(
  216. 'ready' => 'studiou-fpp-status-ready',
  217. 'downloaded' => 'studiou-fpp-status-downloaded',
  218. 'solved' => 'studiou-fpp-status-solved',
  219. );
  220. return isset($classes[$status]) ? $classes[$status] : '';
  221. }
  222. public static function get_order_edit_url($order_id) {
  223. if (class_exists('Automattic\WooCommerce\Utilities\OrderUtil')
  224. && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled()) {
  225. return admin_url('admin.php?page=wc-orders&action=edit&id=' . $order_id);
  226. }
  227. return admin_url('post.php?post=' . $order_id . '&action=edit');
  228. }
  229. }