module($_REQUEST['config']); } private static function getPreference(bool $all = false): array { try { 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 $all ? $preferences : (array_key_exists(self::$type, $preferences) ? $preferences[self::$type] : []); } } } } catch (Exception $e) { } return []; } private static function setPreferences(): bool { if (!empty($_POST['save_preferences'])) { $preferences = self::getPreference(true); $preferences[self::$type] = []; if (!empty($_POST['actions'])) { 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->get(Core::id())->put('preferences', serialize($preferences), 'string', null, true, true); dcAdminNotices::addSuccessNotice(__('Configuration successfully updated')); return true; } return false; } private static function comboModules(): array { $allow_distrib = (bool) dcCore::app()->blog->settings->get(Core::id())->get('allow_distrib'); $official = [ 'plugin' => explode(',', DC_DISTRIB_PLUGINS), 'theme' => explode(',', DC_DISTRIB_THEMES), ]; if (!(dcCore::app()->themes instanceof dcThemes)) { dcCore::app()->themes = new dcThemes(); dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null); } $combo_modules = []; $modules = self::getModules(self::$type == 'plugin' ? 'plugins' : 'themes'); foreach ($modules as $id => $m) { if (!$m['root_writable'] || !$allow_distrib && in_array($id, $official[self::$type])) { continue; } $combo_modules[sprintf(__('%s (%s)'), __($m['name']), $id)] = $id; } dcUtils::lexicalKeySort($combo_modules); return array_merge([__('Select a module') => '-'], $combo_modules); } public static function getModules(string $type, ?string $id = null): ?array { $type = $type == 'themes' ? 'themes' : 'plugins'; $modules = array_merge(dcCore::app()->{$type}->getDisabledModules(), dcCore::app()->{$type}->getModules()); if (empty($id)) { return $modules; } elseif (array_key_exists($id, $modules)) { return $modules[$id]; } return null; } public static function process(): void { if (!self::$init) { return; } $log_id = ''; $done = self::setPreferences(); if (!empty($_POST['fix'])) { if (empty($_POST['actions'])) { dcAdminNotices::addWarningNotice(__('No action selected')); } elseif (self::$module == '-') { dcAdminNotices::addWarningNotice(__('No module selected')); } else { try { $time = self::$improve->fixModule( self::$type, self::$module, self::getModules(self::$type == 'plugin' ? 'plugins' : 'themes', self::$module), $_POST['actions'] ); $log_id = self::$improve->writeLogs(); dcCore::app()->blog->triggerBlog(); if (self::$improve->hasLog('error')) { $notice = ['type' => dcAdminNotices::NOTICE_ERROR, 'msg' => __('Fix of "%s" complete in %s secondes with errors')]; } elseif (self::$improve->hasLog('warning')) { $notice = ['type' => dcAdminNotices::NOTICE_WARNING, 'msg' => __('Fix of "%s" complete in %s secondes with warnings')]; } 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'], self::$module, $time)); $done = true; } catch (Exception $e) { dcCore::app()->error->add($e->getMessage()); $done = false; } } } if ($done) { dcCore::app()->adminurl->redirect('admin.plugin.' . Core::id(), ['type' => self::$type, 'module' => self::$module, 'upd' => $log_id]); } } public static function render(): void { if (!self::$init) { return; } $bc = empty($_REQUEST['config']) ? (self::$type == 'theme' ? __('Themes actions') : __('Plugins actions')) : __('Configure module'); echo '' . __('improve') . '' . dcPage::jsModuleLoad(Core::id() . '/js/index.js') . (self::$action === null ? '' : self::$action->header()) . '' . dcPage::notices() . dcPage::breadcrumb([ __('Plugins') => '', __('improve') => '', $bc => '', ]); if (empty($_REQUEST['config'])) { self::displayActions(); } else { self::displayConfigurator(); } echo ''; } private static function displayConfigurator(): void { $back_url = $_REQUEST['redir'] ?? dcCore::app()->adminurl->get('admin.plugin.' . Core::id(), ['type' => self::$type]); if (null === self::$action) { echo '

' . __('Unknow module') . '

' . __('Back') . '

'; } else { $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"'), self::$action->name()) . '

' . __('Back') . '

' . html::escapeHTML(self::$action->description()) . '

' . (empty($res) ? '

' . __('Nothing to configure') . '

' : $res) . '

' . form::hidden('type', self::$type) . form::hidden('config', self::$action->id()) . form::hidden('redir', $redir) . dcCore::app()->formNonce() . '

' . '
'; } } private static function displayActions(): void { echo '
' . '

' . form::combo('type', [__('Plugins') => 'plugin', __('Themes') => 'theme'], self::$type) . ' ' . '' . form::hidden('p', 'improve') . '

' . '
'; $combo_modules = self::comboModules(); if (count($combo_modules) == 1) { echo '

' . __('No module to manage') . '

'; } else { echo '
' . '' . ''; foreach (self::$improve->modules() as $action) { if (!in_array(self::$type, $action->types())) { continue; } echo '' . '' . '' . '' . '' . (DC_DEBUG ? '' : '') . /* @phpstan-ignore-line */ ''; } echo '
' . __('Action') . '' . '' . __('Description') . '' . '' . __('Configuration') . '' . (DC_DEBUG ? '' . __('Priority') . '' : '') . /* @phpstan-ignore-line */ '
' . form::checkbox( ['actions[]', 'action_' . $action->id(), ], $action->id(), in_array($action->id(), self::getPreference()) && $action->isConfigured(), '', '', !$action->isConfigured() ) . '' . '' . '' . $action->description() . '' . ( false === $action->configurator() ? '' : 'name()) . '">' . __('Configure') . '' ) . '' . $action->priority() . '

' . form::combo('module', $combo_modules, self::$module) . ' ' . form::hidden(['type'], self::$type) . dcCore::app()->formNonce() . '


'; if (!empty($_REQUEST['upd']) && !dcCore::app()->blog->settings->get(Core::id())->get('nodetails')) { $logs = self::$improve->parseLogs((int) $_REQUEST['upd']); if (!empty($logs)) { echo '

' . __('Details') . '

'; foreach ($logs as $path => $types) { echo '
' . $path . '
'; foreach ($types as $type => $tools) { echo '
'; } echo ''; } echo '
'; } } } } }