settings-page.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Admin Settings Page
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. */
  7. // If this file is called directly, abort.
  8. if (!defined('WPINC')) {
  9. die;
  10. }
  11. /**
  12. * Render the Settings | Shared Storage admin page
  13. */
  14. function qdr_tss_render_settings_page() {
  15. // Get current settings
  16. $settings = get_option('qdr_tss_settings', array(
  17. 'api_key' => '',
  18. 'media_category' => 'shared-storage',
  19. 'max_upload_size' => 1073741824, // 1GB default
  20. ));
  21. // Get total storage usage
  22. global $wpdb;
  23. $table_name = $wpdb->prefix . QDR_TSS_PLUGIN_TABLE;
  24. $total_size = $wpdb->get_var("SELECT SUM(file_size) FROM $table_name");
  25. $total_size = $total_size ? intval($total_size) : 0;
  26. ?>
  27. <div class="wrap qdr-tss-admin">
  28. <h1><?php esc_html_e('Shared Storage Settings', 'qdr-temporary-shared-storage'); ?></h1>
  29. <form id="qdr-tss-settings-form" method="post">
  30. <table class="form-table">
  31. <tbody>
  32. <tr>
  33. <th scope="row">
  34. <label for="qdr-tss-api-key"><?php esc_html_e('API Key', 'qdr-temporary-shared-storage'); ?></label>
  35. </th>
  36. <td>
  37. <input type="text" id="qdr-tss-api-key" name="api_key" value="<?php echo esc_attr($settings['api_key']); ?>" class="regular-text">
  38. <p class="description"><?php esc_html_e('API key for REST API authentication.', 'qdr-temporary-shared-storage'); ?></p>
  39. <button type="button" id="qdr-tss-generate-key" class="button"><?php esc_html_e('Generate New Key', 'qdr-temporary-shared-storage'); ?></button>
  40. </td>
  41. </tr>
  42. <tr>
  43. <th scope="row">
  44. <label for="qdr-tss-media-category"><?php esc_html_e('Media Category', 'qdr-temporary-shared-storage'); ?></label>
  45. </th>
  46. <td>
  47. <input type="text" id="qdr-tss-media-category" name="media_category" value="<?php echo esc_attr($settings['media_category']); ?>" class="regular-text">
  48. <p class="description"><?php esc_html_e('Category to assign to uploaded media.', 'qdr-temporary-shared-storage'); ?></p>
  49. </td>
  50. </tr>
  51. <tr>
  52. <th scope="row">
  53. <label for="qdr-tss-max-upload-size"><?php esc_html_e('Maximum Storage Size (bytes)', 'qdr-temporary-shared-storage'); ?></label>
  54. </th>
  55. <td>
  56. <input type="number" id="qdr-tss-max-upload-size" name="max_upload_size" value="<?php echo esc_attr($settings['max_upload_size']); ?>" class="regular-text">
  57. <p class="description">
  58. <?php esc_html_e('Maximum total storage size in bytes. Current usage:', 'qdr-temporary-shared-storage'); ?>
  59. <span id="qdr-tss-current-usage"><?php echo esc_html(qdr_tss_format_bytes($total_size)); ?></span>
  60. (<?php echo esc_html(qdr_tss_format_bytes($settings['max_upload_size'])); ?> <?php esc_html_e('max', 'qdr-temporary-shared-storage'); ?>)
  61. </p>
  62. <div class="qdr-tss-storage-bar">
  63. <div class="qdr-tss-storage-used" style="width: <?php echo min(100, ($total_size / $settings['max_upload_size']) * 100); ?>%"></div>
  64. </div>
  65. <p class="description">
  66. <?php esc_html_e('Common sizes:', 'qdr-temporary-shared-storage'); ?><br>
  67. 1 GB = 1073741824 <?php esc_html_e('bytes', 'qdr-temporary-shared-storage'); ?><br>
  68. 5 GB = 5368709120 <?php esc_html_e('bytes', 'qdr-temporary-shared-storage'); ?><br>
  69. 10 GB = 10737418240 <?php esc_html_e('bytes', 'qdr-temporary-shared-storage'); ?>
  70. </p>
  71. </td>
  72. </tr>
  73. </tbody>
  74. </table>
  75. <p class="submit">
  76. <button type="submit" id="qdr-tss-save-settings" class="button button-primary"><?php esc_html_e('Save Settings', 'qdr-temporary-shared-storage'); ?></button>
  77. <button type="button" id="qdr-tss-purge-expired" class="button"><?php esc_html_e('Purge Expired Files', 'qdr-temporary-shared-storage'); ?></button>
  78. </p>
  79. </form>
  80. </div>
  81. <script type="text/javascript">
  82. jQuery(document).ready(function($) {
  83. // Generate new API key
  84. $('#qdr-tss-generate-key').on('click', function(e) {
  85. e.preventDefault();
  86. // Generate a random string for API key
  87. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  88. var apiKey = '';
  89. for (var i = 0; i < 32; i++) {
  90. apiKey += chars.charAt(Math.floor(Math.random() * chars.length));
  91. }
  92. $('#qdr-tss-api-key').val(apiKey);
  93. });
  94. // Save settings
  95. $('#qdr-tss-settings-form').on('submit', function(e) {
  96. e.preventDefault();
  97. var data = {
  98. action: 'qdr_tss_save_settings',
  99. nonce: qdr_tss.nonce,
  100. api_key: $('#qdr-tss-api-key').val(),
  101. media_category: $('#qdr-tss-media-category').val(),
  102. max_upload_size: $('#qdr-tss-max-upload-size').val()
  103. };
  104. $.post(ajaxurl, data, function(response) {
  105. if (response.success) {
  106. alert(response.data.message);
  107. } else {
  108. alert(response.data.message);
  109. }
  110. });
  111. });
  112. // Purge expired files
  113. $('#qdr-tss-purge-expired').on('click', function(e) {
  114. e.preventDefault();
  115. if (!confirm(qdr_tss.strings.confirm_purge)) {
  116. return;
  117. }
  118. var data = {
  119. action: 'qdr_tss_purge_expired',
  120. nonce: qdr_tss.nonce
  121. };
  122. $.post(ajaxurl, data, function(response) {
  123. if (response.success) {
  124. alert(response.data.message);
  125. // Reload page to update storage usage
  126. location.reload();
  127. } else {
  128. alert(response.data.message);
  129. }
  130. });
  131. });
  132. });
  133. </script>
  134. <?php
  135. }
  136. /**
  137. * Format bytes to human readable format
  138. *
  139. * @param int $bytes
  140. * @param int $precision
  141. * @return string
  142. */
  143. function qdr_tss_format_bytes($bytes, $precision = 2) {
  144. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  145. $bytes = max($bytes, 0);
  146. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  147. $pow = min($pow, count($units) - 1);
  148. $bytes /= pow(1024, $pow);
  149. return round($bytes, $precision) . ' ' . $units[$pow];
  150. }