| @@ -14,11 +14,70 @@ | ||
| 14 | 14 | /** |
| 15 | 15 | * Run all uninstall cleanup. Wrapped in a function so locals don't pollute global scope. |
| 16 | 16 | */ |
| 17 | 17 | function xspeed_uninstall_cleanup() { |
| 18 | + /* | |
| 19 | + * Everything here is ours to delete. `wpdeveloper_xspeed_offer` is NOT — it | |
| 20 | + * is the site's answer about whether it wants this plugin, written by | |
| 21 | + * whichever WPDeveloper plugin offered it. Removing xSpeed is itself an | |
| 22 | + * answer — the siblings read it as one, from that row plus the absent plugin | |
| 23 | + * files. Deleting it here would make every one of them offer xSpeed again to | |
| 24 | + * the user who just took it off. | |
| 25 | + * See docs/guides/installing-from-another-plugin.md. | |
| 26 | + */ | |
| 18 | 27 | delete_option( 'xspeed_options' ); |
| 28 | + | |
| 29 | + /* | |
| 30 | + * Per-module rows, for the modules THIS plugin ships. The slugs are read | |
| 31 | + * out of the module files rather than kept as a list here, so a module | |
| 32 | + * added later cannot leave its row behind; and only Free's, because | |
| 33 | + * xspeed-pro keeps its own rows under the same prefix and a Free uninstall | |
| 34 | + * is not a decision about Pro's settings. | |
| 35 | + * | |
| 36 | + * These have to go. The conflict-safe profile writes an explicit `false` | |
| 37 | + * into every one of them when another plugin owns the page cache, and | |
| 38 | + * seeding on the next install only fills ABSENT keys — so a row that | |
| 39 | + * survived uninstall kept every switch off on a site that had since been | |
| 40 | + * cleared, with no way back but the dashboard (PR #295 review). | |
| 41 | + */ | |
| 42 | + foreach ( glob( __DIR__ . '/includes/modules/*/*Module.php' ) ?: array() as $module_file ) { | |
| 43 | + $source = @file_get_contents( $module_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- own plugin file, uninstall. | |
| 44 | + if ( is_string( $source ) && preg_match( "/const\s+SLUG\s*=\s*'([^']+)'/", $source, $m ) ) { | |
| 45 | + delete_option( 'xspeed_module_' . $m[1] ); | |
| 46 | + } | |
| 47 | + } | |
| 48 | + delete_option( 'xspeed_data_version' ); | |
| 49 | + delete_option( 'xspeed_activity_log' ); | |
| 50 | + delete_option( 'xspeed_server_type' ); | |
| 51 | + delete_option( 'xspeed_oc_dropin_synced' ); | |
| 52 | + delete_option( 'xspeed_oc_generation' ); | |
| 53 | + delete_option( 'xspeed_oc_sync_attempts' ); | |
| 54 | + delete_option( 'xspeed_overridden_constants' ); | |
| 55 | + delete_option( 'xspeed_last_mobile_separate' ); | |
| 56 | + delete_option( 'xspeed_redirect_to_onboarding' ); | |
| 57 | + delete_option( 'xspeed_onboarding_complete' ); | |
| 58 | + // Provenance a host plugin wrote before it activated us, and the profile | |
| 59 | + // that install came up with. | |
| 60 | + delete_option( 'xspeed_installed_by' ); | |
| 61 | + delete_option( 'xspeed_installer' ); | |
| 62 | + delete_option( 'xspeed_install_profile' ); | |
| 63 | + // Written by the copy-vendored Setup that host plugins used to carry | |
| 64 | + // before xSpeed seeded its own conflict-safe profile on activation. We no | |
| 65 | + // longer write it; an older host may have, and it is ours to clean up. | |
| 66 | + delete_option( 'xspeed_setup_snapshot' ); | |
| 19 | 67 | delete_option( 'xspeed_stats' ); |
| 68 | + delete_option( 'xspeed_gc_cursor' ); | |
| 69 | + wp_clear_scheduled_hook( 'xspeed_gc' ); | |
| 20 | 70 | |
| 71 | + // Score history — the plugin's own table, plus the legacy option the | |
| 72 | + // table was migrated from (kept on upgrade so a bad migration is | |
| 73 | + // recoverable; there is nothing to recover on uninstall). | |
| 74 | + global $wpdb; | |
| 75 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- own table, uninstall. | |
| 76 | + $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'xspeed_scores' ); | |
| 77 | + delete_option( 'xspeed_score_schema' ); | |
| 78 | + delete_option( 'xspeed_score_history' ); | |
| 79 | + | |
| 21 | 80 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 22 | 81 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 23 | 82 | } |
| 24 | 83 | WP_Filesystem(); |
| @@ -72,21 +131,74 @@ | ||
| 72 | 131 | } |
| 73 | 132 | insert_with_markers( $htaccess, 'xSpeed Static Cache', array() ); |
| 74 | 133 | } |
| 75 | 134 | |
| 76 | - $drop_in = WP_CONTENT_DIR . '/advanced-cache.php'; | |
| 135 | + // Serialize the shared drop-in/config teardown with Cache::toggle(). If | |
| 136 | + // the lock is unavailable, leave shared state and its receipt untouched. | |
| 137 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen,WordPress.PHP.NoSilencedErrors.Discouraged -- flock requires a local handle; failure is fail-closed. | |
| 138 | + $ownership_lock = @fopen( WP_CONTENT_DIR . '/.xspeed-page-cache.lock', 'c+' ); | |
| 139 | + if ( ! is_resource( $ownership_lock ) || ! flock( $ownership_lock, LOCK_EX ) ) { | |
| 140 | + return; | |
| 141 | + } | |
| 142 | + | |
| 143 | + require_once __DIR__ . '/includes/wp-cache-constant.php'; | |
| 144 | + $drop_in = WP_CONTENT_DIR . '/advanced-cache.php'; | |
| 145 | + $dropin_ours = false; | |
| 77 | 146 | if ( $wp_filesystem->exists( $drop_in ) ) { |
| 78 | - $contents = $wp_filesystem->get_contents( $drop_in ); | |
| 79 | - if ( is_string( $contents ) && false !== strpos( $contents, 'XSPEED_DROPIN' ) ) { | |
| 147 | + $contents = $wp_filesystem->get_contents( $drop_in ); | |
| 148 | + $dropin_ours = is_string( $contents ) && xspeed_has_canonical_dropin_signature( $contents ); | |
| 149 | + if ( $dropin_ours ) { | |
| 80 | 150 | $wp_filesystem->delete( $drop_in ); |
| 81 | 151 | } |
| 82 | 152 | } |
| 83 | 153 | |
| 84 | - $wp_config = ABSPATH . 'wp-config.php'; | |
| 85 | - if ( defined( 'WP_CACHE' ) && WP_CACHE && $wp_filesystem->is_writable( $wp_config ) ) { | |
| 154 | + $wp_config = file_exists( ABSPATH . 'wp-config.php' ) ? ABSPATH . 'wp-config.php' : dirname( ABSPATH ) . '/wp-config.php'; | |
| 155 | + // `defined()` alone, not `&& WP_CACHE`: the old truthiness test skipped | |
| 156 | + // the removal whenever the constant was false/0 — exactly the orphan we | |
| 157 | + // are here to clean up — while still entering it for TRUE/1, where the | |
| 158 | + // hardcoded-lowercase regex then matched nothing and reported success. | |
| 159 | + // Strip whatever spelling is there. (#9) | |
| 160 | + // | |
| 161 | + // Gated on the drop-in being ours: if another caching plugin holds | |
| 162 | + // advanced-cache.php, WP_CACHE is the switch that loads THEIR file, and | |
| 163 | + // stripping it on our way out would silently disable their page cache. | |
| 164 | + if ( $wp_filesystem->is_writable( $wp_config ) ) { | |
| 86 | 165 | $config = $wp_filesystem->get_contents( $wp_config ); |
| 87 | 166 | if ( is_string( $config ) ) { |
| 88 | - $config = preg_replace( "/define\\(\\s*['\"]WP_CACHE['\"]\\s*,\\s*true\\s*\\);\\s*\\n?/", '', $config ); | |
| 89 | - $wp_filesystem->put_contents( $wp_config, $config, FS_CHMOD_FILE ); | |
| 167 | + // Same helper Cache::set_wp_cache_constant() uses — one pattern, | |
| 168 | + // one place. uninstall.php loads no plugin classes, hence a | |
| 169 | + // plain requirable function rather than a method. | |
| 170 | + require_once __DIR__ . '/includes/wp-cache-constant.php'; | |
| 171 | + $receipt = get_option( 'xspeed_page_cache_ownership_receipt', '' ); | |
| 172 | + $marked = xspeed_wp_cache_receipt_matches( $config, $receipt ); | |
| 173 | + /* | |
| 174 | + * A receipt proves WE wrote the line; it does not prove the line | |
| 175 | + * is still ours to remove. If a competitor has since taken over | |
| 176 | + * advanced-cache.php, WP_CACHE is what loads THEIR drop-in — they | |
| 177 | + * had no reason to touch an already-true define, so our receipt | |
| 178 | + * comment is still sitting beside it. Stripping on the receipt | |
| 179 | + * alone silently stopped their live page cache. | |
| 180 | + * | |
| 181 | + * A foreign drop-in therefore vetoes the removal outright, which | |
| 182 | + * is the same rule Cache::set_wp_cache_constant() applies in the | |
| 183 | + * running plugin; only this path was missing it. `$dropin_ours` | |
| 184 | + * covers the file we just deleted, `$marked` the case where there | |
| 185 | + * is no drop-in left at all. | |
| 186 | + */ | |
| 187 | + $foreign_dropin = $wp_filesystem->exists( $drop_in ) && ! $dropin_ours; | |
| 188 | + if ( ! $foreign_dropin && ( $dropin_ours || $marked ) ) { | |
| 189 | + $config = xspeed_strip_wp_cache_define( $config ); | |
| 190 | + $wp_filesystem->put_contents( $wp_config, $config, FS_CHMOD_FILE ); | |
| 191 | + } | |
| 90 | 192 | } |
| 91 | 193 | } |
| 194 | + delete_option( 'xspeed_page_cache_ownership_receipt' ); | |
| 195 | + flock( $ownership_lock, LOCK_UN ); | |
| 196 | + fclose( $ownership_lock ); | |
| 197 | + // Our own lock file, left in wp-content after everything else of ours is | |
| 198 | + // gone. Deleted last, after the handle is closed, so we are not | |
| 199 | + // unlinking a lock we are still inside. That ordering is hygiene, not a | |
| 200 | + // guarantee: a request already past the fopen would hold a handle to an | |
| 201 | + // unlinked inode. Harmless here — by this point the plugin is being | |
| 202 | + // removed and nothing will take the lock again. | |
| 203 | + $wp_filesystem->delete( WP_CONTENT_DIR . '/.xspeed-page-cache.lock' ); | |
| 92 | 204 | } |