PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.1
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 / uninstall.php

uninstall.php in Code Snippets 3.8.1, at php/uninstall.php

111 lines 2.5 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\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