diff --git a/_init.php b/_init.php
index 3e71440..d7df0f1 100644
--- a/_init.php
+++ b/_init.php
@@ -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';
}
diff --git a/src/Backend.php b/src/Backend.php
index 5a7a9b1..4c0d59a 100644
--- a/src/Backend.php
+++ b/src/Backend.php
@@ -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
+ '
' .
+ '
' . __('Optional e-mail address') . '
' .
+ (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() .
+ '';
+ });
+
+ 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
- '' . __('Optional e-mail address') . '
' .
- '
' . form::checkbox(
- initEmailOptionnel::SETTING_NAME,
- '1',
- dcCore::app()->blog->settings->__get(initEmailOptionnel::SETTING_NAME)->enabled ? true : false
- ) .
- '
' .
- '
';
-});
-
-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')
- );
-});
diff --git a/src/Frontend.php b/src/Frontend.php
index 62c5f43..8a33e93 100644
--- a/src/Frontend.php
+++ b/src/Frontend.php
@@ -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')
- );
- }
-});
diff --git a/src/My.php b/src/My.php
new file mode 100644
index 0000000..d43afc3
--- /dev/null
+++ b/src/My.php
@@ -0,0 +1,39 @@
+plugins->moduleInfo(self::id(), 'name'));
+ }
+}