| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions for cleaning data when the plugin is uninstalled. |
| 4 |
* |
| 5 |
* @package Code_Snippets |
| 6 |
* |
| 7 |
* phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Code_Snippets\Uninstall; |
| 11 |
|
| 12 |
/** |
| 13 |
* Determine whether the option for allowing a complete uninstallation is enabled. |
| 14 |
* |
| 15 |
* @return boolean |
| 16 |
*/ |
| 17 |
function complete_uninstall_enabled(): bool { |
| 18 |
$unified = false; |
| 19 |
|
| 20 |
if ( is_multisite() ) { |
| 21 |
$menu_perms = get_site_option( 'menu_items', [] ); |
| 22 |
$unified = empty( $menu_perms['snippets_settings'] ); |
| 23 |
} |
| 24 |
|
| 25 |
$settings = $unified ? get_site_option( 'code_snippets_settings' ) : get_option( 'code_snippets_settings' ); |
| 26 |
|
| 27 |
return isset( $settings['general']['complete_uninstall'] ) && $settings['general']['complete_uninstall']; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Clean up data created by this plugin for a single site |
| 32 |
* |
| 33 |
* phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 34 |
*/ |
| 35 |
function uninstall_current_site() { |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}snippets" ); |
| 39 |
|
| 40 |
delete_option( 'code_snippets_version' ); |
| 41 |
delete_option( 'recently_activated_snippets' ); |
| 42 |
delete_option( 'code_snippets_settings' ); |
| 43 |
|
| 44 |
delete_option( 'code_snippets_cloud_settings' ); |
| 45 |
delete_transient( 'cs_codevault_snippets' ); |
| 46 |
delete_transient( 'cs_local_to_cloud_map' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Clean up data created by this plugin on multisite. |
| 51 |
* |
| 52 |
* phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 53 |
*/ |
| 54 |
function uninstall_multisite() { |
| 55 |
global $wpdb; |
| 56 |
|
| 57 |
// Loop through sites. |
| 58 |
$blog_ids = get_sites( [ 'fields' => 'ids' ] ); |
| 59 |
|
| 60 |
foreach ( $blog_ids as $site_id ) { |
| 61 |
switch_to_blog( $site_id ); |
| 62 |
uninstall_current_site(); |
| 63 |
} |
| 64 |
|
| 65 |
restore_current_blog(); |
| 66 |
|
| 67 |
// Remove network snippets table. |
| 68 |
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ms_snippets" ); |
| 69 |
|
| 70 |
// Remove saved options. |
| 71 |
delete_site_option( 'code_snippets_version' ); |
| 72 |
delete_site_option( 'recently_activated_snippets' ); |
| 73 |
} |
| 74 |
|
| 75 |
function delete_flat_files_directory() { |
| 76 |
$flat_files_dir = WP_CONTENT_DIR . '/code-snippets'; |
| 77 |
|
| 78 |
if ( ! is_dir( $flat_files_dir ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
| 83 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 84 |
} |
| 85 |
|
| 86 |
global $wp_filesystem; |
| 87 |
WP_Filesystem(); |
| 88 |
|
| 89 |
if ( $wp_filesystem && $wp_filesystem->is_dir( $flat_files_dir ) ) { |
| 90 |
$wp_filesystem->delete( $flat_files_dir, true ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Uninstall the Code Snippets plugin. |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
function uninstall_plugin() { |
| 100 |
if ( complete_uninstall_enabled() ) { |
| 101 |
|
| 102 |
if ( is_multisite() ) { |
| 103 |
uninstall_multisite(); |
| 104 |
} else { |
| 105 |
uninstall_current_site(); |
| 106 |
} |
| 107 |
|
| 108 |
delete_flat_files_directory(); |
| 109 |
} |
| 110 |
} |
| 111 |
|