# user-access-manager/trunk/src/Setup/SetupHandler.php

User Access Manager, version trunk. 103 lines.

- Page: https://pluginprobe.com/plugins/user-access-manager/trunk/code/src/Setup/SetupHandler.php
- Raw: https://pluginprobe.com/plugins/user-access-manager/trunk/raw/src/Setup/SetupHandler.php
- Modified: 2026-08-06T11:11:24+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/user-access-manager/trunk/code/src/Setup/SetupHandler.php#L10-L20`.

```php
<?php

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
{
    public function __construct(
        private Wordpress $wordpress,
        private Database $database,
        private DatabaseHandler $databaseHandler,
        private MainConfig $mainConfig,
        private FileHandler $fileHandler
    ) {
    }

    public function getDatabaseHandler(): DatabaseHandler
    {
        return $this->databaseHandler;
    }

    /**
     * @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;
    }

    /**
     * @throws MissingColumnsException
     */
    public function install(bool $networkWide = false): void
    {
        if ($networkWide === false) {
            $this->databaseHandler->install();
            return;
        }

        foreach ($this->getBlogIds() as $blogId) {
            $this->wordpress->switchToBlog($blogId);
            $this->databaseHandler->install();
            $this->wordpress->restoreCurrentBlog();
        }
    }

    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();
    }

    /**
     * @throws MissingColumnsException
     */
    public function uninstall(): void
    {
        $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();
    }

    public function deactivate(): bool
    {
        return $this->fileHandler->deleteFileProtection();
    }
}

```
