forked from polylang/polylang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.php
192 lines (170 loc) · 4.33 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* @package Polylang
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { // If uninstall not called from WordPress exit.
exit;
}
/**
* Manages Polylang uninstallation.
* The goal is to remove **all** Polylang related data in db.
*
* @since 0.5
*/
class PLL_Uninstall {
/**
* Constructor: manages uninstall for multisite.
*
* @since 0.5
*/
public function __construct() {
global $wpdb;
// Don't do anything except if the constant PLL_REMOVE_ALL_DATA is explicitly defined and true.
if ( ! defined( 'PLL_REMOVE_ALL_DATA' ) || ! PLL_REMOVE_ALL_DATA ) {
return;
}
// Check if it is a multisite uninstall - if so, run the uninstall function for each blog id.
if ( is_multisite() ) {
foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) as $blog_id ) {
switch_to_blog( $blog_id );
$this->uninstall();
}
restore_current_blog();
} else {
$this->uninstall();
}
}
/**
* Removes **all** plugin data.
*
* @since 0.5
*/
public function uninstall() {
global $wpdb;
do_action( 'pll_uninstall' );
// We need to register the taxonomies.
$pll_taxonomies = array(
'language',
'term_language',
'post_translations',
'term_translations',
);
foreach ( $pll_taxonomies as $taxonomy ) {
register_taxonomy(
$taxonomy,
null,
array(
'label' => false,
'public' => false,
'query_var' => false,
'rewrite' => false,
)
);
}
$languages = get_terms(
array(
'taxonomy' => 'language',
'hide_empty' => false,
)
);
// Delete users options.
delete_metadata( 'user', 0, 'pll_filter_content', '', true );
delete_metadata( 'user', 0, 'pll_dismissed_notices', '', true ); // Legacy meta.
foreach ( $languages as $lang ) {
delete_metadata( 'user', 0, "description_{$lang->slug}", '', true );
}
// Delete menu language switchers.
$ids = get_posts(
array(
'post_type' => 'nav_menu_item',
'numberposts' => -1,
'nopaging' => true,
'fields' => 'ids',
'meta_key' => '_pll_menu_item',
)
);
foreach ( $ids as $id ) {
wp_delete_post( $id, true );
}
/*
* Backward compatibility with Polylang < 3.4.
* Delete the legacy strings translations.
*/
register_post_type(
'polylang_mo',
array(
'rewrite' => false,
'query_var' => false,
)
);
$ids = get_posts(
array(
'post_type' => 'polylang_mo',
'post_status' => 'any',
'numberposts' => -1,
'nopaging' => true,
'fields' => 'ids',
)
);
foreach ( $ids as $id ) {
wp_delete_post( $id, true );
}
// Delete all what is related to languages and translations.
$term_ids = array();
$tt_ids = array();
$terms = get_terms(
array(
'taxonomy' => $pll_taxonomies,
'hide_empty' => false,
)
);
foreach ( $terms as $term ) {
$term_ids[] = (int) $term->term_id;
$tt_ids[] = (int) $term->term_taxonomy_id;
}
if ( ! empty( $term_ids ) ) {
$term_ids = array_unique( $term_ids );
$wpdb->query(
$wpdb->prepare(
sprintf(
"DELETE FROM {$wpdb->terms} WHERE term_id IN (%s)",
implode( ',', array_fill( 0, count( $term_ids ), '%d' ) )
),
$term_ids
)
);
$wpdb->query(
$wpdb->prepare(
sprintf(
"DELETE FROM {$wpdb->term_taxonomy} WHERE term_id IN (%s)",
implode( ',', array_fill( 0, count( $term_ids ), '%d' ) )
),
$term_ids
)
);
$wpdb->query(
$wpdb->prepare(
sprintf(
"DELETE FROM {$wpdb->termmeta} WHERE term_id IN (%s) AND meta_key='_pll_strings_translations'",
implode( ',', array_fill( 0, count( $term_ids ), '%d' ) )
),
$term_ids
)
);
}
if ( ! empty( $tt_ids ) ) {
$tt_ids = array_unique( $tt_ids );
$wpdb->query( $wpdb->prepare( sprintf( "DELETE FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN (%s)", implode( ',', array_fill( 0, count( $tt_ids ), '%d' ) ) ), $tt_ids ) );
}
// Delete options.
delete_option( 'polylang' );
delete_option( 'widget_polylang' ); // Automatically created by WP.
delete_option( 'polylang_wpml_strings' ); // Strings registered with icl_register_string.
delete_option( 'polylang_licenses' );
delete_option( 'pll_dismissed_notices' );
delete_option( 'pll_language_from_content_available' );
// Delete transients.
delete_transient( 'pll_languages_list' );
}
}
new PLL_Uninstall();