diff --git a/CHANGELOG.md b/CHANGELOG.md index 316afe8..177e4e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +# v1.2.0 +## 07/14/2016 + +1. [](#improved) + * Prevent a missing template problem on ignored routes + * Allow to translate the comments form + * Added spanish and brazilian portuguese translations + * Enhanced german, russian and french translations + * Added cache for comments + * Handle logged in users by not requiring username/email + * Reset the comments form after a comment is submitted + # v1.1.4 ## 02/05/2016 @@ -25,7 +37,7 @@ 1. [](#improved) * Drop the autofocus on the comment form 1. [](#bugfix) - * Fix double encoding (#12) + * Fix double encoding (#12) # v1.1.0 ## 11/24/2015 @@ -36,7 +48,7 @@ 1. [](#improved) * Use date instead of gmdate to respect the server local time (thanks @bovisp) * Now works with multilang (thanks @bovisp) - + # v1.0.2 ## 11/13/2015 diff --git a/README.md b/README.md index e1d4eda..a403d84 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ The **Comments Plugin** for [Grav](http://github.com/getgrav/grav) adds the ability to add comments to pages, and moderate them. -| IMPORTANT!!! This plugin is currently in development as is to be considered a **beta release**. As such, use this in a production environment **at your own risk!**. More features will be added in the future. - # Installation The Comments plugin is easy to install with GPM. @@ -51,12 +49,12 @@ To set the enabled routes, create a `user/config/plugins/comments.yaml` file, co # Enabling Recaptcha -The plugin comes with Recaptcha integration. To make it work, create a `user/config/plugins/comments.yaml` file, copy in it the contents of `user/plugins/comments/comments.yaml` and uncomment the capthca form field and the captcha validation process. +The plugin comes with Recaptcha integration. To make it work, create a `user/config/plugins/comments.yaml` file, copy in it the contents of `user/plugins/comments/comments.yaml` and uncomment the captcha form field and the captcha validation process. Make sure you add your own Recaptcha `site` and `secret` keys too. # Where are the comments stored? -In the `user/data/comments` folder. They're organized by page route, so every page with a comment has a corresponding file. This enabled a quick load of all the page comments. +In the `user/data/comments` folder. They're organized by page route, so every page with a comment has a corresponding file. This enables a quick load of all the page comments. # Visualize comments diff --git a/blueprints.yaml b/blueprints.yaml index bd800f5..947edd0 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Comments -version: 1.1.4 +version: 1.2.0 description: Adds a commenting functionality to your site icon: comment author: diff --git a/comments.php b/comments.php index 49be739..00b9790 100644 --- a/comments.php +++ b/comments.php @@ -17,17 +17,15 @@ class CommentsPlugin extends Plugin { protected $route = 'comments'; protected $enable = false; - + protected $comments_cache_id; + /** * @return array */ public static function getSubscribedEvents() { return [ - 'onPluginsInitialized' => ['onPluginsInitialized', 0], - 'onFormProcessed' => ['onFormProcessed', 0], - 'onPageInitialized' => ['onPageInitialized', 10], - 'onTwigSiteVariables' => ['onTwigSiteVariables', 0] + 'onPluginsInitialized' => ['onPluginsInitialized', 0] ]; } @@ -36,31 +34,24 @@ class CommentsPlugin extends Plugin */ public function onPageInitialized() { - if (!$this->isAdmin()) { - /** @var Page $page */ - $page = $this->grav['page']; - if (!$page) { - return; - } + /** @var Page $page */ + $page = $this->grav['page']; + if (!$page) { + return; + } - if ($this->enable) { - $header = $page->header(); - if (!isset($header->form)) { - $header->form = $this->grav['config']->get('plugins.comments.form'); - $page->header($header); - } + if ($this->enable) { + $header = $page->header(); + if (!isset($header->form)) { + $header->form = $this->grav['config']->get('plugins.comments.form'); + $page->header($header); } } } public function onTwigSiteVariables() { - if (!$this->isAdmin()) { - $this->grav['twig']->enable = $this->enable; - - if ($this->enable) { - $this->grav['twig']->comments = $this->fetchComments(); - } - } + $this->grav['twig']->enable = $this->enable; + $this->grav['twig']->comments = $this->fetchComments(); } /** @@ -95,46 +86,65 @@ class CommentsPlugin extends Plugin } } + /** + * Frontend side initialization + */ + public function initializeFrontend() + { + $this->calculateEnable(); + + if ($this->enable) { + $this->enable([ + 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0], + 'onFormProcessed' => ['onFormProcessed', 0], + 'onPageInitialized' => ['onPageInitialized', 10], + 'onTwigSiteVariables' => ['onTwigSiteVariables', 0] + ]); + } + + $cache = $this->grav['cache']; + //init cache id + $this->comments_cache_id = md5('comments-data' . $cache->getKey()); + } + + /** + * Admin side initialization + */ + public function initializeAdmin() + { + /** @var Uri $uri */ + $uri = $this->grav['uri']; + + $this->enable([ + 'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0], + 'onAdminMenu' => ['onAdminMenu', 0], + 'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0], + ]); + + if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $this->route) === false) { + return; + } + + $page = $this->grav['uri']->param('page'); + $comments = $this->getLastComments($page); + + if ($page > 0) { + echo json_encode($comments); + exit(); + } + + $this->grav['twig']->comments = $comments; + $this->grav['twig']->pages = $this->fetchPages(); + } + /** */ public function onPluginsInitialized() { - if (!$this->isAdmin()) { - - $this->calculateEnable(); - - if ($this->enable) { - $this->enable([ - 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0], - ]); - } - + if ($this->isAdmin()) { + $this->initializeAdmin(); } else { - /** @var Uri $uri */ - $uri = $this->grav['uri']; - - //Admin - $this->enable([ - 'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0], - 'onAdminMenu' => ['onAdminMenu', 0], - 'onAdminTemplateNavPluginHook' => ['onAdminMenu', 0], //DEPRECATED - 'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0], - ]); - - if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $this->route) === false) { - return; - } - - $page = $this->grav['uri']->param('page'); - $comments = $this->getLastComments($page); - - if ($page > 0) { - echo json_encode($comments); - exit(); - } - - $this->grav['twig']->comments = $comments; - $this->grav['twig']->pages = $this->fetchPages(); + $this->initializeFrontend(); } } @@ -164,6 +174,12 @@ class CommentsPlugin extends Plugin $email = filter_var(urldecode($post['email']), FILTER_SANITIZE_STRING); $title = filter_var(urldecode($post['title']), FILTER_SANITIZE_STRING); + $user = $this->grav['user']; + if ($user->authenticated) { + $name = $user->fullname; + $email = $user->email; + } + /** @var Language $language */ $language = $this->grav['language']; $lang = $language->getLanguage(); @@ -196,6 +212,10 @@ class CommentsPlugin extends Plugin } $file->save(Yaml::dump($data)); + + //clear cache + $this->grav['cache']->delete($this->comments_cache_id); + break; } } @@ -298,11 +318,20 @@ class CommentsPlugin extends Plugin * Return the comments associated to the current route */ private function fetchComments() { + $cache = $this->grav['cache']; + //search in cache + if ($comments = $cache->fetch($this->comments_cache_id)) { + return $comments; + } + $lang = $this->grav['language']->getLanguage(); $filename = $lang ? '/' . $lang : ''; $filename .= $this->grav['uri']->path() . '.yaml'; - return $this->getDataFromFilename($filename)['comments']; + $comments = $this->getDataFromFilename($filename)['comments']; + //save to cache if enabled + $cache->save($this->comments_cache_id, $comments); + return $comments; } /** diff --git a/comments.yaml b/comments.yaml index a401c97..05a16eb 100644 --- a/comments.yaml +++ b/comments.yaml @@ -12,23 +12,23 @@ form: name: comments fields: - name: name - label: Name - placeholder: Enter your name + label: PLUGIN_COMMENTS.NAME_LABEL + placeholder: PLUGIN_COMMENTS.NAME_PLACEHOLDER autocomplete: on type: text validate: required: true - name: email - label: Email - placeholder: Enter your email address + label: PLUGIN_COMMENTS.EMAIL_LABEL + placeholder: PLUGIN_COMMENTS.EMAIL_PLACEHOLDER type: email validate: required: true - name: text - label: Message - placeholder: Enter your message + label: PLUGIN_COMMENTS.MESSAGE_LABEL + placeholder: PLUGIN_COMMENTS.MESSAGE_PLACEHOLDER type: textarea validate: required: true @@ -62,15 +62,16 @@ form: buttons: - type: submit - value: Submit + value: PLUGIN_COMMENTS.SUBMIT_COMMENT_BUTTON_TEXT process: - email: - subject: "[New Comment] from {{ form.value.name|e }}" + subject: PLUGIN_COMMENTS.EMAIL_NEW_COMMENT_SUBJECT body: "{% include 'forms/data.html.twig' %}" # - captcha: # recatpcha_secret: ej32oiej23oiej32oijeoi32jeio32je - addComment: - - message: Thank you for writing your comment! + - message: PLUGIN_COMMENTS.THANK_YOU_MESSAGE + - reset: true diff --git a/languages.yaml b/languages.yaml index 086dd21..490ebf0 100644 --- a/languages.yaml +++ b/languages.yaml @@ -1,63 +1,3 @@ -en: - PLUGIN_COMMENTS: - ADD_COMMENT: Add a comment - COMMENTS: Comments - EMAIL_NOT_CONFIGURED: Email not configured - NEW_COMMENT_EMAIL_SUBJECT: 'New comment on %1$s' - NEW_COMMENT_EMAIL_BODY: '

