forked from Awesome-Support/Awesome-Support
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.php
171 lines (139 loc) · 4.06 KB
/
uninstall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Fired when the plugin is uninstalled.
*
* @package Awesome Support/Uninstallation
* @author Julien Liabeuf <[email protected]>
* @license GPL-2.0+
* @link http://themeavenue.net
* @copyright 2014 ThemeAvenue
*/
// If uninstall not called from WordPress, then exit
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
if ( is_multisite() ) {
global $wpdb;
$blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A );
wpas_uninstall();
if ( $blogs ) {
foreach ( $blogs as $blog ) {
switch_to_blog( $blog['blog_id'] );
wpas_uninstall();
restore_current_blog();
}
}
}
else {
wpas_uninstall();
}
/**
* Uninstall function.
*
* The uninstall function will only proceed if
* the user explicitly asks for all data to be removed.
*
* @since 3.0.0
* @return void
*/
function wpas_uninstall() {
$options = maybe_unserialize( get_option( 'wpas_options' ) );
/* Make sure that the user wants to remove all the data. */
if ( isset( $options['delete_data'] ) || '1' === $options['delete_data'] ) {
/* Remove all plugin options. */
delete_option( 'wpas_options' );
delete_option( 'wpas_db_version' );
delete_option( 'wpas_version' );
/* Delete the plugin pages. */
wp_delete_post( intval( $options['ticket_submit'] ), true );
wp_delete_post( intval( $options['ticket_list'] ), true );
/**
* Delete all posts from all custom post types
* that the plugin created.
*/
$args = array(
'post_type' => array( 'ticket', 'ticket_reply', 'ticket_history', 'ticket_log' ),
'post_status' => array( 'any', 'trash', 'auto-draft' ),
'posts_per_page' => -1,
'no_found_rows' => true,
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
);
$posts = new WP_Query( $args );
/* Delete all post types and attachments */
foreach ( $posts->posts as $post ) {
wpas_delete_attachments( $post->ID );
wp_delete_post( $post->ID, true );
$upload_dir = wp_upload_dir();
$dirpath = trailingslashit( $upload_dir['basedir'] ) . "awesome-support/ticket_$post->ID";
if ( $post->post_parent == 0 ) {
/* Delete the uploads folder */
if ( is_dir( $dirpath ) ) {
rmdir( $dirpath );
}
/* Remove transients */
delete_transient( "wpas_activity_meta_post_$post->ID" );
}
}
/* Delete all tag terms. */
wpas_delete_taxonomy( 'ticket-tag' );
/**
* Delete all products if the taxonomy
* was in use on this install.
*/
if ( '1' === $options['support_products'] ) {
wpas_delete_taxonomy( 'product' );
}
}
}
/**
* Delete all terms of the given taxonomy.
*
* As the get_terms function is not available during uninstall
* (because the taxonomies are not registered), we need to work
* directly with the $wpdb class. The function gets all taxonomy terms
* and deletes them one by one.
*
* @since 3.0.0
* @param string $taxonomy Name of the taxonomy to delete
* @link http://wordpress.stackexchange.com/a/119353
* @return void
*/
function wpas_delete_taxonomy( $taxonomy ) {
global $wpdb;
$query = 'SELECT t.name, t.term_id
FROM ' . $wpdb->terms . ' AS t
INNER JOIN ' . $wpdb->term_taxonomy . ' AS tt
ON t.term_id = tt.term_id
WHERE tt.taxonomy = "' . $taxonomy . '"';
$terms = $wpdb->get_results($query);
foreach ( $terms as $term ) {
wp_delete_term( $term->term_id, $taxonomy );
}
}
/**
* Delete attachments.
*
* Delete all tickets and replies attachments.
*
* @since 3.0.0
* @param integer $post_id ID of the post to delete attachments from
* @return void
*/
function wpas_delete_attachments( $post_id ) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_page' => -1,
'post_parent' => $post_id,
'no_found_rows' => true,
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
);
$posts = new WP_Query( $args );
foreach ( $posts->posts as $post ) {
wp_delete_attachment( $post->ID, true );
}
}