Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

glsr_uninstall_minimal_drop_foreign_keys()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 16
c 2
b 1
f 0
nc 6
nop 0
dl 0
loc 26
rs 9.7333
1
<?php
2
3
defined('WP_UNINSTALL_PLUGIN') || die;
4
5
function glsr_uninstall() {
6
    $settings = get_option('site_reviews_v5');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

6
    $settings = /** @scrutinizer ignore-call */ get_option('site_reviews_v5');
Loading history...
7
    $uninstall = isset($settings['settings']['general']['delete_data_on_uninstall'])
8
        ? $settings['settings']['general']['delete_data_on_uninstall']
9
        : '';
10
    if ('all' === $uninstall) {
11
        glsr_uninstall_all();
12
    }
13
    if ('minimal' === $uninstall) {
14
        glsr_uninstall_minimal();
15
        glsr_uninstall_minimal_drop_foreign_keys();
16
    }
17
    delete_option('glsr_activated');
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
    /** @scrutinizer ignore-call */ 
18
    delete_option('glsr_activated');
Loading history...
18
}
19
20
function glsr_uninstall_all() {
21
    glsr_uninstall_minimal();
22
    glsr_uninstall_all_delete_reviews();
23
    glsr_uninstall_all_delete_tables();
24
    glsr_uninstall_all_delete_logs();
25
    glsr_uninstall_all_cleanup();
26
}
27
28
function glsr_uninstall_all_cleanup() {
29
    global $wpdb;
30
    // delete any remaining options
31
    $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '%glsr_%'");
32
    $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE 'site-review-category%'");
33
    // optimise affected database tables
34
    $wpdb->query("OPTIMIZE TABLE {$wpdb->options}");
35
    $wpdb->query("OPTIMIZE TABLE {$wpdb->postmeta}");
36
    $wpdb->query("OPTIMIZE TABLE {$wpdb->posts}");
37
    $wpdb->query("OPTIMIZE TABLE {$wpdb->termmeta}");
38
    $wpdb->query("OPTIMIZE TABLE {$wpdb->terms}");
39
    $wpdb->query("OPTIMIZE TABLE {$wpdb->usermeta}");
40
    // finally, flush the entire cache
41
    wp_cache_flush();
0 ignored issues
show
Bug introduced by
The function wp_cache_flush was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
    /** @scrutinizer ignore-call */ 
42
    wp_cache_flush();
Loading history...
42
}
43
44
function glsr_uninstall_all_delete_logs() {
45
    require_once ABSPATH.'/wp-admin/includes/file.php';
46
    global $wp_filesystem;
47
    // delete the Site Reviews logs directory
48
    if (WP_Filesystem()) {
0 ignored issues
show
Bug introduced by
The function WP_Filesystem was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
    if (/** @scrutinizer ignore-call */ WP_Filesystem()) {
Loading history...
49
        $uploads = wp_upload_dir();
0 ignored issues
show
Bug introduced by
The function wp_upload_dir was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $uploads = /** @scrutinizer ignore-call */ wp_upload_dir();
Loading history...
50
        $dirname = trailingslashit($uploads['basedir'].'/site-reviews/logs');
0 ignored issues
show
Bug introduced by
The function trailingslashit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $dirname = /** @scrutinizer ignore-call */ trailingslashit($uploads['basedir'].'/site-reviews/logs');
Loading history...
51
        $wp_filesystem->rmdir(wp_normalize_path($dirname), true);
0 ignored issues
show
Bug introduced by
The function wp_normalize_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $wp_filesystem->rmdir(/** @scrutinizer ignore-call */ wp_normalize_path($dirname), true);
Loading history...
52
    }
53
}
54
55
function glsr_uninstall_all_delete_reviews() {
56
    global $wpdb;
57
    // delete all reviews and revisions
58
    $wpdb->query("
59
        DELETE p, pr, tr, pm
60
        FROM {$wpdb->posts} p
61
        LEFT JOIN {$wpdb->posts} pr ON (p.ID = pr.post_parent AND pr.post_type = 'revision')
62
        LEFT JOIN {$wpdb->term_relationships} tr ON (p.ID = tr.object_id)
63
        LEFT JOIN {$wpdb->postmeta} pm ON (p.ID = pm.post_id)
64
        WHERE p.post_type = 'site-review'
65
    ");
66
    // delete all review categories
67
    $wpdb->query("
68
        DELETE tt, t, tm
69
        FROM {$wpdb->term_taxonomy} tt
70
        LEFT JOIN {$wpdb->terms} t ON (tt.term_id = t.term_id)
71
        LEFT JOIN {$wpdb->termmeta} tm ON (tt.term_id = tm.term_id)
72
        WHERE tt.taxonomy = 'site-review-category'
73
    ");
74
    // delete all assigned_posts meta
75
    $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '%glsr_%'");
76
    // delete all assigned_users meta
77
    $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE '%glsr_%'");
78
}
79
80
function glsr_uninstall_all_delete_tables() {
81
    global $wpdb;
82
    // order is intentional
83
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}glsr_assigned_users");
84
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}glsr_assigned_terms");
85
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}glsr_assigned_posts");
86
    $wpdb->query("DROP TABLE IF EXISTS {$wpdb->prefix}glsr_ratings");
87
}
88
89
function glsr_uninstall_minimal() {
90
    global $wpdb;
91
    $options = array(
92
        'geminilabs_site_reviews-v2', // v2 settings
93
        'geminilabs_site_reviews_settings', // v1 settings
94
        'site_reviews_v3', // v3 settings
95
        'site_reviews_v4', // v4 settings
96
        'site_reviews_v5', // v5 settings
97
        'theme_mods_site-reviews',
98
        'widget_glsr_site-reviews',
99
        'widget_glsr_site-reviews-form',
100
        'widget_glsr_site-reviews-summary',
101
    );
102
    foreach ($options as $option) {
103
        delete_option($option);
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        /** @scrutinizer ignore-call */ 
104
        delete_option($option);
Loading history...
104
    }
105
    delete_transient('glsr_cloudflare_ips');
0 ignored issues
show
Bug introduced by
The function delete_transient was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
    /** @scrutinizer ignore-call */ 
106
    delete_transient('glsr_cloudflare_ips');
Loading history...
106
    delete_transient('glsr_migrations');
107
    delete_transient('glsr_remote_post_test');
108
    $wpdb->query("DELETE FROM {$wpdb->usermeta} WHERE meta_key = '_glsr_notices'");
109
}
110
111
function glsr_uninstall_minimal_drop_foreign_keys() {
112
    global $wpdb;
113
    $siteId = '';
114
    if (get_current_blog_id() > 1) {
0 ignored issues
show
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
    if (/** @scrutinizer ignore-call */ get_current_blog_id() > 1) {
Loading history...
115
        $siteId = '_'.get_current_blog_id();
116
    }
117
    $constraints = [ // order is intentional
118
        "{$wpdb->prefix}glsr_assigned_users" => "glsr_assigned_users_user_id_foreign{$siteId}",
119
        "{$wpdb->prefix}glsr_assigned_terms" => "glsr_assigned_terms_term_id_foreign{$siteId}",
120
        "{$wpdb->prefix}glsr_assigned_posts" => "glsr_assigned_posts_post_id_foreign{$siteId}",
121
        "{$wpdb->prefix}glsr_ratings" => "glsr_assigned_posts_review_id_foreign{$siteId}",
122
    ];
123
    foreach ($constraints as $table => $constraint) { // This should work for both MyISAM and InnoDB engines
124
        $foreignKey = $wpdb->get_var("
125
            SELECT INDEX_NAME
126
            FROM INFORMATION_SCHEMA.STATISTICS
127
            WHERE INDEX_SCHEMA = '{$wpdb->dbname}' AND TABLE_NAME = '{$table}' AND INDEX_NAME = '{$constraint}'
128
        ");
129
        if (!empty($foreignKey)) {
130
            $wpdb->query("
131
                ALTER TABLE {$table} DROP FOREIGN KEY {$constraint};
132
            ");
133
        }
134
    }
135
    // delete the saved database version
136
    delete_option('glsr_db_version');
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
    /** @scrutinizer ignore-call */ 
137
    delete_option('glsr_db_version');
Loading history...
137
}
138
139
if (!is_multisite()) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
if (!/** @scrutinizer ignore-call */ is_multisite()) {
Loading history...
140
    glsr_uninstall();
141
    return;
142
}
143
if (!function_exists('get_sites')) {
144
    global $wpdb;
145
    $siteIds = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}");
146
} else {
147
    $siteIds = get_sites(array('fields' => 'ids'));
148
}
149
foreach ($siteIds as $siteId) {
150
    switch_to_blog($siteId);
0 ignored issues
show
Bug introduced by
The function switch_to_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
    /** @scrutinizer ignore-call */ 
151
    switch_to_blog($siteId);
Loading history...
151
    glsr_uninstall();
152
    restore_current_blog();
0 ignored issues
show
Bug introduced by
The function restore_current_blog was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
    /** @scrutinizer ignore-call */ 
153
    restore_current_blog();
Loading history...
153
}
154