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 * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { if (!defined('DC_RC_PATH')) {
@ -16,6 +16,9 @@ if (!defined('DC_RC_PATH')) {
class initEmailOptionnel class initEmailOptionnel
{ {
/** @var string Default email */
public const DEFAULT_EMAIL = 'invalid@invalid.fr'; public const DEFAULT_EMAIL = 'invalid@invalid.fr';
/** @var string This plugin setting name */
public const SETTING_NAME = 'emailoptionnel'; public const SETTING_NAME = 'emailoptionnel';
} }

View file

@ -7,34 +7,57 @@
* *
* @author Oleksandr Syenchuk, Pierre Van Glabeke, Gvx and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_CONTEXT_ADMIN')) { declare(strict_types=1);
return null;
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;
} }
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function () { public static function process(): bool
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME); {
if (!static::$init) {
return false;
}
dcCore::app()->addBehavior('adminBlogPreferencesFormV2', function (): void {
echo echo
'<div class="fieldset"><h4 id="emailOptionnelParam">' . __('Optional e-mail address') . '</h4>' . '<div class="fieldset">' .
'<p>' . form::checkbox( '<h4 id="emailOptionnelParam">' . __('Optional e-mail address') . '</h4>' .
initEmailOptionnel::SETTING_NAME, (new Para())->items([
'1', (new Checkbox(My::SETTING_NAME, (bool) dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')))->value(1),
dcCore::app()->blog->settings->__get(initEmailOptionnel::SETTING_NAME)->enabled ? true : false (new Label(__('Make e-mail address optional in comments'), Label::OUTSIDE_LABEL_AFTER))->for(My::SETTING_NAME)->class('classic'),
) . ])->render() .
'<label class="classic" for="' . initEmailOptionnel::SETTING_NAME . '">' . __('Make e-mail address optional in comments') . '</label></p>' .
'</div>'; '</div>';
}); });
dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function ($blog_settings) { dcCore::app()->addBehavior('adminBeforeBlogSettingsUpdate', function (dcSettings $blog_settings): void {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME); $blog_settings->get(My::SETTING_NAME)->put(
$blog_settings->__get(initEmailOptionnel::SETTING_NAME)->put(
'enabled', 'enabled',
empty($_POST[initEmailOptionnel::SETTING_NAME]) ? false : true, !empty($_POST[My::SETTING_NAME]),
'boolean', 'boolean',
__('Make e-mail address optional in comments') __('Make e-mail address optional in comments')
); );
}); });
return true;
}
}

View file

@ -7,36 +7,52 @@
* *
* @author Oleksandr Syenchuk, Pierre Van Glabeke, Gvx and Contributors * @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 * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/ */
if (!defined('DC_RC_PATH')) { declare(strict_types=1);
return null;
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;
} }
dcCore::app()->addBehavior('publicPrependV2', function () { public static function process(): bool
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME); {
if (!static::$init) {
return false;
}
dcCore::app()->addBehaviors([
'publicPrependV2' => function (): void {
if (!isset($_POST['c_content']) if (!isset($_POST['c_content'])
|| !empty($_POST['preview']) || !empty($_POST['preview'])
|| !empty($_POST['c_mail']) || !empty($_POST['c_mail'])
|| !dcCore::app()->blog->settings->get(initEmailOptionnel::SETTING_NAME)->enabled || !dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')
) { ) {
return; return;
} }
$_POST['c_mail'] = initEmailOptionnel::DEFAULT_EMAIL; $_POST['c_mail'] = My::DEFAULT_EMAIL;
}); },
'publicBeforeCommentCreate' => function (cursor $cur) {
dcCore::app()->addBehavior('publicBeforeCommentCreate', function ($cur) { if (dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME); && $cur->getField('comment_email') == My::DEFAULT_EMAIL
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 # désactive l'affichage du mail dans le template
dcCore::app()->ctx->comment_preview['mail'] = ''; dcCore::app()->ctx->comment_preview['mail'] = '';
# n'enregistre pas de mail dans la BDD # n'enregistre pas de mail dans la BDD
$cur->comment_email = ''; $cur->setField('comment_email', '');
# n'enregistre pas le mail dans le cookie # n'enregistre pas le mail dans le cookie
if (empty($_POST['c_remember'])) { if (empty($_POST['c_remember'])) {
return; return;
@ -47,19 +63,21 @@ dcCore::app()->addBehavior('publicBeforeCommentCreate', function ($cur) {
return; return;
} }
} }
$c_cookie = ['name' => $cur->comment_author, 'mail' => $cur->comment_email, 'site' => $cur->comment_site]; $c_cookie = ['name' => $cur->getField('comment_author'), 'mail' => $cur->getField('comment_email'), 'site' => $cur->getField('comment_site')];
$c_cookie = serialize($c_cookie); $c_cookie = serialize($c_cookie);
setcookie('comment_info', $c_cookie, strtotime('+3 month'), '/'); setcookie('comment_info', $c_cookie, strtotime('+3 month'), '/');
} }
}); },
'publicHeadContent' => function () {
dcCore::app()->addBehavior('publicHeadContent', function () { if (dcCore::app()->blog->settings->get(My::SETTING_NAME)->get('enabled')) {
dcCore::app()->blog->settings->addNamespace(initEmailOptionnel::SETTING_NAME);
if (dcCore::app()->blog->settings->get(initEmailOptionnel::SETTING_NAME)->enabled) {
echo dcUtils::jsLoad( echo dcUtils::jsLoad(
dcCore::app()->blog->getPF(basename(__DIR__) . '/public.js'), dcCore::app()->blog->getPF(My::id() . '/js/frontend.js'),
dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') dcCore::app()->plugins->moduleInfo(My::id(), 'version')
); );
} }
}); },
]);
return true;
}
}

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'));
}
}