Add enable_on_routes and disable_on_routes config options
Example usage: enable_on_routes: - '/blog' disable_on_routes: - /blog/blog-post-to-ignore - /ignore-this-route #- '/blog/daring-fireball-link'
This commit is contained in:
parent
fe66c4bfd6
commit
13252376e4
40
comments.php
40
comments.php
|
@ -15,6 +15,7 @@ use Symfony\Component\Yaml\Yaml;
|
||||||
class CommentsPlugin extends Plugin
|
class CommentsPlugin extends Plugin
|
||||||
{
|
{
|
||||||
protected $route = 'comments';
|
protected $route = 'comments';
|
||||||
|
protected $enable = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -51,9 +52,45 @@ class CommentsPlugin extends Plugin
|
||||||
|
|
||||||
public function onTwigSiteVariables() {
|
public function onTwigSiteVariables() {
|
||||||
if (!$this->isAdmin()) {
|
if (!$this->isAdmin()) {
|
||||||
|
$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
|
||||||
|
*/
|
||||||
|
private function startsWith($haystack, $needle) {
|
||||||
|
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the plugin should be enabled based on the enable_on_routes and disable_on_routes config options
|
||||||
|
*/
|
||||||
|
private function calculateEnable() {
|
||||||
|
$uri = $this->grav['uri'];
|
||||||
|
|
||||||
|
$disable_on_routes = (array) $this->config->get('plugins.comments.disable_on_routes');
|
||||||
|
$enable_on_routes = (array) $this->config->get('plugins.comments.enable_on_routes');
|
||||||
|
|
||||||
|
$path = $uri->path();
|
||||||
|
|
||||||
|
if (!in_array($path, $disable_on_routes)) {
|
||||||
|
if (in_array($path, $enable_on_routes)) {
|
||||||
|
$this->enable = true;
|
||||||
|
} else {
|
||||||
|
foreach($enable_on_routes as $route) {
|
||||||
|
if ($this->startsWith($path, $route)) {
|
||||||
|
$this->enable = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
|
@ -61,6 +98,8 @@ class CommentsPlugin extends Plugin
|
||||||
{
|
{
|
||||||
if (!$this->isAdmin()) {
|
if (!$this->isAdmin()) {
|
||||||
|
|
||||||
|
$this->calculateEnable();
|
||||||
|
|
||||||
$this->enable([
|
$this->enable([
|
||||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
|
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
|
||||||
]);
|
]);
|
||||||
|
@ -118,6 +157,7 @@ class CommentsPlugin extends Plugin
|
||||||
$name = filter_var(urldecode($post['name']), FILTER_SANITIZE_STRING);
|
$name = filter_var(urldecode($post['name']), FILTER_SANITIZE_STRING);
|
||||||
$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);
|
||||||
|
|
||||||
/** @var Language $language */
|
/** @var Language $language */
|
||||||
$language = $this->grav['language'];
|
$language = $this->grav['language'];
|
||||||
$lang = $language->getLanguage();
|
$lang = $language->getLanguage();
|
||||||
|
|
|
@ -1,4 +1,13 @@
|
||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
|
enable_on_routes:
|
||||||
|
- '/blog'
|
||||||
|
|
||||||
|
disable_on_routes:
|
||||||
|
- /blog/blog-post-to-ignore
|
||||||
|
- /ignore-this-route
|
||||||
|
#- '/blog/daring-fireball-link'
|
||||||
|
|
||||||
form:
|
form:
|
||||||
name: comments
|
name: comments
|
||||||
fields:
|
fields:
|
||||||
|
@ -45,7 +54,7 @@ form:
|
||||||
# - name: g-recaptcha-response
|
# - name: g-recaptcha-response
|
||||||
# label: Captcha
|
# label: Captcha
|
||||||
# type: captcha
|
# type: captcha
|
||||||
# recatpcha_site_key: 6Lde4gwTAAAAAAZuv4z2AgVU6Xamn5twDYzQr8hv
|
# recatpcha_site_key: e32iojeoi32jeoi32jeoij32oiej32oiej3
|
||||||
# recaptcha_not_validated: 'Captcha not valid!'
|
# recaptcha_not_validated: 'Captcha not valid!'
|
||||||
# validate:
|
# validate:
|
||||||
# required: true
|
# required: true
|
||||||
|
@ -61,7 +70,7 @@ form:
|
||||||
# subject: "[Site Guestbook] {{ form.value.name|e }}"
|
# subject: "[Site Guestbook] {{ form.value.name|e }}"
|
||||||
# body: "{% include 'forms/data.html.twig' %}"
|
# body: "{% include 'forms/data.html.twig' %}"
|
||||||
# - captcha:
|
# - captcha:
|
||||||
# recatpcha_secret: 6Lde4gwTAAAAAPpwVKuaYm53n2bWfFfxcDxSlI54
|
# recatpcha_secret: ej32oiej23oiej32oijeoi32jeio32je
|
||||||
- addComment:
|
- addComment:
|
||||||
- message: Thank you for writing your comment!
|
- message: Thank you for writing your comment!
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
<h3>{{'PLUGIN_COMMENTS.ADD_COMMENT'|t}}</h3>
|
{% if grav.twig.enable %}
|
||||||
|
|
||||||
<form name="{{ grav.config.plugins.comments.form.name }}"
|
<h3>{{'PLUGIN_COMMENTS.ADD_COMMENT'|t}}</h3>
|
||||||
|
|
||||||
|
<form name="{{ grav.config.plugins.comments.form.name }}"
|
||||||
action="{{ uri.rootUrl ~ (grav.config.plugins.comments.form.action|default(page.route)) }}"
|
action="{{ uri.rootUrl ~ (grav.config.plugins.comments.form.action|default(page.route)) }}"
|
||||||
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 %}
|
||||||
|
@ -12,18 +14,18 @@
|
||||||
<div>
|
<div>
|
||||||
{% include "forms/fields/#{field.type}/#{field.type}.html.twig" %}
|
{% include "forms/fields/#{field.type}/#{field.type}.html.twig" %}
|
||||||
</div>
|
</div>
|
||||||
{% 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|default('Submit') }}</button>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div class="alert">{{ form.message }}</div>
|
<div class="alert">{{ form.message }}</div>
|
||||||
|
|
||||||
{% if grav.twig.comments|length %}
|
{% if grav.twig.comments|length %}
|
||||||
|
|
||||||
<h3>{{'PLUGIN_COMMENTS.COMMENTS'|t}}</h3>
|
<h3>{{'PLUGIN_COMMENTS.COMMENTS'|t}}</h3>
|
||||||
|
|
||||||
|
@ -38,4 +40,5 @@
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
Loading…
Reference in New Issue