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