A new comment was made on %1$s by %3$s (%4$s).

Page: %2$s

Text: %5$s

' - EMAIL_FOOTER: '' - NAME: Name: - EMAIL: Email: - WRITTEN_ON: Written on - BY: by -fr: - PLUGIN_COMMENTS: - ADD_COMMENT: Ajouter un commentaire - COMMENTS: Commentaires - EMAIL_NOT_CONFIGURED: Email non configurée - NEW_COMMENT_EMAIL_SUBJECT: 'Nouveau commentaire sur %1$s' - NEW_COMMENT_EMAIL_BODY: '

Un nouveau commentaire a été posté sur %1$s par %3$s (%4$s).

Page: %2$s

Texte: %5$s

' - EMAIL_FOOTER: '' - NAME: Nom: - EMAIL: Email: - WRITTEN_ON: Ecrit le - BY: par -it: - PLUGIN_COMMENTS: - ADD_COMMENT: Aggiungi un commento - COMMENTS: Commenti - EMAIL_NOT_CONFIGURED: Email non configurata - NEW_COMMENT_EMAIL_SUBJECT: 'Nuovo commento su %1$s' - NEW_COMMENT_EMAIL_BODY: '

Un nuovo commento è stato postato su %1$s da %3$s (%4$s).

Pagina: %2$s

