mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-04-02 14:24:27 -04:00
- Consolidate duplicate documentation sections - Move Internationalization section after Plugin Views - Remove redundant Example Plugin Structure and View Hooks sections - Fix PSR-12 brace style in plugin_helper.php - Fix PSR-12 brace style in PluginInterface.php (remove unnecessary PHPdocs) - Fix PSR-12 brace style in BasePlugin.php (remove unnecessary PHPdocs) - Use log_message() instead of error_log() in migration - Add IF NOT EXISTS to plugin_config table creation for resilience - Convert snake_case to camelCase for class names throughout docs
66 lines
1.7 KiB
PHP
66 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\Plugins;
|
|
|
|
use App\Models\Plugin_config;
|
|
|
|
abstract class BasePlugin implements PluginInterface
|
|
{
|
|
protected Plugin_config $configModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->configModel = new Plugin_config();
|
|
}
|
|
|
|
public function install(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function uninstall(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function isEnabled(): bool
|
|
{
|
|
$enabled = $this->configModel->get("{$this->getPluginId()}_enabled");
|
|
return $enabled === '1' || $enabled === 'true';
|
|
}
|
|
|
|
protected function getSetting(string $key, mixed $default = null): mixed
|
|
{
|
|
$value = $this->configModel->get("{$this->getPluginId()}_{$key}");
|
|
return $value ?? $default;
|
|
}
|
|
|
|
protected function setSetting(string $key, mixed $value): bool
|
|
{
|
|
$stringValue = is_array($value) || is_object($value)
|
|
? json_encode($value)
|
|
: (string)$value;
|
|
|
|
return $this->configModel->set("{$this->getPluginId()}_{$key}", $stringValue);
|
|
}
|
|
|
|
public function getSettings(): array
|
|
{
|
|
return $this->configModel->getPluginSettings($this->getPluginId());
|
|
}
|
|
|
|
public function saveSettings(array $settings): bool
|
|
{
|
|
$prefixedSettings = [];
|
|
foreach ($settings as $key => $value) {
|
|
$prefixedSettings["{$this->getPluginId()}_{$key}"] = (string)$value;
|
|
}
|
|
|
|
return $this->configModel->batchSave($prefixedSettings);
|
|
}
|
|
|
|
protected function log(string $level, string $message): void
|
|
{
|
|
log_message($level, "[Plugin:{$this->getPluginName()}] {$message}");
|
|
}
|
|
} |