| 1 |
<?php |
| 2 |
/** |
| 3 |
* All functionality related to uninstalling the plugin. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Plugin; |
| 11 |
|
| 12 |
use SolidWP\Performance\Container; |
| 13 |
use SolidWP\Performance\Database\Provider; |
| 14 |
use SolidWP\Performance\StellarWP\Schema\Register; |
| 15 |
use SolidWP\Performance\StellarWP\Schema\Tables\Contracts\Table; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Handles actions that should run when the plugin is uninstalled. |
| 23 |
* |
| 24 |
* WordPress does not allow anonymous callbacks with register_uninstall_hook. |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
final class Uninstaller { |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Container |
| 32 |
*/ |
| 33 |
private Container $container; |
| 34 |
|
| 35 |
/** |
| 36 |
* The Singleton instance. |
| 37 |
* |
| 38 |
* @var self|null |
| 39 |
*/ |
| 40 |
private static ?self $instance = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param Container $container The container. |
| 44 |
*/ |
| 45 |
private function __construct( Container $container ) { |
| 46 |
$this->container = $container; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the singleton instance. |
| 51 |
* |
| 52 |
* @return self |
| 53 |
*/ |
| 54 |
public static function instance(): self { |
| 55 |
if ( self::$instance === null ) { |
| 56 |
self::$instance = new self( swpsp_plugin()->container() ); |
| 57 |
} |
| 58 |
|
| 59 |
return self::$instance; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Uninstall hook run via register_uninstall_hook(). |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public static function uninstall(): void { |
| 68 |
self::instance()->handle_uninstall(); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Actual uninstall logic. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
private function handle_uninstall(): void { |
| 77 |
$this->remove_database_tables(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Remove database tables. |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
private function remove_database_tables(): void { |
| 86 |
$this->container->get( Provider::class )->register(); |
| 87 |
|
| 88 |
$tables = $this->container->get( Provider::SCHEMA_TABLES ); |
| 89 |
|
| 90 |
/** @var class-string<Table> $table */ |
| 91 |
foreach ( $tables as $table ) { |
| 92 |
Register::remove_table( $table ); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|