Admin
5 years ago
Commands
6 years ago
Db
6 years ago
Ecommerce
6 years ago
Report
6 years ago
Site
5 years ago
TrackingCode
5 years ago
User
5 years ago
views
6 years ago
API.php
5 years ago
Access.php
6 years ago
AjaxTracker.php
5 years ago
Annotations.php
6 years ago
Bootstrap.php
6 years ago
Capabilities.php
6 years ago
Compatibility.php
6 years ago
Email.php
5 years ago
Installer.php
6 years ago
Logger.php
5 years ago
OptOut.php
5 years ago
Paths.php
6 years ago
PrivacyBadge.php
6 years ago
Referral.php
6 years ago
Roles.php
6 years ago
ScheduledTasks.php
6 years ago
Settings.php
5 years ago
Site.php
6 years ago
TrackingCode.php
5 years ago
Uninstaller.php
6 years ago
Updater.php
6 years ago
User.php
6 years ago
Uninstaller.php
150 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo; |
| 11 | |
| 12 | use WpMatomo\Admin\Dashboard; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // if accessed directly |
| 16 | } |
| 17 | |
| 18 | class Uninstaller { |
| 19 | |
| 20 | /** |
| 21 | * @var Logger |
| 22 | */ |
| 23 | private $logger; |
| 24 | |
| 25 | public function __construct() { |
| 26 | $this->logger = self::make_logger(); |
| 27 | } |
| 28 | |
| 29 | private static function make_logger() { |
| 30 | return new Logger(); |
| 31 | } |
| 32 | |
| 33 | public function uninstall( $should_remove_all_data ) { |
| 34 | if ( function_exists( 'is_multisite' ) && is_multisite() ) { |
| 35 | $this->uninstall_multisite( $should_remove_all_data ); |
| 36 | } else { |
| 37 | $this->uninstall_blog( $should_remove_all_data ); |
| 38 | } |
| 39 | |
| 40 | do_action( 'matomo_uninstall', $should_remove_all_data ); |
| 41 | } |
| 42 | |
| 43 | public function uninstall_blog( $should_remove_all_data ) { |
| 44 | $this->logger->log( 'Matomo is now uninstalling blogId ' . get_current_blog_id() ); |
| 45 | |
| 46 | $settings = new Settings(); |
| 47 | |
| 48 | $tasks = new ScheduledTasks( $settings ); |
| 49 | $tasks->uninstall(); |
| 50 | |
| 51 | $roles = new Roles( $settings ); |
| 52 | $roles->uninstall(); |
| 53 | |
| 54 | $dashboard = new Dashboard(); |
| 55 | $dashboard->uninstall(); |
| 56 | |
| 57 | $paths = new Paths(); |
| 58 | |
| 59 | if ( $should_remove_all_data ) { |
| 60 | $this->logger->log( 'Matomo is forced to remove all data' ); |
| 61 | |
| 62 | $settings->uninstall(); |
| 63 | |
| 64 | $this->drop_tables(); |
| 65 | |
| 66 | $site = new Site(); |
| 67 | $site->uninstall(); |
| 68 | |
| 69 | $site = new User(); |
| 70 | $site->uninstall(); |
| 71 | |
| 72 | $paths->uninstall(); |
| 73 | } else { |
| 74 | $paths->clear_cache_dir(); |
| 75 | } |
| 76 | |
| 77 | do_action( 'matomo_uninstall_blog', $should_remove_all_data ); |
| 78 | |
| 79 | $this->logger->log( 'Matomo has finished uninstalling ' . get_current_blog_id() ); |
| 80 | } |
| 81 | |
| 82 | public static function uninstall_options( $prefix ) { |
| 83 | global $wpdb; |
| 84 | |
| 85 | self::make_logger()->log( 'Removing options with prefix ' . $prefix ); |
| 86 | $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '" . $prefix . "%';" ); |
| 87 | |
| 88 | wp_cache_flush(); |
| 89 | } |
| 90 | |
| 91 | public static function uninstall_site_meta( $prefix ) { |
| 92 | global $wpdb; |
| 93 | |
| 94 | if ( ! empty( $wpdb->sitemeta ) ) { |
| 95 | // multisite |
| 96 | self::make_logger()->log( 'Removing sitemeta with prefix ' . $prefix ); |
| 97 | $wpdb->query( "DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE '" . $prefix . "%';" ); |
| 98 | |
| 99 | wp_cache_flush(); |
| 100 | } |
| 101 | |
| 102 | // not multisite |
| 103 | self::uninstall_options( $prefix ); |
| 104 | } |
| 105 | |
| 106 | public static function uninstall_user_meta( $prefix ) { |
| 107 | global $wpdb; |
| 108 | |
| 109 | if ( ! empty( $wpdb->usermeta ) ) { |
| 110 | self::make_logger()->log( 'Removing usermeta with prefix ' . $prefix ); |
| 111 | $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '" . $prefix . "%';" ); |
| 112 | |
| 113 | wp_cache_flush(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public function uninstall_multisite( $should_remove_all_data ) { |
| 118 | global $wpdb; |
| 119 | |
| 120 | $this->logger->log( 'Matomo is now uninstalling all blogs: ' . (int) $should_remove_all_data ); |
| 121 | |
| 122 | $blogs = $wpdb->get_results( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_id', ARRAY_A ); |
| 123 | |
| 124 | if ( is_array( $blogs ) ) { |
| 125 | foreach ( $blogs as $blog ) { |
| 126 | switch_to_blog( $blog['blog_id'] ); |
| 127 | |
| 128 | $this->uninstall_blog( $should_remove_all_data ); |
| 129 | |
| 130 | restore_current_blog(); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private function drop_tables() { |
| 136 | global $wpdb; |
| 137 | |
| 138 | $db_settings = new \WpMatomo\Db\Settings(); |
| 139 | $installed_tables = $db_settings->get_installed_matomo_tables(); |
| 140 | $this->logger->log( sprintf( 'Matomo will now drop %s matomo tables', count( $installed_tables ) ) ); |
| 141 | |
| 142 | foreach ( $installed_tables as $table_name ) { |
| 143 | // temporary table are used in tests and just making sure they are being removed |
| 144 | // $wpdb->query( "DROP TEMPORARY TABLE IF EXISTS `$tableName`" ); |
| 145 | // two spaces between drop and table so it won't be replaced in WP tests |
| 146 | $wpdb->query( "DROP TABLE IF EXISTS `$table_name`" ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 |