| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Disco |
| 5 |
* |
| 6 |
* Fired when the plugin is uninstalled. |
| 7 |
* |
| 8 |
* When populating this file, consider the following flow |
| 9 |
* of control: |
| 10 |
* |
| 11 |
* - This method should be static |
| 12 |
* - Check if the $_REQUEST content actually is the plugin name |
| 13 |
* - Run an admin referrer check to make sure it goes through authentication |
| 14 |
* - Verify the output of $_GET makes sense |
| 15 |
* - Repeat with other user roles. Best directly by using the links/query string parameters. |
| 16 |
* - Repeat things for multisite. Once for a single site in the network, once sitewide. |
| 17 |
* |
| 18 |
* @package Disco |
| 19 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 20 |
* @copyright 2022 WebAppick |
| 21 |
* @license GPL 2.0+ |
| 22 |
* @link http://domain.tld |
| 23 |
*/ |
| 24 |
|
| 25 |
// If uninstall not called from WordPress, then exit. |
| 26 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 27 |
exit; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Loop for uninstall |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
function disco_uninstall_multisite() { |
| 36 |
if ( is_multisite() ) { |
| 37 |
/** @var array<\WP_Site> $blogs */ |
| 38 |
$blogs = get_sites(); |
| 39 |
|
| 40 |
if ( ! empty( $blogs ) ) { |
| 41 |
foreach ( $blogs as $blog ) { |
| 42 |
switch_to_blog( (int) $blog->blog_id ); |
| 43 |
disco_uninstall(); |
| 44 |
restore_current_blog(); |
| 45 |
} |
| 46 |
|
| 47 |
return; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
disco_uninstall(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* What happens on uninstalling? |
| 56 |
* |
| 57 |
* @return void |
| 58 |
* @global WP_Roles $wp_roles |
| 59 |
*/ |
| 60 |
function disco_uninstall() { // phpcs:ignore |
| 61 |
global $wp_roles; |
| 62 |
|
| 63 |
// Ensure $wp_roles is properly initialized |
| 64 |
if ( ! isset( $wp_roles ) ) { |
| 65 |
if ( ! class_exists( 'WP_Roles' ) ) { |
| 66 |
return; // Exit if WP_Roles class does not exist |
| 67 |
} |
| 68 |
|
| 69 |
$wp_roles = new WP_Roles(); // phpcs:ignore |
| 70 |
} |
| 71 |
|
| 72 |
$caps = array( |
| 73 |
'create_plugins', |
| 74 |
'read_demo', |
| 75 |
'read_private_demoes', |
| 76 |
'edit_demo', |
| 77 |
'edit_demoes', |
| 78 |
'edit_private_demoes', |
| 79 |
'edit_published_demoes', |
| 80 |
'edit_others_demoes', |
| 81 |
'publish_demoes', |
| 82 |
'delete_demo', |
| 83 |
'delete_demoes', |
| 84 |
'delete_private_demoes', |
| 85 |
'delete_published_demoes', |
| 86 |
'delete_others_demoes', |
| 87 |
'manage_demoes', |
| 88 |
); |
| 89 |
|
| 90 |
foreach ( $wp_roles->roles as $role_key => $role_name ) { // phpcs:ignore |
| 91 |
$role_cap = get_role( $role_key ); |
| 92 |
|
| 93 |
if ( ! $role_cap ) { |
| 94 |
continue; // Skip if a role does not exist |
| 95 |
} |
| 96 |
|
| 97 |
foreach ( $caps as $cap ) { |
| 98 |
$role_cap->remove_cap( $cap ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
disco_uninstall_multisite(); |
| 105 |
|