| 1 |
<?php |
| 2 |
/** |
| 3 |
* Uninstall handler. |
| 4 |
* |
| 5 |
* Removes all plugin-owned options, transients, capabilities, and |
| 6 |
* scheduled cron events when the plugin is deleted from the |
| 7 |
* WordPress admin. |
| 8 |
* |
| 9 |
* @package zip-ai |
| 10 |
*/ |
| 11 |
|
| 12 |
// Only run on uninstall, not activation or deactivation. |
| 13 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Delete a single plugin option (site-wide on multisite). |
| 19 |
* |
| 20 |
* @param string $option Option name. |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
function zip_ai_delete_option( $option ) { |
| 24 |
if ( is_multisite() ) { |
| 25 |
delete_site_option( $option ); |
| 26 |
} |
| 27 |
delete_option( $option ); |
| 28 |
} |
| 29 |
|
| 30 |
// Options. |
| 31 |
zip_ai_delete_option( 'zip_ai_settings' ); |
| 32 |
zip_ai_delete_option( 'zip_ai_audit_log' ); |
| 33 |
zip_ai_delete_option( 'zip_ai_encryption_key' ); |
| 34 |
|
| 35 |
// Transients. |
| 36 |
delete_transient( 'zip_ai_last_scan_time' ); |
| 37 |
|
| 38 |
// Per-user OAuth state transients (prefix match). A direct query is the |
| 39 |
// only way to bulk-delete prefix-matched transients; caching and the |
| 40 |
// standard API are not applicable during uninstall. |
| 41 |
global $wpdb; |
| 42 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 43 |
$wpdb->query( |
| 44 |
$wpdb->prepare( |
| 45 |
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", |
| 46 |
$wpdb->esc_like( '_transient_zip_ai_oauth_state_' ) . '%', |
| 47 |
$wpdb->esc_like( '_transient_timeout_zip_ai_oauth_state_' ) . '%' |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
// Capability removal from every role. |
| 52 |
$zip_ai_roles = wp_roles(); |
| 53 |
if ( isset( $zip_ai_roles->role_objects ) && is_array( $zip_ai_roles->role_objects ) ) { |
| 54 |
foreach ( $zip_ai_roles->role_objects as $zip_ai_role ) { |
| 55 |
if ( $zip_ai_role->has_cap( 'manage_zip_ai_assistant' ) ) { |
| 56 |
$zip_ai_role->remove_cap( 'manage_zip_ai_assistant' ); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Scheduled cron events. |
| 62 |
wp_clear_scheduled_hook( 'zip_ai_site_scan' ); |
| 63 |
|