wp-smushit
Last commit date
_src
4 years ago
app
4 years ago
core
4 years ago
languages
4 years ago
readme.txt
4 years ago
uninstall.php
4 years ago
wp-smush.php
4 years ago
uninstall.php
189 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Remove plugin settings data. |
| 4 | * |
| 5 | * @since 1.7 |
| 6 | * @package Smush |
| 7 | */ |
| 8 | |
| 9 | use Smush\Core\Settings; |
| 10 | |
| 11 | // If uninstall not called from WordPress exit. |
| 12 | if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 13 | exit(); |
| 14 | } |
| 15 | |
| 16 | if ( ! class_exists( '\\Smush\\Core\\Settings' ) ) { |
| 17 | /* @noinspection PhpIncludeInspection */ |
| 18 | include_once plugin_dir_path( __FILE__ ) . '/core/class-settings.php'; |
| 19 | } |
| 20 | $keep_data = Settings::get_instance()->get( 'keep_data' ); |
| 21 | |
| 22 | // Check if someone want to keep the stats and settings. |
| 23 | if ( ( defined( 'WP_SMUSH_PRESERVE_STATS' ) && WP_SMUSH_PRESERVE_STATS ) || true === $keep_data ) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | global $wpdb; |
| 28 | |
| 29 | $smushit_keys = array( |
| 30 | 'wp-smush-resmush-list', |
| 31 | 'wp-smush-nextgen-resmush-list', |
| 32 | 'wp-smush-resize_sizes', |
| 33 | 'wp-smush-transparent_png', |
| 34 | 'wp-smush-image_sizes', |
| 35 | 'wp-smush-super_smushed', |
| 36 | 'wp-smush-super_smushed_nextgen', |
| 37 | 'wp-smush-settings_updated', |
| 38 | 'wp-smush-hide_smush_welcome', |
| 39 | 'wp-smush-hide_upgrade_notice', |
| 40 | 'wp-smush-hide_update_info', |
| 41 | 'wp-smush-install-type', |
| 42 | 'wp-smush-version', |
| 43 | 'wp-smush-scan', |
| 44 | 'wp-smush-settings', |
| 45 | 'wp-smush-cdn_status', |
| 46 | 'wp-smush-lazy_load', |
| 47 | 'wp-smush-last_run_sync', |
| 48 | 'wp-smush-networkwide', |
| 49 | 'wp-smush-cron_update_running', |
| 50 | 'wp-smush-hide-conflict-notice', |
| 51 | 'wp-smush-show_upgrade_modal', |
| 52 | 'wp-smush-preset_configs', |
| 53 | 'wp-smush-webp_hide_wizard', |
| 54 | 'wp-smush-hide-tutorials', |
| 55 | 'wp-smush-hide_tutorials_from_bulk_smush', // Possible leftover from 3.8.4. |
| 56 | ); |
| 57 | |
| 58 | $db_keys = array( |
| 59 | 'skip-smush-setup', |
| 60 | 'smush_global_stats', |
| 61 | 'wp_smush_stats_nextgen', |
| 62 | ); |
| 63 | |
| 64 | // Cache Keys. |
| 65 | $cache_smush_group = array( |
| 66 | 'exceeding_items', |
| 67 | 'wp-smush-resize_count', |
| 68 | 'wp-smush-resize_savings', |
| 69 | 'wp-smush-pngjpg_savings', |
| 70 | 'wp-smush-smushed_ids', |
| 71 | 'media_attachments', |
| 72 | 'skipped_images', |
| 73 | 'images_with_backups', |
| 74 | 'wp-smush-dir_total_stats', |
| 75 | ); |
| 76 | |
| 77 | $cache_nextgen_group = array( |
| 78 | 'wp_smush_images', |
| 79 | 'wp_smush_images_smushed', |
| 80 | 'wp_smush_images_unsmushed', |
| 81 | 'wp_smush_stats_nextgen', |
| 82 | ); |
| 83 | |
| 84 | if ( ! is_multisite() ) { |
| 85 | // Delete Options. |
| 86 | foreach ( $smushit_keys as $key ) { |
| 87 | delete_option( $key ); |
| 88 | delete_site_option( $key ); |
| 89 | } |
| 90 | |
| 91 | foreach ( $db_keys as $key ) { |
| 92 | delete_option( $key ); |
| 93 | delete_site_option( $key ); |
| 94 | } |
| 95 | |
| 96 | // Delete Cache data. |
| 97 | foreach ( $cache_smush_group as $s_key ) { |
| 98 | wp_cache_delete( $s_key, 'wp-smush' ); |
| 99 | } |
| 100 | |
| 101 | foreach ( $cache_nextgen_group as $n_key ) { |
| 102 | wp_cache_delete( $n_key, 'nextgen' ); |
| 103 | } |
| 104 | |
| 105 | wp_cache_delete( 'get_image_sizes', 'smush_image_sizes' ); |
| 106 | |
| 107 | delete_transient( 'wp-smush-conflict_check' ); |
| 108 | } |
| 109 | |
| 110 | // Delete Directory Smush stats. |
| 111 | delete_option( 'dir_smush_stats' ); |
| 112 | delete_option( 'wp_smush_scan' ); |
| 113 | delete_option( 'wp_smush_api_auth' ); |
| 114 | delete_site_option( 'wp_smush_api_auth' ); |
| 115 | |
| 116 | // Delete Post meta. |
| 117 | $meta_type = 'post'; |
| 118 | $meta_key = 'wp-smpro-smush-data'; |
| 119 | $meta_value = ''; |
| 120 | $delete_all = true; |
| 121 | |
| 122 | if ( is_multisite() ) { |
| 123 | $offset = 0; |
| 124 | $limit = 100; |
| 125 | while ( $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} LIMIT $offset, $limit", ARRAY_A ) ) { |
| 126 | if ( $blogs ) { |
| 127 | foreach ( $blogs as $blog ) { |
| 128 | switch_to_blog( $blog['blog_id'] ); |
| 129 | delete_metadata( $meta_type, null, $meta_key, $meta_value, $delete_all ); |
| 130 | delete_metadata( $meta_type, null, 'wp-smush-lossy', '', $delete_all ); |
| 131 | delete_metadata( $meta_type, null, 'wp-smush-resize_savings', '', $delete_all ); |
| 132 | delete_metadata( $meta_type, null, 'wp-smush-original_file', '', $delete_all ); |
| 133 | delete_metadata( $meta_type, null, 'wp-smush-pngjpg_savings', '', $delete_all ); |
| 134 | |
| 135 | foreach ( $smushit_keys as $key ) { |
| 136 | delete_option( $key ); |
| 137 | delete_site_option( $key ); |
| 138 | } |
| 139 | |
| 140 | foreach ( $db_keys as $key ) { |
| 141 | delete_option( $key ); |
| 142 | delete_site_option( $key ); |
| 143 | } |
| 144 | |
| 145 | // Delete Cache data. |
| 146 | foreach ( $cache_smush_group as $s_key ) { |
| 147 | wp_cache_delete( $s_key, 'wp-smush' ); |
| 148 | } |
| 149 | |
| 150 | foreach ( $cache_nextgen_group as $n_key ) { |
| 151 | wp_cache_delete( $n_key, 'nextgen' ); |
| 152 | } |
| 153 | |
| 154 | wp_cache_delete( 'get_image_sizes', 'smush_image_sizes' ); |
| 155 | } |
| 156 | restore_current_blog(); |
| 157 | } |
| 158 | $offset += $limit; |
| 159 | } |
| 160 | } else { |
| 161 | delete_metadata( $meta_type, null, $meta_key, $meta_value, $delete_all ); |
| 162 | delete_metadata( $meta_type, null, 'wp-smush-lossy', '', $delete_all ); |
| 163 | delete_metadata( $meta_type, null, 'wp-smush-resize_savings', '', $delete_all ); |
| 164 | delete_metadata( $meta_type, null, 'wp-smush-original_file', '', $delete_all ); |
| 165 | delete_metadata( $meta_type, null, 'wp-smush-pngjpg_savings', '', $delete_all ); |
| 166 | } |
| 167 | // Delete Directory smush table. |
| 168 | $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}smush_dir_images" ); |
| 169 | |
| 170 | // Delete directory scan data. |
| 171 | delete_option( 'wp-smush-scan-step' ); |
| 172 | |
| 173 | // Delete all WebP images. |
| 174 | global $wp_filesystem; |
| 175 | if ( is_null( $wp_filesystem ) ) { |
| 176 | WP_Filesystem(); |
| 177 | } |
| 178 | |
| 179 | $upload_dir = wp_get_upload_dir(); |
| 180 | $webp_dir = dirname( $upload_dir['basedir'] ) . '/smush-webp'; |
| 181 | $wp_filesystem->delete( $webp_dir, true ); |
| 182 | |
| 183 | // Delete WebP test image. |
| 184 | $webp_img = $upload_dir['basedir'] . '/smush-webp-test.png'; |
| 185 | $wp_filesystem->delete( $webp_img ); |
| 186 | |
| 187 | // TODO: Add procedure to delete backup files |
| 188 | // TODO: Update NextGen Metadata to remove Smush stats on plugin deletion. |
| 189 |