| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- /**
- * Settings operations for the plugin.
- *
- * This class handles all settings operations, including getting and setting
- * plugin options.
- *
- * @package QDR_Temporary_Shared_Storage
- * @since 1.0.0
- */
- class QDR_TSS_Settings {
- /**
- * The single instance of the class.
- *
- * @var QDR_TSS_Settings
- * @since 1.0.0
- */
- protected static $_instance = null;
- /**
- * Option name for settings.
- *
- * @var string
- * @since 1.0.0
- */
- private $option_name = 'qdr_tss_settings';
- /**
- * Default settings.
- *
- * @var array
- * @since 1.0.0
- */
- private $defaults = array(
- 'api_key' => '',
- '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];
- }
- }
|