PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Install.php

Install.php in Site Reviews trunk, at plugin/Install.php

100 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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