-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuninstall.php
119 lines (106 loc) · 3.72 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
<?php
/**
* Cleans up the plugin options, custom post type posts, and custom database tables.
*
* @copyright Copyright (c) 2013-2022, <Michael Uno>
* @author Michael Uno
* @authorurl http://michaeluno.jp
* @since 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Plugin specific constant.
* We are going to load the main file to get the registry class. And in the main file,
* if this constant is set, it will return after declaring the registry class.
*/
if ( ! defined( 'DOING_PLUGIN_UNINSTALL' ) ) {
define( 'DOING_PLUGIN_UNINSTALL', true );
}
/**
* Set the main plugin file name here.
*/
$_sMainPluginFileName = 'amazon-auto-links.php';
if ( file_exists( dirname( __FILE__ ). '/' . $_sMainPluginFileName ) ) {
include( $_sMainPluginFileName );
}
if ( ! class_exists( 'AmazonAutoLinks_Registry' ) ) {
return;
}
// Delete the plugin temporary directory
$_aTempDirPaths = array(
AmazonAutoLinks_Registry::getPluginSiteTempDirPath(),
untrailingslashit( wp_normalize_path( sys_get_temp_dir() ) ) . '/WPAAL', // old deprecated one
);
foreach( $_aTempDirPaths as $_sDirPath ) {
if ( AmazonAutoLinks_Utility::doesDirectoryExist( $_sDirPath ) ) {
AmazonAutoLinks_Utility::removeDirectoryRecursive( $_sDirPath );
}
}
// Delete transients
$_aPrefixes = array(
AmazonAutoLinks_Registry::TRANSIENT_PREFIX, // the plugin transients
'apf_', // the admin page framework transients
);
foreach( $_aPrefixes as $_sPrefix ) {
if ( ! $_sPrefix ) {
continue;
}
$GLOBALS[ 'wpdb' ]->query( "DELETE FROM `" . $GLOBALS[ 'table_prefix' ] . "options` WHERE `option_name` LIKE ( '_transient_%{$_sPrefix}%' )" );
$GLOBALS[ 'wpdb' ]->query( "DELETE FROM `" . $GLOBALS[ 'table_prefix' ] . "options` WHERE `option_name` LIKE ( '_transient_timeout_%{$_sPrefix}%' )" );
}
// Delete options
$_aOptions = get_option( AmazonAutoLinks_Registry::$aOptionKeys[ 'main' ], array() );
$_bDelete = isset( $_aOptions[ 'reset_settings' ][ 'reset_on_uninstall' ] )
? $_aOptions[ 'reset_settings' ][ 'reset_on_uninstall' ]
: false;
if ( ! $_bDelete ) {
return;
}
// Delete Pages
$_aDisclosurePage = AmazonAutoLinks_WPUtility::getPostByGUID( 'https://aal-affiliate-disclosure-page', 'ID' );
$_iDisclosurePageID = AmazonAutoLinks_WPUtility::getElement( $_aDisclosurePage, 'ID' );
if ( $_iDisclosurePageID ) {
wp_delete_post( $_iDisclosurePageID, true );
}
// User Meta
foreach( AmazonAutoLinks_Registry::$aUserMeta as $_sUserMetaKey ) {
delete_metadata(
'user', // the meta type
0, // this doesn't actually matter in this call
$_sUserMetaKey, // the meta key to be removed everywhere
'', // this also doesn't actually matter in this call
true // tells the function "yes, please remove them all"
);
}
array_walk_recursive(
AmazonAutoLinks_Registry::$aOptionKeys, // subject array
'delete_option' // function name
);
// Delete tables
$_oTables = new AmazonAutoLinks_DatabaseTables();
$_oTables->uninstallAll();
// [3.6.6+] Delete Custom Post Type Posts
foreach( AmazonAutoLinks_Registry::$aPostTypes as $_sKey => $_sPostTypeSlug ) {
_deleteAmazonAutoLinksPosts( $_sPostTypeSlug );
}
/**
* @param string $sPostTypeSlug
* @since 3.6.6
*/
function _deleteAmazonAutoLinksPosts( $sPostTypeSlug ) {
$_oResults = new WP_Query(
array(
'post_type' => $sPostTypeSlug,
'posts_per_page' => -1, // `-1` for all
'fields' => 'ids', // return only post IDs by default.
)
);
foreach( $_oResults->posts as $_iPostID ) {
wp_delete_post( $_iPostID, true );
}
}