SetupHandler.php
4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* SetupHandler.php
*
* The SetupHandler class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Setup;
use UserAccessManager\Config\MainConfig;
use UserAccessManager\Database\Database;
use UserAccessManager\File\FileHandler;
use UserAccessManager\Setup\Database\DatabaseHandler;
use UserAccessManager\Setup\Database\MissingColumnsException;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class SetupHandler
*
* @package UserAccessManager\SetupHandler
*/
class SetupHandler
{
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var Database
*/
private $database;
/**
* @var DatabaseHandler
*/
private $databaseHandler;
/**
* @var MainConfig
*/
private $mainConfig;
/**
* @var FileHandler
*/
private $fileHandler;
/**
* SetupHandler constructor.
* @param Wordpress $wordpress
* @param Database $database
* @param DatabaseHandler $databaseHandler
* @param MainConfig $mainConfig
* @param FileHandler $fileHandler
*/
public function __construct(
Wordpress $wordpress,
Database $database,
DatabaseHandler $databaseHandler,
MainConfig $mainConfig,
FileHandler $fileHandler
) {
$this->wordpress = $wordpress;
$this->database = $database;
$this->databaseHandler = $databaseHandler;
$this->mainConfig = $mainConfig;
$this->fileHandler = $fileHandler;
}
/**
* Returns the database handler object.
* @return DatabaseHandler
*/
public function getDatabaseHandler(): DatabaseHandler
{
return $this->databaseHandler;
}
/**
* Returns all blog of the network.
* @return integer[]
*/
public function getBlogIds(): array
{
$currentBlogId = $this->database->getCurrentBlogId();
$blogIds = [$currentBlogId => $currentBlogId];
$sites = $this->wordpress->getSites();
foreach ($sites as $site) {
$blogIds[$site->blog_id] = $site->blog_id;
}
return $blogIds;
}
/**
* Creates the needed tables at the database and adds the options
* @throws MissingColumnsException
*/
private function runInstall()
{
$this->databaseHandler->install();
}
/**
* Installs the user access manager.
* @param bool $networkWide
* @throws MissingColumnsException
*/
public function install($networkWide = false)
{
if ($networkWide === true) {
$blogIds = $this->getBlogIds();
foreach ($blogIds as $blogId) {
$this->wordpress->switchToBlog($blogId);
$this->runInstall();
$this->wordpress->restoreCurrentBlog();
}
} else {
$this->runInstall();
}
}
/**
* Updates the user access manager if an old version was installed.
* @return bool
*/
public function update(): bool
{
$uamVersion = (string) $this->wordpress->getOption('uam_version', '0');
if (version_compare($uamVersion, '1.0', '<') === true) {
$this->wordpress->deleteOption('allow_comments_locked');
}
if (version_compare($uamVersion, '2.1.7', '<') === true) {
$this->mainConfig->setConfigParameters(['locked_directory_type' => 'all']);
}
return $this->databaseHandler->updateDatabase();
}
/**
* Clean up wordpress if the plugin will be uninstalled.
* @throws MissingColumnsException
*/
public function uninstall()
{
$blogIds = $this->getBlogIds();
foreach ($blogIds as $blogId) {
$this->wordpress->switchToBlog($blogId);
$this->databaseHandler->removeTables();
$this->wordpress->deleteOption(MainConfig::MAIN_CONFIG_KEY);
$this->wordpress->deleteOption('uam_version');
$this->wordpress->deleteOption('uam_db_version');
$this->wordpress->restoreCurrentBlog();
}
$this->fileHandler->deleteFileProtection();
}
/**
* Remove the htaccess file if the plugin is deactivated.
* @return bool
*/
public function deactivate(): bool
{
return $this->fileHandler->deleteFileProtection();
}
}