Skip to content

Commit

Permalink
clean settings after migrations are run
Browse files Browse the repository at this point in the history
  • Loading branch information
pryley committed Nov 1, 2024
1 parent ae2877d commit 6646cfe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 10 additions & 0 deletions plugin/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public function onInit(): void
glsr(OptionManager::class)->updateVersion();
}

/**
* @action site-reviews/migration/end
*/
public function onMigrationEnd(): void
{
$settings = glsr(OptionManager::class)->wp(OptionManager::databaseKey(), []);
$settings = glsr(OptionManager::class)->clean($settings);
update_option(OptionManager::databaseKey(), $settings, true);
}

/**
* @action plugins_loaded
*/
Expand Down
19 changes: 16 additions & 3 deletions plugin/Database/OptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public function all(): array
return $settings;
}

public function clean(array $data = []): array
{
$settings = $this->kses($data);
if (!empty(glsr()->settings)) { // access the property directly to prevent an infinite loop
$settings = $savedSettings;
$defaults = glsr()->defaults(); // @phpstan-ignore-line
$defaults = Arr::flatten($defaults);
$settings = Arr::flatten($settings);
$settings = shortcode_atts($defaults, $settings);
$settings = Arr::unflatten($settings);
$settings = $this->restoreOrphanedSettings($settings, $savedSettings);
}
return $settings;
}

public static function databaseKey(?int $version = null): string
{
$versions = static::databaseKeys();
Expand Down Expand Up @@ -135,13 +150,11 @@ public function normalize(array $data = []): array
{
$settings = $this->kses($data);
if (!empty(glsr()->settings)) { // access the property directly to prevent an infinite loop
$savedSettings = $settings;
$defaults = glsr()->defaults(); // @phpstan-ignore-line
$defaults = Arr::flatten($defaults);
$settings = Arr::flatten($settings);
$settings = shortcode_atts($defaults, $settings);
$settings = wp_parse_args($settings, $defaults);
$settings = Arr::unflatten($settings);
$settings = $this->restoreOrphanedSettings($settings, $savedSettings);
}
return $settings;
}
Expand Down
1 change: 1 addition & 0 deletions plugin/Hooks/MainHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function run(): void
['logOnce', 'admin_footer'],
['logOnce', 'wp_footer'],
['onInit', 'init', 5], // run after possible init:1 migration
['onMigrationEnd', 'site-reviews/migration/end'],
['registerAddons', 'plugins_loaded'],
['registerLanguages', 'after_setup_theme'],
['registerPostMeta', 'init'],
Expand Down

0 comments on commit 6646cfe

Please sign in to comment.