1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Deactivation event to be launched on Uninstall. |
7
|
|
|
* |
8
|
|
|
* @package PinkCrab\Perique\Migration\Plugin_Lifecycle |
9
|
|
|
* @author Glynn Quelch [email protected] |
10
|
|
|
* @since 0.0.1 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PinkCrab\Perique\Migration\Event; |
14
|
|
|
|
15
|
|
|
use PinkCrab\Perique\Migration\Migration; |
16
|
|
|
use PinkCrab\DB_Migration\Migration_Manager; |
17
|
|
|
use PinkCrab\DB_Migration\Database_Migration; |
18
|
|
|
use PinkCrab\Plugin_Lifecycle\State_Event\Uninstall as State_Events_Uninstall; |
19
|
|
|
|
20
|
|
|
class Uninstall implements State_Events_Uninstall { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Array of tables to be dropped. |
24
|
|
|
* |
25
|
|
|
* @var Migration[] |
26
|
|
|
*/ |
27
|
|
|
protected array $tables = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The migration log key to clear after dropping tables. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected string $migration_log_key; |
35
|
|
|
|
36
|
|
|
public function __construct( Migration_Manager $migration_manager ) { |
37
|
|
|
$this->set_tables( $migration_manager->get_migrations() ); |
38
|
|
|
$this->migration_log_key = $migration_manager->migration_log()->get_log_key(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set the table to remove |
44
|
|
|
* |
45
|
|
|
* @param Database_Migration[] $migrations |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function set_tables( array $migrations ): void { |
49
|
|
|
$this->tables = \array_filter( |
50
|
|
|
$migrations, |
51
|
|
|
fn( Database_Migration $migration ) => is_a( $migration, Migration::class ) && $migration->drop_on_uninstall() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Invokes the run method. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function __invoke(): void { |
63
|
|
|
$this->run(); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Runs the dropping of all valid tables. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function run(): void { |
73
|
|
|
// If no tables, return. |
74
|
|
|
if ( empty( $this->tables ) ) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->remove_migration_log(); |
79
|
|
|
$this->drop_tables(); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Drops all tables. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
protected function drop_tables() { |
89
|
|
|
/** @var \wpdb $wpdb */ |
90
|
|
|
global $wpdb; |
91
|
|
|
|
92
|
|
|
// Temp disable warnings. |
93
|
|
|
$original_state = (bool) $wpdb->suppress_errors; |
94
|
|
|
$wpdb->suppress_errors( true ); |
95
|
|
|
|
96
|
|
|
foreach ( $this->tables as $table ) { |
97
|
|
|
$table_name = $table->get_table_name(); |
98
|
|
|
$wpdb->get_results( "DROP TABLE IF EXISTS {$table_name}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Reset warnings to initial state. |
102
|
|
|
$wpdb->suppress_errors( $original_state ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Delete the migration log. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
protected function remove_migration_log(): void { |
111
|
|
|
\delete_option( $this->migration_log_key ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|