Add a theme if enabled

@param \Laranix\Themer\ThemeSettings $settings
@param string|null                   $key
@return \Laranix\Themer\Theme|null
@throws \Laranix\Support\Exception\KeyExistsException
547062
public function add(ThemeSettings $settings, string $key = null) : ?Theme
    {
        if ($key !== null) {
            $settings->key = $key;
        }

        if ($this->has($settings->key)) {
            throw new KeyExistsException("Theme with key '{$settings->key}' already exists");
        }

        if ($settings->enabled) {
            $theme = new Theme($settings);

            if ($settings->default === true && $this->default === null) {
                $this->setDefault($theme);
            }

            if ($settings->override === true && $this->override === null) {
                $this->setOverride($theme);
            }

            $this->themes[$settings->key] = $theme;
        }

        return $theme ?? null;
    }
261462
protected function get($key)
    {
        $this->checkForNonEmptyKey($key);
        if (!$this->settingsHaveBeenRetrieved) {
            $this->retrieveSettings();
        }
        if (!isset($this->cachedSettings[$key])) {
            return null;
        }

        return $this->cachedSettings[$key];
    }
249651
public function set($key, $value = null)
     {
         if (isset($this->_settings->$key)) {
             $this->_settings->$key = $value;
         }

         return $this->_settings->$key;
     }
1203737
public function themeIsDefault($key = null) : bool
    {
        if ($key instanceof Theme) {
            return $key->getKey() === $this->getDefaultTheme()->getKey();
        }

        return $this->getTheme($key)->getKey() === $this->getDefaultTheme()->getKey();
    }
549549
public function addKey( $key ) {
		/*
		 * @todo The majority of this method has been copied from
		 * Boldgrid\Library\Library\Notice\KeyPrompt\addKey() because the logic to add a key should
		 * be in this class and not the KeyPrompt class. that KeyPrompt method needs to be refactored.
		 */

		// When adding Keys, delete the transient to make sure we get new license info.
		delete_site_transient( 'bg_license_data' );
		delete_site_transient( 'boldgrid_api_data' );

		$data = $this->callCheckVersion( array( 'key' => $key ) );

		if ( is_object( $data ) ) {
			$this->save( $data, $key );
			return true;
		} else {
			return false;
		}
	}
926347
public function add(Theme $theme)
    {
        if ($this->exists($theme->name)) {
            throw new Exceptions\themeAlreadyExists($theme);
        }
        $this->themes[] = $theme;
        return $theme;
    }
935974
public function addSettings( $settings )
    {
        $this->settings = ArrayUtils::merge(
            $this->settings,
            ArrayUtils::iteratorToArray( $settings )
        );

        return $this;
    }
1029240
private function setSettings($settings)
    {
        if ($settings) {
            foreach ($this->settings as $key => &$value) {
                if (!empty($settings->{$key}) && $settings->{$key}) {
                    $value = $settings->{$key};
                }
            }
        }
    }
1267149
public function getByKey($key, array $queryParams = [])
    {
        $response = $this->api->settings()->getByKey($key, $queryParams);

        return new SettingResponse($response);
    }
1363700
public function settings(array $settings = null)
    {
        if ($settings) {
            $this->_settings = array_merge($this->_settings, $settings);
        }
        return $this->_settings;
    }
