From 9a55cd1f2c670cdd43449876190b5609614d1d3e Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 25 Mar 2023 00:19:15 +0100 Subject: [PATCH] use namespace --- src/Backend.php | 66 ++++++++++++----- src/Frontend.php | 49 +++++++++---- src/Install.php | 51 ++++++++----- src/Manage.php | 185 ++++++++++++++++++++++++++++------------------- src/My.php | 39 ++++++++++ src/Prepend.php | 48 ++++++++---- 6 files changed, 297 insertions(+), 141 deletions(-) create mode 100644 src/My.php diff --git a/src/Backend.php b/src/Backend.php index d84fa73..e5c7ab4 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -10,24 +10,50 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; +declare(strict_types=1); + +namespace Dotclear\Plugin\moreCSS; + +use dcAdmin; +use dcAuth; +use dcCore; +use dcFavorites; +use dcNsProcess; +use dcPage; + +class Backend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN'); + + return static::$init; + } + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem( + My::name(), + dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + [dcPage::getPF(My::id() . '/icon.png')], + preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) + ); + + dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void { + $favs->register(My::id(), [ + 'title' => My::name(), + 'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()), + 'small-icon' => [dcPage::getPF(My::id() . '/icon.png')], + 'large-icon' => [dcPage::getPF(My::id() . '/icon-big.png')], + 'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), + ]); + }); + + return true; + } } - -dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem( - __('Style sheet'), - dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), - [dcPage::getPF(basename(__DIR__) . '/icon.png')], - preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/', $_SERVER['REQUEST_URI']), - dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) -); - -dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs) { - $favs->register(basename(__DIR__), [ - 'title' => __('Style sheet'), - 'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)), - 'small-icon' => [dcPage::getPF(basename(__DIR__) . '/icon.png')], - 'large-icon' => [dcPage::getPF(basename(__DIR__) . '/icon-big.png')], - 'permissions' => dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), - ]); -}); diff --git a/src/Frontend.php b/src/Frontend.php index 73cf1da..42c25fd 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -10,21 +10,40 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -if (!dcCore::app()->blog->settings->get('themes')->get('morecss_active')) { - return null; -} +namespace Dotclear\Plugin\moreCSS; -dcCore::app()->addBehavior('publicHeadContent', function () { - $css = (string) base64_decode((string) dcCore::app()->blog->settings->get('themes')->get('morecss_min')); - if (!empty($css)) { - echo dcUtils::cssLoad( - dcCore::app()->blog->url . dcCore::app()->url->getURLFor(basename(__DIR__)), - 'screen', - md5($css) //no cache on content change - ); +use dcCore; +use dcNsProcess; +use dcUtils; + +class Frontend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_RC_PATH'); + + return static::$init; } -}); + + public static function process(): bool + { + if (!static::$init || !dcCore::app()->blog->settings->get('themes')->get('morecss_active')) { + return false; + } + + dcCore::app()->addBehavior('publicHeadContent', function (): void { + $css = (string) base64_decode((string) dcCore::app()->blog->settings->get('themes')->get('morecss_min')); + if (!empty($css)) { + echo dcUtils::cssLoad( + dcCore::app()->blog->url . dcCore::app()->url->getURLFor(My::id()), + 'screen', + md5($css) //no cache on content change + ); + } + }); + + return true; + } +} diff --git a/src/Install.php b/src/Install.php index 19edabe..f7327a1 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,26 +10,41 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -try { - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; +namespace Dotclear\Plugin\moreCSS; + +use dcCore; +use dcNsProcess; +use Exception; + +class Install extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version')); + + return static::$init; } - $s = dcCore::app()->blog->settings->get('themes'); - $s->put('morecss_active', true, 'boolean', 'Enable additionnal CSS for the active theme', false, true); - $s->put('morecss', '', 'string', 'Additionnal CSS for the active theme', false, true); - $s->put('morecss_min', '', 'string', 'Minified addtionnal CSS for the active theme', false, true); + public static function process(): bool + { + if (!static::$init) { + return false; + } - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + try { + $s = dcCore::app()->blog->settings->get('themes'); + $s->put('morecss_active', true, 'boolean', 'Enable additionnal CSS for the active theme', false, true); + $s->put('morecss', '', 'string', 'Additionnal CSS for the active theme', false, true); + $s->put('morecss_min', '', 'string', 'Minified addtionnal CSS for the active theme', false, true); + + return true; + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + return true; + } } - -return false; diff --git a/src/Manage.php b/src/Manage.php index 7aeefda..8f9242a 100644 --- a/src/Manage.php +++ b/src/Manage.php @@ -10,86 +10,121 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -dcPage::check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_CONTENT_ADMIN, -])); +namespace Dotclear\Plugin\moreCSS; -$s = dcCore::app()->blog->settings->get('themes'); +use dcAuth; +use dcCore; +use dcNsProcess; +use dcPage; +use Exception; +use html; +use form; -if (isset($_POST['morecss'])) { - try { - // Save CSS - $css = base64_encode($_POST['morecss']); - $s->put('morecss', $css); - $s->put('morecss_active', !empty($_POST['morecss_active'])); +class Manage extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_CONTEXT_ADMIN') + && dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + ]), dcCore::app()->blog->id); - // Minify it - $css_min = preg_replace('` {2,}`', ' ', $_POST['morecss']); - $css_min = preg_replace('/(\/\*[\s\S]*?\*\/)/', '', $css_min); - $css_min = preg_replace('/(\t|\r|\n)/', '', $css_min); - $css_min = str_replace([' { ', ' {', '{ '], '{', $css_min); - $css_min = str_replace([' } ', ' }', '} '], '}', $css_min); - $css_min = str_replace([' : ', ' :', ': '], ':', $css_min); - $css_min = str_replace([' ; ', ' ;', '; '], ';', $css_min); - $css_min = str_replace([' , ', ' ,', ', '], ',', $css_min); - $s->put('morecss_min', base64_encode($css_min)); + return static::$init; + } - dcAdminNotices::addSuccessNotice( - __('Configuration successfully updated.') + public static function process(): bool + { + if (!static::$init) { + return false; + } + + $s = dcCore::app()->blog->settings->get('themes'); + + if (isset($_POST['morecss'])) { + try { + // Save CSS + $css = base64_encode($_POST['morecss']); + $s->put('morecss', $css); + $s->put('morecss_active', !empty($_POST['morecss_active'])); + + // Minify it + $css_min = preg_replace('` {2,}`', ' ', $_POST['morecss']); + $css_min = preg_replace('/(\/\*[\s\S]*?\*\/)/', '', $css_min); + $css_min = preg_replace('/(\t|\r|\n)/', '', $css_min); + $css_min = str_replace([' { ', ' {', '{ '], '{', $css_min); + $css_min = str_replace([' } ', ' }', '} '], '}', $css_min); + $css_min = str_replace([' : ', ' :', ': '], ':', $css_min); + $css_min = str_replace([' ; ', ' ;', '; '], ';', $css_min); + $css_min = str_replace([' , ', ' ,', ', '], ',', $css_min); + $s->put('morecss_min', is_string($css_min) ? base64_encode($css_min) : ''); + + dcPage::addSuccessNotice( + __('Configuration successfully updated.') + ); + dcCore::app()->adminurl->redirect( + 'admin.plugin.' . My::id() + ); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + } + + return true; + } + + public static function render(): void + { + if (!static::$init) { + return; + } + + $s = dcCore::app()->blog->settings->get('themes'); + + dcPage::openModule( + My::name(), + ( + dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax') ? + dcPage::jsJson('dotclear_colorsyntax', ['colorsyntax' => dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax')]) . + dcPage::jsLoadCodeMirror(dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax_theme')) + : '' + ) ); - dcCore::app()->adminurl->redirect( - 'admin.plugin.' . basename(__DIR__) - ); - } catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + + echo + dcPage::breadcrumb([ + html::escapeHTML(dcCore::app()->blog->name) => '', + My::name() => '', + ]) . + dcPage::notices() . ' + +
+ +

+

' . form::textarea('morecss', 72, 25, [ + 'default' => html::escapeHTML((string) base64_decode((string) $s->get('morecss'))), + 'class' => 'maximal', + ]) . '

+ +

+ +

' . + form::hidden('p', 'moreCSS') . + dcCore::app()->formNonce() . ' +

+ '; + + if (dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax')) { + echo + dcPage::jsJson('theme_editor_mode', ['mode' => 'css']) . + dcPage::jsModuleLoad('themeEditor/js/mode.js') . + dcPage::jsRunCodeMirror('editor', 'morecss', 'dotclear', dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax_theme')); + } + + dcPage::closeModule(); } } - -echo ' -' . __('Style sheet') . ''; -if (dcCore::app()->auth->user_prefs->interface->colorsyntax) { - echo - dcPage::jsJson('dotclear_colorsyntax', ['colorsyntax' => dcCore::app()->auth->user_prefs->interface->colorsyntax]) . - dcPage::jsLoadCodeMirror(dcCore::app()->auth->user_prefs->interface->colorsyntax_theme); -} -echo ' -' . -dcPage::breadcrumb([ - html::escapeHTML(dcCore::app()->blog->name) => '', - __('Style sheet') => '', -]) . -dcPage::notices() . ' - -
- -

-

' . form::textarea('morecss', 72, 25, [ - 'default' => html::escapeHTML((string) base64_decode((string) $s->get('morecss'))), - 'class' => 'maximal', -]) . '

- -

- -

' . -form::hidden('p', 'moreCSS') . -dcCore::app()->formNonce() . ' -

-'; - -if (dcCore::app()->auth->user_prefs->interface->colorsyntax) { - echo - dcPage::jsJson('theme_editor_mode', ['mode' => 'css']) . - dcPage::jsModuleLoad('themeEditor/js/mode.js') . - dcPage::jsRunCodeMirror('editor', 'morecss', 'dotclear', dcCore::app()->auth->user_prefs->interface->colorsyntax_theme); -} - -echo ' - -'; diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..540eca3 --- /dev/null +++ b/src/My.php @@ -0,0 +1,39 @@ +plugins->moduleInfo(self::id(), 'name')); + } +} diff --git a/src/Prepend.php b/src/Prepend.php index 7f1aa3f..d3b8bee 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -10,20 +10,42 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); -dcCore::app()->url->register( - basename(__DIR__), - 'morecss.css', - '^morecss\.css(.*?)$', - function ($args) { - header('Content-Type: text/css; charset=UTF-8'); +namespace Dotclear\Plugin\moreCSS; - echo "/* CSS for plugin moreCss */ \n"; - echo (string) base64_decode((string) dcCore::app()->blog->settings->get('themes')->get('morecss_min')); +use dcCore; +use dcNsProcess; - exit; +class Prepend extends dcNsProcess +{ + public static function init(): bool + { + static::$init = defined('DC_RC_PATH'); + + return static::$init; } -); + + public static function process(): bool + { + if (!static::$init) { + return false; + } + + dcCore::app()->url->register( + My::id(), + 'morecss.css', + '^morecss\.css(.*?)$', + function (string $args): void { + header('Content-Type: text/css; charset=UTF-8'); + + echo "/* CSS for plugin moreCss */ \n"; + echo (string) base64_decode((string) dcCore::app()->blog->settings->get('themes')->get('morecss_min')); + + exit; + } + ); + + return true; + } +}