Rework a bit the organization of the code. Added cache for comments
This commit is contained in:
parent
04078ba7e0
commit
60c8d091e6
61
comments.php
61
comments.php
|
@ -17,6 +17,7 @@ class CommentsPlugin extends Plugin
|
|||
{
|
||||
protected $route = 'comments';
|
||||
protected $enable = false;
|
||||
protected $comments_cache_id;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
|
@ -24,10 +25,7 @@ class CommentsPlugin extends Plugin
|
|||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0],
|
||||
'onFormProcessed' => ['onFormProcessed', 0],
|
||||
'onPageInitialized' => ['onPageInitialized', 10],
|
||||
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0]
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -36,7 +34,6 @@ class CommentsPlugin extends Plugin
|
|||
*/
|
||||
public function onPageInitialized()
|
||||
{
|
||||
if (!$this->isAdmin()) {
|
||||
/** @var Page $page */
|
||||
$page = $this->grav['page'];
|
||||
if (!$page) {
|
||||
|
@ -51,17 +48,11 @@ class CommentsPlugin extends Plugin
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function onTwigSiteVariables() {
|
||||
if (!$this->isAdmin()) {
|
||||
$this->grav['twig']->enable = $this->enable;
|
||||
|
||||
if ($this->enable) {
|
||||
$this->grav['twig']->comments = $this->fetchComments();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if $haystack starts with $needle. Credit: http://stackoverflow.com/a/10473026/205039
|
||||
|
@ -96,21 +87,34 @@ class CommentsPlugin extends Plugin
|
|||
}
|
||||
|
||||
/**
|
||||
* Frontend side initialization
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
public function initializeFrontend()
|
||||
{
|
||||
if (!$this->isAdmin()) {
|
||||
|
||||
$this->calculateEnable();
|
||||
|
||||
if ($this->enable) {
|
||||
$this->enable([
|
||||
'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 */
|
||||
$uri = $this->grav['uri'];
|
||||
|
||||
//Admin
|
||||
$this->enable([
|
||||
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
|
||||
'onAdminMenu' => ['onAdminMenu', 0],
|
||||
|
@ -132,6 +136,16 @@ class CommentsPlugin extends Plugin
|
|||
$this->grav['twig']->comments = $comments;
|
||||
$this->grav['twig']->pages = $this->fetchPages();
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
{
|
||||
if ($this->isAdmin()) {
|
||||
$this->initializeAdmin();
|
||||
} else {
|
||||
$this->initializeFrontend();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -192,6 +206,10 @@ class CommentsPlugin extends Plugin
|
|||
}
|
||||
|
||||
$file->save(Yaml::dump($data));
|
||||
|
||||
//clear cache
|
||||
$this->grav['cache']->delete($this->comments_cache_id);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -294,11 +312,20 @@ class CommentsPlugin extends Plugin
|
|||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue