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