Testo: %5$s

' - EMAIL_FOOTER: '' - NAME: Nome: - EMAIL: Email: - WRITTEN_ON: Scritto il - BY: da -ru: - PLUGIN_COMMENTS: - ADD_COMMENT: Добавить комментарий - COMMENTS: Комментарии - EMAIL_NOT_CONFIGURED: Email не настроен - NEW_COMMENT_EMAIL_SUBJECT: 'Новый комментарий к %1$s' - NEW_COMMENT_EMAIL_BODY: '

Новый комментарий был сделан на %1$s by %3$s (%4$s).

Страница: %2$s

Текст: %5$s

' - EMAIL_FOOTER: '' - NAME: Имя: - EMAIL: Email: - WRITTEN_ON: Написан в - BY: от -pl: - PLUGIN_COMMENTS: - ADD_COMMENT: Dodaj komentarz - COMMENTS: Komentarzy - EMAIL_NOT_CONFIGURED: Email jest nie skofigurowany - NEW_COMMENT_EMAIL_SUBJECT: 'Nowy komentarz %1$s' - NEW_COMMENT_EMAIL_BODY: '

Pojawił się nowy komentarz, napisany %1$s przez %3$s (%4$s).

Strona: %2$s

Treść: %5$s

' - EMAIL_FOOTER: '' - NAME: Imię: - EMAIL: Email: - WRITTEN_ON: Napisany przez - BY: przez de: PLUGIN_COMMENTS: ADD_COMMENT: Kommentar hinzufügen @@ -70,3 +10,147 @@ de: EMAIL: Email: WRITTEN_ON: geschrieben am BY: von + NAME_LABEL: "Name" + NAME_PLACEHOLDER: "Namen eingeben" + EMAIL_LABEL: "Email" + EMAIL_PLACEHOLDER: "Email-Adresse eingeben" + MESSAGE_LABEL: "Kommentar" + MESSAGE_PLACEHOLDER: "Kommentar eingeben" + SUBMIT_COMMENT_BUTTON_TEXT: "Absenden" + EMAIL_NEW_COMMENT_SUBJECT: "[Neuer Kommentar] von {{ form.value.name|e }}" + THANK_YOU_MESSAGE: "Vielen Dank für den Kommentar!" +en: + PLUGIN_COMMENTS: + ADD_COMMENT: Add a comment + COMMENTS: Comments + EMAIL_NOT_CONFIGURED: Email not configured + NEW_COMMENT_EMAIL_SUBJECT: 'New comment on %1$s' + NEW_COMMENT_EMAIL_BODY: '

