PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
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 4.0.0-beta.1, at php/Core/Uninstaller.php

130 lines 3.1 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( 'cs_codevault_snippets' );
76 delete_transient( 'cs_local_to_cloud_map' );
77 }
78
79 /**
80 * Clean up data created by this plugin on multisite.
81 *
82 * phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange
83 */
84 private function uninstall_multisite() {
85 global $wpdb;
86
87 // Loop through sites.
88 $blog_ids = get_sites( [ 'fields' => 'ids' ] );
89
90 foreach ( $blog_ids as $site_id ) {
91 switch_to_blog( $site_id );
92 $this->uninstall_current_site();
93 }
94
95 restore_current_blog();
96
97 // Remove network snippets table.
98 $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ms_snippets" );
99
100 // Remove saved options.
101 delete_site_option( 'code_snippets_version' );
102 delete_site_option( 'recently_active_snippets' );
103 delete_site_option( 'recently_activated_snippets' );
104 }
105
106 /**
107 * Clean up directory used to store snippet flat files.
108 *
109 * @return void
110 */
111 private function delete_flat_files_directory() {
112 $flat_files_dir = WP_CONTENT_DIR . '/code-snippets';
113
114 if ( ! is_dir( $flat_files_dir ) ) {
115 return;
116 }
117
118 if ( ! function_exists( 'request_filesystem_credentials' ) ) {
119 require_once ABSPATH . 'wp-admin/includes/file.php';
120 }
121
122 global $wp_filesystem;
123 WP_Filesystem();
124
125 if ( $wp_filesystem && $wp_filesystem->is_dir( $flat_files_dir ) ) {
126 $wp_filesystem->delete( $flat_files_dir, true );
127 }
128 }
129 }
130