-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathuninstall.php
111 lines (95 loc) · 2.42 KB
/
uninstall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* Uninstall file.
*
* @package hcaptcha-wp
*/
use HCaptcha\Abstracts\LoginBase;
use HCaptcha\Migrations\Migrations;
use HCaptcha\Settings\PluginSettingsBase;
use KAGG\Settings\Abstracts\SettingsBase;
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
// @codeCoverageIgnoreStart
exit();
// @codeCoverageIgnoreEnd
}
/**
* Path to the plugin dir.
*/
if ( ! defined( 'HCAPTCHA_PATH' ) ) {
define( 'HCAPTCHA_PATH', __DIR__ );
}
/**
* Delete several options from 'sitemeta' table.
*
* @param array $options Options.
*
* @return void
*/
function hcap_delete_site_meta_options( array $options ): void {
foreach ( $options as $option ) {
delete_site_option( $option );
}
}
/**
* Delete several options from site 'options' table.
*
* @param array $options Options.
*
* @return void
*/
function hcap_delete_options( array $options ): void {
foreach ( $options as $option ) {
delete_option( $option );
}
}
/**
* Delete 'hcaptcha_events' table.
*
* @return void
*/
function hcap_delete_events_table(): void {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}hcaptcha_events`" );
}
/**
* Cleanup site data.
*
* @param array $options Options.
*
* @return void
*/
function hcap_cleanup_site_data( array $options ): void {
hcap_delete_options( $options );
hcap_delete_events_table();
}
/**
* Delete options from all sites.
*
* @return void
* @noinspection DisconnectedForeachInstructionInspection
*/
function hcap_cleanup_data(): void {
$settings = [ PluginSettingsBase::OPTION_NAME, PluginSettingsBase::OPTION_NAME . SettingsBase::NETWORK_WIDE ];
$other = [ LoginBase::LOGIN_DATA, Migrations::MIGRATED_VERSIONS_OPTION_NAME ];
$all_options = array_merge( $settings, $other );
if ( ! is_multisite() ) {
// If not multisite, just delete the option from the single site.
hcap_cleanup_site_data( $all_options );
return;
}
// Delete site meta options.
hcap_delete_site_meta_options( $settings );
// Get all sites.
$sites = get_sites();
foreach ( $sites as $site ) {
// Switch to each site and delete options.
switch_to_blog( (int) $site->blog_id );
hcap_cleanup_site_data( $all_options );
restore_current_blog();
}
}
require_once HCAPTCHA_PATH . '/vendor/autoload.php';
// Perform plugin cleanup tasks.
hcap_cleanup_data();