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