diff --git a/inc/Admin.php b/inc/Admin.php
index fddc4cd..7303a9b 100644
--- a/inc/Admin.php
+++ b/inc/Admin.php
@@ -12,11 +12,7 @@
*/
declare(strict_types=1);
-namespace plugins\improve;
-
-if (!defined('DC_CONTEXT_ADMIN')) {
- return;
-}
+namespace Dotclear\Plugin\improve;
/* dotclear */
use dcAdmin;
@@ -33,58 +29,52 @@ use files;
*
* Add menu and dashboard icons, load Improve action modules.
*/
-class admin
+class Admin
{
- public static function process(): void
+ private static $init = false;
+
+ public static function init(): bool
{
- self::addSettingsNamespace();
- self::addAdminBehaviors();
- self::addAdminMenu();
- self::addImproveActions();
+ if (defined('DC_CONTEXT_ADMIN')) {
+ dcCore::app()->blog->settings->addNamespace(Core::id());
+ self::$init = true;
+ }
+
+ return self::$init;
}
- private static function addSettingsNamespace(): void
+ public static function process()
{
- dcCore::app()->blog->settings->addNamespace('improve');
- }
+ if (!self::$init) {
+ return false;
+ }
- private static function addAdminBehaviors(): void
- {
dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void {
$favs->register(
'improve',
[
'title' => __('improve'),
- 'url' => dcCore::app()->adminurl->get('admin.plugin.improve'),
- 'small-icon' => dcPage::getPF('improve/icon.svg'),
- 'large-icon' => dcPage::getPF('improve/icon.svg'),
+ 'url' => dcCore::app()->adminurl->get('admin.plugin.' . Core::id()),
+ 'small-icon' => dcPage::getPF(Core::id() . '/icon.svg'),
+ 'large-icon' => dcPage::getPF(Core::id() . '/icon.svg'),
//'permissions' => null,
]
);
});
- }
- private static function addAdminMenu(): void
- {
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
__('improve'),
- dcCore::app()->adminurl->get('admin.plugin.improve'),
- dcPage::getPF('improve/icon.svg'),
- preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.improve')) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
+ dcCore::app()->adminurl->get('admin.plugin.' . Core::id()),
+ dcPage::getPF(Core::id() . '/icon.svg'),
+ preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . Core::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->isSuperAdmin()
);
- }
- private static function addImproveActions(): void
- {
- foreach (files::scandir(prepend::getActionsDir()) as $file) {
- if (is_file(prepend::getActionsDir() . $file) && '.php' == substr($file, -4)) {
- Clearbricks::lib()->autoload([prepend::getActionsNS() . substr($file, 0, -4) => prepend::getActionsDir() . $file]);
- dcCore::app()->addBehavior('improveAddAction', [prepend::getActionsNS() . substr($file, 0, -4), 'create']); /* @phpstan-ignore-line */
+ foreach (files::scandir(Prepend::getActionsDir()) as $file) {
+ if (is_file(Prepend::getActionsDir() . $file) && '.php' == substr($file, -4)) {
+ Clearbricks::lib()->autoload([Prepend::getActionsNS() . substr($file, 0, -4) => Prepend::getActionsDir() . $file]);
+ dcCore::app()->addBehavior('improveAddAction', [Prepend::getActionsNS() . substr($file, 0, -4), 'create']); /* @phpstan-ignore-line */
}
}
}
}
-
-/* process */
-admin::process();
diff --git a/inc/Config.php b/inc/Config.php
index b52bf51..754fcd2 100644
--- a/inc/Config.php
+++ b/inc/Config.php
@@ -12,11 +12,7 @@
*/
declare(strict_types=1);
-namespace plugins\improve;
-
-if (!defined('DC_CONTEXT_ADMIN')) {
- return;
-}
+namespace Dotclear\Plugin\improve;
/* dotclear */
use dcCore;
@@ -33,34 +29,28 @@ use Exception;
*
* Set preference for this plugin.
*/
-class config
+class Config
{
- /** @var improve $improve improve core instance */
- private $improve = null;
+ private static $init = false;
- public function __construct()
+ public static function init(): bool
{
- dcPage::checkSuper();
+ if (defined('DC_CONTEXT_ADMIN')) {
+ dcPage::checkSuper();
- $this->improve = new improve();
-
- $this->saveConfig();
- $this->displayConfig();
- }
-
- private function getModules(): array
- {
- $modules = [];
- foreach ($this->improve->modules() as $action) {
- $modules[$action->name()] = $action->id();
+ dcCore::app()->blog->settings->addNamespace(Core::id());
+ self::$init = true;
}
- $modules = array_merge($modules, array_flip($this->improve->disabled()));
- return $modules;
+ return self::$init;
}
- private function saveConfig(): void
+ public static function process(): void
{
+ if (!self::$init) {
+ return;
+ }
+
if (empty($_POST['save'])) {
return;
}
@@ -70,8 +60,8 @@ class config
if (!empty($_POST['disabled']) && is_array($_POST['disabled'])) {
$pdisabled = implode(';', $_POST['disabled']);
}
- dcCore::app()->blog->settings->improve->put('disabled', $pdisabled);
- dcCore::app()->blog->settings->improve->put('nodetails', !empty($_POST['nodetails']));
+ dcCore::app()->blog->settings->get(Core::id())->put('disabled', $pdisabled);
+ dcCore::app()->blog->settings->get(Core::id())->put('nodetails', !empty($_POST['nodetails']));
dcPage::addSuccessNotice(__('Configuration successfully updated'));
@@ -84,24 +74,33 @@ class config
}
}
- private function displayConfig(): void
+ public static function render()
{
+ if (!self::$init) {
+ return;
+ }
+
+ $improve = new Core();
+
+ $modules = [];
+ foreach ($improve->modules() as $action) {
+ $modules[$action->name()] = $action->id();
+ }
+ $modules = array_merge($modules, array_flip($improve->disabled()));
+
echo '
' . __('List of disabled actions:') . '
';
- foreach ($this->getModules() as $name => $id) {
+ foreach ($modules as $name => $id) {
echo
'
';
}
echo
'
' . __('Options') . '
' .
'
' .
'
';
}
}
-
-/* process */
-new config();
diff --git a/inc/Install.php b/inc/Install.php
index 81a78da..31f5793 100644
--- a/inc/Install.php
+++ b/inc/Install.php
@@ -12,15 +12,10 @@
*/
declare(strict_types=1);
-namespace plugins\improve;
-
-if (!defined('DC_CONTEXT_ADMIN')) {
- return;
-}
+namespace Dotclear\Plugin\improve;
/* dotclear */
use dcCore;
-use dcUtils;
/* php */
use Exception;
@@ -31,7 +26,7 @@ use Exception;
* Set default settings and version
* and manage changes on updates.
*/
-class install
+class Install
{
/** @var array Improve default settings */
private static $default_settings = [[
@@ -41,26 +36,37 @@ class install
'string',
]];
+ // Nothing to change below
+ private static $init = false;
+
+ public static function init(): bool
+ {
+ self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(Core::id(), dcCore::app()->plugins->moduleInfo(Core::id(), 'version'));
+
+ return self::$init;
+ }
+
public static function process(): ?bool
{
- if (!dcCore::app()->newVersion(
- basename(__DIR__),
- dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
- )) {
- return null;
+ if (!self::$init) {
+ return false;
}
- dcCore::app()->blog->settings->addNamespace(basename(__DIR__));
- self::update_0_8_0();
- self::putSettings();
+ try {
+ self::update_0_8_0();
+ self::putSettings();
+ return true;
+ } catch (Exception $e) {
+ dcCore::app()->error->add($e->getMessage());
- return true;
+ return false;
+ }
}
private static function putSettings(): void
{
foreach (self::$default_settings as $v) {
- dcCore::app()->blog->settings->__get(basename(__DIR__))->put(
+ dcCore::app()->blog->settings->get(Core::id())->put(
$v[0],
$v[2],
$v[3],
@@ -74,22 +80,13 @@ class install
/** Update improve < 0.8 : action modules settings name */
private static function update_0_8_0(): void
{
- if (version_compare(dcCore::app()->getVersion(basename(__DIR__)) ?? '0', '0.8', '<')) {
- foreach (dcCore::app()->blog->settings->__get(basename(__DIR__))->dumpGlobalSettings() as $id => $values) {
+ if (version_compare(dcCore::app()->getVersion(Core::id()) ?? '0', '0.8', '<')) {
+ foreach (dcCore::app()->blog->settings->get(Core::id())->dumpGlobalSettings() as $id => $values) {
$newId = str_replace('ImproveAction', '', $id);
if ($id != $newId) {
- dcCore::app()->blog->settings->__get(basename(__DIR__))->rename($id, strtolower($newId));
+ dcCore::app()->blog->settings->get(Core::id())->rename($id, strtolower($newId));
}
}
}
}
}
-
-/* process */
-try {
- return install::process();
-} catch (Exception $e) {
- dcCore::app()->error->add($e->getMessage());
-
- return false;
-}
diff --git a/inc/Manage.php b/inc/Manage.php
index d9981c1..c6b7645 100644
--- a/inc/Manage.php
+++ b/inc/Manage.php
@@ -12,11 +12,7 @@
*/
declare(strict_types=1);
-namespace plugins\improve;
-
-if (!defined('DC_CONTEXT_ADMIN')) {
- return;
-}
+namespace Dotclear\Plugin\improve;
/* dotclear */
use dcCore;
@@ -38,59 +34,63 @@ use Exception;
* Display page and configure modules
* and launch action.
*/
-class index
+class Manage
{
/** @var improve $improve improve core instance */
- private $improve = null;
+ private static $improve = null;
/** @var string $type Current module(s) type */
- private $type = 'plugin';
+ private static $type = 'plugin';
/** @var string $module Current module id */
- private $module = '-';
+ private static $module = '-';
/** @var action|null $action Current action module */
- private $action = null;
+ private static $action = null;
- public function __construct()
+ private static $init = false;
+
+ public static function init(): bool
{
- dcPage::checkSuper();
+ if (defined('DC_CONTEXT_ADMIN')) {
+ dcPage::checkSuper();
- $this->improve = new improve();
- $this->type = $this->getType();
- $this->module = $this->getModule();
- $this->action = $this->getAction();
+ self::$improve = new Core();
+ self::$type = self::getType();
+ self::$module = self::getModule();
+ self::$action = self::getAction();
+ self::$init = true;
+ }
- $this->doAction();
- $this->displayPage();
+ return self::$init;
}
- private function getType(): string
+ private static function getType(): string
{
return $_REQUEST['type'] ?? 'plugin';
}
- private function getModule(): string
+ private static function getModule(): string
{
$module = $_REQUEST['module'] ?? '';
- if (!in_array($module, $this->comboModules())) {
+ if (!in_array($module, self::comboModules())) {
$module = '-';
}
return $module;
}
- private function getAction(): ?action
+ private static function getAction(): ?action
{
- return empty($_REQUEST['config']) ? null : $this->improve->module($_REQUEST['config']);
+ return empty($_REQUEST['config']) ? null : self::$improve->module($_REQUEST['config']);
}
- private function getPreference(): array
+ private static function getPreference(): array
{
try {
- if (!empty($this->type)) {
- $preferences = dcCore::app()->blog->settings->improve->preferences;
+ if (!empty(self::$type)) {
+ $preferences = dcCore::app()->blog->settings->get(Core::id())->get('preferences');
if (is_string($preferences)) {
$preferences = unserialize($preferences);
if (is_array($preferences)) {
- return array_key_exists($this->type, $preferences) ? $preferences[$this->type] : [];
+ return array_key_exists(self::$type, $preferences) ? $preferences[self::$type] : [];
}
}
}
@@ -100,18 +100,18 @@ class index
return [];
}
- private function setPreferences(): bool
+ private static function setPreferences(): bool
{
if (!empty($_POST['save_preferences'])) {
- $preferences[$this->type] = [];
+ $preferences[self::$type] = [];
if (!empty($_POST['actions'])) {
- foreach ($this->improve->modules() as $action) {
- if (in_array($this->type, $action->types()) && in_array($action->id(), $_POST['actions'])) {
- $preferences[$this->type][] = $action->id();
+ foreach (self::$improve->modules() as $action) {
+ if (in_array(self::$type, $action->types()) && in_array($action->id(), $_POST['actions'])) {
+ $preferences[self::$type][] = $action->id();
}
}
}
- dcCore::app()->blog->settings->improve->put('preferences', serialize($preferences), 'string', null, true, true);
+ dcCore::app()->blog->settings->get(Core::id())->put('preferences', serialize($preferences), 'string', null, true, true);
dcAdminNotices::addSuccessNotice(__('Configuration successfully updated'));
return true;
@@ -120,9 +120,9 @@ class index
return false;
}
- private function comboModules(): array
+ private static function comboModules(): array
{
- $allow_distrib = (bool) dcCore::app()->blog->settings->improve->allow_distrib;
+ $allow_distrib = (bool) dcCore::app()->blog->settings->get(Core::id())->get('allow_distrib');
$official = [
'plugin' => explode(',', DC_DISTRIB_PLUGINS),
'theme' => explode(',', DC_DISTRIB_THEMES),
@@ -134,9 +134,9 @@ class index
}
$combo_modules = [];
- $modules = self::getModules($this->type == 'plugin' ? 'plugins' : 'themes');
+ $modules = self::getModules(self::$type == 'plugin' ? 'plugins' : 'themes');
foreach ($modules as $id => $m) {
- if (!$m['root_writable'] || !$allow_distrib && in_array($id, $official[$this->type])) {
+ if (!$m['root_writable'] || !$allow_distrib && in_array($id, $official[self::$type])) {
continue;
}
$combo_modules[sprintf(__('%s (%s)'), __($m['name']), $id)] = $id;
@@ -161,37 +161,41 @@ class index
return null;
}
- private function doAction(): void
+ public static function process(): void
{
+ if (!self::$init) {
+ return;
+ }
+
$log_id = '';
- $done = $this->setPreferences();
+ $done = self::setPreferences();
if (!empty($_POST['fix'])) {
if (empty($_POST['actions'])) {
dcAdminNotices::addWarningNotice(__('No action selected'));
- } elseif ($this->module == '-') {
+ } elseif (self::$module == '-') {
dcAdminNotices::addWarningNotice(__('No module selected'));
} else {
try {
- $time = $this->improve->fixModule(
- $this->type,
- $this->module,
- self::getModules($this->type == 'plugin' ? 'plugins' : 'themes', $this->module),
+ $time = self::$improve->fixModule(
+ self::$type,
+ self::$module,
+ self::getModules(self::$type == 'plugin' ? 'plugins' : 'themes', self::$module),
$_POST['actions']
);
- $log_id = $this->improve->writeLogs();
+ $log_id = self::$improve->writeLogs();
dcCore::app()->blog->triggerBlog();
- if ($this->improve->hasLog('error')) {
+ if (self::$improve->hasLog('error')) {
$notice = ['type' => dcAdminNotices::NOTICE_ERROR, 'msg' => __('Fix of "%s" complete in %s secondes with errors')];
- } elseif ($this->improve->hasLog('warning')) {
+ } elseif (self::$improve->hasLog('warning')) {
$notice = ['type' => dcAdminNotices::NOTICE_WARNING, 'msg' => __('Fix of "%s" complete in %s secondes with warnings')];
- } elseif ($this->improve->hasLog('success')) {
+ } elseif (self::$improve->hasLog('success')) {
$notice = ['type' => dcAdminNotices::NOTICE_SUCCESS, 'msg' => __('Fix of "%s" complete in %s secondes')];
} else {
$notice = ['type' => dcAdminNotices::NOTICE_SUCCESS, 'msg' => __('Fix of "%s" complete in %s secondes without messages')];
}
- dcAdminNotices::addNotice($notice['type'], sprintf($notice['msg'], $this->module, $time));
+ dcAdminNotices::addNotice($notice['type'], sprintf($notice['msg'], self::$module, $time));
$done = true;
} catch (Exception $e) {
@@ -202,19 +206,23 @@ class index
}
if ($done) {
- dcCore::app()->adminurl->redirect('admin.plugin.improve', ['type' => $this->type, 'module' => $this->module, 'upd' => $log_id]);
+ dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), ['type' => self::$type, 'module' => self::$module, 'upd' => $log_id]);
}
}
- private function displayPage(): void
+ public static function render(): void
{
+ if (!self::$init) {
+ return;
+ }
+
$bc = empty($_REQUEST['config']) ?
- ($this->type == 'theme' ? __('Themes actions') : __('Plugins actions')) :
+ (self::$type == 'theme' ? __('Themes actions') : __('Plugins actions')) :
__('Configure module');
echo '' . __('improve') . '' .
- dcPage::jsLoad(dcPage::getPF('improve/js/index.js')) .
- ($this->action === null ? '' : $this->action->header()) .
+ dcPage::jsModuleLoad(Core::id() . '/js/index.js') .
+ (self::$action === null ? '' : self::$action->header()) .
'' .
dcPage::notices() .
dcPage::breadcrumb([
@@ -224,64 +232,64 @@ class index
]);
if (empty($_REQUEST['config'])) {
- $this->displayActions();
+ self::displayActions();
} else {
- $this->displayConfigurator();
+ self::displayConfigurator();
}
echo '';
}
- private function displayConfigurator(): void
+ private static function displayConfigurator(): void
{
- $back_url = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.improve', ['type' => $this->type]);
+ $back_url = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.' . Core::id(), ['type' => self::$type]);
- if (null === $this->action) {
+ if (null === self::$action) {
echo '
' . __('Unknow module') . '
' . __('Back') . '
';
} else {
- $redir = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.improve', ['type' => $this->type, 'config' => $this->action->id()]);
- $res = $this->action->configure($redir);
+ $redir = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.' . Core::id(), ['type' => self::$type, 'config' => self::$action->id()]);
+ $res = self::$action->configure($redir);
echo '
- ' . sprintf(__('Configure module "%s"'), $this->action->name()) . '
+ ' . sprintf(__('Configure module "%s"'), self::$action->name()) . '
' . __('Back') . '
- ' . html::escapeHTML($this->action->description()) . '
- ';
}
}
- private function displayActions(): void
+ private static function displayActions(): void
{
echo
- '';
- $combo_modules = $this->comboModules();
+ $combo_modules = self::comboModules();
if (count($combo_modules) == 1) {
echo '' . __('No module to manage') . '
';
} else {
- echo '