PluginProbe
Code Snippets / trunk
Code Snippets vtrunk
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / php / Core / Uninstaller.php

Uninstaller.php in Code Snippets trunk, at php/Core/Uninstaller.php

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