forked from ThemeAvenue/BetterOptin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.php
104 lines (80 loc) · 2.4 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
<?php
/**
* Fired when the plugin is uninstalled.
*
* @package BetterOptin
* @author ThemeAvenue <[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;
}
/**
* Delete the custom table
*/
global $wpdb;
$table = $wpdb->prefix . 'wpbo_analytics';
$wpdb->query( "DROP TABLE IF EXISTS $table" );
/**
* Get options before deletion
*/
$options = maybe_unserialize( get_option( 'wpbo_options' ) );
/* Delete default options */
delete_option( 'wpbo_options' );
delete_option( 'wpbo_fonts' );
/* Delete transients */
delete_transient( 'wpbo_fonts' );
delete_transient( 'wpbo_documentation' );
/* Delete database version */
delete_option( 'wpbo_db_version' );
/**
* Delete relationships
*/
delete_option( 'wpbo_popup_relationships' );
/**
* Delete all popups and their metas.
*/
$args = array(
'post_type' => 'wpbo-popup',
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
'posts_per_page' => -1,
);
$popups = new WP_Query( $args );
if( count( $popups->posts ) >= 1 ) {
foreach( $popups->posts as $popup )
wp_delete_post( $popup->ID, true );
}
/**
* Deal with the users now
*/
if( isset( $options['wp_delete_users_uninstall'] ) && '1' == $options['wp_delete_users_uninstall'] ) {
$reassign = 1; // The user that all posts will fall back to, other wise they will be deleted
$user_query = new WP_User_Query( array( 'meta_key' => 'wpbo_subscription', 'meta_value' => 'yes' ) );
/**
* Delete the subscribers from DB
*/
if( !empty( $user_query->results ) ) {
foreach( $user_query->results as $user ) {
wp_delete_user( $user->ID, $reassign );
}
}
} else {
$role = 'subscriber'; // The new role to assign to subscribers
$user_query = new WP_User_Query( array( 'meta_key' => 'wpbo_subscription', 'meta_value' => 'yes' ) );
if( !empty( $user_query->results ) ) {
foreach( $user_query->results as $user ) {
/* Update user role */
wp_update_user( array( 'ID' => $user->ID, 'role' => $role ) );
/* Possibly delete the marker */
if( isset( $options['wp_delete_users_marker'] ) && '1' == $options['wp_delete_users_marker'] )
delete_user_meta( $user->ID, 'wpbo_subscription', 'yes' );
}
}
}
/**
* Delete custom role
*/
remove_role( 'betteroptin' );