Merge branch 'release/1.2.0'

This commit is contained in:
Flavio Copes 2016-07-14 21:53:00 +02:00
commit b7b109b67c
7 changed files with 282 additions and 146 deletions

View File

@ -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 # v1.1.4
## 02/05/2016 ## 02/05/2016

View File

@ -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. 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 # Installation
The Comments plugin is easy to install with GPM. 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 # 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. Make sure you add your own Recaptcha `site` and `secret` keys too.
# Where are the comments stored? # 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 # Visualize comments

View File

@ -1,5 +1,5 @@
name: Comments name: Comments
version: 1.1.4 version: 1.2.0
description: Adds a commenting functionality to your site description: Adds a commenting functionality to your site
icon: comment icon: comment
author: author:

View File

@ -17,6 +17,7 @@ class CommentsPlugin extends Plugin
{ {
protected $route = 'comments'; protected $route = 'comments';
protected $enable = false; protected $enable = false;
protected $comments_cache_id;
/** /**
* @return array * @return array
@ -24,10 +25,7 @@ class CommentsPlugin extends Plugin
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
'onPluginsInitialized' => ['onPluginsInitialized', 0], 'onPluginsInitialized' => ['onPluginsInitialized', 0]
'onFormProcessed' => ['onFormProcessed', 0],
'onPageInitialized' => ['onPageInitialized', 10],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]; ];
} }
@ -36,7 +34,6 @@ class CommentsPlugin extends Plugin
*/ */
public function onPageInitialized() public function onPageInitialized()
{ {
if (!$this->isAdmin()) {
/** @var Page $page */ /** @var Page $page */
$page = $this->grav['page']; $page = $this->grav['page'];
if (!$page) { if (!$page) {
@ -51,17 +48,11 @@ class CommentsPlugin extends Plugin
} }
} }
} }
}
public function onTwigSiteVariables() { public function onTwigSiteVariables() {
if (!$this->isAdmin()) {
$this->grav['twig']->enable = $this->enable; $this->grav['twig']->enable = $this->enable;
if ($this->enable) {
$this->grav['twig']->comments = $this->fetchComments(); $this->grav['twig']->comments = $this->fetchComments();
} }
}
}
/** /**
* Determine if $haystack starts with $needle. Credit: http://stackoverflow.com/a/10473026/205039 * Determine if $haystack starts with $needle. Credit: http://stackoverflow.com/a/10473026/205039
@ -96,28 +87,37 @@ class CommentsPlugin extends Plugin
} }
/** /**
* Frontend side initialization
*/ */
public function onPluginsInitialized() public function initializeFrontend()
{ {
if (!$this->isAdmin()) {
$this->calculateEnable(); $this->calculateEnable();
if ($this->enable) { if ($this->enable) {
$this->enable([ $this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onFormProcessed' => ['onFormProcessed', 0],
'onPageInitialized' => ['onPageInitialized', 10],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]); ]);
} }
} else { $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 */ /** @var Uri $uri */
$uri = $this->grav['uri']; $uri = $this->grav['uri'];
//Admin
$this->enable([ $this->enable([
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0], 'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
'onAdminMenu' => ['onAdminMenu', 0], 'onAdminMenu' => ['onAdminMenu', 0],
'onAdminTemplateNavPluginHook' => ['onAdminMenu', 0], //DEPRECATED
'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0], 'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0],
]); ]);
@ -136,6 +136,16 @@ class CommentsPlugin extends Plugin
$this->grav['twig']->comments = $comments; $this->grav['twig']->comments = $comments;
$this->grav['twig']->pages = $this->fetchPages(); $this->grav['twig']->pages = $this->fetchPages();
} }
/**
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->initializeAdmin();
} else {
$this->initializeFrontend();
}
} }
/** /**
@ -164,6 +174,12 @@ class CommentsPlugin extends Plugin
$email = filter_var(urldecode($post['email']), FILTER_SANITIZE_STRING); $email = filter_var(urldecode($post['email']), FILTER_SANITIZE_STRING);
$title = filter_var(urldecode($post['title']), 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 */ /** @var Language $language */
$language = $this->grav['language']; $language = $this->grav['language'];
$lang = $language->getLanguage(); $lang = $language->getLanguage();
@ -196,6 +212,10 @@ class CommentsPlugin extends Plugin
} }
$file->save(Yaml::dump($data)); $file->save(Yaml::dump($data));
//clear cache
$this->grav['cache']->delete($this->comments_cache_id);
break; break;
} }
} }
@ -298,11 +318,20 @@ class CommentsPlugin extends Plugin
* Return the comments associated to the current route * Return the comments associated to the current route
*/ */
private function fetchComments() { 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(); $lang = $this->grav['language']->getLanguage();
$filename = $lang ? '/' . $lang : ''; $filename = $lang ? '/' . $lang : '';
$filename .= $this->grav['uri']->path() . '.yaml'; $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;
} }
/** /**

View File

@ -12,23 +12,23 @@ form:
name: comments name: comments
fields: fields:
- name: name - name: name
label: Name label: PLUGIN_COMMENTS.NAME_LABEL
placeholder: Enter your name placeholder: PLUGIN_COMMENTS.NAME_PLACEHOLDER
autocomplete: on autocomplete: on
type: text type: text
validate: validate:
required: true required: true
- name: email - name: email
label: Email label: PLUGIN_COMMENTS.EMAIL_LABEL
placeholder: Enter your email address placeholder: PLUGIN_COMMENTS.EMAIL_PLACEHOLDER
type: email type: email
validate: validate:
required: true required: true
- name: text - name: text
label: Message label: PLUGIN_COMMENTS.MESSAGE_LABEL
placeholder: Enter your message placeholder: PLUGIN_COMMENTS.MESSAGE_PLACEHOLDER
type: textarea type: textarea
validate: validate:
required: true required: true
@ -62,15 +62,16 @@ form:
buttons: buttons:
- type: submit - type: submit
value: Submit value: PLUGIN_COMMENTS.SUBMIT_COMMENT_BUTTON_TEXT
process: process:
- email: - email:
subject: "[New Comment] from {{ form.value.name|e }}" subject: PLUGIN_COMMENTS.EMAIL_NEW_COMMENT_SUBJECT
body: "{% include 'forms/data.html.twig' %}" body: "{% include 'forms/data.html.twig' %}"
# - captcha: # - captcha:
# recatpcha_secret: ej32oiej23oiej32oijeoi32jeio32je # recatpcha_secret: ej32oiej23oiej32oijeoi32jeio32je
- addComment: - addComment:
- message: Thank you for writing your comment! - message: PLUGIN_COMMENTS.THANK_YOU_MESSAGE
- reset: true

View File

@ -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: '<p>A new comment was made on %1$s by %3$s (%4$s).</p><p>Page: %2$s</p><p>Text: %5$s</p>'
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: '<p>Un nouveau commentaire a été posté sur %1$s par %3$s (%4$s).</p><p>Page: %2$s</p><p>Texte: %5$s</p>'
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: '<p>Un nuovo commento è stato postato su %1$s da %3$s (%4$s).</p><p>Pagina: %2$s</p><p>Testo: %5$s</p>'
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: '<p>Новый комментарий был сделан на %1$s by %3$s (%4$s).</p><p>Страница: %2$s</p><p>Текст: %5$s</p>'
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: '<p>Pojawił się nowy komentarz, napisany %1$s przez %3$s (%4$s).</p><p>Strona: %2$s</p><p>Treść: %5$s</p>'
EMAIL_FOOTER: ''
NAME: Imię:
EMAIL: Email:
WRITTEN_ON: Napisany przez
BY: przez
de: de:
PLUGIN_COMMENTS: PLUGIN_COMMENTS:
ADD_COMMENT: Kommentar hinzufügen ADD_COMMENT: Kommentar hinzufügen
@ -70,3 +10,147 @@ de:
EMAIL: Email: EMAIL: Email:
WRITTEN_ON: geschrieben am WRITTEN_ON: geschrieben am
BY: von 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: '<p>A new comment was made on %1$s by %3$s (%4$s).</p><p>Page: %2$s</p><p>Text: %5$s</p>'
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: '<p>Un nuevo comentario se hizo en %1$s por %3$s (%4$s).</p><p>Page: %2$s</p><p>Text: %5$s</p>'
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: '<p>Un nouveau commentaire a été publié sur %1$s par %3$s (%4$s).</p><p>Page : %2$s</p><p>Texte : %5$s</p>'
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: '<p>Un nuovo commento è stato postato su %1$s da %3$s (%4$s).</p><p>Pagina: %2$s</p><p>Testo: %5$s</p>'
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: '<p>Pojawił się nowy komentarz, napisany %1$s przez %3$s (%4$s).</p><p>Strona: %2$s</p><p>Treść: %5$s</p>'
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: '<p>Новый комментарий был сделан на %1$s by %3$s (%4$s).</p><p>Страница: %2$s</p><p>Текст: %5$s</p>'
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: '<p>Um novo comentário foi feito em %1$s por %3$s (%4$s).</p><p>Página: %2$s</p><p>Texto: %5$s</p>'
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!"

View File

@ -5,20 +5,32 @@
<form name="{{ grav.config.plugins.comments.form.name }}" <form name="{{ grav.config.plugins.comments.form.name }}"
action="{{ grav.config.plugins.comments.form.action ? base_url ~ grav.config.plugins.comments.form.action : page.url }}" action="{{ grav.config.plugins.comments.form.action ? base_url ~ grav.config.plugins.comments.form.action : page.url }}"
method="{{ grav.config.plugins.comments.form.method|upper|default('POST') }}"> method="{{ grav.config.plugins.comments.form.method|upper|default('POST') }}">
{% for field in grav.config.plugins.comments.form.fields %}
{% for field in grav.config.plugins.comments.form.fields %}
{% set value = form.value(field.name) %} {% set value = form.value(field.name) %}
{% if field.evaluateDefault %} {% if field.evaluateDefault %}
{% set value = evaluate(field.evaluateDefault) %} {% set value = evaluate(field.evaluateDefault) %}
{% endif %} {% endif %}
{% if grav.user.authenticated %}
{% if field.name == 'name' %}
<input type="hidden" name="{{field.name}}" value="{{grav.user.fullname}}">
{% elseif field.name == 'email' %}
<input type="hidden" name="{{field.name}}" value="{{grav.user.email}}">
{% else %}
<div> <div>
{% include "forms/fields/#{field.type}/#{field.type}.html.twig" %} {% include "forms/fields/#{field.type}/#{field.type}.html.twig" %}
</div> </div>
{% endif %}
{% else %}
<div>
{% include "forms/fields/#{field.type}/#{field.type}.html.twig" %}
</div>
{% endif %}
{% endfor %} {% endfor %}
<div class="buttons"> <div class="buttons">
{% for button in grav.config.plugins.comments.form.buttons %} {% for button in grav.config.plugins.comments.form.buttons %}
<button class="button" type="{{ button.type|default('submit') }}">{{ button.value|default('Submit') }}</button> <button class="button" type="{{ button.type|default('submit') }}">{{ button.value|t|default('Submit') }}</button>
{% endfor %} {% endfor %}
</div> </div>