548470
public static function configureWithPreset($preset, $settings) {
        // Load from JSON file if filename provided
        if (is_string($settings) && is_file($settings)) {
            throw new Maestrano_Config_Error("Metadata files are not accepted anymore, please use the Developer Platform");
        }

        // Ensure preset is initialized
        if (!array_key_exists($preset, self::$config) || is_null(self::$config[$preset])) {
            self::$config[$preset] = array();
        }

        //-------------------------------
        // App Config
        //-------------------------------
        if (array_key_exists('nid', $settings)) {
            self::$config[$preset]['nid'] = $settings['nid'];
        }

        if (array_key_exists('marketplace', $settings)) {
            self::$config[$preset]['marketplace'] = $settings['marketplace'];
        }

        if (array_key_exists('environment', $settings)) {
            self::$config[$preset]['environment'] = $settings['environment'];
        }

        if (array_key_exists('app', $settings) && array_key_exists('host', $settings['app'])) {
            self::$config[$preset]['app.host'] = $settings['app']['host'];
        }

        if (array_key_exists('app', $settings) && array_key_exists('synchronization_start_path', $settings['app'])) {
            self::$config[$preset]['app.synchronization_start_path'] = $settings['app']['synchronization_start_path'];
        }

        if (array_key_exists('app', $settings) && array_key_exists('synchronization_toggle_path', $settings['app'])) {
            self::$config[$preset]['app.synchronization_toggle_path'] = $settings['app']['synchronization_toggle_path'];
        }

        if (array_key_exists('app', $settings) && array_key_exists('synchronization_status_path', $settings['app'])) {
            self::$config[$preset]['app.synchronization_status_path'] = $settings['app']['synchronization_status_path'];
        }

        //-------------------------------
        // API Config
        //-------------------------------
        if (array_key_exists('api', $settings) && array_key_exists('id', $settings['api'])) {
            self::$config[$preset]['api.id'] = $settings['api']['id'];
        }

        if (array_key_exists('api', $settings) && array_key_exists('key', $settings['api'])) {
            self::$config[$preset]['api.key'] = $settings['api']['key'];
        }

        if (array_key_exists('api', $settings) && array_key_exists('host', $settings['api'])) {
            self::$config[$preset]['api.host'] = $settings['api']['host'];
        }

        if (array_key_exists('api', $settings) && array_key_exists('base', $settings['api'])) {
            self::$config[$preset]['api.base'] = $settings['api']['base'];
        }

        // Get lang/platform version
        self::$config[$preset]['api.version'] = Maestrano::VERSION;
        self::$config[$preset]['api.lang'] = 'php';
        self::$config[$preset]['api.lang_version'] = phpversion() . " " . php_uname();

        // Build api.token from api.id and api.key
        self::$config[$preset]['api.token'] = self::$config[$preset]['api.id'] . ":" . self::$config[$preset]['api.key'];

        //-------------------------------
        // SSO Config
        //-------------------------------
        if (array_key_exists('sso', $settings) && array_key_exists('idm', $settings['sso'])) {
            self::$config[$preset]['sso.idm'] = $settings['sso']['idm'];
        }

        if (array_key_exists('sso', $settings) && array_key_exists('init_path', $settings['sso'])) {
            self::$config[$preset]['sso.init_path'] = $settings['sso']['init_path'];
        }

        if (array_key_exists('sso', $settings) && array_key_exists('consume_path', $settings['sso'])) {
            self::$config[$preset]['sso.consume_path'] = $settings['sso']['consume_path'];
        }

        if (array_key_exists('sso', $settings) && array_key_exists('idp', $settings['sso'])) {
            self::$config[$preset]['sso.idp'] = $settings['sso']['idp'];
        }

        if (array_key_exists('sso', $settings) && array_key_exists('x509_fingerprint', $settings['sso'])) {
            self::$config[$preset]['sso.x509_fingerprint'] = $settings['sso']['x509_fingerprint'];
        }

        if (array_key_exists('sso', $settings) && array_key_exists('x509_certificate', $settings['sso'])) {
            self::$config[$preset]['sso.x509_certificate'] = $settings['sso']['x509_certificate'];
        }

        //-------------------------------
        // Connec! Config
        //-------------------------------
        if (array_key_exists('connec', $settings) && array_key_exists('host', $settings['connec'])) {
            self::$config[$preset]['connec.host'] = $settings['connec']['host'];
        }

        if (array_key_exists('connec', $settings) && array_key_exists('base_path', $settings['connec'])) {
            self::$config[$preset]['connec.base_path'] = $settings['connec']['base_path'];
        }

        if (array_key_exists('connec', $settings) && array_key_exists('timeout', $settings['connec'])) {
            self::$config[$preset]['connec.timeout'] = $settings['connec']['timeout'];
        }

        //-------------------------------
        // Webhook Config - Account
        //-------------------------------
        if (array_key_exists('webhooks', $settings)
            && array_key_exists('account', $settings['webhooks'])
            && array_key_exists('group_path', $settings['webhooks']['account'])) {
            self::$config[$preset]['webhooks.account.group_path'] = $settings['webhooks']['account']['group_path'];
        }

        if (array_key_exists('webhooks', $settings)
            && array_key_exists('account', $settings['webhooks'])
            && array_key_exists('group_user_path', $settings['webhooks']['account'])) {
            self::$config[$preset]['webhooks.account.group_user_path'] = $settings['webhooks']['account']['group_user_path'];
        }

        //-------------------------------
        // Webhook Config - Connec
        //-------------------------------
        if (array_key_exists('webhooks', $settings)
            && array_key_exists('connec', $settings['webhooks'])
            && array_key_exists('external_ids', $settings['webhooks']['connec'])) {
            self::$config[$preset]['webhooks.connec.external_ids'] = $settings['webhooks']['connec']['external_ids'];
        }

        if (array_key_exists('webhooks', $settings)
            && array_key_exists('connec', $settings['webhooks'])
            && array_key_exists('initialization_path', $settings['webhooks']['connec'])) {
            self::$config[$preset]['webhooks.connec.initialization_path'] = $settings['webhooks']['connec']['initialization_path'];
        }

        if (array_key_exists('webhooks', $settings)
            && array_key_exists('connec', $settings['webhooks'])
            && array_key_exists('notification_path', $settings['webhooks']['connec'])) {
            self::$config[$preset]['webhooks.connec.notification_path'] = $settings['webhooks']['connec']['notification_path'];
        }

        return true;
    }
