1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\SqlSchema; |
7
|
|
|
|
8
|
|
|
class Install |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param bool $isNetworkDeactivating |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public function deactivate($isNetworkDeactivating) |
15
|
|
|
{ |
16
|
|
|
if (!$isNetworkDeactivating) { |
17
|
|
|
$this->dropForeignConstraints(); |
18
|
|
|
delete_option(glsr()->prefix.'activated'); |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
foreach ($this->sites() as $siteId) { |
22
|
|
|
switch_to_blog($siteId); |
23
|
|
|
$this->dropForeignConstraints(); |
24
|
|
|
delete_option(glsr()->prefix.'activated'); |
25
|
|
|
restore_current_blog(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function dropForeignConstraints() |
33
|
|
|
{ |
34
|
|
|
glsr(SqlSchema::class)->dropForeignConstraint('assigned_posts', 'assigned_posts_post_id'); |
35
|
|
|
glsr(SqlSchema::class)->dropForeignConstraint('assigned_terms', 'assigned_terms_term_id'); |
36
|
|
|
glsr(SqlSchema::class)->dropForeignConstraint('assigned_users', 'assigned_users_user_id'); |
37
|
|
|
glsr(SqlSchema::class)->dropForeignConstraint('ratings', 'assigned_posts_review_id'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param bool $dropAll |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function dropTables($dropAll = true) |
45
|
|
|
{ |
46
|
|
|
$tables = $this->tables(); |
47
|
|
|
if (is_multisite() && $dropAll) { |
48
|
|
|
foreach ($this->sites() as $siteId) { |
49
|
|
|
switch_to_blog($siteId); |
50
|
|
|
$tables = array_unique(array_merge($tables, $this->tables())); |
51
|
|
|
delete_option(glsr()->prefix.'db_version'); |
52
|
|
|
restore_current_blog(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
foreach ($tables as $table) { |
56
|
|
|
glsr(Database::class)->dbQuery( |
57
|
|
|
glsr(Query::class)->sql("DROP TABLE IF EXISTS {$table}") |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
delete_option(glsr()->prefix.'db_version'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
14 |
|
public function run() |
67
|
|
|
{ |
68
|
14 |
|
require_once ABSPATH.'/wp-admin/includes/plugin.php'; |
69
|
14 |
|
if (is_plugin_active_for_network(plugin_basename(glsr()->file))) { |
70
|
|
|
foreach ($this->sites() as $siteId) { |
71
|
|
|
$this->runOnSite($siteId); |
72
|
|
|
} |
73
|
|
|
return; |
74
|
|
|
} |
75
|
14 |
|
$this->install(); |
76
|
14 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $siteId |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function runOnSite($siteId) |
83
|
|
|
{ |
84
|
|
|
switch_to_blog($siteId); |
85
|
|
|
$this->install(); |
86
|
|
|
restore_current_blog(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function sites() |
93
|
|
|
{ |
94
|
|
|
return get_sites([ |
95
|
|
|
'fields' => 'ids', |
96
|
|
|
'network_id' => get_current_network_id(), |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
14 |
|
protected function createRoleCapabilities() |
104
|
|
|
{ |
105
|
14 |
|
glsr(Role::class)->resetAll(); |
106
|
14 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
14 |
|
protected function createTables() |
112
|
|
|
{ |
113
|
14 |
|
glsr(SqlSchema::class)->createTables(); |
114
|
14 |
|
glsr(SqlSchema::class)->addForeignConstraints(); |
115
|
14 |
|
if (glsr(SqlSchema::class)->tablesExist() && empty(get_option(glsr()->prefix.'db_version'))) { |
116
|
|
|
$version = '1.0'; // @compat |
117
|
|
|
if (glsr(SqlSchema::class)->columnExists('ratings', 'terms')) { |
118
|
|
|
$version = Application::DB_VERSION; |
119
|
|
|
} |
120
|
|
|
add_option(glsr()->prefix.'db_version', $version); |
121
|
|
|
} |
122
|
14 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
14 |
|
protected function deleteInvalidAssignments() |
128
|
|
|
{ |
129
|
14 |
|
glsr(Database::class)->deleteInvalidPostAssignments(); |
130
|
14 |
|
glsr(Database::class)->deleteInvalidTermAssignments(); |
131
|
14 |
|
glsr(Database::class)->deleteInvalidUserAssignments(); |
132
|
14 |
|
glsr(Database::class)->deleteInvalidReviews(); |
133
|
14 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
14 |
|
protected function install() |
139
|
|
|
{ |
140
|
14 |
|
$this->createRoleCapabilities(); |
141
|
14 |
|
$this->createTables(); |
142
|
14 |
|
$this->deleteInvalidAssignments(); |
143
|
14 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
protected function tables() |
149
|
|
|
{ |
150
|
|
|
return [ |
151
|
|
|
glsr(SqlSchema::class)->table('assigned_posts'), |
152
|
|
|
glsr(SqlSchema::class)->table('assigned_terms'), |
153
|
|
|
glsr(SqlSchema::class)->table('assigned_users'), |
154
|
|
|
glsr(SqlSchema::class)->table('ratings'), |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|