Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviocopes committed Jul 14, 2016
2 parents f764c0f + ee89137 commit b7b109b
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 108 deletions.
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
141 changes: 85 additions & 56 deletions comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
];
}

Expand All @@ -36,31 +34,24 @@ public static function getSubscribedEvents()
*/
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();
}

/**
Expand Down Expand Up @@ -96,45 +87,64 @@ private function calculateEnable() {
}

/**
* Frontend side initialization
*/
public function onPluginsInitialized()
public function initializeFrontend()
{
if (!$this->isAdmin()) {

$this->calculateEnable();

if ($this->enable) {
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
]);
}

} else {
/** @var Uri $uri */
$uri = $this->grav['uri'];

//Admin
$this->calculateEnable();

if ($this->enable) {
$this->enable([
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
'onAdminMenu' => ['onAdminMenu', 0],
'onAdminTemplateNavPluginHook' => ['onAdminMenu', 0], //DEPRECATED
'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0],
'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'];

if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $this->route) === false) {
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
'onAdminMenu' => ['onAdminMenu', 0],
'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0],
]);

$page = $this->grav['uri']->param('page');
$comments = $this->getLastComments($page);
if (strpos($uri->path(), $this->config->get('plugins.admin.route') . '/' . $this->route) === false) {
return;
}

if ($page > 0) {
echo json_encode($comments);
exit();
}
$page = $this->grav['uri']->param('page');
$comments = $this->getLastComments($page);

$this->grav['twig']->comments = $comments;
$this->grav['twig']->pages = $this->fetchPages();
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->initializeAdmin();
} else {
$this->initializeFrontend();
}
}

Expand Down Expand Up @@ -164,6 +174,12 @@ public function onFormProcessed(Event $event)
$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();
Expand Down Expand Up @@ -196,6 +212,10 @@ public function onFormProcessed(Event $event)
}

$file->save(Yaml::dump($data));

//clear cache
$this->grav['cache']->delete($this->comments_cache_id);

break;
}
}
Expand Down Expand Up @@ -298,11 +318,20 @@ private function getLastComments($page = 0) {
* 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;
}

/**
Expand Down
19 changes: 10 additions & 9 deletions comments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Loading

0 comments on commit b7b109b

Please sign in to comment.