| 1 |
<?php |
| 2 |
/** |
| 3 |
* Uninstall handler — deletes options, cache, and drop-in. |
| 4 |
* |
| 5 |
* @package XSpeed |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
xspeed_uninstall_cleanup(); |
| 13 |
|
| 14 |
/** |
| 15 |
* Run all uninstall cleanup. Wrapped in a function so locals don't pollute global scope. |
| 16 |
*/ |
| 17 |
function xspeed_uninstall_cleanup() { |
| 18 |
delete_option( 'xspeed_options' ); |
| 19 |
delete_option( 'xspeed_stats' ); |
| 20 |
|
| 21 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 22 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 23 |
} |
| 24 |
WP_Filesystem(); |
| 25 |
global $wp_filesystem; |
| 26 |
if ( ! $wp_filesystem ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$cache_dir = WP_CONTENT_DIR . '/cache/xspeed'; |
| 31 |
if ( $wp_filesystem->is_dir( $cache_dir ) ) { |
| 32 |
$wp_filesystem->delete( $cache_dir, true ); |
| 33 |
} |
| 34 |
|
| 35 |
// nginx hit log (FBS-82478). It lives under uploads/xspeed/, NOT the |
| 36 |
// cache dir, precisely so that a pasted nginx `access_log` directive |
| 37 |
// pointing at it does NOT get its parent directory deleted here — if it |
| 38 |
// did, `nginx -t` would fail [emerg] and refuse to (re)start, taking |
| 39 |
// down EVERY vhost on the host until someone manually finds the orphaned |
| 40 |
// directive. We therefore EMPTY the log file but DELIBERATELY LEAVE THE |
| 41 |
// DIRECTORY in place, so any still-pasted directive keeps a valid, |
| 42 |
// openable target after the plugin is gone. (A stray empty dir is |
| 43 |
// harmless; a broken nginx is not.) Users are also warned in the admin |
| 44 |
// UI to remove the snippet before uninstalling. |
| 45 |
$hits_log = WP_CONTENT_DIR . '/uploads/xspeed/hits.log'; |
| 46 |
if ( function_exists( 'wp_upload_dir' ) ) { |
| 47 |
$uploads = wp_upload_dir( null, false ); |
| 48 |
if ( is_array( $uploads ) && empty( $uploads['error'] ) && ! empty( $uploads['basedir'] ) ) { |
| 49 |
$hits_log = rtrim( (string) $uploads['basedir'], '/' ) . '/xspeed/hits.log'; |
| 50 |
} |
| 51 |
} |
| 52 |
if ( $wp_filesystem->exists( $hits_log ) ) { |
| 53 |
// Truncate, don't delete the dir — keep the access_log target openable. |
| 54 |
$wp_filesystem->put_contents( $hits_log, '', FS_CHMOD_FILE ); |
| 55 |
} |
| 56 |
|
| 57 |
// Static-cache tree — separate from the flat-hash cache dir, holds |
| 58 |
// the {host}/{path}/index.html files the .htaccess rewrite block |
| 59 |
// serves directly. Same teardown rules as XSPEED_CACHE_DIR. |
| 60 |
$static_dir = WP_CONTENT_DIR . '/cache/xspeed-static'; |
| 61 |
if ( $wp_filesystem->is_dir( $static_dir ) ) { |
| 62 |
$wp_filesystem->delete( $static_dir, true ); |
| 63 |
} |
| 64 |
|
| 65 |
// Rewrite block in site-root .htaccess. WP's marker helpers handle |
| 66 |
// the "remove only our block" semantics — passing an empty array |
| 67 |
// strips the markers in place. |
| 68 |
$htaccess = ABSPATH . '.htaccess'; |
| 69 |
if ( $wp_filesystem->exists( $htaccess ) && $wp_filesystem->is_writable( $htaccess ) ) { |
| 70 |
if ( ! function_exists( 'insert_with_markers' ) ) { |
| 71 |
require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 72 |
} |
| 73 |
insert_with_markers( $htaccess, 'xSpeed Static Cache', array() ); |
| 74 |
} |
| 75 |
|
| 76 |
$drop_in = WP_CONTENT_DIR . '/advanced-cache.php'; |
| 77 |
if ( $wp_filesystem->exists( $drop_in ) ) { |
| 78 |
$contents = $wp_filesystem->get_contents( $drop_in ); |
| 79 |
if ( is_string( $contents ) && false !== strpos( $contents, 'XSPEED_DROPIN' ) ) { |
| 80 |
$wp_filesystem->delete( $drop_in ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$wp_config = ABSPATH . 'wp-config.php'; |
| 85 |
if ( defined( 'WP_CACHE' ) && WP_CACHE && $wp_filesystem->is_writable( $wp_config ) ) { |
| 86 |
$config = $wp_filesystem->get_contents( $wp_config ); |
| 87 |
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 ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|