-
Notifications
You must be signed in to change notification settings - Fork 4
/
uninstall.php
109 lines (87 loc) · 3.26 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
<?php
/**
* Fired when the plugin is uninstalled.
*
* When populating this file, consider the following flow
* of control:
*
* - This method should be static
* - Check if the $_REQUEST content actually is the plugin name
* - Run an admin referrer check to make sure it goes through authentication
* - Verify the output of $_GET makes sense
* - Repeat with other user roles. Best directly by using the links/query string parameters.
* - Repeat things for multisite. Once for a single site in the network, once sitewide.
*
* This file may be updated more in future version of the Boilerplate; however, this is the
* general skeleton and outline for how the file should work.
*
* For more information, see the following discussion:
* https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/pull/123#issuecomment-28541913
*
* @link https://github.com/Books4Languages/pressbooks-metadata
* @since 0.16
*
* @package Pressbooks_Metadata
* @author Daniil Zhitnitskii @danzhik
*/
// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
//declaring global DB connection variable
global $wpdb;
//get all the sites for multisite, if not a multisite, set blog id to 1
if (is_multisite()) {
$blogs_ids = get_sites();
} else {
$blogs_ids = [1];
}
//delete plugin options and posts/chapter related metadata from every site
foreach( $blogs_ids as $b ){
//if multisite, each iteration changes site
if (is_multisite()) {
switch_to_blog( $b->blog_id );
}
//get all the options from database
$all_options = wp_load_alloptions();
$plugin_options = [];
$related_meta = [];
//check if PressBooks plugin is used
if ((@include_once( WP_PLUGIN_DIR . '/pressbooks/pressbooks.php')) &&
//Checking if the plugin is active
is_plugin_active('pressbooks/pressbooks.php')) {
$pb = true;
}
else{
$pb = false;
}
//gather all post types, including built-in of without PressBooks
if($pb){
$allPostTypes = get_post_types( array( 'public' => true, '_builtin' => false )) ;
}else{
$allPostTypes = get_post_types( array( 'public' => true)) ;
}
$allPostTypes['site-meta'] = 'site-meta';
$allPostTypes['metadata'] = 'metadata';
//extract plugin options from all options
foreach ( $all_options as $name => $value ) {
if ( stristr( $name, '_properties_dis' ) || stristr( $name, 'schema_properties' ) || stristr( $name, 'schemaTypes' )
|| stristr( $name, '_type_overwrite' ) || stristr($name, 'saoverwr') || stristr($name, '_checkbox')
|| stristr( $name, 'property_network_value' ) || $name == 'schema_locations' || $name == 'jsonld_output'
|| $name == 'parent_filter_settings') {
$plugin_options[ $name ] = $value;
}
}
//delete plugin options
foreach ( $plugin_options as $key => $value ) {
if ( get_option( $key ) || get_option($key, 'nonex') !== 'nonex') {
delete_option( $key );
}
}
// Delete plugin related posts' meta
//if blog is root, do not add blog number to table name
$blog_id = $b->blog_id == 1 || $b == 1 ? '' : $b->blog_id.'_';
//DELETE query to postmeta database
$wpdb->query( "DELETE FROM `".$wpdb->prefix.$blog_id."postmeta` WHERE `meta_key` LIKE 'pb%type%'");
$wpdb->query( "DELETE FROM `".$wpdb->prefix.$blog_id."postmeta` WHERE `meta_key` LIKE 'pb%vocab%'");
}