class-qdr-tss-settings.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Settings operations for the plugin.
  4. *
  5. * This class handles all settings operations, including getting and setting
  6. * plugin options.
  7. *
  8. * @package QDR_Temporary_Shared_Storage
  9. * @since 1.0.0
  10. */
  11. class QDR_TSS_Settings {
  12. /**
  13. * The single instance of the class.
  14. *
  15. * @var QDR_TSS_Settings
  16. * @since 1.0.0
  17. */
  18. protected static $_instance = null;
  19. /**
  20. * Option name for settings.
  21. *
  22. * @var string
  23. * @since 1.0.0
  24. */
  25. private $option_name = 'qdr_tss_settings';
  26. /**
  27. * Default settings.
  28. *
  29. * @var array
  30. * @since 1.0.0
  31. */
  32. private $defaults = array(
  33. 'api_key' => '',
  34. 'media_category' => 'shared-storage',
  35. 'max_upload_size' => 1073741824, // 1GB default
  36. );
  37. /**
  38. * Current settings.
  39. *
  40. * @var array
  41. * @since 1.0.0
  42. */
  43. private $settings = array();
  44. /**
  45. * Main QDR_TSS_Settings Instance.
  46. *
  47. * Ensures only one instance of QDR_TSS_Settings is loaded or can be loaded.
  48. *
  49. * @since 1.0.0
  50. * @static
  51. * @return QDR_TSS_Settings - Main instance.
  52. */
  53. public static function instance() {
  54. if (is_null(self::$_instance)) {
  55. self::$_instance = new self();
  56. }
  57. return self::$_instance;
  58. }
  59. /**
  60. * QDR_TSS_Settings Constructor.
  61. */
  62. public function __construct() {
  63. $this->load_settings();
  64. }
  65. /**
  66. * Load settings from database.
  67. *
  68. * @since 1.0.0
  69. */
  70. public function load_settings() {
  71. $settings = get_option($this->option_name, array());
  72. // Generate API key if it doesn't exist
  73. if (empty($settings['api_key'])) {
  74. $settings['api_key'] = $this->generate_api_key();
  75. }
  76. $this->settings = wp_parse_args($settings, $this->defaults);
  77. }
  78. /**
  79. * Get all settings.
  80. *
  81. * @since 1.0.0
  82. * @return array The settings.
  83. */
  84. public function get_settings() {
  85. return $this->settings;
  86. }
  87. /**
  88. * Get a specific setting.
  89. *
  90. * @since 1.0.0
  91. * @param string $key The setting key.
  92. * @param mixed $default The default value if setting doesn't exist.
  93. * @return mixed The setting value.
  94. */
  95. public function get_setting($key, $default = null) {
  96. if (isset($this->settings[$key])) {
  97. return $this->settings[$key];
  98. }
  99. return $default !== null ? $default : (isset($this->defaults[$key]) ? $this->defaults[$key] : null);
  100. }
  101. /**
  102. * Update settings.
  103. *
  104. * @since 1.0.0
  105. * @param array $settings The settings to update.
  106. * @return bool True on success, false on failure.
  107. */
  108. public function update_settings($settings) {
  109. // Merge with existing settings
  110. $this->settings = wp_parse_args($settings, $this->settings);
  111. // Save to database
  112. $result = update_option($this->option_name, $this->settings);
  113. return $result;
  114. }
  115. /**
  116. * Update a specific setting.
  117. *
  118. * @since 1.0.0
  119. * @param string $key The setting key.
  120. * @param mixed $value The setting value.
  121. * @return bool True on success, false on failure.
  122. */
  123. public function update_setting($key, $value) {
  124. $this->settings[$key] = $value;
  125. // Save to database
  126. return update_option($this->option_name, $this->settings);
  127. }
  128. /**
  129. * Generate a random API key.
  130. *
  131. * @since 1.0.0
  132. * @return string The generated API key.
  133. */
  134. public function generate_api_key() {
  135. return wp_generate_password(32, false);
  136. }
  137. /**
  138. * Validate API key.
  139. *
  140. * @since 1.0.0
  141. * @param string $api_key The API key to validate.
  142. * @return bool True if valid, false otherwise.
  143. */
  144. public function validate_api_key($api_key) {
  145. return $api_key === $this->get_setting('api_key');
  146. }
  147. /**
  148. * Format bytes to human readable format.
  149. *
  150. * @since 1.0.0
  151. * @param int $bytes The size in bytes.
  152. * @param int $precision The number of decimal places.
  153. * @return string Human readable size.
  154. */
  155. public function format_bytes($bytes, $precision = 2) {
  156. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  157. $bytes = max($bytes, 0);
  158. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  159. $pow = min($pow, count($units) - 1);
  160. $bytes /= pow(1024, $pow);
  161. return round($bytes, $precision) . ' ' . $units[$pow];
  162. }
  163. }