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