| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- /**
- * Admin Settings Page
- *
- * @package QDR_Temporary_Shared_Storage
- */
- // If this file is called directly, abort.
- if (!defined('WPINC')) {
- die;
- }
- /**
- * Render the Settings | Shared Storage admin page
- */
- function qdr_tss_render_settings_page() {
- // Get current settings
- $settings = get_option('qdr_tss_settings', array(
- 'api_key' => '',
- 'media_category' => 'shared-storage',
- 'max_upload_size' => 1073741824, // 1GB default
- ));
-
- // Get total storage usage
- global $wpdb;
- $table_name = $wpdb->prefix . QDR_TSS_PLUGIN_TABLE;
- $total_size = $wpdb->get_var("SELECT SUM(file_size) FROM $table_name");
- $total_size = $total_size ? intval($total_size) : 0;
- ?>
-
- <div class="wrap qdr-tss-admin">
- <h1><?php esc_html_e('Shared Storage Settings', 'qdr-temporary-shared-storage'); ?></h1>
-
- <form id="qdr-tss-settings-form" method="post">
- <table class="form-table">
- <tbody>
- <tr>
- <th scope="row">
- <label for="qdr-tss-api-key"><?php esc_html_e('API Key', 'qdr-temporary-shared-storage'); ?></label>
- </th>
- <td>
- <input type="text" id="qdr-tss-api-key" name="api_key" value="<?php echo esc_attr($settings['api_key']); ?>" class="regular-text">
- <p class="description"><?php esc_html_e('API key for REST API authentication.', 'qdr-temporary-shared-storage'); ?></p>
- <button type="button" id="qdr-tss-generate-key" class="button"><?php esc_html_e('Generate New Key', 'qdr-temporary-shared-storage'); ?></button>
- </td>
- </tr>
-
- <tr>
- <th scope="row">
- <label for="qdr-tss-media-category"><?php esc_html_e('Media Category', 'qdr-temporary-shared-storage'); ?></label>
- </th>
- <td>
- <input type="text" id="qdr-tss-media-category" name="media_category" value="<?php echo esc_attr($settings['media_category']); ?>" class="regular-text">
- <p class="description"><?php esc_html_e('Category to assign to uploaded media.', 'qdr-temporary-shared-storage'); ?></p>
- </td>
- </tr>
-
- <tr>
- <th scope="row">
- <label for="qdr-tss-max-upload-size"><?php esc_html_e('Maximum Storage Size (bytes)', 'qdr-temporary-shared-storage'); ?></label>
- </th>
- <td>
- <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">
- <p class="description">
- <?php esc_html_e('Maximum total storage size in bytes. Current usage:', 'qdr-temporary-shared-storage'); ?>
- <span id="qdr-tss-current-usage"><?php echo esc_html(qdr_tss_format_bytes($total_size)); ?></span>
- (<?php echo esc_html(qdr_tss_format_bytes($settings['max_upload_size'])); ?> <?php esc_html_e('max', 'qdr-temporary-shared-storage'); ?>)
- </p>
- <div class="qdr-tss-storage-bar">
- <div class="qdr-tss-storage-used" style="width: <?php echo min(100, ($total_size / $settings['max_upload_size']) * 100); ?>%"></div>
- </div>
-
- <p class="description">
- <?php esc_html_e('Common sizes:', 'qdr-temporary-shared-storage'); ?><br>
- 1 GB = 1073741824 <?php esc_html_e('bytes', 'qdr-temporary-shared-storage'); ?><br>
- 5 GB = 5368709120 <?php esc_html_e('bytes', 'qdr-temporary-shared-storage'); ?><br>
- 10 GB = 10737418240 <?php esc_html_e('bytes', 'qdr-temporary-shared-storage'); ?>
- </p>
- </td>
- </tr>
- </tbody>
- </table>
-
- <p class="submit">
- <button type="submit" id="qdr-tss-save-settings" class="button button-primary"><?php esc_html_e('Save Settings', 'qdr-temporary-shared-storage'); ?></button>
- <button type="button" id="qdr-tss-purge-expired" class="button"><?php esc_html_e('Purge Expired Files', 'qdr-temporary-shared-storage'); ?></button>
- </p>
- </form>
- </div>
-
- <script type="text/javascript">
- jQuery(document).ready(function($) {
- // Generate new API key
- $('#qdr-tss-generate-key').on('click', function(e) {
- e.preventDefault();
-
- // Generate a random string for API key
- var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- var apiKey = '';
- for (var i = 0; i < 32; i++) {
- apiKey += chars.charAt(Math.floor(Math.random() * chars.length));
- }
-
- $('#qdr-tss-api-key').val(apiKey);
- });
-
- // Save settings
- $('#qdr-tss-settings-form').on('submit', function(e) {
- e.preventDefault();
-
- var data = {
- action: 'qdr_tss_save_settings',
- nonce: qdr_tss.nonce,
- api_key: $('#qdr-tss-api-key').val(),
- media_category: $('#qdr-tss-media-category').val(),
- max_upload_size: $('#qdr-tss-max-upload-size').val()
- };
-
- $.post(ajaxurl, data, function(response) {
- if (response.success) {
- alert(response.data.message);
- } else {
- alert(response.data.message);
- }
- });
- });
-
- // Purge expired files
- $('#qdr-tss-purge-expired').on('click', function(e) {
- e.preventDefault();
-
- if (!confirm(qdr_tss.strings.confirm_purge)) {
- return;
- }
-
- var data = {
- action: 'qdr_tss_purge_expired',
- nonce: qdr_tss.nonce
- };
-
- $.post(ajaxurl, data, function(response) {
- if (response.success) {
- alert(response.data.message);
- // Reload page to update storage usage
- location.reload();
- } else {
- alert(response.data.message);
- }
- });
- });
- });
- </script>
- <?php
- }
- /**
- * Format bytes to human readable format
- *
- * @param int $bytes
- * @param int $precision
- * @return string
- */
- function qdr_tss_format_bytes($bytes, $precision = 2) {
- $units = array('B', 'KB', 'MB', 'GB', 'TB');
-
- $bytes = max($bytes, 0);
- $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
- $pow = min($pow, count($units) - 1);
-
- $bytes /= pow(1024, $pow);
-
- return round($bytes, $precision) . ' ' . $units[$pow];
- }
|