| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class ModuleSettings |
| 8 |
{ |
| 9 |
|
| 10 |
const MODULE_SETTINGS_OPTION = 'fluent_cart_modules_settings'; |
| 11 |
|
| 12 |
public static function isActive($moduleName = '') |
| 13 |
{ |
| 14 |
$allSettings = self::getAllSettings(); |
| 15 |
|
| 16 |
return Arr::get($allSettings, $moduleName . '.active') === 'yes'; |
| 17 |
} |
| 18 |
|
| 19 |
public static function fileds() |
| 20 |
{ |
| 21 |
return apply_filters('fluent_cart/module_setting/fields', [], []); |
| 22 |
} |
| 23 |
|
| 24 |
public static function saveSettings(array $data) |
| 25 |
{ |
| 26 |
return update_option(static::MODULE_SETTINGS_OPTION, $data, true); |
| 27 |
} |
| 28 |
|
| 29 |
public static function getAllSettings($cached = true) |
| 30 |
{ |
| 31 |
static $settings; |
| 32 |
if (isset($settings) && $cached) { |
| 33 |
return $settings; |
| 34 |
} |
| 35 |
|
| 36 |
$savedSettings = get_option(static::MODULE_SETTINGS_OPTION, []); |
| 37 |
|
| 38 |
if (!$savedSettings || !is_array($savedSettings)) { |
| 39 |
$savedSettings = []; |
| 40 |
} |
| 41 |
|
| 42 |
$defaults = apply_filters('fluent_cart/module_setting/default_values', [], []); |
| 43 |
|
| 44 |
foreach ($defaults as $key => $value) { |
| 45 |
if (!isset($savedSettings[$key])) { |
| 46 |
$savedSettings[$key] = $value; |
| 47 |
} else { |
| 48 |
$savedSettings[$key] = wp_parse_args(Arr::get($savedSettings, $key, []), $value); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$settings = $savedSettings; |
| 53 |
|
| 54 |
return $settings; |
| 55 |
} |
| 56 |
|
| 57 |
public static function getSettings($key = null) |
| 58 |
{ |
| 59 |
$settings = static::getAllSettings(); |
| 60 |
if ($key) { |
| 61 |
return Arr::get($settings, $key); |
| 62 |
} |
| 63 |
|
| 64 |
return $settings; |
| 65 |
} |
| 66 |
|
| 67 |
public static function validKeys(): array |
| 68 |
{ |
| 69 |
return array_keys(static::fileds()); |
| 70 |
} |
| 71 |
|
| 72 |
} |
| 73 |
|