| @@ -1,17 +1,29 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -/** | |
| 4 | - * | |
| 5 | - * This file runs when the plugin in uninstalled (deleted). | |
| 6 | - * This will not run when the plugin is deactivated. | |
| 7 | - * Ideally you will add all your clean-up scripts here | |
| 8 | - * that will clean-up unused meta, options, etc. in the database. | |
| 9 | - * | |
| 10 | - */ | |
| 3 | +defined('WP_UNINSTALL_PLUGIN') || exit; | |
| 11 | 4 | |
| 12 | -// If plugin is not being uninstalled, exit (do nothing) | |
| 13 | -if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { | |
| 14 | - exit; | |
| 5 | +require_once __DIR__ . '/src/SettingName.php'; | |
| 6 | + | |
| 7 | +$deleteOptions = static function (): void { | |
| 8 | + foreach (SimpleAnalytics\SettingName::cases() as $key) { | |
| 9 | + delete_option($key); | |
| 10 | + } | |
| 11 | +}; | |
| 12 | + | |
| 13 | +if (is_multisite()) { | |
| 14 | + $offset = 0; | |
| 15 | + do { | |
| 16 | + $siteIds = get_sites(['fields' => 'ids', 'number' => 100, 'offset' => $offset, 'orderby' => 'id', 'order' => 'ASC']); | |
| 17 | + foreach ($siteIds as $siteId) { | |
| 18 | + switch_to_blog($siteId); | |
| 19 | + try { | |
| 20 | + $deleteOptions(); | |
| 21 | + } finally { | |
| 22 | + restore_current_blog(); | |
| 23 | + } | |
| 24 | + } | |
| 25 | + $offset += count($siteIds); | |
| 26 | + } while (count($siteIds) === 100); | |
| 27 | +} else { | |
| 28 | + $deleteOptions(); | |
| 15 | 29 | } |
| 16 | - | |
| 17 | -// Do something here if plugin is being uninstalled. | |