-
Notifications
You must be signed in to change notification settings - Fork 12
/
uninstall.php
95 lines (72 loc) · 2.21 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
<?php
/**
* Handle uninstalling the plugin
*
* @package Mesh
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) || ! WP_UNINSTALL_PLUGIN || dirname( WP_UNINSTALL_PLUGIN ) !== dirname( plugin_basename( __FILE__ ) ) ) {
status_header( 404 );
exit;
}
/**
*
* @param $post_type
*/
function mesh_uninstall_delete_posts( $post_type ) {
$post_types = array( 'mesh_template', 'mesh_section' );
if ( ! in_array( $post_type, $post_types, true ) ) {
return;
}
$total = 0;
$args = array(
'post_type' => $post_type,
'post_status' => array( 'any', 'auto-draft' ),
'posts_per_page' => 200,
'no_found_rows' => true,
'fields' => 'ids',
'offset' => 0,
);
$mesh_posts = new WP_Query( $args );
if ( $mesh_posts->have_posts() ) {
while ( $mesh_posts->have_posts() && $total < 10000 ) {
$mesh_posts->the_post();
wp_delete_post( get_the_ID(), true );
$total += $args['posts_per_page'];
}
$mesh_posts = new WP_Query( $args );
}
}
/**
* Delete all terms in in the given taxonomy
*
* @param $taxonomy
*/
function mesh_uninstall_delete_terms( $taxonomy ) {
$taxonomies = array( 'mesh_template_usage' );
if ( ! in_array( $taxonomy, $taxonomies, true ) ) { // Only allow our taxonomies to be removed using our method
return;
}
if ( ! taxonomy_exists( $taxonomy ) ) { // Make sure the taxonomy actually exists.
return;
}
$terms = get_terms( array( 'taxonomy' => $taxonomy, 'fields' => 'ids', 'hide_empty' => false ) );
foreach ( $terms as $value ) {
wp_delete_term( $value, $taxonomy );
}
}
/**
* If our mesh uninstall setting is enabled clear everything out on uninstall/delete of the plugin
*/
$mesh_settings = get_option( 'mesh_settings' );
if ( ! empty( $mesh_settings['uninstall'] ) ) {
// Delete all Mesh related custom post types
mesh_uninstall_delete_posts( 'mesh_section' ); // Delete all mesh sections (this includes children)
mesh_uninstall_delete_posts( 'mesh_template' ); // Delete all mesh templates we have
// Delete taxonomy terms
mesh_uninstall_delete_terms( 'mesh_template_usage' );
// Delete all Mesh Settings.
delete_option( 'mesh_settings' );
delete_option( 'mesh_post_types' );
delete_option( 'mesh_version' );
delete_option( 'mesh_activation' );
}