'', 'media_category' => 'shared-storage', 'max_upload_size' => 1073741824, // 1GB default ); /** * Current settings. * * @var array * @since 1.0.0 */ private $settings = array(); /** * Main QDR_TSS_Settings Instance. * * Ensures only one instance of QDR_TSS_Settings is loaded or can be loaded. * * @since 1.0.0 * @static * @return QDR_TSS_Settings - Main instance. */ public static function instance() { if (is_null(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } /** * QDR_TSS_Settings Constructor. */ public function __construct() { $this->load_settings(); } /** * Load settings from database. * * @since 1.0.0 */ public function load_settings() { $settings = get_option($this->option_name, array()); // Generate API key if it doesn't exist if (empty($settings['api_key'])) { $settings['api_key'] = $this->generate_api_key(); } $this->settings = wp_parse_args($settings, $this->defaults); } /** * Get all settings. * * @since 1.0.0 * @return array The settings. */ public function get_settings() { return $this->settings; } /** * Get a specific setting. * * @since 1.0.0 * @param string $key The setting key. * @param mixed $default The default value if setting doesn't exist. * @return mixed The setting value. */ public function get_setting($key, $default = null) { if (isset($this->settings[$key])) { return $this->settings[$key]; } return $default !== null ? $default : (isset($this->defaults[$key]) ? $this->defaults[$key] : null); } /** * Update settings. * * @since 1.0.0 * @param array $settings The settings to update. * @return bool True on success, false on failure. */ public function update_settings($settings) { // Merge with existing settings $this->settings = wp_parse_args($settings, $this->settings); // Save to database $result = update_option($this->option_name, $this->settings); return $result; } /** * Update a specific setting. * * @since 1.0.0 * @param string $key The setting key. * @param mixed $value The setting value. * @return bool True on success, false on failure. */ public function update_setting($key, $value) { $this->settings[$key] = $value; // Save to database return update_option($this->option_name, $this->settings); } /** * Generate a random API key. * * @since 1.0.0 * @return string The generated API key. */ public function generate_api_key() { return wp_generate_password(32, false); } /** * Validate API key. * * @since 1.0.0 * @param string $api_key The API key to validate. * @return bool True if valid, false otherwise. */ public function validate_api_key($api_key) { return $api_key === $this->get_setting('api_key'); } /** * Format bytes to human readable format. * * @since 1.0.0 * @param int $bytes The size in bytes. * @param int $precision The number of decimal places. * @return string Human readable size. */ public function 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]; } }