| 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 |
/** |
| 43 |
* Narrowed type for `$wpdb`. |
| 44 |
* |
| 45 |
* @var \wpdb $wpdb |
| 46 |
*/ |
| 47 |
$zip_ai_delete_transients_query = $wpdb->prepare( |
| 48 |
'DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s', |
| 49 |
$wpdb->options, |
| 50 |
$wpdb->esc_like( '_transient_zip_ai_oauth_state_' ) . '%', |
| 51 |
$wpdb->esc_like( '_transient_timeout_zip_ai_oauth_state_' ) . '%' |
| 52 |
); |
| 53 |
if ( is_string( $zip_ai_delete_transients_query ) ) { |
| 54 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- built via $wpdb->prepare() with a %i table placeholder above; guarded is_string. |
| 55 |
$wpdb->query( $zip_ai_delete_transients_query ); |
| 56 |
} |
| 57 |
|
| 58 |
// Capability removal from every role. |
| 59 |
// `manage_zip_mcp_assistant` is the capability the plugin adds today; |
| 60 |
// `manage_zip_ai_assistant` may linger from older releases. |
| 61 |
$zip_ai_roles = wp_roles(); |
| 62 |
foreach ( $zip_ai_roles->role_objects as $zip_ai_role ) { |
| 63 |
foreach ( array( 'manage_zip_mcp_assistant', 'manage_zip_ai_assistant' ) as $zip_ai_cap ) { |
| 64 |
if ( $zip_ai_role->has_cap( $zip_ai_cap ) ) { |
| 65 |
$zip_ai_role->remove_cap( $zip_ai_cap ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// Scheduled cron events. |
| 71 |
wp_clear_scheduled_hook( 'zip_ai_site_scan' ); |
| 72 |
|