| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- /**
- * Media Page functionality of the plugin.
- *
- * @package QDR_Temporary_Shared_Storage
- * @since 1.0.0
- */
- class QDR_TSS_Media_Page {
- /**
- * The ID of this plugin.
- *
- * @since 1.0.0
- * @access private
- * @var string $plugin_name The ID of this plugin.
- */
- private $plugin_name;
- /**
- * The version of this plugin.
- *
- * @since 1.0.0
- * @access private
- * @var string $version The current version of this plugin.
- */
- private $version;
- /**
- * The database manager instance.
- *
- * @since 1.0.0
- * @access private
- * @var QDR_TSS_DB_Manager $db_manager The database manager instance.
- */
- private $db_manager;
- /**
- * The file manager instance.
- *
- * @since 1.0.0
- * @access private
- * @var QDR_TSS_File_Manager $file_manager The file manager instance.
- */
- private $file_manager;
- /**
- * Initialize the class and set its properties.
- *
- * @since 1.0.0
- * @param string $plugin_name The name of this plugin.
- * @param string $version The version of this plugin.
- */
- public function __construct($plugin_name, $version) {
- $this->plugin_name = $plugin_name;
- $this->version = $version;
- $this->db_manager = QDR_TSS_DB_Manager::instance();
- $this->file_manager = QDR_TSS_File_Manager::instance();
- }
- /**
- * AJAX handler for getting items.
- *
- * @since 1.0.0
- */
- public function ajax_get_items() {
- // Check nonce
- if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
- wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
- }
-
- // Parse parameters
- $args = array(
- 'per_page' => isset($_POST['per_page']) ? intval($_POST['per_page']) : 10,
- 'page' => isset($_POST['page']) ? intval($_POST['page']) : 1,
- 'orderby' => isset($_POST['orderby']) ? sanitize_text_field($_POST['orderby']) : 'upload_date',
- 'order' => isset($_POST['order']) ? sanitize_text_field($_POST['order']) : 'DESC',
- 'original_file_name' => isset($_POST['original_file_name']) ? sanitize_text_field($_POST['original_file_name']) : '',
- 'reference' => isset($_POST['reference']) ? sanitize_text_field($_POST['reference']) : '',
- );
-
- $result = $this->db_manager->get_items($args);
-
- wp_send_json_success($result);
- }
- /**
- * AJAX handler for editing an item.
- *
- * @since 1.0.0
- */
- public function ajax_edit_item() {
- // Check nonce
- if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
- wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
- }
-
- // Check required parameters
- if (!isset($_POST['id']) || empty($_POST['id'])) {
- wp_send_json_error(array('message' => __('Missing item ID.', 'qdr-temporary-shared-storage')));
- }
-
- $id = intval($_POST['id']);
-
- // Get the item
- $item = $this->db_manager->get_item($id);
- if (!$item) {
- wp_send_json_error(array('message' => __('Item not found.', 'qdr-temporary-shared-storage')));
- }
-
- // Prepare update data
- $data = array();
-
- if (isset($_POST['description'])) {
- $data['description'] = sanitize_textarea_field($_POST['description']);
- }
-
- if (isset($_POST['message'])) {
- $data['message'] = sanitize_textarea_field($_POST['message']);
- }
-
- if (isset($_POST['active_from'])) {
- $data['active_from'] = sanitize_text_field($_POST['active_from']);
- }
-
- if (isset($_POST['active_to'])) {
- $data['active_to'] = sanitize_text_field($_POST['active_to']);
- }
-
- if (isset($_POST['reference'])) {
- $data['reference'] = sanitize_text_field($_POST['reference']);
- }
-
- if (empty($data)) {
- wp_send_json_error(array('message' => __('No changes to update.', 'qdr-temporary-shared-storage')));
- }
-
- // Update in database
- $result = $this->db_manager->update_item($id, $data);
-
- if ($result === false) {
- wp_send_json_error(array('message' => __('Failed to update item.', 'qdr-temporary-shared-storage')));
- }
-
- // Get updated record
- $updated_item = $this->db_manager->get_item($id);
-
- wp_send_json_success(array(
- 'item' => $updated_item,
- 'message' => __('Item updated successfully.', 'qdr-temporary-shared-storage')
- ));
- }
- /**
- * AJAX handler for deleting an item.
- *
- * @since 1.0.0
- */
- public function ajax_delete_item() {
- // Check nonce
- if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
- wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
- }
-
- // Check required parameters
- if (!isset($_POST['id']) || empty($_POST['id'])) {
- wp_send_json_error(array('message' => __('Missing item ID.', 'qdr-temporary-shared-storage')));
- }
-
- $id = intval($_POST['id']);
-
- // Get the item
- $item = $this->db_manager->get_item($id);
- if (!$item) {
- wp_send_json_error(array('message' => __('Item not found.', 'qdr-temporary-shared-storage')));
- }
-
- // Delete file
- $this->file_manager->delete_file($item->file_path);
-
- // Delete from database
- $result = $this->db_manager->delete_item($id);
-
- if ($result === false) {
- wp_send_json_error(array('message' => __('Failed to delete item.', 'qdr-temporary-shared-storage')));
- }
-
- wp_send_json_success(array(
- 'message' => __('Item deleted successfully.', 'qdr-temporary-shared-storage')
- ));
- }
- /**
- * AJAX handler for getting item details.
- *
- * @since 1.0.0
- */
- public function ajax_get_item_details() {
- // Check nonce
- if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
- wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
- }
-
- // Check required parameters
- if (!isset($_POST['id']) || empty($_POST['id'])) {
- wp_send_json_error(array('message' => __('Missing item ID.', 'qdr-temporary-shared-storage')));
- }
-
- $id = intval($_POST['id']);
-
- // Get the item
- $item = $this->db_manager->get_item($id);
- if (!$item) {
- wp_send_json_error(array('message' => __('Item not found.', 'qdr-temporary-shared-storage')));
- }
-
- // Add permalink and shortcode
- $item->permalink = site_url('shared-file-download/' . $item->media_id);
- $item->shortcode = '[qdr_tss_download id="' . $item->media_id . '"]';
-
- // Remove password from response
- unset($item->password);
-
- wp_send_json_success(array(
- 'item' => $item
- ));
- }
- }
|