| 1 |
<?php |
| 2 |
/** |
| 3 |
* Uninstall — remove plugin data only when the user opted in. |
| 4 |
* |
| 5 |
* Per WordPress.org guidance, user data is preserved by default. Data is only |
| 6 |
* removed when Settings → "Remove all data on uninstall" was enabled (the |
| 7 |
* `remove_data_on_uninstall` flag in the easyfonts_settings option). |
| 8 |
* |
| 9 |
* On multisite, this runs per-site for each site the plugin is removed from. |
| 10 |
* |
| 11 |
* @package EasyFonts |
| 12 |
*/ |
| 13 |
|
| 14 |
defined( 'WP_UNINSTALL_PLUGIN' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Should we wipe everything for the current site? |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
function easyfonts_should_remove_data(): bool { |
| 22 |
$settings = get_option( 'easyfonts_settings' ); |
| 23 |
|
| 24 |
return is_array( $settings ) && ! empty( $settings['remove_data_on_uninstall'] ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Remove every trace of the plugin for the current site. |
| 29 |
*/ |
| 30 |
function easyfonts_purge_site(): void { |
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
// Always clear scheduled events (cheap, avoids orphaned cron). |
| 34 |
wp_clear_scheduled_hook( 'easyfonts_cleanup' ); |
| 35 |
|
| 36 |
if ( ! easyfonts_should_remove_data() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Drop tables. |
| 41 |
foreach ( array( 'easyfonts_fonts', 'easyfonts_usage', 'easyfonts_decisions' ) as $table ) { |
| 42 |
$name = $wpdb->prefix . $table; |
| 43 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.NotPrepared |
| 44 |
$wpdb->query( "DROP TABLE IF EXISTS `{$name}`" ); |
| 45 |
} |
| 46 |
|
| 47 |
// Delete options + transients. |
| 48 |
foreach ( |
| 49 |
array( |
| 50 |
'easyfonts_settings', |
| 51 |
'easyfonts_db_version', |
| 52 |
'easyfonts_cache_buster', |
| 53 |
'easyfonts_activated_at', |
| 54 |
'easyfonts_processed_local', |
| 55 |
'easyfonts_processed_external', |
| 56 |
'easyfonts_last_beacon', |
| 57 |
'easyfonts_global_preload', |
| 58 |
'easyfonts_async_urls', |
| 59 |
) as $option |
| 60 |
) { |
| 61 |
delete_option( $option ); |
| 62 |
} |
| 63 |
|
| 64 |
delete_transient( 'easyfonts_schema_checked' ); |
| 65 |
delete_transient( 'easyfonts_metrics_lock' ); |
| 66 |
|
| 67 |
// Purge the cache directory. |
| 68 |
$upload = wp_upload_dir(); |
| 69 |
$dir = trailingslashit( $upload['basedir'] ) . 'easyfonts'; |
| 70 |
|
| 71 |
if ( is_dir( $dir ) ) { |
| 72 |
$iterator = new RecursiveIteratorIterator( |
| 73 |
new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ), |
| 74 |
RecursiveIteratorIterator::CHILD_FIRST |
| 75 |
); |
| 76 |
|
| 77 |
foreach ( $iterator as $file ) { |
| 78 |
// phpcs:disable WordPress.WP.AlternativeFunctions |
| 79 |
$file->isDir() ? @rmdir( $file->getRealPath() ) : @unlink( $file->getRealPath() ); |
| 80 |
// phpcs:enable |
| 81 |
} |
| 82 |
|
| 83 |
@rmdir( $dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Run for every site on multisite; otherwise just the current site. |
| 88 |
if ( is_multisite() ) { |
| 89 |
$site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) ); |
| 90 |
|
| 91 |
foreach ( $site_ids as $site_id ) { |
| 92 |
switch_to_blog( (int) $site_id ); |
| 93 |
easyfonts_purge_site(); |
| 94 |
restore_current_blog(); |
| 95 |
} |
| 96 |
} else { |
| 97 |
easyfonts_purge_site(); |
| 98 |
} |
| 99 |
|