| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Database\Query; |
| 6 |
use GeminiLabs\SiteReviews\Database\Tables; |
| 7 |
|
| 8 |
class Install |
| 9 |
{ |
| 10 |
public function deactivate(bool $isNetworkDeactivation): void |
| 11 |
{ |
| 12 |
if (!$isNetworkDeactivation) { |
| 13 |
glsr(Tables::class)->dropForeignConstraints(); |
| 14 |
delete_option(glsr()->prefix.'activated'); |
| 15 |
glsr()->action('deactivated'); |
| 16 |
return; |
| 17 |
} |
| 18 |
foreach ($this->sites() as $siteId) { |
| 19 |
switch_to_blog($siteId); |
| 20 |
glsr(Tables::class)->dropForeignConstraints(); |
| 21 |
delete_option(glsr()->prefix.'activated'); |
| 22 |
glsr()->action('deactivated'); |
| 23 |
restore_current_blog(); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public function dropTables(bool $dropAll = true): void |
| 28 |
{ |
| 29 |
$tables = $this->tables(); |
| 30 |
if (is_multisite() && $dropAll) { |
| 31 |
foreach ($this->sites() as $siteId) { |
| 32 |
switch_to_blog($siteId); |
| 33 |
$tables = array_unique(array_merge($tables, $this->tables())); |
| 34 |
delete_option(glsr()->prefix.'db_version'); |
| 35 |
restore_current_blog(); |
| 36 |
} |
| 37 |
} |
| 38 |
foreach ($tables as $table) { |
| 39 |
glsr(Database::class)->dbQuery( |
| 40 |
glsr(Query::class)->sql("DROP TABLE IF EXISTS {$table}") |
| 41 |
); |
| 42 |
} |
| 43 |
delete_option(glsr()->prefix.'db_version'); |
| 44 |
} |
| 45 |
|
| 46 |
public function run(): void |
| 47 |
{ |
| 48 |
require_once \ABSPATH.'wp-admin/includes/plugin.php'; |
| 49 |
if (is_plugin_active_for_network(glsr()->basename)) { |
| 50 |
foreach ($this->sites() as $siteId) { |
| 51 |
$this->runOnSite((int) $siteId); |
| 52 |
} |
| 53 |
return; |
| 54 |
} |
| 55 |
$this->install(); |
| 56 |
} |
| 57 |
|
| 58 |
public function runOnSite(int $siteId): void |
| 59 |
{ |
| 60 |
switch_to_blog($siteId); |
| 61 |
$this->install(); |
| 62 |
restore_current_blog(); |
| 63 |
} |
| 64 |
|
| 65 |
public function sites(): array |
| 66 |
{ |
| 67 |
return (array) get_sites([ |
| 68 |
'count' => false, // this ensures we return an array |
| 69 |
'fields' => 'ids', |
| 70 |
'network_id' => get_current_network_id(), |
| 71 |
]); |
| 72 |
} |
| 73 |
|
| 74 |
protected function install(): void |
| 75 |
{ |
| 76 |
glsr(Role::class)->resetAll(); |
| 77 |
glsr(Tables::class)->createTables(); |
| 78 |
glsr(Tables::class)->addForeignConstraints(); |
| 79 |
if (glsr(Tables::class)->tablesExist() && empty(get_option(glsr()->prefix.'db_version'))) { |
| 80 |
$version = '1.0'; // @compat |
| 81 |
if (glsr(Tables::class)->columnExists('ratings', 'terms')) { |
| 82 |
$version = '1.1'; |
| 83 |
} |
| 84 |
if (glsr(Tables::class)->columnExists('ratings', 'score')) { |
| 85 |
$version = Application::DB_VERSION; |
| 86 |
} |
| 87 |
add_option(glsr()->prefix.'db_version', $version); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
protected function tables(): array |
| 92 |
{ |
| 93 |
$tables = []; |
| 94 |
foreach (glsr(Tables::class)->tables() as $table) { |
| 95 |
$tables[] = glsr($table)->tablename; |
| 96 |
} |
| 97 |
return $tables; |
| 98 |
} |
| 99 |
} |
| 100 |
|