-
Notifications
You must be signed in to change notification settings - Fork 4
/
sp_uninstall.php
112 lines (100 loc) · 4.81 KB
/
sp_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
<?php
/**
* Uninstalls SmartPost
* User: ryagudin
* Date: 4/8/14
* Time: 10:33 AM
*/
if ( !class_exists("sp_uninstall") ) {
class sp_uninstall{
/**
* Renders uninstall settings and details
*/
public static function render_uninstall_settings(){
?>
<p style="color: red;"><img src="<?php echo SP_IMAGE_PATH .'/alert.png'?>" stye="vertical-align: text-top;" /> Important: please read the below info before uninstalling this plugin!</p>
<h3>Saving SmartPost shortcodes to HTML form</h3>
<p>In the current version of this plugin, SmartPost "post component" content blocks are saved in a separate table in the WordPress database.</p>
<p>Completely deleting or uninstalling the plugin will result in the content blocks not rendering properly or being completely lost.</p>
<p>Before uninstalling or removing the plugin completely, it is recommended to convert all the components to HTML form by clicking the button below.</p>
<button id="save_sp_posts_to_html" class="button">Save SmartPost posts as HTML</button>
<p></p>
<h3>Uninstall SmartPost</h3>
<p>Deleting or de-activating this plugin will not remove the database tables and options due to its destructive nature (content may be permanently lost!).</p>
<p>If you are sure you want to completely remove this plugin, including all the database tables and options, you can do so by clicking the button below.</p>
<p style="color:red;">WARNING: do NOT click this button unless you really know what you're getting yourself into: <button id="uninstall_smartpost" class="button">Uninstall SmartPost</button></p>
<?php
}
/**
* Saves component data to posts
*/
public static function uninstall_sp_data(){
global $wpdb;
// Save all SmartPost posts into HTML format
$sp_cat_ids = get_option( 'sp_categories' );
foreach($sp_cat_ids as $cat_id){
$sp_posts = get_posts( array( 'category' => $cat_id, 'numberposts' => -1 ) );
if( !empty( $sp_posts ) ){
foreach( $sp_posts as $post ){
if(sp_post::is_sp_post( $post->ID) ){
$sp_post_comps = sp_post::get_components_from_ID( $post->ID );
$post_comp_html = '';
$wpdb->is_single = true;
$post->post_content = self::strip_shortcode('sp_component', $post->post_content);
foreach( $sp_post_comps as $sp_post_comp ){
$post_comp_html .= $sp_post_comp->render();
}
$post->post_content .= $post_comp_html;
wp_update_post( $post );
$wpdb->is_single = false;
}
}
}
}
// Call uninstall() for each component type
$component_types = sp_core::get_component_types();
foreach( $component_types as $id => $comp_type ){
$class_name = 'sp_cat' . $comp_type->name;
if( class_exists( $class_name ) ){
$class_name::uninstall();
}
}
// Remove misc global options
delete_option( 'sp_categories' );
delete_option( 'sp_db_version' );
delete_option( 'sp_defaultCat' );
delete_option( 'sp_cat_icons' );
delete_option( 'sp_responseCats' );
delete_option( 'sp_cat_save_error' );
// Delete all SmartPost tables
$sp_tables = array( 'sp_postComponents', 'sp_catComponents', 'sp_compTypes');
foreach( $sp_tables as $sp_table ){
$table_name = $wpdb->prefix . $sp_table;
$wpdb->query( $wpdb->prepare( "DROP TABLE $table_name" ) );
}
}
/**
* Call this function to uninstall SmartPost
*/
public static function uninstall_smartpost(){
if ( __FILE__ != WP_UNINSTALL_PLUGIN )
return;
// For Single site
if ( !is_multisite() )
{
self::uninstall_sp_data();
}else{
// For regular options.
global $wpdb;
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
$original_blog_id = get_current_blog_id();
foreach ( $blog_ids as $blog_id )
{
switch_to_blog( $blog_id );
self::uninstall_sp_data();
}
switch_to_blog( $original_blog_id );
}
}
}
}