| 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\Core; |
| 11 |
|
| 12 |
/** |
| 13 |
* Uninstaller class. |
| 14 |
* |
| 15 |
* This class handles the uninstallation of the Code Snippets plugin, including |
| 16 |
* cleaning up data created by the plugin on both single sites and multisite installations. |
| 17 |
* |
| 18 |
* @package Code_Snippets\Core |
| 19 |
*/ |
| 20 |
class Uninstaller { |
| 21 |
|
| 22 |
/** |
| 23 |
* Uninstall the Code Snippets plugin. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function uninstall_plugin() { |
| 28 |
if ( ! $this->is_complete_uninstall_enabled() ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
if ( is_multisite() ) { |
| 33 |
$this->uninstall_multisite(); |
| 34 |
} else { |
| 35 |
$this->uninstall_current_site(); |
| 36 |
} |
| 37 |
|
| 38 |
$this->delete_flat_files_directory(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Determine whether the option for allowing a complete uninstallation is enabled. |
| 43 |
* |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
public function is_complete_uninstall_enabled(): bool { |
| 47 |
$unified = false; |
| 48 |
|
| 49 |
if ( is_multisite() ) { |
| 50 |
$menu_perms = get_site_option( 'menu_items', array() ); |
| 51 |
$unified = empty( $menu_perms['snippets_settings'] ); |
| 52 |
} |
| 53 |
|
| 54 |
$settings = $unified ? get_site_option( 'code_snippets_settings' ) : get_option( 'code_snippets_settings' ); |
| 55 |
|
| 56 |
return isset( $settings['general']['complete_uninstall'] ) && $settings['general']['complete_uninstall']; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Clean up data created by this plugin for a single site |
| 61 |
* |
| 62 |
* phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 63 |
*/ |
| 64 |
private function uninstall_current_site() { |
| 65 |
global $wpdb; |
| 66 |
|
| 67 |
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}snippets" ); |
| 68 |
|
| 69 |
delete_option( 'code_snippets_version' ); |
| 70 |
delete_option( 'recently_active_snippets' ); |
| 71 |
delete_option( 'recently_activated_snippets' ); |
| 72 |
delete_option( 'code_snippets_settings' ); |
| 73 |
|
| 74 |
delete_option( 'code_snippets_cloud_settings' ); |
| 75 |
delete_transient( 'code_snippets_cloud_links' ); |
| 76 |
delete_transient( 'cs_codevault_snippets' ); |
| 77 |
delete_transient( 'cs_local_to_cloud_map' ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Clean up data created by this plugin on multisite. |
| 82 |
* |
| 83 |
* phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 84 |
*/ |
| 85 |
private function uninstall_multisite() { |
| 86 |
global $wpdb; |
| 87 |
|
| 88 |
// Loop through sites. |
| 89 |
$blog_ids = get_sites( [ 'fields' => 'ids' ] ); |
| 90 |
|
| 91 |
foreach ( $blog_ids as $site_id ) { |
| 92 |
switch_to_blog( $site_id ); |
| 93 |
$this->uninstall_current_site(); |
| 94 |
} |
| 95 |
|
| 96 |
restore_current_blog(); |
| 97 |
|
| 98 |
// Remove network snippets table. |
| 99 |
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ms_snippets" ); |
| 100 |
|
| 101 |
// Remove saved options. |
| 102 |
delete_site_option( 'code_snippets_version' ); |
| 103 |
delete_site_option( 'recently_active_snippets' ); |
| 104 |
delete_site_option( 'recently_activated_snippets' ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Clean up directory used to store snippet flat files. |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function delete_flat_files_directory() { |
| 113 |
$flat_files_dir = WP_CONTENT_DIR . '/code-snippets'; |
| 114 |
|
| 115 |
if ( ! is_dir( $flat_files_dir ) ) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
| 120 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 121 |
} |
| 122 |
|
| 123 |
global $wp_filesystem; |
| 124 |
WP_Filesystem(); |
| 125 |
|
| 126 |
if ( $wp_filesystem && $wp_filesystem->is_dir( $flat_files_dir ) ) { |
| 127 |
$wp_filesystem->delete( $flat_files_dir, true ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|