class-qdr-tss-settings-page.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Settings Page functionality of the plugin.
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. * @since 1.0.0
  7. */
  8. class QDR_TSS_Settings_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 settings instance.
  27. *
  28. * @since 1.0.0
  29. * @access private
  30. * @var QDR_TSS_Settings $settings The settings instance.
  31. */
  32. private $settings;
  33. /**
  34. * The database manager instance.
  35. *
  36. * @since 1.0.0
  37. * @access private
  38. * @var QDR_TSS_DB_Manager $db_manager The database manager instance.
  39. */
  40. private $db_manager;
  41. /**
  42. * The file manager instance.
  43. *
  44. * @since 1.0.0
  45. * @access private
  46. * @var QDR_TSS_File_Manager $file_manager The file manager instance.
  47. */
  48. private $file_manager;
  49. /**
  50. * The cron manager instance.
  51. *
  52. * @since 1.0.0
  53. * @access private
  54. * @var QDR_TSS_Cron_Manager $cron_manager The cron manager instance.
  55. */
  56. private $cron_manager;
  57. /**
  58. * Initialize the class and set its properties.
  59. *
  60. * @since 1.0.0
  61. * @param string $plugin_name The name of this plugin.
  62. * @param string $version The version of this plugin.
  63. */
  64. public function __construct($plugin_name, $version) {
  65. $this->plugin_name = $plugin_name;
  66. $this->version = $version;
  67. $this->settings = QDR_TSS_Settings::instance();
  68. $this->db_manager = QDR_TSS_DB_Manager::instance();
  69. $this->file_manager = QDR_TSS_File_Manager::instance();
  70. $this->cron_manager = new QDR_TSS_Cron_Manager($plugin_name, $version);
  71. }
  72. /**
  73. * AJAX handler for saving settings.
  74. *
  75. * @since 1.0.0
  76. */
  77. public function ajax_save_settings() {
  78. // Check nonce
  79. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
  80. wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
  81. }
  82. // Get settings
  83. $api_key = isset($_POST['api_key']) ? sanitize_text_field($_POST['api_key']) : '';
  84. $media_category = isset($_POST['media_category']) ? sanitize_text_field($_POST['media_category']) : 'shared-storage';
  85. $max_upload_size = isset($_POST['max_upload_size']) ? intval($_POST['max_upload_size']) : 1073741824; // 1GB default
  86. // Update settings
  87. $this->settings->update_settings(array(
  88. 'api_key' => $api_key,
  89. 'media_category' => $media_category,
  90. 'max_upload_size' => $max_upload_size
  91. ));
  92. wp_send_json_success(array(
  93. 'message' => __('Settings saved successfully.', 'qdr-temporary-shared-storage')
  94. ));
  95. }
  96. /**
  97. * AJAX handler for purging expired files.
  98. *
  99. * @since 1.0.0
  100. */
  101. public function ajax_purge_expired() {
  102. // Check nonce
  103. if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'qdr_tss_nonce')) {
  104. wp_send_json_error(array('message' => __('Security check failed.', 'qdr-temporary-shared-storage')));
  105. }
  106. // Purge expired files
  107. $purged_count = $this->cron_manager->manual_purge_expired_files();
  108. // Translators: %d is the number of purged files
  109. $message = sprintf(
  110. _n(
  111. '%d expired file has been purged.',
  112. '%d expired files have been purged.',
  113. $purged_count,
  114. 'qdr-temporary-shared-storage'
  115. ),
  116. $purged_count
  117. );
  118. wp_send_json_success(array(
  119. 'message' => $message,
  120. 'purged_count' => $purged_count
  121. ));
  122. }
  123. }