use namespace

This commit is contained in:
Jean-Christian Denis 2023-03-25 00:19:15 +01:00
parent 9ac6481d85
commit 9a55cd1f2c
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
6 changed files with 297 additions and 141 deletions

View file

@ -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);
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']),
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) {
$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')],
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;
}
}

View file

@ -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 () {
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(basename(__DIR__)),
dcCore::app()->blog->url . dcCore::app()->url->getURLFor(My::id()),
'screen',
md5($css) //no cache on content change
);
}
});
});
return true;
}
}

View file

@ -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;
}
public static function process(): bool
{
if (!static::$init) {
return false;
}
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) {
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
}
return false;
return true;
}
}

View file

@ -10,17 +10,39 @@
* @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([
namespace Dotclear\Plugin\moreCSS;
use dcAuth;
use dcCore;
use dcNsProcess;
use dcPage;
use Exception;
use html;
use form;
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);
$s = dcCore::app()->blog->settings->get('themes');
return static::$init;
}
if (isset($_POST['morecss'])) {
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']);
@ -36,60 +58,73 @@ if (isset($_POST['morecss'])) {
$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));
$s->put('morecss_min', is_string($css_min) ? base64_encode($css_min) : '');
dcAdminNotices::addSuccessNotice(
dcPage::addSuccessNotice(
__('Configuration successfully updated.')
);
dcCore::app()->adminurl->redirect(
'admin.plugin.' . basename(__DIR__)
'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'))
: ''
)
);
echo '
<html><head><title>' . __('Style sheet') . '</title>';
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 '
</head><body>' .
dcPage::breadcrumb([
dcPage::breadcrumb([
html::escapeHTML(dcCore::app()->blog->name) => '',
__('Style sheet') => '',
]) .
dcPage::notices() . '
My::name() => '',
]) .
dcPage::notices() . '
<form action="' . dcCore::app()->admin->getPageURL() . '" id="file-form" method="post">
<form action="' . dcCore::app()->admin->getPageURL() . '" id="file-form" method="post">
<div><h3><label for="morecss">' . __('Style sheet:') . '</strong></label></h3>
<p>' . form::textarea('morecss', 72, 25, [
<div><h3><label for="morecss">' . __('Style sheet:') . '</strong></label></h3>
<p>' . form::textarea('morecss', 72, 25, [
'default' => html::escapeHTML((string) base64_decode((string) $s->get('morecss'))),
'class' => 'maximal',
]) . '</p>
]) . '</p>
<p><label class="classic" for="morecss_active">' .
form::checkbox('morecss_active', 1, $s->get('morecss_active')) . ' ' .
__('Enable additionnal CSS for the active theme') .
'</label></p>
<p><label class="classic" for="morecss_active">' .
form::checkbox('morecss_active', 1, $s->get('morecss_active')) . ' ' .
__('Enable additionnal CSS for the active theme') .
'</label></p>
<p>' .
form::hidden('p', 'moreCSS') .
dcCore::app()->formNonce() . '
<input type="submit" name="write" value="' . __('Save') . ' (s)" accesskey="s" /></p>
</form>';
<p>' .
form::hidden('p', 'moreCSS') .
dcCore::app()->formNonce() . '
<input type="submit" name="write" value="' . __('Save') . ' (s)" accesskey="s" /></p>
</form>';
if (dcCore::app()->auth->user_prefs->interface->colorsyntax) {
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->interface->colorsyntax_theme);
}
dcPage::jsRunCodeMirror('editor', 'morecss', 'dotclear', dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax_theme'));
}
echo '
</body>
</html>';
dcPage::closeModule();
}
}

39
src/My.php Normal file
View file

@ -0,0 +1,39 @@
<?php
/**
* @brief moreCSS, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Osku and contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
declare(strict_types=1);
namespace Dotclear\Plugin\moreCSS;
use dcCore;
/**
* Plugin definitions
*/
class My
{
/**
* This module id
*/
public static function id(): string
{
return basename(dirname(__DIR__));
}
/**
* This module name
*/
public static function name(): string
{
return __((string) dcCore::app()->plugins->moduleInfo(self::id(), 'name'));
}
}

View file

@ -10,15 +10,33 @@
* @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__),
namespace Dotclear\Plugin\moreCSS;
use dcCore;
use dcNsProcess;
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 ($args) {
function (string $args): void {
header('Content-Type: text/css; charset=UTF-8');
echo "/* CSS for plugin moreCss */ \n";
@ -26,4 +44,8 @@ dcCore::app()->url->register(
exit;
}
);
);
return true;
}
}