A new comment was made on %1$s by %3$s (%4$s).

Page: %2$s

Text: %5$s

' + EMAIL_FOOTER: '' + NAME: Name: + EMAIL: Email: + WRITTEN_ON: Written on + BY: by + NAME_LABEL: "Name" + NAME_PLACEHOLDER: "Enter your name" + EMAIL_LABEL: "Email" + EMAIL_PLACEHOLDER: "Enter your email address" + MESSAGE_LABEL: "Comment" + MESSAGE_PLACEHOLDER: "Enter your comment" + SUBMIT_COMMENT_BUTTON_TEXT: "Submit" + EMAIL_NEW_COMMENT_SUBJECT: "[New Comment] from {{ form.value.name|e }}" + THANK_YOU_MESSAGE: "Thank you for writing your comment!" + +es: + PLUGIN_COMMENTS: + ADD_COMMENT: Agregar un comentario + COMMENTS: Comentarios + EMAIL_NOT_CONFIGURED: El Email no está configurado + NEW_COMMENT_EMAIL_SUBJECT: 'Nuevo comentario en %1$s' + NEW_COMMENT_EMAIL_BODY: '

Un nuevo comentario se hizo en %1$s por %3$s (%4$s).

Page: %2$s

Text: %5$s

' + EMAIL_FOOTER: '' + NAME: Nombre: + EMAIL: Email: + WRITTEN_ON: Escrito en + BY: por + NAME_LABEL: "Nombre" + NAME_PLACEHOLDER: "Escriba su nombre" + EMAIL_LABEL: "Email" + EMAIL_PLACEHOLDER: "Escriba su email" + MESSAGE_LABEL: "Comentario" + MESSAGE_PLACEHOLDER: "Escriba su comentario" + SUBMIT_COMMENT_BUTTON_TEXT: "Enviar" + EMAIL_NEW_COMMENT_SUBJECT: "[Nuevo comentario] de {{ form.value.name|e }}" + THANK_YOU_MESSAGE: "Gracias por escribir su comentario!" + +fr: + PLUGIN_COMMENTS: + ADD_COMMENT: Ajouter un commentaire + COMMENTS: Commentaires + EMAIL_NOT_CONFIGURED: E-mail non configuré + NEW_COMMENT_EMAIL_SUBJECT: 'Nouveau commentaire sur %1$s' + NEW_COMMENT_EMAIL_BODY: '

Un nouveau commentaire a été publié sur %1$s par %3$s (%4$s).

Page : %2$s

Texte : %5$s

' + EMAIL_FOOTER: '' + NAME: Nom : + EMAIL: E-mail : + WRITTEN_ON: Écrit le + BY: par + NAME_LABEL: "Nom" + NAME_PLACEHOLDER: "Indiquez votre nom" + EMAIL_LABEL: "E-mail" + EMAIL_PLACEHOLDER: "Indiquez votre adresse e-mail" + MESSAGE_LABEL: "Commentaire" + MESSAGE_PLACEHOLDER: "Rédigez votre commentaire" + SUBMIT_COMMENT_BUTTON_TEXT: "Envoyer" + EMAIL_NEW_COMMENT_SUBJECT: "[Nouveau commentaire] de {{ form.value.name|e }}" + THANK_YOU_MESSAGE: "Merci d'avoir rédigé votre commentaire !" + +it: + PLUGIN_COMMENTS: + ADD_COMMENT: Aggiungi un commento + COMMENTS: Commenti + EMAIL_NOT_CONFIGURED: Email non configurata + NEW_COMMENT_EMAIL_SUBJECT: 'Nuovo commento su %1$s' + NEW_COMMENT_EMAIL_BODY: '

Un nuovo commento è stato postato su %1$s da %3$s (%4$s).

Pagina: %2$s

Testo: %5$s

