PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
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 3.0.1 All 64 releases
code-snippets / php / Core / Uninstaller.php

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

131 lines 3.2 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 /**
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