Rework a bit the organization of the code. Added cache for comments
This commit is contained in:
parent
04078ba7e0
commit
60c8d091e6
137
comments.php
137
comments.php
|
@ -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,31 +34,24 @@ 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) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->enable) {
|
if ($this->enable) {
|
||||||
$header = $page->header();
|
$header = $page->header();
|
||||||
if (!isset($header->form)) {
|
if (!isset($header->form)) {
|
||||||
$header->form = $this->grav['config']->get('plugins.comments.form');
|
$header->form = $this->grav['config']->get('plugins.comments.form');
|
||||||
$page->header($header);
|
$page->header($header);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onTwigSiteVariables() {
|
public function onTwigSiteVariables() {
|
||||||
if (!$this->isAdmin()) {
|
$this->grav['twig']->enable = $this->enable;
|
||||||
$this->grav['twig']->enable = $this->enable;
|
$this->grav['twig']->comments = $this->fetchComments();
|
||||||
|
|
||||||
if ($this->enable) {
|
|
||||||
$this->grav['twig']->comments = $this->fetchComments();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -95,42 +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()
|
public function onPluginsInitialized()
|
||||||
{
|
{
|
||||||
if (!$this->isAdmin()) {
|
if ($this->isAdmin()) {
|
||||||
|
$this->initializeAdmin();
|
||||||
$this->calculateEnable();
|
|
||||||
$this->enable([
|
|
||||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
|
|
||||||
]);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
/** @var Uri $uri */
|
$this->initializeFrontend();
|
||||||
$uri = $this->grav['uri'];
|
|
||||||
|
|
||||||
//Admin
|
|
||||||
$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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,6 +206,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -294,11 +312,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue