Ip\Internal\Plugins\Model::activatePlugin PHP Method

activatePlugin() public static method

public static activatePlugin ( $pluginName )
    public static function activatePlugin($pluginName)
    {
        $activePlugins = self::getActivePluginNames();
        if (in_array($pluginName, $activePlugins)) {
            //already activated
            return true;
        }
        $pluginRecord = self::getPluginRecord($pluginName);
        $config = Model::getPluginConfig($pluginName);
        if (!$config) {
            throw new \Ip\Exception(ipFile('Plugin/' . esc($pluginName) . '/Setup/plugin.json') . ' doesn\'t exist or is incorrect');
        }
        if (empty($config['name']) || $config['name'] !== $pluginName) {
            throw new \Ip\Exception('Plugin name setting in ' . ipFile('Plugin/' . esc($pluginName) . '/Setup/plugin.json') . " doesn't match the folder name.");
        }
        if (empty($config['version'])) {
            throw new \Ip\Exception('Missing plugin version number in ' . ipFile('Plugin/' . esc($pluginName) . '/Setup/plugin.json') . " file.");
        }
        if (!empty($pluginRecord['version']) && (double) $pluginRecord['version'] > (double) $config['version']) {
            throw new \Ip\Exception\Plugin\Setup("You can't downgrade the plugin. Please remove the plugin completely and reinstall if you want to use older version.");
        }
        self::executeSqlIfExists(ipFile('Plugin/' . esc($pluginName) . '/Setup/activate.sql'));
        $workerClass = 'Plugin\\' . $pluginName . '\\Setup\\Worker';
        if (class_exists($workerClass) && method_exists($workerClass, 'activate')) {
            $worker = new $workerClass($config['version']);
            $worker->activate();
        }
        if (!empty($config['title'])) {
            $pluginTitle = $config['title'];
        } else {
            $pluginTitle = $pluginName;
        }
        $keys = array('name' => $pluginName);
        $values = array('title' => $pluginTitle, 'isActive' => 1, 'version' => $config['version']);
        IpDb()->upsert('plugin', $keys, $values);
        // set default plugin options
        if (!empty($config['options'])) {
            static::importDefaultOptions($pluginName, $config['options']);
        }
        ipLog()->info('Ip.pluginActivated: {plugin} {version} activated.', array('plugin' => $pluginName, 'version' => $config['version']));
        ipEvent('ipPluginActivated', array('name' => $pluginName, 'version' => $config['version']));
        return null;
    }