| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { |
| 4 |
exit(); |
| 5 |
} |
| 6 |
|
| 7 |
// Recursively delete a directory and it's contents |
| 8 |
function mt_destroy_dir($dir) { |
| 9 |
if (!is_dir($dir) || is_link($dir)) { |
| 10 |
return wp_delete_file($dir); |
| 11 |
} |
| 12 |
foreach (scandir($dir) as $file) { |
| 13 |
if ($file == '.' || $file == '..') continue; |
| 14 |
if (!mt_destroy_dir($dir . DIRECTORY_SEPARATOR . $file)) { |
| 15 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- uninstall recursive cleanup when a path is not writable. |
| 16 |
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777); |
| 17 |
if (!mt_destroy_dir($dir . DIRECTORY_SEPARATOR . $file)) return false; |
| 18 |
}; |
| 19 |
} |
| 20 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- uninstall recursive cleanup of empty directories under micro-themes. |
| 21 |
return rmdir($dir); |
| 22 |
} |
| 23 |
|
| 24 |
/* |
| 25 |
Reliably getting the dir path for the wp content folder isn't straight forward e.g. content_dir(). |
| 26 |
This is my best attempt so far (inspired by get-dir-paths.inc.php). |
| 27 |
For the micro themes dir, we need to check for old and new multi-site directory structure. |
| 28 |
*/ |
| 29 |
function micro_themes_dir_best_guess(){ |
| 30 |
$wp_content_dir = str_replace(site_url(), ABSPATH, content_url()); |
| 31 |
// if it still has http, use fallback dir method |
| 32 |
if (strpos($wp_content_dir, 'http') !== false){ |
| 33 |
$wp_content_dir = WP_CONTENT_DIR; |
| 34 |
} |
| 35 |
// now for the micro-themes dir |
| 36 |
$micro_root_dir = $wp_content_dir . '/micro-themes/'; |
| 37 |
// exception for multisite |
| 38 |
global $wp_version; |
| 39 |
global $blog_id; |
| 40 |
if ($wp_version >= 3 and is_multisite()) { |
| 41 |
$filename = $wp_content_dir . "/blogs.dir/"; |
| 42 |
if(file_exists($filename)){ |
| 43 |
if ($blog_id == '1') { |
| 44 |
$micro_root_dir = $wp_content_dir . '/blogs.dir/micro-themes/'; |
| 45 |
} else { |
| 46 |
$micro_root_dir = $wp_content_dir . '/blogs.dir/' . $blog_id . '/micro-themes/'; |
| 47 |
} |
| 48 |
} else { |
| 49 |
if ($blog_id == '1') { |
| 50 |
$micro_root_dir = $wp_content_dir . '/uploads/sites/micro-themes/'; |
| 51 |
} else { |
| 52 |
$micro_root_dir = $wp_content_dir . '/uploads/sites/' . $blog_id . '/micro-themes/'; |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
return $micro_root_dir; |
| 57 |
} |
| 58 |
|
| 59 |
// Run the cleanup code - if user sets this option |
| 60 |
$p_name = 'preferences_themer_loader'; |
| 61 |
$options_name = 'microthemer_ui_settings'; |
| 62 |
$p = get_option($p_name); |
| 63 |
if (!empty($p['clean_uninstall'])){ |
| 64 |
delete_option($p_name); |
| 65 |
delete_option($options_name); |
| 66 |
global $wpdb; |
| 67 |
$wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . 'micro_revisions'); |
| 68 |
$wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . 'micro_content'); |
| 69 |
mt_destroy_dir( micro_themes_dir_best_guess() ); |
| 70 |
} |