Skip to content

Grav comment classes #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions comments.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function onTwigSiteVariables() {
// New way
$this->grav['twig']->twig_vars['enable_comments_plugin'] = $enabled;
$this->grav['twig']->twig_vars['comments'] = $comments;

}

/**
Expand All @@ -95,6 +94,11 @@ private function calculateEnable() {
$disable_on_routes = (array) $this->config->get('plugins.comments.disable_on_routes');
$enable_on_routes = (array) $this->config->get('plugins.comments.enable_on_routes');

$re_disable_on_routes = (array) $this->config->get('plugins.comments.disable_on_routes_regex');
$re_enable_on_routes = (array) $this->config->get('plugins.comments.enable_on_routes_regex');

$also_route_aliases = (array) $this->config->get('plugins.comments.check_route_aliases');

$path = $uri->path();

if (!in_array($path, $disable_on_routes)) {
Expand All @@ -104,11 +108,44 @@ private function calculateEnable() {
foreach($enable_on_routes as $route) {
if (Utils::startsWith($path, $route)) {
$this->enable = true;
break;
return true;
}
}
}
}

//loop through including regex' first – then through excluding
$re_match = false;
foreach ($re_enable_on_routes as $re):
try {
$pattern = $re;
//if no delimiters are present, add them
if (!preg_match("/^\/.+\/[a-z]*$/i",$pattern)) $pattern = "!".$re."!su";
if (preg_match($pattern, $path)):
$re_match = true;
break;
endif;
} catch(\Exception $e) {
$this->grav['debugger']->addMessage($e);
continue;
}
endforeach;
foreach ($re_disable_on_routes as $re):
try {
$pattern = $re;
//if no delimiters are present, add them
if (!preg_match("/^\/.+\/[a-z]*$/i",$pattern)) $pattern = "!".$re."!su";
if (preg_match($pattern, $path)):
$re_match = false;
break;
endif;
} catch(\Exception $e) {
$this->grav['debugger']->addMessage($e);
continue;
}
endforeach;

$this->enable = $re_match;
}

/**
Expand Down
41 changes: 22 additions & 19 deletions templates/partials/comments.html.twig
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<h3>{{'PLUGIN_COMMENTS.ADD_COMMENT'|t}}</h3>

<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 }}#comments-section"
method="{{ grav.config.plugins.comments.form.method|upper|default('POST') }}">

{% for field in grav.config.plugins.comments.form.fields %}
Expand Down Expand Up @@ -39,22 +39,25 @@
{{ nonce_field('form', 'form-nonce')|raw }}
</form>

<div class="alert">{{ form.message }}</div>

{% if grav.twig.comments|length %}

<h3>{{'PLUGIN_COMMENTS.COMMENTS'|t}}</h3>

<table>
{% for comment in comments|array_reverse %}
<tr>
<td>
{{comment.text}}
<br />
{{'PLUGIN_COMMENTS.WRITTEN_ON'|t}} {{comment.date|e}} {{'PLUGIN_COMMENTS.BY'|t}} {{comment.author}}
</td>
</tr>
{% endfor %}
</table>
{% endif %}
<div id="comments-section" class="{{ grav.config.plugins.comments.classes.wrapper|default('') }}">
<div class="{{ grav.config.plugins.comments.classes.alert|default('alert') }}">{{ form.message }}</div>

{% if grav.twig.comments|length %}

<h3>{{'PLUGIN_COMMENTS.COMMENTS'|t}}</h3>

<table class="{{ grav.config.plugins.comments.classes.table|default('') }}">
{% for comment in comments|array_reverse %}
<tr class="{{ grav.config.plugins.comments.classes.rows|default('') }}">
<td class="{{ grav.config.plugins.comments.classes.cols|default('') }}">
<p>{{comment.text}}</p>
<p class="{{ grav.config.plugins.comments.classes.meta|default('') }}">
{{'PLUGIN_COMMENTS.WRITTEN_ON'|t}} {{comment.date|e}} {{'PLUGIN_COMMENTS.BY'|t}} {{comment.author}}
</p>
</td>
</tr>
{% endfor %}
</table>
{% endif %}
</div>
{% endif %}