refactor(Core): refactor and optimize code
This commit is contained in:
125
app/Core/Settings.php
Normal file
125
app/Core/Settings.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
namespace Sodino\Core;
|
||||
|
||||
/**
|
||||
* Settings Manager
|
||||
*/
|
||||
class Settings {
|
||||
private static $instance = null;
|
||||
private $settings = null;
|
||||
private $option_name = 'sodino_settings';
|
||||
|
||||
private $defaults = [
|
||||
'plugin_enabled' => 1,
|
||||
'pricing_enabled' => 1,
|
||||
'upsell_enabled' => 1,
|
||||
'banner_enabled' => 1,
|
||||
'allow_multiple_rules' => 0,
|
||||
'strategy' => 'priority',
|
||||
'max_discount_percent' => 100,
|
||||
'min_product_price' => 0,
|
||||
'ab_testing_enabled' => 0,
|
||||
'cart_pricing_enabled' => 1,
|
||||
'scheduled_campaigns_enabled' => 1,
|
||||
'cache_enabled' => 1,
|
||||
'cache_duration' => 3600,
|
||||
'debug_mode' => 0,
|
||||
];
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings
|
||||
*/
|
||||
public function all() {
|
||||
if ($this->settings === null) {
|
||||
$this->settings = wp_parse_args(
|
||||
get_option($this->option_name, []),
|
||||
$this->defaults
|
||||
);
|
||||
}
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get single setting
|
||||
*/
|
||||
public function get($key, $default = null) {
|
||||
$settings = $this->all();
|
||||
return $settings[$key] ?? $default ?? $this->defaults[$key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set single setting
|
||||
*/
|
||||
public function set($key, $value) {
|
||||
$settings = $this->all();
|
||||
$settings[$key] = $value;
|
||||
$this->settings = $settings;
|
||||
return update_option($this->option_name, $settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update multiple settings
|
||||
*/
|
||||
public function update(array $settings) {
|
||||
$current = $this->all();
|
||||
$this->settings = array_merge($current, $settings);
|
||||
return update_option($this->option_name, $this->settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset to defaults
|
||||
*/
|
||||
public function reset() {
|
||||
$this->settings = $this->defaults;
|
||||
return update_option($this->option_name, $this->defaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plugin is enabled
|
||||
*/
|
||||
public function isEnabled() {
|
||||
return (bool) $this->get('plugin_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if pricing is enabled
|
||||
*/
|
||||
public function isPricingEnabled() {
|
||||
return $this->isEnabled() && (bool) $this->get('pricing_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if upsell is enabled
|
||||
*/
|
||||
public function isUpsellEnabled() {
|
||||
return $this->isEnabled() && (bool) $this->get('upsell_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if banner is enabled
|
||||
*/
|
||||
public function isBannerEnabled() {
|
||||
return $this->isEnabled() && (bool) $this->get('banner_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if cache is enabled
|
||||
*/
|
||||
public function isCacheEnabled() {
|
||||
return (bool) $this->get('cache_enabled');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if debug mode is enabled
|
||||
*/
|
||||
public function isDebugMode() {
|
||||
return (bool) $this->get('debug_mode');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user