-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.php
More file actions
142 lines (125 loc) · 2.97 KB
/
uninstall.php
File metadata and controls
142 lines (125 loc) · 2.97 KB
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
<?php
/**
* {{name}} Uninstall
*
* Fired when the plugin is uninstalled to clean up all plugin data.
*
* @package {{namespace}}
*/
// If uninstall not called from WordPress, exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
global $wpdb;
$plugin_slug = '{{plugin_slug}}';
$custom_post_type = '{{post_type_slug}}';
$custom_taxonomy = '{{taxonomy_slug}}';
/**
* Delete all posts of the custom post type.
*/
$all_posts = get_posts(
array(
'post_type' => $custom_post_type,
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
)
);
foreach ( $all_posts as $current_post_id ) {
wp_delete_post( $current_post_id, true );
}
/**
* Delete all terms from the custom taxonomy.
*/
$all_terms = get_terms(
array(
'taxonomy' => $custom_taxonomy,
'hide_empty' => false,
'fields' => 'ids',
)
);
if ( ! is_wp_error( $all_terms ) ) {
foreach ( $all_terms as $term_id ) {
wp_delete_term( $term_id, $custom_taxonomy );
}
}
/**
* Delete plugin options.
*/
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
$wpdb->esc_like( $plugin_slug . '_' ) . '%'
)
);
/**
* Delete transients.
*/
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
$wpdb->esc_like( '_transient_' . $plugin_slug . '_' ) . '%',
$wpdb->esc_like( '_site_transient_' . $plugin_slug . '_' ) . '%'
)
);
/**
* Delete user meta.
*/
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
$wpdb->esc_like( $plugin_slug . '_' ) . '%'
)
);
/**
* Delete post meta (including ACF fields).
*/
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
$wpdb->esc_like( $plugin_slug . '_' ) . '%'
)
);
/**
* Delete term meta.
*/
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE %s",
$wpdb->esc_like( $plugin_slug . '_' ) . '%'
)
);
/**
* Clear scheduled cron hooks.
*/
$hooks = array(
"{$plugin_slug}_cron",
"{$plugin_slug}_daily",
"{$plugin_slug}_hourly",
"{$plugin_slug}_cleanup",
);
foreach ( $hooks as $hook ) {
$timestamp = wp_next_scheduled( $hook );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, $hook );
}
wp_clear_scheduled_hook( $hook );
}
/**
* Flush rewrite rules.
*/
flush_rewrite_rules();
/**
* Clear any cached data.
*/
wp_cache_flush();