| @@ -1,193 +1,65 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { // If uninstall not called from WordPress exit. | |
| 7 | - exit; | |
| 8 | -} | |
| 3 | +class Polylang_Uninstall { | |
| 9 | 4 | |
| 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() { | |
| 5 | + function __construct() { | |
| 24 | 6 | global $wpdb; |
| 25 | 7 | |
| 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 ); | |
| 8 | + // check if it is a multisite uninstall - if so, run the uninstall function for each blog id | |
| 9 | + if (is_multisite()) { | |
| 10 | + foreach ($wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs")) as $blog_id) { | |
| 11 | + switch_to_blog($blog_id); | |
| 35 | 12 | $this->uninstall(); |
| 36 | 13 | } |
| 37 | 14 | restore_current_blog(); |
| 38 | - } else { | |
| 15 | + } | |
| 16 | + else | |
| 39 | 17 | $this->uninstall(); |
| 40 | - } | |
| 41 | 18 | } |
| 42 | 19 | |
| 43 | - /** | |
| 44 | - * Removes **all** plugin data. | |
| 45 | - * | |
| 46 | - * @since 0.5 | |
| 47 | - */ | |
| 48 | - public function uninstall() { | |
| 20 | + // removes ALL plugin data (languages, translation, and the termmeta table if empty | |
| 21 | + function uninstall() { | |
| 49 | 22 | global $wpdb; |
| 23 | + $wpdb->termmeta = $wpdb->prefix . 'termmeta'; // registers the termmeta table in wpdb | |
| 24 | + register_taxonomy('language', get_post_types(array('show_ui' => true)), array('label' => false, 'query_var'=>'lang')); // need to register the language taxonomy | |
| 50 | 25 | |
| 51 | - do_action( 'pll_uninstall' ); | |
| 26 | + $languages = get_terms('language', array('hide_empty'=>false)); | |
| 52 | 27 | |
| 53 | - // We need to register the taxonomies. | |
| 54 | - $pll_taxonomies = array( | |
| 55 | - 'language', | |
| 56 | - 'term_language', | |
| 57 | - 'post_translations', | |
| 58 | - 'term_translations', | |
| 59 | - ); | |
| 28 | + // delete posts translations | |
| 29 | + $ids = get_posts(array('numberposts'=> -1, 'fields' => 'ids', 'meta_key'=>'_translations', 'post_type'=>'any', 'post_status'=>'any')); | |
| 30 | + foreach ($ids as $id) | |
| 31 | + delete_post_meta($id, '_translations'); | |
| 60 | 32 | |
| 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 | - ); | |
| 33 | + // delete terms translations | |
| 34 | + $ids = get_terms(get_taxonomies(array('show_ui'=>true)), array('get'=>'all', 'fields'=>'ids')); | |
| 35 | + foreach ($ids as $id) { | |
| 36 | + delete_metadata('term', $id, '_translations'); | |
| 37 | + delete_metadata('term', $id, '_language'); | |
| 72 | 38 | } |
| 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 ); | |
| 39 | + | |
| 40 | + foreach ($languages as $lang) { | |
| 41 | + delete_metadata('term', $lang->term_id, '_rtl'); // delete rtl meta | |
| 42 | + delete_option('polylang_mo'.$lang->term_id); // delete the string translations | |
| 43 | + wp_delete_term($lang->term_id, 'language'); // finally delete languages | |
| 86 | 44 | } |
| 87 | 45 | |
| 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 ); | |
| 46 | + // delete the termmeta table only if it is empty as other plugins may use it | |
| 47 | + $table = $wpdb->termmeta; | |
| 48 | + $count = $wpdb->get_var("SELECT COUNT(*) FROM $table;"); | |
| 49 | + if (!$count) { | |
| 50 | + $wpdb->query($wpdb->prepare("DROP TABLE $table;")); | |
| 51 | + unset($wpdb->termmeta); | |
| 101 | 52 | } |
| 102 | 53 | |
| 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 | - delete_option( 'pll_language_taxonomies' ); | |
| 187 | - | |
| 188 | - // Delete transients. | |
| 189 | - delete_option( '_transient_pll_languages_list' ); // Force deletion of the transient from the options table even in case external object cache is used. | |
| 54 | + // delete options | |
| 55 | + delete_option('polylang'); | |
| 56 | + delete_option('polylang_nav_menus'); | |
| 57 | + delete_option('polylang_widgets'); | |
| 58 | + delete_option('polylang_widget'); // automatically created by WP | |
| 190 | 59 | } |
| 191 | 60 | } |
| 192 | 61 | |
| 193 | -new PLL_Uninstall(); | |
| 62 | +if (class_exists("Polylang_Uninstall")) | |
| 63 | + new Polylang_Uninstall(); | |
| 64 | + | |
| 65 | +?> | |