| @@ -1,16 +1,13 @@ | ||
| 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; | |
| 3 | +if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) { // If uninstall not called from WordPress exit | |
| 4 | + exit(); | |
| 8 | 5 | } |
| 9 | 6 | |
| 10 | 7 | /** |
| 11 | - * Manages Polylang uninstallation. | |
| 12 | - * The goal is to remove **all** Polylang related data in db. | |
| 8 | + * Manages Polylang uninstallation | |
| 9 | + * The goal is to remove ALL Polylang related data in db | |
| 13 | 10 | * |
| 14 | 11 | * @since 0.5 |
| 15 | 12 | */ |
| 16 | 13 | class PLL_Uninstall { |
| @@ -15,21 +12,16 @@ | ||
| 15 | 12 | */ |
| 16 | 13 | class PLL_Uninstall { |
| 17 | 14 | |
| 18 | 15 | /** |
| 19 | - * Constructor: manages uninstall for multisite. | |
| 16 | + * Constructor: manages uninstall for multisite | |
| 20 | 17 | * |
| 21 | 18 | * @since 0.5 |
| 22 | 19 | */ |
| 23 | - public function __construct() { | |
| 20 | + function __construct() { | |
| 24 | 21 | global $wpdb; |
| 25 | 22 | |
| 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. | |
| 23 | + // Check if it is a multisite uninstall - if so, run the uninstall function for each blog id | |
| 32 | 24 | if ( is_multisite() ) { |
| 33 | 25 | foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) as $blog_id ) { |
| 34 | 26 | switch_to_blog( $blog_id ); |
| 35 | 27 | $this->uninstall(); |
| @@ -34,160 +26,116 @@ | ||
| 34 | 26 | switch_to_blog( $blog_id ); |
| 35 | 27 | $this->uninstall(); |
| 36 | 28 | } |
| 37 | 29 | restore_current_blog(); |
| 38 | - } else { | |
| 30 | + } | |
| 31 | + else { | |
| 39 | 32 | $this->uninstall(); |
| 40 | 33 | } |
| 41 | 34 | } |
| 42 | 35 | |
| 43 | 36 | /** |
| 44 | - * Removes **all** plugin data. | |
| 37 | + * Removes ALL plugin data | |
| 38 | + * only when the relevant option is active | |
| 45 | 39 | * |
| 46 | 40 | * @since 0.5 |
| 47 | 41 | */ |
| 48 | - public function uninstall() { | |
| 42 | + function uninstall() { | |
| 43 | + $options = get_option( 'polylang' ); | |
| 44 | + | |
| 45 | + if ( empty( $options['uninstall'] ) ) { | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + | |
| 49 | + // Suppress data of the old model < 1.2 | |
| 50 | + // FIXME: to remove when support for v1.1.6 will be dropped | |
| 49 | 51 | global $wpdb; |
| 52 | + $wpdb->termmeta = $wpdb->prefix . 'termmeta'; // registers the termmeta table in wpdb | |
| 50 | 53 | |
| 51 | - do_action( 'pll_uninstall' ); | |
| 54 | + // Do nothing if the termmeta table does not exists | |
| 55 | + if ( count( $wpdb->get_results( "SHOW TABLES LIKE '$wpdb->termmeta'" ) ) ) { | |
| 56 | + $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_translations'" ); | |
| 57 | + $wpdb->query( "DELETE FROM $wpdb->termmeta WHERE meta_key = '_language'" ); | |
| 58 | + $wpdb->query( "DELETE FROM $wpdb->termmeta WHERE meta_key = '_rtl'" ); | |
| 59 | + $wpdb->query( "DELETE FROM $wpdb->termmeta WHERE meta_key = '_translations'" ); | |
| 60 | + } | |
| 52 | 61 | |
| 53 | - // We need to register the taxonomies. | |
| 54 | - $pll_taxonomies = array( | |
| 55 | - 'language', | |
| 56 | - 'term_language', | |
| 57 | - 'post_translations', | |
| 58 | - 'term_translations', | |
| 59 | - ); | |
| 60 | - | |
| 62 | + // Need to register the taxonomies | |
| 63 | + $pll_taxonomies = array( 'language', 'term_language', 'post_translations', 'term_translations' ); | |
| 61 | 64 | 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 | - ); | |
| 65 | + register_taxonomy( $taxonomy, null, array( 'label' => false, 'public' => false, 'query_var' => false, 'rewrite' => false ) ); | |
| 72 | 66 | } |
| 73 | 67 | |
| 74 | - $languages = get_terms( | |
| 75 | - array( | |
| 76 | - 'taxonomy' => 'language', | |
| 77 | - 'hide_empty' => false, | |
| 78 | - ) | |
| 79 | - ); | |
| 68 | + $languages = get_terms( 'language', array( 'hide_empty' => false ) ); | |
| 80 | 69 | |
| 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 ); | |
| 70 | + // Delete users options | |
| 71 | + foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) { | |
| 72 | + delete_user_meta( $user_id, 'pll_filter_content' ); | |
| 73 | + delete_user_meta( $user_id, 'pll_duplicate_content' ); | |
| 74 | + foreach ( $languages as $lang ) { | |
| 75 | + delete_user_meta( $user_id, 'description_' . $lang->slug ); | |
| 76 | + } | |
| 86 | 77 | } |
| 87 | 78 | |
| 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 | - ); | |
| 79 | + // Delete menu language switchers | |
| 80 | + $ids = get_posts( array( | |
| 81 | + 'post_type' => 'nav_menu_item', | |
| 82 | + 'numberposts' => -1, | |
| 83 | + 'nopaging' => true, | |
| 84 | + 'fields' => 'ids', | |
| 85 | + 'meta_key' => '_pll_menu_item', | |
| 86 | + ) ); | |
| 98 | 87 | |
| 99 | 88 | foreach ( $ids as $id ) { |
| 100 | 89 | wp_delete_post( $id, true ); |
| 101 | 90 | } |
| 102 | 91 | |
| 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 | - ); | |
| 92 | + // Delete the strings translations ( <1.2 ) | |
| 93 | + // FIXME: to remove when support for v1.1.6 will be dropped | |
| 94 | + foreach ( $languages as $lang ) { | |
| 95 | + delete_option( 'polylang_mo' . $lang->term_id ); | |
| 96 | + } | |
| 97 | + | |
| 98 | + // Delete the strings translations 1.2+ | |
| 99 | + register_post_type( 'polylang_mo', array( 'rewrite' => false, 'query_var' => false ) ); | |
| 100 | + $ids = get_posts( array( | |
| 101 | + 'post_type' => 'polylang_mo', | |
| 102 | + 'post_status' => 'any', | |
| 103 | + 'numberposts' => -1, | |
| 104 | + 'nopaging' => true, | |
| 105 | + 'fields' => 'ids', | |
| 106 | + ) ); | |
| 123 | 107 | foreach ( $ids as $id ) { |
| 124 | 108 | wp_delete_post( $id, true ); |
| 125 | 109 | } |
| 126 | 110 | |
| 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 ) { | |
| 111 | + // Delete all what is related to languages and translations | |
| 112 | + foreach ( get_terms( $pll_taxonomies, array( 'hide_empty' => false ) ) as $term ) { | |
| 139 | 113 | $term_ids[] = (int) $term->term_id; |
| 140 | - $tt_ids[] = (int) $term->term_taxonomy_id; | |
| 114 | + $tt_ids[] = (int) $term->term_taxonomy_id; | |
| 141 | 115 | } |
| 142 | 116 | |
| 143 | 117 | if ( ! empty( $term_ids ) ) { |
| 144 | 118 | $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 | - ); | |
| 119 | + $wpdb->query( "DELETE FROM $wpdb->terms WHERE term_id IN ( " . implode( ',', $term_ids ) . ' )' ); | |
| 120 | + $wpdb->query( "DELETE FROM $wpdb->term_taxonomy WHERE term_id IN ( " . implode( ',', $term_ids ) . ' )' ); | |
| 172 | 121 | } |
| 173 | 122 | |
| 174 | 123 | if ( ! empty( $tt_ids ) ) { |
| 175 | 124 | $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 ) ); | |
| 125 | + $wpdb->query( "DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ( " . implode( ',', $tt_ids ) . ' )' ); | |
| 177 | 126 | } |
| 178 | 127 | |
| 179 | - // Delete options. | |
| 128 | + // Delete options | |
| 180 | 129 | delete_option( 'polylang' ); |
| 181 | - delete_option( 'widget_polylang' ); // Automatically created by WP. | |
| 182 | - delete_option( 'polylang_wpml_strings' ); // Strings registered with icl_register_string. | |
| 130 | + delete_option( 'widget_polylang' ); // Automatically created by WP | |
| 131 | + delete_option( 'polylang_wpml_strings' ); // Strings registered with icl_register_string | |
| 183 | 132 | 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 | 133 | |
| 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. | |
| 134 | + // Delete transients | |
| 135 | + delete_transient( 'pll_languages_list' ); | |
| 136 | + delete_transient( 'pll_upgrade_1_4' ); | |
| 137 | + delete_transient( 'pll_translated_slugs' ); | |
| 190 | 138 | } |
| 191 | 139 | } |
| 192 | 140 | |
| 193 | 141 | new PLL_Uninstall(); |