-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.php
More file actions
154 lines (129 loc) · 3.33 KB
/
uninstall.php
File metadata and controls
154 lines (129 loc) · 3.33 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* This will be executed when the plugin is uninstalled.
*
* @package OneAccess
*/
declare( strict_types=1 );
namespace OneAccess;
// If uninstall not called from WordPress, exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Multisite loop for uninstalling from all sites.
*/
function multisite_uninstall(): void {
if ( ! is_multisite() ) {
uninstall();
return;
}
delete_network_plugin_data();
$site_ids = get_sites(
[
'fields' => 'ids',
'number' => 0,
]
) ?: [];
foreach ( $site_ids as $site_id ) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog
if ( ! switch_to_blog( (int) $site_id ) ) {
continue;
}
uninstall();
restore_current_blog();
}
}
/**
* The (site-specific) uninstall function.
*/
function uninstall(): void {
remove_user_roles_caps();
delete_plugin_data();
}
/**
* Delete multisite network plugin data.
*/
function delete_network_plugin_data(): void {
$options = [
'oneaccess_multisite_governing_site',
];
foreach ( $options as $option ) {
delete_site_option( $option );
}
}
/**
* Deletes meta, options, transients, etc.
*/
function delete_plugin_data(): void {
// list of actions to be cleared on uninstall.
$actions_to_clear = [
'oneaccess_governing_site_configured',
'oneaccess_add_deduplicated_users',
];
// Clear scheduled actions.
if ( function_exists( 'as_unschedule_all_actions' ) ) {
foreach ( $actions_to_clear as $action ) {
as_unschedule_all_actions( $action );
}
}
// Options to clean up.
$options = [
'oneaccess_child_site_api_key',
'oneaccess_consumer_api_key',
'oneaccess_db_version',
'oneaccess_governing_site_url',
'oneaccess_new_users',
'oneaccess_parent_site_url',
'oneaccess_profile_update_requests',
'oneaccess_shared_sites',
'oneaccess_site_type',
];
foreach ( $options as $option ) {
delete_option( $option );
}
// Drop custom tables created by the OneAccess.
$tables_to_drop = [
'oneaccess_deduplicated_users',
'oneaccess_profile_requests',
];
global $wpdb;
foreach ( $tables_to_drop as $table ) {
$full_table_name = $wpdb->prefix . $table;
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $full_table_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this is to drop table on uninstall
}
}
/**
* Remove custom roles and capabilities from users.
*/
function remove_user_roles_caps(): void {
// remove custom roles and capabilities from users.
$roles = [ 'oneaccess_brand_admin', 'oneaccess_network_admin' ];
$capabilities = [
'oneaccess_manage_requests',
'oneaccess_manage_settings',
'oneaccess_manage_sites',
'oneaccess_manage_users',
];
// get all users with custom roles & remove the role and capabilities.
foreach ( $roles as $role ) {
$users = get_users( [ 'role' => $role ] );
foreach ( $users as $user ) {
if ( ! in_array( $role, (array) $user->roles, true ) ) {
continue;
}
foreach ( $capabilities as $cap ) {
// remove capability if user has it.
if ( ! $user->has_cap( $cap ) ) {
continue;
}
$user->remove_cap( $cap );
}
$user->remove_role( $role );
}
// Finally remove the role itself.
remove_role( $role );
}
}
// Run the uninstaller.
multisite_uninstall();