592213
public function setTheme(?string $key = null, bool $overrideKey = false) : Theme
    {
        if ($this->useTheme !== null && ($key === null || $this->useTheme === $key)) {
            return $this->useTheme;
        }

        if ($overrideKey) {
            return $this->useTheme = $this->getTheme($key);
        }

        if ($this->themes->hasOverride()) {
            return $this->useTheme = $this->themes->getOverride();
        }

        if ($this->hasUserOverride()) {
            return $this->useTheme = $this->themes->setOverride($this->getUserOverride());
        }

        return $this->useTheme = $this->getTheme($key);
    }
296533
public function get($key)
    {
        if (!$this->has($key)) {
            return null;
        }

        return (new SettingModel)
            ->cached()
            ->where('key', $key)
            ->first()
            ->value;
    }
810119
public function getAdminSetting($key)
    {
        if (!$this->adminSettings) {
            $this->adminSettings = new AdminSettings();
        }

        return $this->adminSettings->getSetting($key);
    }
556854
public function addRemoveThemeSupport($key, $props = [])
    {
        // Setup theme
        add_action('after_setup_theme', function () use ($key, $props) {
            // Check props
            if (is_bool($props) && !$props) {
                // Remove theme support
                remove_theme_support($key);
            } else if (empty($props)) {
                // Add theme support with default configuration
                add_theme_support($key);
            } else {
                // Add theme support with configurations
                add_theme_support($key, $props);
            }
        });
    }
387095
protected function settings($key = false)
    {
        if ($this->settings == false) {
            return false;
        }
        if ($key == false) {
            return $this->settings;
        }
        if (isset($this->settings[$key])) {
            return $this->settings[$key];
        }
        return false;
    }
480086
public function getSetting($key): SettingSchema
    {
        if (!$this->hasSetting($key)) {
            throw new \InvalidArgumentException("No setting key `$key` in this section.`");
        }

        return $this->settings[$key];
    }