Skip to content

Commit 1e5af95

Browse files
committed
Fix default values
1 parent 389ef8b commit 1e5af95

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

src/Plugin.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function onBoot(): void
2222
#[\Override]
2323
public function onActivation(): void
2424
{
25-
$this->registerOptions();
25+
$this->addOptions();
2626
}
2727

2828
#[\Override]
@@ -52,9 +52,17 @@ public function onInit(): void
5252
* to preload them on each page and avoid
5353
* extra database queries.
5454
*/
55-
protected function registerOptions(): void
55+
protected function addOptions(): void
5656
{
57-
foreach (SettingName::cases() as $name) {
57+
// Add options with default values
58+
add_option(SettingName::ENABLED, '1', null, true);
59+
60+
// Autoload the option introduced in a previous release.
61+
update_option(SettingName::CUSTOM_DOMAIN, get_option(SettingName::CUSTOM_DOMAIN), true);
62+
63+
// Add all remaining options
64+
$optionNames = array_diff(SettingName::cases(), [SettingName::ENABLED]);
65+
foreach ($optionNames as $name) {
5866
add_option($name, null, null, true);
5967
}
6068
}
@@ -94,8 +102,7 @@ protected function defineAdminPage(): void
94102
->docs('https://docs.simpleanalytics.com/bypass-ad-blockers');
95103

96104
$tab->checkbox(SettingName::ENABLED, 'Enabled')
97-
->description('Enable or disable Simple Analytics on your website.')
98-
->default(true);
105+
->description('Enable or disable Simple Analytics on your website.');
99106
})
100107
->tab('Ignore Rules', function (Tab $tab) {
101108
$tab->icon(get_icon('eye-slash'));
@@ -130,8 +137,7 @@ protected function defineAdminPage(): void
130137
->docs('https://docs.simpleanalytics.com/trigger-custom-page-views#use-custom-collection-anyway');
131138

132139
$tab->checkbox(SettingName::NOSCRIPT, 'Support no JavaScript mode')
133-
->description('Collect analytics from visitors with disabled or no JavaScript.')
134-
->default(false);
140+
->description('Collect analytics from visitors with disabled or no JavaScript.');
135141

136142
$tab->input(SettingName::ONLOAD_CALLBACK, 'Onload Callback')
137143
->description('JavaScript function to call when the script is loaded.')

src/Setting.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ public static function get(string $key, $default = null): mixed
1515
return $value;
1616
}
1717

18-
public static function boolean(string $key, bool $default = false): bool
18+
public static function boolean(string $key, ?bool $default = null): ?bool
1919
{
20-
return (bool)self::get($key, $default);
20+
$value = get_option($key);
21+
22+
if (empty($value)) {
23+
return $default;
24+
}
25+
26+
return (bool)$value;
2127
}
2228

2329
public static function array(string $key): array

src/Settings/Blocks/Fields/Field.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ abstract class Field implements Block
1010

1111
protected string $label;
1212

13-
protected mixed $default = null;
14-
1513
public function __construct(string $key, string $label)
1614
{
1715
$this->key = $key;
@@ -33,16 +31,4 @@ abstract public function getValueSanitizer(): callable;
3331
abstract public function getValueType(): string;
3432

3533
abstract public function render(): void;
36-
37-
public function default(mixed $default): static
38-
{
39-
$this->default = $default;
40-
41-
return $this;
42-
}
43-
44-
public function getDefaultValue(): mixed
45-
{
46-
return $this->default;
47-
}
4834
}

src/Settings/Concerns/WordPressPageIntegration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ protected function registerField(Field $field, Tab $tab): void
5555
$field->getKey(),
5656
[
5757
'type' => $field->getValueType(),
58-
'default' => $field->getDefaultValue(),
5958
'sanitize_callback' => $field->getValueSanitizer(),
6059
]
6160
);

0 commit comments

Comments
 (0)