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_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_db_version'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
7 |
|
public function run() |
67
|
|
|
{ |
68
|
7 |
|
require_once ABSPATH.'/wp-admin/includes/plugin.php'; |
69
|
7 |
|
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
|
7 |
|
$this->install(); |
76
|
7 |
|
} |
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 void |
91
|
|
|
*/ |
92
|
7 |
|
protected function createRoleCapabilities() |
93
|
|
|
{ |
94
|
7 |
|
glsr(Role::class)->resetAll(); |
95
|
7 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
7 |
|
protected function createTables() |
101
|
|
|
{ |
102
|
7 |
|
glsr(SqlSchema::class)->createTables(); |
103
|
7 |
|
glsr(SqlSchema::class)->addForeignConstraints(); |
104
|
7 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
7 |
|
protected function deleteInvalidAssignments() |
110
|
|
|
{ |
111
|
7 |
|
glsr(Database::class)->deleteInvalidPostAssignments(); |
112
|
7 |
|
glsr(Database::class)->deleteInvalidTermAssignments(); |
113
|
7 |
|
glsr(Database::class)->deleteInvalidUserAssignments(); |
114
|
7 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
7 |
|
protected function install() |
120
|
|
|
{ |
121
|
7 |
|
$this->createRoleCapabilities(); |
122
|
7 |
|
$this->createTables(); |
123
|
7 |
|
$this->deleteInvalidAssignments(); |
124
|
7 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
protected function sites() |
130
|
|
|
{ |
131
|
|
|
return get_sites([ |
|
|
|
|
132
|
|
|
'fields' => 'ids', |
133
|
|
|
'network_id' => get_current_network_id(), |
|
|
|
|
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function tables() |
141
|
|
|
{ |
142
|
|
|
return [ |
143
|
|
|
glsr(SqlSchema::class)->table('assigned_posts'), |
144
|
|
|
glsr(SqlSchema::class)->table('assigned_terms'), |
145
|
|
|
glsr(SqlSchema::class)->table('assigned_users'), |
146
|
|
|
glsr(SqlSchema::class)->table('ratings'), |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|