PluginProbe
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant / 2.0.4
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant v2.0.4
2.0.4 2.0.3 2.0.2 2.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.3 2.0.0
easyfonts / uninstall.php

uninstall.php in EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant 2.0.4, at uninstall.php

100 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'easyfonts_warm_key',
60 ) as $option
61 ) {
62 delete_option( $option );
63 }
64
65 delete_transient( 'easyfonts_schema_checked' );
66 delete_transient( 'easyfonts_metrics_lock' );
67
68 // Purge the cache directory.
69 $upload = wp_upload_dir();
70 $dir = trailingslashit( $upload['basedir'] ) . 'easyfonts';
71
72 if ( is_dir( $dir ) ) {
73 $iterator = new RecursiveIteratorIterator(
74 new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ),
75 RecursiveIteratorIterator::CHILD_FIRST
76 );
77
78 foreach ( $iterator as $file ) {
79 // phpcs:disable WordPress.WP.AlternativeFunctions
80 $file->isDir() ? @rmdir( $file->getRealPath() ) : @unlink( $file->getRealPath() );
81 // phpcs:enable
82 }
83
84 @rmdir( $dir ); // phpcs:ignore WordPress.WP.AlternativeFunctions
85 }
86 }
87
88 // Run for every site on multisite; otherwise just the current site.
89 if ( is_multisite() ) {
90 $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) );
91
92 foreach ( $site_ids as $site_id ) {
93 switch_to_blog( (int) $site_id );
94 easyfonts_purge_site();
95 restore_current_blog();
96 }
97 } else {
98 easyfonts_purge_site();
99 }
100