| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { // If uninstall not called from WordPress exit. |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Manages Polylang uninstallation. |
| 12 |
* The goal is to remove **all** Polylang related data in db. |
| 13 |
* |
| 14 |
* @since 0.5 |
| 15 |
*/ |
| 16 |
class PLL_Uninstall { |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructor: manages uninstall for multisite. |
| 20 |
* |
| 21 |
* @since 0.5 |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
global $wpdb; |
| 25 |
|
| 26 |
// Don't do anything except if the constant PLL_REMOVE_ALL_DATA is explicitly defined and true. |
| 27 |
if ( ! defined( 'PLL_REMOVE_ALL_DATA' ) || ! PLL_REMOVE_ALL_DATA ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
// Check if it is a multisite uninstall - if so, run the uninstall function for each blog id. |
| 32 |
if ( is_multisite() ) { |
| 33 |
foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) as $blog_id ) { |
| 34 |
switch_to_blog( $blog_id ); |
| 35 |
$this->uninstall(); |
| 36 |
} |
| 37 |
restore_current_blog(); |
| 38 |
} else { |
| 39 |
$this->uninstall(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Removes **all** plugin data. |
| 45 |
* |
| 46 |
* @since 0.5 |
| 47 |
*/ |
| 48 |
public function uninstall() { |
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
do_action( 'pll_uninstall' ); |
| 52 |
|
| 53 |
// We need to register the taxonomies. |
| 54 |
$pll_taxonomies = array( |
| 55 |
'language', |
| 56 |
'term_language', |
| 57 |
'post_translations', |
| 58 |
'term_translations', |
| 59 |
); |
| 60 |
|
| 61 |
foreach ( $pll_taxonomies as $taxonomy ) { |
| 62 |
register_taxonomy( |
| 63 |
$taxonomy, |
| 64 |
null, |
| 65 |
array( |
| 66 |
'label' => false, |
| 67 |
'public' => false, |
| 68 |
'query_var' => false, |
| 69 |
'rewrite' => false, |
| 70 |
) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
$languages = get_terms( |
| 75 |
array( |
| 76 |
'taxonomy' => 'language', |
| 77 |
'hide_empty' => false, |
| 78 |
) |
| 79 |
); |
| 80 |
|
| 81 |
// Delete users options. |
| 82 |
delete_metadata( 'user', 0, 'pll_filter_content', '', true ); |
| 83 |
delete_metadata( 'user', 0, 'pll_dismissed_notices', '', true ); // Legacy meta. |
| 84 |
foreach ( $languages as $lang ) { |
| 85 |
delete_metadata( 'user', 0, "description_{$lang->slug}", '', true ); |
| 86 |
} |
| 87 |
|
| 88 |
// Delete menu language switchers. |
| 89 |
$ids = get_posts( |
| 90 |
array( |
| 91 |
'post_type' => 'nav_menu_item', |
| 92 |
'numberposts' => -1, |
| 93 |
'nopaging' => true, |
| 94 |
'fields' => 'ids', |
| 95 |
'meta_key' => '_pll_menu_item', |
| 96 |
) |
| 97 |
); |
| 98 |
|
| 99 |
foreach ( $ids as $id ) { |
| 100 |
wp_delete_post( $id, true ); |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* Backward compatibility with Polylang < 3.4. |
| 105 |
* Delete the legacy strings translations. |
| 106 |
*/ |
| 107 |
register_post_type( |
| 108 |
'polylang_mo', |
| 109 |
array( |
| 110 |
'rewrite' => false, |
| 111 |
'query_var' => false, |
| 112 |
) |
| 113 |
); |
| 114 |
$ids = get_posts( |
| 115 |
array( |
| 116 |
'post_type' => 'polylang_mo', |
| 117 |
'post_status' => 'any', |
| 118 |
'numberposts' => -1, |
| 119 |
'nopaging' => true, |
| 120 |
'fields' => 'ids', |
| 121 |
) |
| 122 |
); |
| 123 |
foreach ( $ids as $id ) { |
| 124 |
wp_delete_post( $id, true ); |
| 125 |
} |
| 126 |
|
| 127 |
// Delete all what is related to languages and translations. |
| 128 |
$term_ids = array(); |
| 129 |
$tt_ids = array(); |
| 130 |
|
| 131 |
$terms = get_terms( |
| 132 |
array( |
| 133 |
'taxonomy' => $pll_taxonomies, |
| 134 |
'hide_empty' => false, |
| 135 |
) |
| 136 |
); |
| 137 |
|
| 138 |
foreach ( $terms as $term ) { |
| 139 |
$term_ids[] = (int) $term->term_id; |
| 140 |
$tt_ids[] = (int) $term->term_taxonomy_id; |
| 141 |
} |
| 142 |
|
| 143 |
if ( ! empty( $term_ids ) ) { |
| 144 |
$term_ids = array_unique( $term_ids ); |
| 145 |
$wpdb->query( |
| 146 |
$wpdb->prepare( |
| 147 |
sprintf( |
| 148 |
"DELETE FROM {$wpdb->terms} WHERE term_id IN (%s)", |
| 149 |
implode( ',', array_fill( 0, count( $term_ids ), '%d' ) ) |
| 150 |
), |
| 151 |
$term_ids |
| 152 |
) |
| 153 |
); |
| 154 |
$wpdb->query( |
| 155 |
$wpdb->prepare( |
| 156 |
sprintf( |
| 157 |
"DELETE FROM {$wpdb->term_taxonomy} WHERE term_id IN (%s)", |
| 158 |
implode( ',', array_fill( 0, count( $term_ids ), '%d' ) ) |
| 159 |
), |
| 160 |
$term_ids |
| 161 |
) |
| 162 |
); |
| 163 |
$wpdb->query( |
| 164 |
$wpdb->prepare( |
| 165 |
sprintf( |
| 166 |
"DELETE FROM {$wpdb->termmeta} WHERE term_id IN (%s) AND meta_key='_pll_strings_translations'", |
| 167 |
implode( ',', array_fill( 0, count( $term_ids ), '%d' ) ) |
| 168 |
), |
| 169 |
$term_ids |
| 170 |
) |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
if ( ! empty( $tt_ids ) ) { |
| 175 |
$tt_ids = array_unique( $tt_ids ); |
| 176 |
$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 ) ); |
| 177 |
} |
| 178 |
|
| 179 |
// Delete options. |
| 180 |
delete_option( 'polylang' ); |
| 181 |
delete_option( 'widget_polylang' ); // Automatically created by WP. |
| 182 |
delete_option( 'polylang_wpml_strings' ); // Strings registered with icl_register_string. |
| 183 |
delete_option( 'polylang_licenses' ); |
| 184 |
delete_option( 'pll_dismissed_notices' ); |
| 185 |
delete_option( 'pll_language_from_content_available' ); |
| 186 |
|
| 187 |
// Delete transients. |
| 188 |
delete_transient( 'pll_languages_list' ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
new PLL_Uninstall(); |
| 193 |
|