Skip to content

Commit d6045bf

Browse files
committed
add plugin functionalities
1 parent 51c4778 commit d6045bf

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

classes/PopularArticles.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Grav\Plugin\PopularArticles;
4+
5+
use Grav\Common\Grav;
6+
use Grav\Plugin\Admin\Popularity;
7+
use Grav\Common\Page\Collection;
8+
9+
class PopularArticles
10+
{
11+
private $popularityFile;
12+
private $grav;
13+
private $articles;
14+
15+
public function __construct()
16+
{
17+
$this->grav = Grav::instance();
18+
19+
// Admin plugin is required
20+
$this->popularityFile = $this->grav['locator']->findResource('log://popularity', true, true)
21+
. DIRECTORY_SEPARATOR
22+
. Popularity::TOTALS_FILE;
23+
}
24+
25+
public function get()
26+
{
27+
if ( !$this->articles ) $this->articles = $this->build();
28+
29+
return $this->articles;
30+
}
31+
32+
public function build()
33+
{
34+
// Get config or default
35+
$NUMBER_OF_ARTICLES = $this->grav['config']->get('plugins.popular-articles.articles_count') ?? 5;
36+
$BLOG_ROUTE = $this->grav['config']->get('plugins.popular-articles.blog_route') ?? '/blog';
37+
38+
$popularityData = [];
39+
40+
if ( file_exists($this->popularityFile) ) {
41+
// Read json from popularity file
42+
$popularityData = (array)json_decode(
43+
file_get_contents($this->popularityFile),
44+
true
45+
);
46+
47+
// Sort data by number of visits desc
48+
arsort( $popularityData );
49+
50+
// Filter on existing pages
51+
$popularityData = array_filter(
52+
$popularityData,
53+
function($key) use ($BLOG_ROUTE) {
54+
$page = $this->grav['page']->find($key);
55+
return $page !== null && strpos($key, $BLOG_ROUTE . '/') === 0;
56+
},
57+
ARRAY_FILTER_USE_KEY
58+
);
59+
}
60+
61+
// Limit number of articles returned
62+
$popularityData = array_slice($popularityData, 0, $NUMBER_OF_ARTICLES);
63+
64+
// Return the collection of pages
65+
$pagesCollection = new Collection();
66+
67+
foreach($popularityData as $route => $count) {
68+
$page = $this->grav['page']->find($route);
69+
70+
$pagesCollection->addPage($page);
71+
}
72+
73+
return $pagesCollection;
74+
}
75+
}

languages.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
en:
22
PLUGIN_POPULAR_ARTICLES:
3+
POPULAR_ARTICLES: Popular articles
34
BLOG_ROUTE_LABEL: Route to blog
45
ARTICLES_COUNT_LABEL: Number of articles to display
6+
7+
fr:
8+
PLUGIN_POPULAR_ARTICLES:
9+
POPULAR_ARTICLES: Articles populaires

popular-articles.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Composer\Autoload\ClassLoader;
55
use Grav\Common\Plugin;
6+
use Grav\Plugin\PopularArticles\PopularArticles;
67

78
/**
89
* Class PopularArticlesPlugin
@@ -24,8 +25,7 @@ public static function getSubscribedEvents(): array
2425
{
2526
return [
2627
'onPluginsInitialized' => [
27-
// Uncomment following line when plugin requires Grav < 1.7
28-
// ['autoload', 100000],
28+
['autoload', 100000], // TODO: Remove when plugin requires Grav >=1.7
2929
['onPluginsInitialized', 0]
3030
]
3131
];
@@ -53,7 +53,25 @@ public function onPluginsInitialized(): void
5353

5454
// Enable the main events we are interested in
5555
$this->enable([
56-
// Put your main events here
56+
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
57+
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
5758
]);
5859
}
60+
61+
/**
62+
* Add current directory to twig lookup paths.
63+
*/
64+
public function onTwigTemplatePaths()
65+
{
66+
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
67+
}
68+
69+
/**
70+
* Set needed variables to display the popular articles.
71+
*/
72+
public function onTwigSiteVariables()
73+
{
74+
$twig = $this->grav['twig'];
75+
$twig->twig_vars['popular_articles'] = new PopularArticles();
76+
}
5977
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% set articles_list = popular_articles.get() %}
2+
3+
<h4>
4+
{{ 'PLUGIN_POPULAR_ARTICLES.POPULAR_ARTICLES'|t }}
5+
</h4>
6+
7+
<ul>
8+
{% for article in articles_list %}
9+
<li>
10+
<a href="{{ base_url }}{{ article.route }}">
11+
{{ article.title }}
12+
</a>
13+
</li>
14+
{% endfor %}
15+
</ul>

0 commit comments

Comments
 (0)