forked from getgrav/grav-plugin-random
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.php
64 lines (54 loc) · 1.46 KB
/
random.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Grav\Plugin;
use Grav\Common\Page\Collection;
use Grav\Common\Plugin;
use Grav\Common\Registry;
use Grav\Common\Uri;
use Grav\Common\Taxonomy;
class RandomPlugin extends Plugin
{
/**
* @var bool
*/
protected $active = false;
/**
* @var Uri
*/
protected $uri;
/**
* @var array
*/
protected $filters = array();
/**
* Activate plugin if path matches to the configured one.
*/
public function onAfterInitPlugins()
{
$this->uri = Registry::get('Uri');
$route = $this->config->get('plugins.random.route');
if ($route && $route == $this->uri->path()) {
$this->active = true;
}
}
/**
* Display random page.
*/
public function onAfterGetPage()
{
if ($this->active) {
/** @var Taxonomy $taxonomy_map */
$taxonomy_map = Registry::get('Taxonomy');
$filters = (array) $this->config->get('plugins.random.filters');
if (count($filters) > 0) {
$collection = new Collection();
foreach ($filters as $taxonomy => $items) {
if (isset($items)) {
$collection->append($taxonomy_map->findTaxonomy([$taxonomy => $items])->toArray());
}
}
$grav = Registry::get('Grav');
$grav->page = $collection->random()->current();
}
}
}
}