options = $options; } /** * Get a certain key from options * * @param string $key * @param mixed|null $default * @return mixed */ public function get(string $key, $default = null) { return Arr::ternary($this->options, $key, $default); } /** * Merge new data into current settings * * @param mixed[] $data * @return GeneralSettings */ public function merge(array $data): GeneralSettings { $this->options = array_merge($this->options, $data); return $this; } /** * Return all options as array * * @return mixed[] */ public function all(): array { return $this->options; } /** * Get options of all or a certain module * * @param string|null $key * @return mixed[] */ public function module(?string $key = null): array { // Get All Modules $options = $this->get('modules', []); // Force to Array if (Arr::not($options)) $options = []; // Looking for Specific Module if ($key) return Arr::ternary($options, $key, []); // Return all Modules return $options; } /** * Currency * * @return string */ public function currency(): string { return $this->get('currency', 'USD'); } public function primaryColor(): string { return $this->primaryColorOverride() ?: self::DEFAULT_PRIMARY_COLOR; } public function primaryColorOverride(): string { $color = $this->get('primary_color', ''); if (!is_string($color) || !self::isValidPrimaryColor($color)) return ''; return $color; } public static function isValidPrimaryColor(string $color): bool { if (!sanitize_hex_color($color)) return false; $hex = substr($color, 1); if (strlen($hex) === 3) $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; $channels = []; foreach (str_split($hex, 2) as $channel) { $value = hexdec($channel) / 255; $channels[] = $value <= 0.04045 ? $value / 12.92 : pow(($value + 0.055) / 1.055, 2.4); } $luminance = 0.2126 * $channels[0] + 0.7152 * $channels[1] + 0.0722 * $channels[2]; return 1.05 / ($luminance + 0.05) >= 4.5; } /** * Currency Sign Position * * @return string */ public function currencyPosition(): string { return $this->get('currency_position', Currencies::POSITION_BEFORE); } /** * Midnight Hour * * @return int */ public function midnight(): int { return (int) $this->get('midnight', 12); } public function deleteDataOnUninstall(): bool { return (int) $this->get(DataRetention::SETTING_KEY, 0) === 1; } /** * Date Format * * @return string */ public function dateFormat(): string { return $this->get('date_format', DateFormat::wp()); } /** * Time Format * * @return string */ public function timeFormat(): string { return $this->get('time_format', TimeFormat::wp()); } /** * Date & Time Format * * @return string */ public function datetimeFormat(): string { return $this->dateFormat().' '.$this->timeFormat(); } /** * @return mixed[] */ public function rangeFormat(): array { return $this->get('range_format', [ 'hour' => 'F1 j1, H1:i1 - i2', 'day' => 'F1 j1, H1:i1 - H2:i2', 'month' => 'F1 j1 - j2, H1:i1 - H2:i2', 'year' => 'F1 j1 - F2 j2, H1:i1 - H2:i2', 'other' => 'F1 j1, Y1 H1:i1 - F2 j2, Y2 H2:i2', ]); } }