' + EMAIL_FOOTER: '' + NAME: Nome: + EMAIL: Email: + WRITTEN_ON: Scritto il + BY: da + NAME_LABEL: "Nome" + NAME_PLACEHOLDER: "Inserisci il tuo nome" + EMAIL_LABEL: "Email" + EMAIL_PLACEHOLDER: "Inserisci il tuo indirizzo email" + MESSAGE_LABEL: "Messaggio" + MESSAGE_PLACEHOLDER: "Inserisci il tuo commento" + SUBMIT_COMMENT_BUTTON_TEXT: "Invia" + EMAIL_NEW_COMMENT_SUBJECT: "[Nuovo commento] da {{ form.value.name|e }}" + THANK_YOU_MESSAGE: "Grazie per il tuo commento!" + +pl: + PLUGIN_COMMENTS: + ADD_COMMENT: Dodaj komentarz + COMMENTS: Komentarzy + EMAIL_NOT_CONFIGURED: Email jest nie skofigurowany + NEW_COMMENT_EMAIL_SUBJECT: 'Nowy komentarz %1$s' + NEW_COMMENT_EMAIL_BODY: '

Pojawił się nowy komentarz, napisany %1$s przez %3$s (%4$s).

Strona: %2$s

Treść: %5$s

' + EMAIL_FOOTER: '' + NAME: Imię: + EMAIL: Email: + WRITTEN_ON: Napisany przez + BY: przez + +ru: + PLUGIN_COMMENTS: + ADD_COMMENT: Добавить комментарий + COMMENTS: Комментарии + EMAIL_NOT_CONFIGURED: Email не настроен + NEW_COMMENT_EMAIL_SUBJECT: 'Новый комментарий к %1$s' + NEW_COMMENT_EMAIL_BODY: '

Новый комментарий был сделан на %1$s by %3$s (%4$s).

Страница: %2$s

Текст: %5$s

' + EMAIL_FOOTER: '' + NAME: Имя: + EMAIL: Email: + WRITTEN_ON: Написан в + BY: от + +pt-br: + PLUGIN_COMMENTS: + ADD_COMMENT: Escreva um comentário + COMMENTS: Comentários + EMAIL_NOT_CONFIGURED: E-mail não configurado + NEW_COMMENT_EMAIL_SUBJECT: 'Novo comentário em %1$s' + NEW_COMMENT_EMAIL_BODY: '

Um novo comentário foi feito em %1$s por %3$s (%4$s).

Página: %2$s

Texto: %5$s

' + EMAIL_FOOTER: '' + NAME: Name: + EMAIL: Email: + WRITTEN_ON: Publicado em + BY: por + NAME_LABEL: "Nome" + NAME_PLACEHOLDER: "Escreva seu nome" + EMAIL_LABEL: "E-mail" + EMAIL_PLACEHOLDER: "Escreva seu e-mail. Ex.: seunome@provedor.com.br" + MESSAGE_LABEL: "Comentário" + MESSAGE_PLACEHOLDER: "Escreva seu comentário" + SUBMIT_COMMENT_BUTTON_TEXT: "Enviar" + EMAIL_NEW_COMMENT_SUBJECT: "[Novo comentário] de {{ form.value.name|e }}" + THANK_YOU_MESSAGE: "Obrigada por enviar seu comentário!" diff --git a/templates/partials/comments.html.twig b/templates/partials/comments.html.twig index 691dd86..4a161ed 100644 --- a/templates/partials/comments.html.twig +++ b/templates/partials/comments.html.twig @@ -5,20 +5,32 @@
- {% for field in grav.config.plugins.comments.form.fields %} - {% set value = form.value(field.name) %} - {% if field.evaluateDefault %} - {% set value = evaluate(field.evaluateDefault) %} - {% endif %} -
- {% include "forms/fields/#{field.type}/#{field.type}.html.twig" %} -
- {% endfor %} + {% for field in grav.config.plugins.comments.form.fields %} + {% set value = form.value(field.name) %} + {% if field.evaluateDefault %} + {% set value = evaluate(field.evaluateDefault) %} + {% endif %} + {% if grav.user.authenticated %} + {% if field.name == 'name' %} + + {% elseif field.name == 'email' %} + + {% else %} +
+ {% include "forms/fields/#{field.type}/#{field.type}.html.twig" %} +
+ {% endif %} + {% else %} +
+ {% include "forms/fields/#{field.type}/#{field.type}.html.twig" %} +
+ {% endif %} + {% endfor %}
{% for button in grav.config.plugins.comments.form.buttons %} - + {% endfor %}