class-qdr-tss-media-page.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Media Page functionality of the plugin.
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. * @since 1.0.0
  7. */
  8. class QDR_TSS_Media_Page {
  9. /**
  10. * The ID of this plugin.
  11. *
  12. * @since 1.0.0
  13. * @access private
  14. * @var string $plugin_name The ID of this plugin.
  15. */
  16. private $plugin_name;
  17. /**
  18. * The version of this plugin.
  19. *
  20. * @since 1.0.0
  21. * @access private
  22. * @var string $version The current version of this plugin.
  23. */
  24. private $version;
  25. /**
  26. * The database manager instance.
  27. *
  28. * @since 1.0.0
  29. * @access private
  30. * @var QDR_TSS_DB_Manager $db_manager The database manager instance.
  31. */
  32. private $db_manager;
  33. /**
  34. * The file manager instance.
  35. *
  36. * @since 1.0.0
  37. * @access private
  38. * @var QDR_TSS_File_Manager $file_manager The file manager instance.
  39. */
  40. private $file_manager;
  41. /**
  42. * Initialize the class and set its properties.
  43. *
  44. * @since 1.0.0
  45. * @param string $plugin_name The name of this plugin.
  46. * @param string $version The version of this plugin.
  47. */
  48. public function __construct($plugin_name, $version) {
  49. $this->plugin_name = $plugin_name;
  50. $this->version = $version;
  51. $this->db_manager = QDR_TSS_DB_Manager::instance();
  52. $this->file_manager = QDR_TSS_File_Manager::instance();
  53. }
  54. /**
  55. * AJAX handler for getting items.
  56. *
  57. * @since 1.0.0
  58. */
  59. public function ajax_get_items() {
  60. // Check nonce
  61. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
  62. wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
  63. }
  64. // Parse parameters
  65. $args = array(
  66. 'per_page' => isset($_POST['per_page']) ? intval($_POST['per_page']) : 10,
  67. 'page' => isset($_POST['page']) ? intval($_POST['page']) : 1,
  68. 'orderby' => isset($_POST['orderby']) ? sanitize_text_field($_POST['orderby']) : 'upload_date',
  69. 'order' => isset($_POST['order']) ? sanitize_text_field($_POST['order']) : 'DESC',
  70. 'original_file_name' => isset($_POST['original_file_name']) ? sanitize_text_field($_POST['original_file_name']) : '',
  71. 'reference' => isset($_POST['reference']) ? sanitize_text_field($_POST['reference']) : '',
  72. );
  73. $result = $this->db_manager->get_items($args);
  74. wp_send_json_success($result);
  75. }
  76. /**
  77. * AJAX handler for editing an item.
  78. *
  79. * @since 1.0.0
  80. */
  81. public function ajax_edit_item() {
  82. // Check nonce
  83. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
  84. wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
  85. }
  86. // Check required parameters
  87. if (!isset($_POST['id']) || empty($_POST['id'])) {
  88. wp_send_json_error(array('message' => __('Missing item ID.', 'qdr-temporary-shared-storage')));
  89. }
  90. $id = intval($_POST['id']);
  91. // Get the item
  92. $item = $this->db_manager->get_item($id);
  93. if (!$item) {
  94. wp_send_json_error(array('message' => __('Item not found.', 'qdr-temporary-shared-storage')));
  95. }
  96. // Prepare update data
  97. $data = array();
  98. if (isset($_POST['description'])) {
  99. $data['description'] = sanitize_textarea_field($_POST['description']);
  100. }
  101. if (isset($_POST['message'])) {
  102. $data['message'] = sanitize_textarea_field($_POST['message']);
  103. }
  104. if (isset($_POST['active_from'])) {
  105. $data['active_from'] = sanitize_text_field($_POST['active_from']);
  106. }
  107. if (isset($_POST['active_to'])) {
  108. $data['active_to'] = sanitize_text_field($_POST['active_to']);
  109. }
  110. if (isset($_POST['reference'])) {
  111. $data['reference'] = sanitize_text_field($_POST['reference']);
  112. }
  113. if (empty($data)) {
  114. wp_send_json_error(array('message' => __('No changes to update.', 'qdr-temporary-shared-storage')));
  115. }
  116. // Update in database
  117. $result = $this->db_manager->update_item($id, $data);
  118. if ($result === false) {
  119. wp_send_json_error(array('message' => __('Failed to update item.', 'qdr-temporary-shared-storage')));
  120. }
  121. // Get updated record
  122. $updated_item = $this->db_manager->get_item($id);
  123. wp_send_json_success(array(
  124. 'item' => $updated_item,
  125. 'message' => __('Item updated successfully.', 'qdr-temporary-shared-storage')
  126. ));
  127. }
  128. /**
  129. * AJAX handler for deleting an item.
  130. *
  131. * @since 1.0.0
  132. */
  133. public function ajax_delete_item() {
  134. // Check nonce
  135. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
  136. wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
  137. }
  138. // Check required parameters
  139. if (!isset($_POST['id']) || empty($_POST['id'])) {
  140. wp_send_json_error(array('message' => __('Missing item ID.', 'qdr-temporary-shared-storage')));
  141. }
  142. $id = intval($_POST['id']);
  143. // Get the item
  144. $item = $this->db_manager->get_item($id);
  145. if (!$item) {
  146. wp_send_json_error(array('message' => __('Item not found.', 'qdr-temporary-shared-storage')));
  147. }
  148. // Delete file
  149. $this->file_manager->delete_file($item->file_path);
  150. // Delete from database
  151. $result = $this->db_manager->delete_item($id);
  152. if ($result === false) {
  153. wp_send_json_error(array('message' => __('Failed to delete item.', 'qdr-temporary-shared-storage')));
  154. }
  155. wp_send_json_success(array(
  156. 'message' => __('Item deleted successfully.', 'qdr-temporary-shared-storage')
  157. ));
  158. }
  159. /**
  160. * AJAX handler for getting item details.
  161. *
  162. * @since 1.0.0
  163. */
  164. public function ajax_get_item_details() {
  165. // Check nonce
  166. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
  167. wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
  168. }
  169. // Check required parameters
  170. if (!isset($_POST['id']) || empty($_POST['id'])) {
  171. wp_send_json_error(array('message' => __('Missing item ID.', 'qdr-temporary-shared-storage')));
  172. }
  173. $id = intval($_POST['id']);
  174. // Get the item
  175. $item = $this->db_manager->get_item($id);
  176. if (!$item) {
  177. wp_send_json_error(array('message' => __('Item not found.', 'qdr-temporary-shared-storage')));
  178. }
  179. // Add permalink and shortcode
  180. $item->permalink = site_url('shared-file-download/' . $item->media_id);
  181. $item->shortcode = '[qdr_tss_download id="' . $item->media_id . '"]';
  182. // Remove password from response
  183. unset($item->password);
  184. wp_send_json_success(array(
  185. 'item' => $item
  186. ));
  187. }
  188. }