use namespace

This commit is contained in:
Jean-Christian Denis 2023-03-26 22:27:21 +02:00
parent 1ae6909f78
commit 9584c96c92
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
4 changed files with 166 additions and 83 deletions

View file

@ -7,7 +7,7 @@
*
* @author Oleksandr Syenchuk, Pierre Van Glabeke, Gvx and Contributors
*
* @copyright Jean-Crhistian Denis
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
if (!defined('DC_RC_PATH')) {
@ -16,6 +16,9 @@ if (!defined('DC_RC_PATH')) {
class initEmailOptionnel
{
/** @var string Default email */
public const DEFAULT_EMAIL = 'invalid@invalid.fr';
public const SETTING_NAME = 'emailoptionnel';
/** @var string This plugin setting name */
public const SETTING_NAME = 'emailoptionnel';
}

View file

@ -7,34 +7,57 @@
*
* @author Oleksandr Syenchuk, Pierre Van Glabeke, Gvx and Contributors
*
* @copyright Jean-Crhistian Denis
* @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\emailOptionnel;
use dcCore;
use dcNsProcess;
use dcSettings;
use Dotclear\Helper\Html\Form\{
Checkbox,
Label,
Para
};
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()->addBehavior('adminBlogPreferencesFormV2', function (): void {
echo
'<div class="fieldset">' .
'<h4 id="emailOptionnelParam">' . __('Optional e-mail address') . '</h4>' .
(new Para())->items([
(new Checkbox(My::SETTING_NAME, (bool) dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')))->value(1),
(new Label(__('Make e-mail address optional in comments'), Label::OUTSIDE_LABEL_AFTER))->for(My::SETTING_NAME)->class('classic'),
])->render() .
'</div>';
});
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function (dcSettings $blog_settings): void {
$blog_settings->get(My::SETTING_NAME)->put(
'enabled',
!empty($_POST[My::SETTING_NAME]),
'boolean',
__('Make e-mail address optional in comments')
);
});
return true;
}
}
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function () {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME);
echo
'<div class="fieldset"><h4 id="emailOptionnelParam">' . __('Optional e-mail address') . '</h4>' .
'<p>' . form::checkbox(
initEmailOptionnel::SETTING_NAME,
'1',
dcCore::app()->blog->settings->__get(initEmailOptionnel::SETTING_NAME)->enabled ? true : false
) .
'<label class="classic" for="' . initEmailOptionnel::SETTING_NAME . '">' . __('Make e-mail address optional in comments') . '</label></p>' .
'</div>';
});
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function ($blog_settings) {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME);
$blog_settings->__get(initEmailOptionnel::SETTING_NAME)->put(
'enabled',
empty($_POST[initEmailOptionnel::SETTING_NAME]) ? false : true,
'boolean',
__('Make e-mail address optional in comments')
);
});

View file

@ -7,59 +7,77 @@
*
* @author Oleksandr Syenchuk, Pierre Van Glabeke, Gvx and Contributors
*
* @copyright Jean-Crhistian Denis
* @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);
namespace Dotclear\Plugin\emailOptionnel;
use cursor;
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) {
return false;
}
dcCore::app()->addBehaviors([
'publicPrependV2' => function (): void {
if (!isset($_POST['c_content'])
|| !empty($_POST['preview'])
|| !empty($_POST['c_mail'])
|| !dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')
) {
return;
}
$_POST['c_mail'] = My::DEFAULT_EMAIL;
},
'publicBeforeCommentCreate' => function (cursor $cur) {
if (dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')
&& $cur->getField('comment_email') == My::DEFAULT_EMAIL
) {
# désactive l'affichage du mail dans le template
dcCore::app()->ctx->comment_preview['mail'] = '';
# n'enregistre pas de mail dans la BDD
$cur->setField('comment_email', '');
# n'enregistre pas le mail dans le cookie
if (empty($_POST['c_remember'])) {
return;
}
if (!empty($_COOKIE['comment_info'])) {
$cookie_info = explode("\n", $_COOKIE['comment_info']);
if (count($cookie_info) == 3) {
return;
}
}
$c_cookie = ['name' => $cur->getField('comment_author'), 'mail' => $cur->getField('comment_email'), 'site' => $cur->getField('comment_site')];
$c_cookie = serialize($c_cookie);
setcookie('comment_info', $c_cookie, strtotime('+3 month'), '/');
}
},
'publicHeadContent' => function () {
if (dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')) {
echo dcUtils::jsLoad(
dcCore::app()->blog->getPF(My::id() . '/js/frontend.js'),
dcCore::app()->plugins->moduleInfo(My::id(), 'version')
);
}
},
]);
return true;
}
}
dcCore::app()->addBehavior('publicPrependV2', function () {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME);
if (!isset($_POST['c_content'])
|| !empty($_POST['preview'])
|| !empty($_POST['c_mail'])
|| !dcCore::app()->blog->settings->get(initEmailOptionnel::SETTING_NAME)->enabled
) {
return;
}
$_POST['c_mail'] = initEmailOptionnel::DEFAULT_EMAIL;
});
dcCore::app()->addBehavior('publicBeforeCommentCreate', function ($cur) {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME);
if (dcCore::app()->blog->settings->get(initEmailOptionnel::SETTING_NAME)->enabled
&& $cur->comment_email == initEmailOptionnel::DEFAULT_EMAIL
) {
# désactive l'affichage du mail dans le template
dcCore::app()->ctx->comment_preview['mail'] = '';
# n'enregistre pas de mail dans la BDD
$cur->comment_email = '';
# n'enregistre pas le mail dans le cookie
if (empty($_POST['c_remember'])) {
return;
}
if (!empty($_COOKIE['comment_info'])) {
$cookie_info = explode("\n", $_COOKIE['comment_info']);
if (count($cookie_info) == 3) {
return;
}
}
$c_cookie = ['name' => $cur->comment_author, 'mail' => $cur->comment_email, 'site' => $cur->comment_site];
$c_cookie = serialize($c_cookie);
setcookie('comment_info', $c_cookie, strtotime('+3 month'), '/');
}
});
dcCore::app()->addBehavior('publicHeadContent', function () {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME);
if (dcCore::app()->blog->settings->get(initEmailOptionnel::SETTING_NAME)->enabled) {
echo dcUtils::jsLoad(
dcCore::app()->blog->getPF(basename(__DIR__) . '/public.js'),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version')
);
}
});

39
src/My.php Normal file
View file

@ -0,0 +1,39 @@
<?php
/**
* @brief emailOptionnel, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Oleksandr Syenchuk, Pierre Van Glabeke, Gvx 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\emailOptionnel;
use dcCore;
/**
* Plugin definitions
*/
class My extends \initEmailOptionnel
{
/**
* 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'));
}
}